api.c 2.3 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #ifndef USER_CRYPTO_ERROR
  215. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  216. #endif
  217. #endif
  218. #ifndef NO_SIG_WRAPPER
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #endif
  221. #ifdef HAVE_AESCCM
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #endif
  224. #ifdef HAVE_PKCS7
  225. #include <wolfssl/wolfcrypt/pkcs7.h>
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #ifdef HAVE_LIBZ
  228. #include <wolfssl/wolfcrypt/compress.h>
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  232. #include <wolfssl/wolfcrypt/asn.h>
  233. #endif
  234. #ifndef NO_DSA
  235. #include <wolfssl/wolfcrypt/dsa.h>
  236. #ifndef ONEK_BUF
  237. #define ONEK_BUF 1024
  238. #endif
  239. #ifndef TWOK_BUF
  240. #define TWOK_BUF 2048
  241. #endif
  242. #ifndef FOURK_BUF
  243. #define FOURK_BUF 4096
  244. #endif
  245. #ifndef DSA_SIG_SIZE
  246. #define DSA_SIG_SIZE 40
  247. #endif
  248. #ifndef MAX_DSA_PARAM_SIZE
  249. #define MAX_DSA_PARAM_SIZE 256
  250. #endif
  251. #endif
  252. #ifdef WOLFSSL_CMAC
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #endif
  255. #ifdef HAVE_ED25519
  256. #include <wolfssl/wolfcrypt/ed25519.h>
  257. #endif
  258. #ifdef HAVE_CURVE25519
  259. #include <wolfssl/wolfcrypt/curve25519.h>
  260. #endif
  261. #ifdef HAVE_ED448
  262. #include <wolfssl/wolfcrypt/ed448.h>
  263. #endif
  264. #ifdef HAVE_CURVE448
  265. #include <wolfssl/wolfcrypt/curve448.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #ifndef NO_RC4
  312. #include <wolfssl/openssl/rc4.h>
  313. #endif
  314. #ifdef HAVE_ECC
  315. #include <wolfssl/openssl/ecdsa.h>
  316. #endif
  317. #ifdef HAVE_PKCS7
  318. #include <wolfssl/openssl/pkcs7.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_ED448
  324. #include <wolfssl/openssl/ed448.h>
  325. #endif
  326. #endif /* OPENSSL_EXTRA */
  327. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  328. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  329. #include <wolfssl/wolfcrypt/srp.h>
  330. #endif
  331. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  332. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  333. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  334. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  335. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  336. || !defined(WOLFSSL_NO_TLS12)
  337. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  338. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  339. * number tracking */
  340. #include "wolfssl/internal.h"
  341. #endif
  342. /* force enable test buffers */
  343. #ifndef USE_CERT_BUFFERS_2048
  344. #define USE_CERT_BUFFERS_2048
  345. #endif
  346. #ifndef USE_CERT_BUFFERS_256
  347. #define USE_CERT_BUFFERS_256
  348. #endif
  349. #include <wolfssl/certs_test.h>
  350. #include "tests/utils.h"
  351. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  352. * have default (hidden) visibility, and in the absence of visibility, it's
  353. * benign to mask out the library implementation.
  354. */
  355. #define WOLFSSL_MISC_INCLUDED
  356. #include <wolfcrypt/src/misc.c>
  357. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  358. /* FIPS build has replaced ecc.h. */
  359. #define wc_ecc_key_get_priv(key) (&((key)->k))
  360. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. #endif
  362. typedef struct testVector {
  363. const char* input;
  364. const char* output;
  365. size_t inLen;
  366. size_t outLen;
  367. } testVector;
  368. #if defined(HAVE_PKCS7)
  369. typedef struct {
  370. const byte* content;
  371. word32 contentSz;
  372. int contentOID;
  373. int encryptOID;
  374. int keyWrapOID;
  375. int keyAgreeOID;
  376. byte* cert;
  377. size_t certSz;
  378. byte* privateKey;
  379. word32 privateKeySz;
  380. } pkcs7EnvelopedVector;
  381. #ifndef NO_PKCS7_ENCRYPTED_DATA
  382. typedef struct {
  383. const byte* content;
  384. word32 contentSz;
  385. int contentOID;
  386. int encryptOID;
  387. byte* encryptionKey;
  388. word32 encryptionKeySz;
  389. } pkcs7EncryptedVector;
  390. #endif
  391. #endif /* HAVE_PKCS7 */
  392. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  393. typedef int (*ssl_cb)(WOLFSSL* ssl);
  394. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  395. typedef struct test_ssl_cbf {
  396. method_provider method;
  397. ctx_cb ctx_ready;
  398. ssl_cb ssl_ready;
  399. ssl_cb on_result;
  400. ssl_cb on_cleanup;
  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. /*----------------------------------------------------------------------------*
  741. | Context
  742. *----------------------------------------------------------------------------*/
  743. #ifndef NO_WOLFSSL_SERVER
  744. static int test_wolfSSL_CTX_new(void)
  745. {
  746. EXPECT_DECLS;
  747. WOLFSSL_CTX *ctx;
  748. WOLFSSL_METHOD* method;
  749. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  750. ExpectNotNull(method = wolfSSLv23_server_method());
  751. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  752. wolfSSL_CTX_free(ctx);
  753. return EXPECT_RESULT();
  754. }
  755. #endif
  756. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  757. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  758. static int test_for_double_Free(void)
  759. {
  760. EXPECT_DECLS;
  761. WOLFSSL_CTX* ctx = NULL;
  762. WOLFSSL* ssl = NULL;
  763. int skipTest = 0;
  764. const char* testCertFile;
  765. const char* testKeyFile;
  766. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  767. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  768. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  769. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  770. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  771. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  772. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  773. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  774. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  775. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  776. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  777. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  778. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  779. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  780. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  781. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  782. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  783. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  784. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  785. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  786. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  787. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  788. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  789. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  790. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  791. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  792. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  793. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  794. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  795. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  796. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  797. #ifdef OPENSSL_EXTRA
  798. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  799. "!SRP:!kRSA:!aNULL:!eNULL";
  800. #endif
  801. #ifndef NO_RSA
  802. testCertFile = svrCertFile;
  803. testKeyFile = svrKeyFile;
  804. #elif defined(HAVE_ECC)
  805. testCertFile = eccCertFile;
  806. testKeyFile = eccKeyFile;
  807. #else
  808. skipTest = 1;
  809. #endif
  810. if (skipTest != 1) {
  811. #ifndef NO_WOLFSSL_SERVER
  812. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  813. #else
  814. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  815. #endif
  816. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  817. WOLFSSL_FILETYPE_PEM));
  818. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  819. WOLFSSL_FILETYPE_PEM));
  820. ExpectNotNull(ssl = wolfSSL_new(ctx));
  821. /* First test freeing SSL, then CTX */
  822. wolfSSL_free(ssl);
  823. ssl = NULL;
  824. wolfSSL_CTX_free(ctx);
  825. ctx = NULL;
  826. #ifndef NO_WOLFSSL_CLIENT
  827. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  828. #else
  829. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  830. #endif
  831. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  832. WOLFSSL_FILETYPE_PEM));
  833. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  834. WOLFSSL_FILETYPE_PEM));
  835. ExpectNotNull(ssl = wolfSSL_new(ctx));
  836. /* Next test freeing CTX then SSL */
  837. wolfSSL_CTX_free(ctx);
  838. ctx = NULL;
  839. wolfSSL_free(ssl);
  840. ssl = NULL;
  841. #ifndef NO_WOLFSSL_SERVER
  842. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  843. #else
  844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  845. #endif
  846. /* Test setting ciphers at ctx level */
  847. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  848. WOLFSSL_FILETYPE_PEM));
  849. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  850. WOLFSSL_FILETYPE_PEM));
  851. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  852. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  853. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  854. /* only update TLSv13 suites */
  855. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  856. #endif
  857. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  858. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  859. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  860. /* only update pre-TLSv13 suites */
  861. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  862. "ECDHE-RSA-AES128-GCM-SHA256"));
  863. #endif
  864. #ifdef OPENSSL_EXTRA
  865. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  866. #endif
  867. ExpectNotNull(ssl = wolfSSL_new(ctx));
  868. wolfSSL_CTX_free(ctx);
  869. ctx = NULL;
  870. wolfSSL_free(ssl);
  871. ssl = NULL;
  872. #ifndef NO_WOLFSSL_CLIENT
  873. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  874. #else
  875. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  876. #endif
  877. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  878. WOLFSSL_FILETYPE_PEM));
  879. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  880. WOLFSSL_FILETYPE_PEM));
  881. ExpectNotNull(ssl = wolfSSL_new(ctx));
  882. /* test setting ciphers at SSL level */
  883. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  884. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  885. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  886. /* only update TLSv13 suites */
  887. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  888. #endif
  889. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  890. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  891. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  892. /* only update pre-TLSv13 suites */
  893. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  894. #endif
  895. wolfSSL_CTX_free(ctx);
  896. ctx = NULL;
  897. wolfSSL_free(ssl);
  898. ssl = NULL;
  899. }
  900. return EXPECT_RESULT();
  901. }
  902. #endif
  903. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  904. {
  905. EXPECT_DECLS;
  906. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  907. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  908. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  909. const char* testCertFile;
  910. const char* testKeyFile;
  911. WOLFSSL_CTX* ctx = NULL;
  912. WOLFSSL* ssl = NULL;
  913. const byte cipherList[] =
  914. {
  915. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  916. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  917. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  918. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  919. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  920. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  921. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  922. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  923. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  924. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  925. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  926. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  927. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  928. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  929. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  930. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  931. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  932. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  933. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  934. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  935. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  936. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  937. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  938. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  939. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  940. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  941. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  942. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  943. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  944. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  945. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  946. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  947. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  948. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  949. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  950. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  951. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  952. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  953. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  954. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  955. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  956. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  957. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  958. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  959. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  960. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  961. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  962. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  963. /* SHA256 */
  964. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  965. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  966. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  967. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  968. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  969. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  970. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  971. /* SHA384 */
  972. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  973. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  974. /* AES-GCM */
  975. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  976. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  977. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  978. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  979. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  980. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  981. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  982. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  983. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  984. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  985. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  986. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  987. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  988. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  989. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  990. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  991. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  992. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  993. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  994. * also, in some of the other AES-CCM suites
  995. * there will be second byte number conflicts
  996. * with non-ECC AES-GCM */
  997. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  998. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  999. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1000. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1001. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1002. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1003. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1004. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1005. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1006. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1007. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1008. /* Camellia */
  1009. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1010. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1011. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1012. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1013. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1014. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1015. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1016. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1017. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1018. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1019. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1020. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1021. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1022. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1023. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1024. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1025. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1026. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1027. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1028. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1029. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1030. /* TLS v1.3 cipher suites */
  1031. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1032. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1033. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1034. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1035. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1036. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1037. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1038. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1039. };
  1040. #ifndef NO_RSA
  1041. testCertFile = svrCertFile;
  1042. testKeyFile = svrKeyFile;
  1043. #elif defined(HAVE_ECC)
  1044. testCertFile = eccCertFile;
  1045. testKeyFile = eccKeyFile;
  1046. #endif
  1047. #ifndef NO_WOLFSSL_SERVER
  1048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1049. #else
  1050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1051. #endif
  1052. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1053. sizeof(cipherList)));
  1054. wolfSSL_CTX_free(ctx);
  1055. ctx = NULL;
  1056. #ifndef NO_WOLFSSL_SERVER
  1057. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1058. #else
  1059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1060. #endif
  1061. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1062. WOLFSSL_FILETYPE_PEM));
  1063. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1064. WOLFSSL_FILETYPE_PEM));
  1065. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1066. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1067. sizeof(cipherList)));
  1068. wolfSSL_free(ssl);
  1069. wolfSSL_CTX_free(ctx);
  1070. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1071. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1072. return EXPECT_RESULT();
  1073. }
  1074. static int test_wolfSSL_CTX_use_certificate_file(void)
  1075. {
  1076. EXPECT_DECLS;
  1077. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1078. WOLFSSL_CTX *ctx = NULL;
  1079. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1080. /* invalid context */
  1081. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1082. WOLFSSL_FILETYPE_PEM));
  1083. /* invalid cert file */
  1084. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1085. WOLFSSL_FILETYPE_PEM));
  1086. /* invalid cert type */
  1087. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1088. #ifdef NO_RSA
  1089. /* rsa needed */
  1090. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1091. WOLFSSL_FILETYPE_PEM));
  1092. #else
  1093. /* success */
  1094. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1095. WOLFSSL_FILETYPE_PEM));
  1096. #endif
  1097. wolfSSL_CTX_free(ctx);
  1098. #endif
  1099. return EXPECT_RESULT();
  1100. }
  1101. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1102. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1103. {
  1104. EXPECT_DECLS;
  1105. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1106. WOLFSSL_CTX* ctx = NULL;
  1107. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1108. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1109. server_cert_der_2048), WOLFSSL_SUCCESS);
  1110. wolfSSL_CTX_free(ctx);
  1111. #endif
  1112. return EXPECT_RESULT();
  1113. }
  1114. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1115. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1116. * context using buffer.
  1117. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1118. * --enable-testcert flag.
  1119. */
  1120. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1121. {
  1122. EXPECT_DECLS;
  1123. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1124. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1125. WOLFSSL_CTX* ctx = NULL;
  1126. int ret;
  1127. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1128. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1129. server_cert_der_2048, sizeof_server_cert_der_2048,
  1130. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1131. wolfSSL_CTX_free(ctx);
  1132. #endif
  1133. return EXPECT_RESULT();
  1134. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1135. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1136. {
  1137. EXPECT_DECLS;
  1138. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1139. WOLFSSL_CTX *ctx = NULL;
  1140. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1141. /* invalid context */
  1142. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1143. WOLFSSL_FILETYPE_PEM));
  1144. /* invalid key file */
  1145. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1146. WOLFSSL_FILETYPE_PEM));
  1147. /* invalid key type */
  1148. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1149. /* success */
  1150. #ifdef NO_RSA
  1151. /* rsa needed */
  1152. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1153. WOLFSSL_FILETYPE_PEM));
  1154. #else
  1155. /* success */
  1156. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1157. WOLFSSL_FILETYPE_PEM));
  1158. #endif
  1159. wolfSSL_CTX_free(ctx);
  1160. #endif
  1161. return EXPECT_RESULT();
  1162. }
  1163. /* test both file and buffer versions along with unloading trusted peer certs */
  1164. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1165. {
  1166. EXPECT_DECLS;
  1167. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1168. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1169. WOLFSSL_CTX *ctx = NULL;
  1170. WOLFSSL* ssl = NULL;
  1171. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1172. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1173. #if !defined(NO_FILESYSTEM)
  1174. /* invalid file */
  1175. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1176. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1177. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1178. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1179. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1180. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1181. /* success */
  1182. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1183. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1184. /* unload cert */
  1185. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1186. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1187. /* invalid file */
  1188. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1189. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1190. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1191. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1192. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1193. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1194. /* success */
  1195. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1196. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1197. #ifdef WOLFSSL_LOCAL_X509_STORE
  1198. /* unload cert */
  1199. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1200. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1201. #endif
  1202. #endif
  1203. /* Test of loading certs from buffers */
  1204. /* invalid buffer */
  1205. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1206. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1207. /* success */
  1208. #ifdef USE_CERT_BUFFERS_1024
  1209. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1210. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1211. #endif
  1212. #ifdef USE_CERT_BUFFERS_2048
  1213. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1214. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1215. #endif
  1216. /* unload cert */
  1217. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1218. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1219. wolfSSL_free(ssl);
  1220. wolfSSL_CTX_free(ctx);
  1221. #endif
  1222. return EXPECT_RESULT();
  1223. }
  1224. static int test_wolfSSL_CTX_load_verify_locations(void)
  1225. {
  1226. EXPECT_DECLS;
  1227. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1228. WOLFSSL_CTX *ctx = NULL;
  1229. #ifndef NO_RSA
  1230. WOLFSSL_CERT_MANAGER* cm = NULL;
  1231. #ifdef PERSIST_CERT_CACHE
  1232. int cacheSz = 0;
  1233. unsigned char* cache = NULL;
  1234. int used = 0;
  1235. #ifndef NO_FILESYSTEM
  1236. const char* cacheFile = "./tests/cert_cache.tmp";
  1237. #endif
  1238. int i;
  1239. int t;
  1240. int* p;
  1241. #endif
  1242. #endif
  1243. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1244. const char* load_certs_path = "./certs/external";
  1245. const char* load_no_certs_path = "./examples";
  1246. const char* load_expired_path = "./certs/test/expired";
  1247. #endif
  1248. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1249. /* invalid arguments */
  1250. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1251. WOLFSSL_FAILURE);
  1252. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1253. WOLFSSL_FAILURE);
  1254. /* invalid ca file */
  1255. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1256. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1257. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1258. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1259. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1260. /* invalid path */
  1261. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1262. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1263. #endif
  1264. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1265. /* test ignoring the invalid path */
  1266. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1267. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1268. #endif
  1269. /* load ca cert */
  1270. #ifdef NO_RSA
  1271. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1272. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1273. #else /* Skip the following test without RSA certs. */
  1274. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1275. WOLFSSL_SUCCESS);
  1276. #ifdef PERSIST_CERT_CACHE
  1277. /* Get cert cache size */
  1278. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1279. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1280. DYNAMIC_TYPE_TMP_BUFFER));
  1281. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1282. BAD_FUNC_ARG);
  1283. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1284. BAD_FUNC_ARG);
  1285. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1286. BAD_FUNC_ARG);
  1287. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1288. BAD_FUNC_ARG);
  1289. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1290. BAD_FUNC_ARG);
  1291. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1292. BAD_FUNC_ARG);
  1293. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1294. BAD_FUNC_ARG);
  1295. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1296. BAD_FUNC_ARG);
  1297. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1298. BAD_FUNC_ARG);
  1299. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1300. BUFFER_E);
  1301. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1302. ExpectIntEQ(cacheSz, used);
  1303. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1304. BAD_FUNC_ARG);
  1305. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1306. BAD_FUNC_ARG);
  1307. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1308. BAD_FUNC_ARG);
  1309. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1310. BAD_FUNC_ARG);
  1311. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1312. BAD_FUNC_ARG);
  1313. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1314. BAD_FUNC_ARG);
  1315. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1316. BAD_FUNC_ARG);
  1317. /* Smaller than header. */
  1318. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1319. for (i = 1; i < cacheSz; i++) {
  1320. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1321. BUFFER_E);
  1322. }
  1323. if (EXPECT_SUCCESS()) {
  1324. /* Modify header for bad results! */
  1325. p = (int*)cache;
  1326. /* version */
  1327. t = p[0]; p[0] = 0xff;
  1328. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1329. CACHE_MATCH_ERROR);
  1330. p[0] = t; p++;
  1331. /* rows */
  1332. t = p[0]; p[0] = 0xff;
  1333. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1334. CACHE_MATCH_ERROR);
  1335. p[0] = t; p++;
  1336. /* columns[0] */
  1337. t = p[0]; p[0] = -1;
  1338. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1339. PARSE_ERROR);
  1340. p[0] = t; p += CA_TABLE_SIZE;
  1341. /* signerSz*/
  1342. t = p[0]; p[0] = 0xff;
  1343. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1344. CACHE_MATCH_ERROR);
  1345. p[0] = t;
  1346. }
  1347. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1348. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1349. #ifndef NO_FILESYSTEM
  1350. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1351. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1352. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1353. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1354. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1355. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1356. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1357. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1358. WOLFSSL_BAD_FILE);
  1359. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1360. /* File contents is not a cache. */
  1361. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1362. CACHE_MATCH_ERROR);
  1363. #endif
  1364. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1365. #endif
  1366. /* Test unloading CA's */
  1367. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1368. #ifdef PERSIST_CERT_CACHE
  1369. /* Verify no certs (result is less than cacheSz) */
  1370. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1371. #endif
  1372. /* load ca cert again */
  1373. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1374. WOLFSSL_SUCCESS);
  1375. /* Test getting CERT_MANAGER */
  1376. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1377. /* Test unloading CA's using CM */
  1378. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1379. #ifdef PERSIST_CERT_CACHE
  1380. /* Verify no certs (result is less than cacheSz) */
  1381. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1382. #endif
  1383. #endif
  1384. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1385. /* Test loading CA certificates using a path */
  1386. #ifdef NO_RSA
  1387. /* failure here okay since certs in external directory are RSA */
  1388. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1389. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1390. #else
  1391. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1392. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1393. #endif
  1394. /* Test loading path with no files */
  1395. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1396. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1397. /* Test loading expired CA certificates */
  1398. #ifdef NO_RSA
  1399. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1400. load_expired_path,
  1401. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1402. WOLFSSL_SUCCESS);
  1403. #else
  1404. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1405. load_expired_path,
  1406. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1407. WOLFSSL_SUCCESS);
  1408. #endif
  1409. /* Test loading CA certificates and ignoring all errors */
  1410. #ifdef NO_RSA
  1411. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1412. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1413. #else
  1414. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1415. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1416. #endif
  1417. #endif
  1418. wolfSSL_CTX_free(ctx);
  1419. #endif
  1420. return EXPECT_RESULT();
  1421. }
  1422. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1423. {
  1424. int res = TEST_SKIPPED;
  1425. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1426. (!defined(NO_RSA) || defined(HAVE_ECC))
  1427. WOLFSSL_CTX* ctx;
  1428. byte dirValid = 0;
  1429. int ret = 0;
  1430. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1431. if (ctx == NULL) {
  1432. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1433. ret = -1;
  1434. }
  1435. if (ret == 0) {
  1436. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1437. dirValid = 1;
  1438. #else
  1439. word32 numDirs;
  1440. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1441. if (caDirs == NULL || numDirs == 0) {
  1442. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1443. ret = -1;
  1444. }
  1445. else {
  1446. ReadDirCtx dirCtx;
  1447. word32 i;
  1448. for (i = 0; i < numDirs; ++i) {
  1449. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1450. /* Directory isn't empty. */
  1451. dirValid = 1;
  1452. wc_ReadDirClose(&dirCtx);
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. #endif
  1458. }
  1459. /*
  1460. * If the directory isn't empty, we should be able to load CA
  1461. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1462. * usable.
  1463. */
  1464. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1465. WOLFSSL_SUCCESS) {
  1466. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1467. ret = -1;
  1468. }
  1469. #ifdef OPENSSL_EXTRA
  1470. if (ret == 0 &&
  1471. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1472. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1473. ret = -1;
  1474. }
  1475. #endif /* OPENSSL_EXTRA */
  1476. wolfSSL_CTX_free(ctx);
  1477. res = TEST_RES_CHECK(ret == 0);
  1478. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1479. return res;
  1480. }
  1481. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1482. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1483. int file_type)
  1484. {
  1485. int ret;
  1486. WOLFSSL_CERT_MANAGER* cm;
  1487. cm = wolfSSL_CertManagerNew();
  1488. if (cm == NULL) {
  1489. fprintf(stderr, "test_cm_load_ca failed\n");
  1490. return -1;
  1491. }
  1492. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1493. wolfSSL_CertManagerFree(cm);
  1494. return ret;
  1495. }
  1496. static int test_cm_load_ca_file(const char* ca_cert_file)
  1497. {
  1498. int ret = 0;
  1499. byte* cert_buf = NULL;
  1500. size_t cert_sz = 0;
  1501. #if defined(WOLFSSL_PEM_TO_DER)
  1502. DerBuffer* pDer = NULL;
  1503. #endif
  1504. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1505. if (ret == 0) {
  1506. /* normal test */
  1507. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1508. if (ret == WOLFSSL_SUCCESS) {
  1509. /* test including null terminator in length */
  1510. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1511. if (tmp == NULL) {
  1512. ret = MEMORY_E;
  1513. }
  1514. else {
  1515. cert_buf = tmp;
  1516. cert_buf[cert_sz] = '\0';
  1517. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1518. WOLFSSL_FILETYPE_PEM);
  1519. }
  1520. }
  1521. #if defined(WOLFSSL_PEM_TO_DER)
  1522. if (ret == WOLFSSL_SUCCESS) {
  1523. /* test loading DER */
  1524. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1525. if (ret == 0 && pDer != NULL) {
  1526. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1527. WOLFSSL_FILETYPE_ASN1);
  1528. wc_FreeDer(&pDer);
  1529. }
  1530. }
  1531. #endif
  1532. }
  1533. free(cert_buf);
  1534. return ret;
  1535. }
  1536. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1537. int file_type, word32 flags)
  1538. {
  1539. int ret;
  1540. WOLFSSL_CERT_MANAGER* cm;
  1541. cm = wolfSSL_CertManagerNew();
  1542. if (cm == NULL) {
  1543. fprintf(stderr, "test_cm_load_ca failed\n");
  1544. return -1;
  1545. }
  1546. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1547. 0, flags);
  1548. wolfSSL_CertManagerFree(cm);
  1549. return ret;
  1550. }
  1551. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1552. {
  1553. int ret = 0;
  1554. byte* cert_buf = NULL;
  1555. size_t cert_sz = 0;
  1556. #if defined(WOLFSSL_PEM_TO_DER)
  1557. DerBuffer* pDer = NULL;
  1558. #endif
  1559. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1560. if (ret == 0) {
  1561. /* normal test */
  1562. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1563. WOLFSSL_FILETYPE_PEM, flags);
  1564. if (ret == WOLFSSL_SUCCESS) {
  1565. /* test including null terminator in length */
  1566. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1567. if (tmp == NULL) {
  1568. ret = MEMORY_E;
  1569. }
  1570. else {
  1571. cert_buf = tmp;
  1572. cert_buf[cert_sz] = '\0';
  1573. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1574. WOLFSSL_FILETYPE_PEM, flags);
  1575. }
  1576. }
  1577. #if defined(WOLFSSL_PEM_TO_DER)
  1578. if (ret == WOLFSSL_SUCCESS) {
  1579. /* test loading DER */
  1580. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1581. if (ret == 0 && pDer != NULL) {
  1582. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1583. WOLFSSL_FILETYPE_ASN1, flags);
  1584. wc_FreeDer(&pDer);
  1585. }
  1586. }
  1587. #endif
  1588. }
  1589. free(cert_buf);
  1590. return ret;
  1591. }
  1592. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1593. static int test_wolfSSL_CertManagerAPI(void)
  1594. {
  1595. EXPECT_DECLS;
  1596. #ifndef NO_CERTS
  1597. WOLFSSL_CERT_MANAGER* cm = NULL;
  1598. unsigned char c;
  1599. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1600. wolfSSL_CertManagerFree(NULL);
  1601. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1602. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1603. #ifdef WOLFSSL_TRUST_PEER_CERT
  1604. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1605. #endif
  1606. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1607. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1608. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1609. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1610. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1611. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1612. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1614. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1615. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1616. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1617. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1618. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1619. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1620. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1621. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1622. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1623. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1624. WOLFSSL_BAD_FILETYPE);
  1625. #endif
  1626. #if !defined(NO_FILESYSTEM)
  1627. {
  1628. const char* ca_cert = "./certs/ca-cert.pem";
  1629. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1630. const char* ca_cert_der = "./certs/ca-cert.der";
  1631. #endif
  1632. const char* ca_path = "./certs";
  1633. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1634. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1635. BAD_FUNC_ARG);
  1636. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1639. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1641. WOLFSSL_BAD_FILETYPE);
  1642. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1643. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1644. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1645. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1646. #endif
  1647. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1648. WOLFSSL_FATAL_ERROR);
  1649. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1650. WOLFSSL_FATAL_ERROR);
  1651. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1652. WOLFSSL_FATAL_ERROR);
  1653. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1654. WOLFSSL_FATAL_ERROR);
  1655. }
  1656. #endif
  1657. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1658. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1659. #elif !defined(HAVE_CRL)
  1660. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1661. #endif
  1662. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1663. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1664. #ifdef HAVE_CRL
  1665. /* Test APIs when CRL is disabled. */
  1666. #ifdef HAVE_CRL_IO
  1667. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1668. #endif
  1669. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1670. sizeof_server_cert_der_2048), 1);
  1671. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1672. #endif
  1673. /* OCSP */
  1674. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1675. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1676. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1677. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1678. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1679. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1680. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1681. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1682. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  1683. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1684. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1685. #endif
  1686. #ifdef HAVE_OCSP
  1687. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  1688. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  1689. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  1690. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  1691. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  1692. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  1693. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  1694. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  1695. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1696. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  1697. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1698. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  1699. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1700. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  1701. BAD_FUNC_ARG);
  1702. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  1703. BAD_FUNC_ARG);
  1704. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  1705. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  1706. BAD_FUNC_ARG);
  1707. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  1708. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  1709. /* Test APIs when OCSP is disabled. */
  1710. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  1711. NULL, NULL, NULL, NULL), 1);
  1712. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  1713. #endif
  1714. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  1715. wolfSSL_CertManagerFree(cm);
  1716. wolfSSL_CertManagerFree(cm);
  1717. cm = NULL;
  1718. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1719. #ifdef HAVE_OCSP
  1720. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  1721. WOLFSSL_OCSP_CHECKALL), 1);
  1722. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1723. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1724. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1725. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1726. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  1727. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1728. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  1729. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  1730. #endif
  1731. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1732. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1733. #endif
  1734. #ifdef WOLFSSL_TRUST_PEER_CERT
  1735. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  1736. #endif
  1737. wolfSSL_CertManagerFree(cm);
  1738. #endif
  1739. return EXPECT_RESULT();
  1740. }
  1741. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1742. {
  1743. EXPECT_DECLS;
  1744. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1745. const char* ca_cert = "./certs/ca-cert.pem";
  1746. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1747. int ret;
  1748. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  1749. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1750. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1751. #elif defined(NO_RSA)
  1752. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1753. #else
  1754. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1755. #endif
  1756. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  1757. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1758. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1759. #elif defined(NO_RSA)
  1760. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1761. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1762. !defined(NO_ASN_TIME)
  1763. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1764. #else
  1765. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1766. #endif
  1767. #endif
  1768. return EXPECT_RESULT();
  1769. }
  1770. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1771. {
  1772. EXPECT_DECLS;
  1773. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1774. const char* ca_cert = "./certs/ca-cert.pem";
  1775. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1776. int ret;
  1777. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  1778. 1);
  1779. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1780. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1781. #elif defined(NO_RSA)
  1782. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1783. #else
  1784. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1785. #endif
  1786. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1787. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  1788. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1789. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1790. #elif defined(NO_RSA)
  1791. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1792. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1793. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1794. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1795. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1796. #else
  1797. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1798. #endif
  1799. #endif
  1800. return EXPECT_RESULT();
  1801. }
  1802. static int test_wolfSSL_CertManagerGetCerts(void)
  1803. {
  1804. EXPECT_DECLS;
  1805. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1806. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1807. defined(WOLFSSL_SIGNER_DER_CERT)
  1808. WOLFSSL_CERT_MANAGER* cm = NULL;
  1809. WOLFSSL_STACK* sk = NULL;
  1810. X509* x509 = NULL;
  1811. X509* cert1 = NULL;
  1812. FILE* file1 = NULL;
  1813. #ifdef DEBUG_WOLFSSL_VERBOSE
  1814. WOLFSSL_BIO* bio = NULL;
  1815. #endif
  1816. int i = 0;
  1817. int ret = 0;
  1818. const byte* der;
  1819. int derSz = 0;
  1820. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1821. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1822. if (file1 != NULL) {
  1823. fclose(file1);
  1824. }
  1825. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  1826. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1827. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1828. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1829. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1830. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1831. * and full OpenSSL compatibility */
  1832. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1833. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1834. #else
  1835. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1836. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1837. #endif
  1838. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1839. "./certs/ca-cert.pem", NULL));
  1840. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1841. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  1842. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1843. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1844. #ifdef DEBUG_WOLFSSL_VERBOSE
  1845. bio = BIO_new(wolfSSL_BIO_s_file());
  1846. if (bio != NULL) {
  1847. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1848. X509_print(bio, x509);
  1849. BIO_free(bio);
  1850. }
  1851. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1852. }
  1853. wolfSSL_X509_free(cert1);
  1854. sk_X509_pop_free(sk, NULL);
  1855. wolfSSL_CertManagerFree(cm);
  1856. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1857. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1858. defined(WOLFSSL_SIGNER_DER_CERT) */
  1859. return EXPECT_RESULT();
  1860. }
  1861. static int test_wolfSSL_CertManagerSetVerify(void)
  1862. {
  1863. EXPECT_DECLS;
  1864. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1865. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1866. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1867. WOLFSSL_CERT_MANAGER* cm = NULL;
  1868. int tmp = myVerifyAction;
  1869. const char* ca_cert = "./certs/ca-cert.pem";
  1870. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1871. wolfSSL_CertManagerSetVerify(NULL, NULL);
  1872. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  1873. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1874. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1875. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1876. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1877. #else
  1878. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1879. WOLFSSL_SUCCESS);
  1880. #endif
  1881. /* Use the test CB that always accepts certs */
  1882. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1883. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  1884. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1885. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1886. {
  1887. const char* verifyCert = "./certs/server-cert.der";
  1888. /* Use the test CB that always fails certs */
  1889. myVerifyAction = VERIFY_FORCE_FAIL;
  1890. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  1891. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  1892. }
  1893. #endif
  1894. wolfSSL_CertManagerFree(cm);
  1895. myVerifyAction = tmp;
  1896. #endif
  1897. return EXPECT_RESULT();
  1898. }
  1899. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1900. defined(DEBUG_UNIT_TEST_CERTS)
  1901. /* Used when debugging name constraint tests. Not static to allow use in
  1902. * multiple locations with complex define guards. */
  1903. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1904. {
  1905. BIO* out = BIO_new_file(fileName, "wb");
  1906. if (out != NULL) {
  1907. PEM_write_bio_X509(out, x509);
  1908. BIO_free(out);
  1909. }
  1910. }
  1911. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1912. {
  1913. BIO* out = BIO_new_file(fileName, "wb");
  1914. if (out != NULL) {
  1915. BIO_write(out, der, derSz);
  1916. BIO_free(out);
  1917. }
  1918. }
  1919. #else
  1920. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  1921. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  1922. #endif
  1923. static int test_wolfSSL_CertManagerNameConstraint(void)
  1924. {
  1925. EXPECT_DECLS;
  1926. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1927. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1928. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1929. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1930. !defined(NO_SHA256)
  1931. WOLFSSL_CERT_MANAGER* cm = NULL;
  1932. WOLFSSL_EVP_PKEY *priv = NULL;
  1933. WOLFSSL_X509_NAME* name = NULL;
  1934. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1935. const char* server_cert = "./certs/test/server-goodcn.pem";
  1936. int i = 0;
  1937. static const byte extNameConsOid[] = {85, 29, 30};
  1938. RsaKey key;
  1939. WC_RNG rng;
  1940. byte *der = NULL;
  1941. int derSz = 0;
  1942. word32 idx = 0;
  1943. byte *pt;
  1944. WOLFSSL_X509 *x509 = NULL;
  1945. WOLFSSL_X509 *ca = NULL;
  1946. wc_InitRng(&rng);
  1947. /* load in CA private key for signing */
  1948. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1949. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1950. sizeof_server_key_der_2048), 0);
  1951. /* get ca certificate then alter it */
  1952. ExpectNotNull(der =
  1953. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1954. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1955. WOLFSSL_FILETYPE_ASN1));
  1956. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1957. if (EXPECT_SUCCESS() && (der != NULL)) {
  1958. XMEMCPY(der, pt, derSz);
  1959. /* find the name constraint extension and alter it */
  1960. pt = der;
  1961. for (i = 0; i < derSz - 3; i++) {
  1962. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1963. pt += 3;
  1964. break;
  1965. }
  1966. pt++;
  1967. }
  1968. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1969. /* go to the length value and set it to 0 */
  1970. while (i < derSz && *pt != 0x81) {
  1971. pt++;
  1972. i++;
  1973. }
  1974. ExpectIntNE(i, derSz); /* did not place to alter */
  1975. pt++;
  1976. *pt = 0x00;
  1977. }
  1978. /* resign the altered certificate */
  1979. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1980. FOURK_BUF, &key, NULL, &rng)), 0);
  1981. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1982. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1983. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1984. wolfSSL_CertManagerFree(cm);
  1985. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1986. wolfSSL_X509_free(x509);
  1987. wc_FreeRsaKey(&key);
  1988. wc_FreeRng(&rng);
  1989. /* add email alt name to satisfy constraint */
  1990. pt = (byte*)server_key_der_2048;
  1991. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1992. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1993. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1994. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1995. WOLFSSL_FILETYPE_ASN1));
  1996. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1997. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1998. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1999. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2000. /* Good cert test with proper alt email name */
  2001. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2002. WOLFSSL_FILETYPE_PEM));
  2003. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2004. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2005. name = NULL;
  2006. ExpectNotNull(name = X509_NAME_new());
  2007. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2008. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2009. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2010. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2012. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2013. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2014. X509_NAME_free(name);
  2015. name = NULL;
  2016. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2017. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2018. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2019. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2020. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2021. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2022. wolfSSL_X509_free(x509);
  2023. x509 = NULL;
  2024. /* Cert with bad alt name list */
  2025. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2026. WOLFSSL_FILETYPE_PEM));
  2027. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2028. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2029. name = NULL;
  2030. ExpectNotNull(name = X509_NAME_new());
  2031. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2032. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2034. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2035. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2036. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2037. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2038. X509_NAME_free(name);
  2039. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2040. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2041. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2042. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2043. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2045. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2046. wolfSSL_CertManagerFree(cm);
  2047. wolfSSL_X509_free(x509);
  2048. wolfSSL_X509_free(ca);
  2049. wolfSSL_EVP_PKEY_free(priv);
  2050. #endif
  2051. return EXPECT_RESULT();
  2052. }
  2053. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2054. {
  2055. EXPECT_DECLS;
  2056. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2057. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2058. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2059. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2060. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2061. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2062. const char* server_cert = "./certs/server-cert.pem";
  2063. WOLFSSL_CERT_MANAGER* cm = NULL;
  2064. WOLFSSL_X509 *x509 = NULL;
  2065. WOLFSSL_X509 *ca = NULL;
  2066. const unsigned char *der = NULL;
  2067. const unsigned char *pt;
  2068. WOLFSSL_EVP_PKEY *priv = NULL;
  2069. WOLFSSL_X509_NAME* name = NULL;
  2070. int derSz = 0;
  2071. /* C=US*/
  2072. char altName[] = {
  2073. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2074. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2075. };
  2076. /* C=ID */
  2077. char altNameFail[] = {
  2078. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2079. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2080. };
  2081. /* C=US ST=California*/
  2082. char altNameExc[] = {
  2083. 0x30, 0x22,
  2084. 0x31, 0x0B,
  2085. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2086. 0x31, 0x13,
  2087. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2088. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2089. };
  2090. /* load in CA private key for signing */
  2091. pt = ca_key_der_2048;
  2092. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2093. sizeof_ca_key_der_2048));
  2094. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2095. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2096. WOLFSSL_FILETYPE_ASN1));
  2097. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2098. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2099. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2100. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2101. WOLFSSL_FILETYPE_PEM));
  2102. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2103. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2104. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2105. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2106. #else
  2107. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2108. #endif
  2109. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2110. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2111. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2112. /* add in matching DIR alt name and resign */
  2113. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2114. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2115. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2116. #else
  2117. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2118. #endif
  2119. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2120. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2121. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2122. wolfSSL_X509_free(x509);
  2123. x509 = NULL;
  2124. /* check verify fail */
  2125. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2126. WOLFSSL_FILETYPE_PEM));
  2127. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2128. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2129. /* add in miss matching DIR alt name and resign */
  2130. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2131. ASN_DIR_TYPE);
  2132. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2133. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2134. #else
  2135. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2136. #endif
  2137. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2138. #ifndef WOLFSSL_NO_ASN_STRICT
  2139. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2140. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2141. #else
  2142. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2143. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2144. #endif
  2145. /* check that it still fails if one bad altname and one good altname is in
  2146. * the certificate */
  2147. wolfSSL_X509_free(x509);
  2148. x509 = NULL;
  2149. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2150. WOLFSSL_FILETYPE_PEM));
  2151. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2152. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2153. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2154. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2155. ASN_DIR_TYPE);
  2156. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2157. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2158. #else
  2159. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2160. #endif
  2161. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2162. #ifndef WOLFSSL_NO_ASN_STRICT
  2163. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2164. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2165. #else
  2166. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2167. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2168. #endif
  2169. /* check it fails with switching position of bad altname */
  2170. wolfSSL_X509_free(x509);
  2171. x509 = NULL;
  2172. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2173. WOLFSSL_FILETYPE_PEM));
  2174. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2175. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2176. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2177. ASN_DIR_TYPE);
  2178. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2179. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2180. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2181. #else
  2182. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2183. #endif
  2184. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2185. #ifndef WOLFSSL_NO_ASN_STRICT
  2186. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2187. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2188. #else
  2189. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2191. #endif
  2192. wolfSSL_CertManagerFree(cm);
  2193. wolfSSL_X509_free(x509);
  2194. x509 = NULL;
  2195. wolfSSL_X509_free(ca);
  2196. ca = NULL;
  2197. /* now test with excluded name constraint */
  2198. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2199. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2200. WOLFSSL_FILETYPE_ASN1));
  2201. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2202. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2203. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2204. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2205. WOLFSSL_FILETYPE_PEM));
  2206. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2207. ASN_DIR_TYPE);
  2208. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2209. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2210. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2211. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2212. #else
  2213. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2214. #endif
  2215. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2216. #ifndef WOLFSSL_NO_ASN_STRICT
  2217. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2218. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2219. #else
  2220. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2221. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2222. #endif
  2223. wolfSSL_CertManagerFree(cm);
  2224. wolfSSL_X509_free(x509);
  2225. wolfSSL_X509_free(ca);
  2226. wolfSSL_EVP_PKEY_free(priv);
  2227. #endif
  2228. return EXPECT_RESULT();
  2229. }
  2230. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2231. {
  2232. EXPECT_DECLS;
  2233. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2234. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2235. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2236. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2237. !defined(NO_SHA256)
  2238. WOLFSSL_CERT_MANAGER* cm = NULL;
  2239. WOLFSSL_EVP_PKEY *priv = NULL;
  2240. WOLFSSL_X509_NAME* name = NULL;
  2241. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2242. const char* server_cert = "./certs/test/server-goodcn.pem";
  2243. byte *der = NULL;
  2244. int derSz = 0;
  2245. byte *pt;
  2246. WOLFSSL_X509 *x509 = NULL;
  2247. WOLFSSL_X509 *ca = NULL;
  2248. pt = (byte*)server_key_der_2048;
  2249. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2250. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2251. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2252. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2253. WOLFSSL_FILETYPE_ASN1));
  2254. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2255. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2256. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2257. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2258. /* check satisfying .wolfssl.com constraint passes */
  2259. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2260. WOLFSSL_FILETYPE_PEM));
  2261. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2262. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2263. name = NULL;
  2264. ExpectNotNull(name = X509_NAME_new());
  2265. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2266. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2267. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2268. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2269. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2270. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2271. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2272. X509_NAME_free(name);
  2273. name = NULL;
  2274. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2275. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2276. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2277. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2278. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2279. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2280. wolfSSL_X509_free(x509);
  2281. x509 = NULL;
  2282. /* check satisfying .random.com constraint passes */
  2283. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2284. WOLFSSL_FILETYPE_PEM));
  2285. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2286. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2287. name = NULL;
  2288. ExpectNotNull(name = X509_NAME_new());
  2289. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2290. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2291. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2292. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2293. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2294. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2295. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2296. X509_NAME_free(name);
  2297. name = NULL;
  2298. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2299. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2300. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2301. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2302. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2303. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2304. wolfSSL_X509_free(x509);
  2305. x509 = NULL;
  2306. /* check fail case when neither constraint is matched */
  2307. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2308. WOLFSSL_FILETYPE_PEM));
  2309. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2310. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2311. name = NULL;
  2312. ExpectNotNull(name = X509_NAME_new());
  2313. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2314. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2315. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2316. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2317. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2318. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2319. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2320. X509_NAME_free(name);
  2321. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2322. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2323. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2324. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2325. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2326. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2327. wolfSSL_CertManagerFree(cm);
  2328. wolfSSL_X509_free(x509);
  2329. wolfSSL_X509_free(ca);
  2330. wolfSSL_EVP_PKEY_free(priv);
  2331. #endif
  2332. return EXPECT_RESULT();
  2333. }
  2334. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2335. {
  2336. EXPECT_DECLS;
  2337. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2338. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2339. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2340. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2341. !defined(NO_SHA256)
  2342. WOLFSSL_CERT_MANAGER* cm = NULL;
  2343. WOLFSSL_EVP_PKEY *priv = NULL;
  2344. WOLFSSL_X509_NAME* name = NULL;
  2345. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2346. const char* server_cert = "./certs/test/server-goodcn.pem";
  2347. byte *der = NULL;
  2348. int derSz;
  2349. byte *pt;
  2350. WOLFSSL_X509 *x509 = NULL;
  2351. WOLFSSL_X509 *ca = NULL;
  2352. pt = (byte*)server_key_der_2048;
  2353. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2354. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2355. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2356. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2357. WOLFSSL_FILETYPE_ASN1));
  2358. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2359. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2360. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2361. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2362. /* check satisfying wolfssl.com constraint passes */
  2363. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2364. WOLFSSL_FILETYPE_PEM));
  2365. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2366. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2367. name = NULL;
  2368. ExpectNotNull(name = X509_NAME_new());
  2369. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2370. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2371. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2372. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2373. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2374. X509_NAME_free(name);
  2375. name = NULL;
  2376. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2377. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2378. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2379. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2380. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2381. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2382. wolfSSL_X509_free(x509);
  2383. x509 = NULL;
  2384. /* check satisfying example.com constraint passes */
  2385. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2386. WOLFSSL_FILETYPE_PEM));
  2387. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2388. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2389. name = NULL;
  2390. ExpectNotNull(name = X509_NAME_new());
  2391. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2392. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2393. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2394. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2395. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2396. X509_NAME_free(name);
  2397. name = NULL;
  2398. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2399. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2400. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2401. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2402. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2403. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2404. wolfSSL_X509_free(x509);
  2405. x509 = NULL;
  2406. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2407. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2408. WOLFSSL_FILETYPE_PEM));
  2409. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2410. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2411. name = NULL;
  2412. ExpectNotNull(name = X509_NAME_new());
  2413. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2414. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2415. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2416. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2417. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2418. X509_NAME_free(name);
  2419. name = NULL;
  2420. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2421. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2422. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2423. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2424. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2425. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2426. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2427. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2428. wolfSSL_X509_free(x509);
  2429. x509 = NULL;
  2430. /* check fail when one DNS in the list is bad */
  2431. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2432. WOLFSSL_FILETYPE_PEM));
  2433. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2434. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2435. name = NULL;
  2436. ExpectNotNull(name = X509_NAME_new());
  2437. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2438. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2439. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2440. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2441. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2442. X509_NAME_free(name);
  2443. name = NULL;
  2444. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2445. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2446. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2447. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2448. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2449. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2450. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2451. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2452. wolfSSL_X509_free(x509);
  2453. x509 = NULL;
  2454. /* check fail case when neither constraint is matched */
  2455. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2456. WOLFSSL_FILETYPE_PEM));
  2457. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2458. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2459. name = NULL;
  2460. ExpectNotNull(name = X509_NAME_new());
  2461. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2462. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2463. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2464. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2465. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2466. X509_NAME_free(name);
  2467. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2468. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2469. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2470. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2471. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2472. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2473. wolfSSL_CertManagerFree(cm);
  2474. wolfSSL_X509_free(x509);
  2475. wolfSSL_X509_free(ca);
  2476. wolfSSL_EVP_PKEY_free(priv);
  2477. #endif
  2478. return EXPECT_RESULT();
  2479. }
  2480. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2481. {
  2482. EXPECT_DECLS;
  2483. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2484. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2485. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2486. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2487. !defined(NO_SHA256)
  2488. WOLFSSL_CERT_MANAGER* cm = NULL;
  2489. WOLFSSL_EVP_PKEY *priv = NULL;
  2490. WOLFSSL_X509_NAME* name = NULL;
  2491. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2492. const char* server_cert = "./certs/test/server-goodcn.pem";
  2493. byte *der = NULL;
  2494. int derSz;
  2495. byte *pt;
  2496. WOLFSSL_X509 *x509 = NULL;
  2497. WOLFSSL_X509 *ca = NULL;
  2498. pt = (byte*)server_key_der_2048;
  2499. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2500. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2501. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2502. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2503. WOLFSSL_FILETYPE_ASN1));
  2504. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2505. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2506. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2507. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2508. /* check satisfying wolfssl.com constraint passes */
  2509. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2510. WOLFSSL_FILETYPE_PEM));
  2511. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2512. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2513. name = NULL;
  2514. ExpectNotNull(name = X509_NAME_new());
  2515. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2516. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2517. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2518. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2519. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2520. X509_NAME_free(name);
  2521. name = NULL;
  2522. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2523. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2524. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2525. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2526. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2527. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2528. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2529. wolfSSL_X509_free(x509);
  2530. x509 = NULL;
  2531. /* fail with DNS check because of common name */
  2532. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2533. WOLFSSL_FILETYPE_PEM));
  2534. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2535. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2536. name = NULL;
  2537. ExpectNotNull(name = X509_NAME_new());
  2538. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2539. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2540. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2541. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2542. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2543. X509_NAME_free(name);
  2544. name = NULL;
  2545. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2546. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2547. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2548. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2549. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2550. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2551. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2552. wolfSSL_X509_free(x509);
  2553. x509 = NULL;
  2554. /* fail on permitted DNS name constraint */
  2555. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2556. WOLFSSL_FILETYPE_PEM));
  2557. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2558. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2559. name = NULL;
  2560. ExpectNotNull(name = X509_NAME_new());
  2561. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2562. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2563. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2564. X509_NAME_free(name);
  2565. name = NULL;
  2566. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2567. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2568. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2569. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2570. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2571. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2572. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2573. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2574. wolfSSL_X509_free(x509);
  2575. x509 = NULL;
  2576. /* fail on permitted email name constraint */
  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(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2586. X509_NAME_free(name);
  2587. name = NULL;
  2588. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2589. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2590. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2591. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2592. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2593. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2594. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2595. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2596. wolfSSL_X509_free(x509);
  2597. x509 = NULL;
  2598. /* success with empty email name */
  2599. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2600. WOLFSSL_FILETYPE_PEM));
  2601. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2602. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2603. name = NULL;
  2604. ExpectNotNull(name = X509_NAME_new());
  2605. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2606. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2607. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2608. X509_NAME_free(name);
  2609. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2610. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2611. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2612. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2615. wolfSSL_X509_free(x509);
  2616. wolfSSL_CertManagerFree(cm);
  2617. wolfSSL_X509_free(ca);
  2618. wolfSSL_EVP_PKEY_free(priv);
  2619. #endif
  2620. return EXPECT_RESULT();
  2621. }
  2622. static int test_wolfSSL_CertManagerCRL(void)
  2623. {
  2624. EXPECT_DECLS;
  2625. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2626. !defined(NO_RSA)
  2627. const char* ca_cert = "./certs/ca-cert.pem";
  2628. const char* crl1 = "./certs/crl/crl.pem";
  2629. const char* crl2 = "./certs/crl/crl2.pem";
  2630. const unsigned char crl_buff[] = {
  2631. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2632. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2633. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2634. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2635. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2636. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2637. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2638. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2639. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2640. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2641. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2642. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2643. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2644. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2645. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2646. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2647. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2648. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2649. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2650. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2651. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2652. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2653. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2654. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2655. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2656. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2657. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2658. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2659. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2660. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2661. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2662. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2663. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2664. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2665. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2666. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2667. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2668. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2669. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2670. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2671. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2672. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2673. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2674. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2675. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2676. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2677. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  2678. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  2679. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  2680. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  2681. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  2682. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  2683. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  2684. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  2685. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  2686. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  2687. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  2688. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  2689. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  2690. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  2691. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  2692. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  2693. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  2694. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  2695. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  2696. };
  2697. WOLFSSL_CERT_MANAGER* cm = NULL;
  2698. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2699. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  2700. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2701. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  2702. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  2703. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  2704. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  2705. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2706. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  2707. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  2708. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  2709. BAD_FUNC_ARG);
  2710. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  2711. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  2712. BAD_FUNC_ARG);
  2713. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  2714. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  2715. BAD_FUNC_ARG);
  2716. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2717. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  2718. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  2719. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  2720. #ifdef HAVE_CRL_IO
  2721. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  2722. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2723. #endif
  2724. #ifndef NO_FILESYSTEM
  2725. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  2726. 0), BAD_FUNC_ARG);
  2727. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  2728. 0), BAD_FUNC_ARG);
  2729. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2730. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  2731. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  2732. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2733. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2734. BAD_FUNC_ARG);
  2735. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2736. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  2737. ASN_PARSE_E);
  2738. #endif
  2739. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  2740. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2741. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  2742. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2743. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  2744. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2745. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  2746. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2747. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  2748. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2749. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  2750. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2751. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  2752. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2753. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  2754. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2755. ExpectIntEQ(WOLFSSL_SUCCESS,
  2756. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2757. ExpectIntEQ(WOLFSSL_SUCCESS,
  2758. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2759. ExpectIntEQ(WOLFSSL_SUCCESS,
  2760. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2761. wolfSSL_CertManagerFreeCRL(cm);
  2762. ExpectIntEQ(WOLFSSL_SUCCESS,
  2763. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2764. ExpectIntEQ(WOLFSSL_SUCCESS,
  2765. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2766. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2767. sizeof_server_cert_der_2048), CRL_MISSING);
  2768. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  2769. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  2770. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  2771. WOLFSSL_FILETYPE_ASN1), 1);
  2772. wolfSSL_CertManagerFree(cm);
  2773. #endif
  2774. return EXPECT_RESULT();
  2775. }
  2776. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  2777. {
  2778. EXPECT_DECLS;
  2779. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  2780. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  2781. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2782. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  2783. defined(HAVE_LIGHTY)
  2784. WOLFSSL_CERT_MANAGER* cm = NULL;
  2785. /* Raw OCSP response bytes captured using the following setup:
  2786. * - Run responder with
  2787. * openssl ocsp -port 9999 -ndays 9999
  2788. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  2789. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  2790. * -rkey certs/ocsp/ocsp-responder-key.pem
  2791. * -CA certs/ocsp/intermediate1-ca-cert.pem
  2792. * - Run client with
  2793. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  2794. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  2795. * -cert certs/ocsp/server1-cert.pem
  2796. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  2797. * - Select the response packet in Wireshark, and export it using
  2798. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  2799. * packets only". After importing into the editor, remove the initial
  2800. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  2801. */
  2802. static const byte response[] = {
  2803. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  2804. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  2805. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  2806. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  2807. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  2808. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  2809. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2810. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2811. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2812. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2813. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2814. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2815. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2816. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2817. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2818. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2819. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2820. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  2821. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  2822. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  2823. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  2824. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  2825. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  2826. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  2827. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  2828. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  2829. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  2830. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  2831. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  2832. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  2833. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  2834. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  2835. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  2836. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  2837. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  2838. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  2839. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  2840. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  2841. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  2842. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  2843. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  2844. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  2845. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  2846. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  2847. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  2848. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  2849. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  2850. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  2851. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  2852. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  2853. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  2854. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  2855. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  2856. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  2857. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  2858. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  2859. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  2860. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  2861. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  2862. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  2863. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  2864. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  2865. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  2866. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  2867. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  2868. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  2869. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  2870. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  2871. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  2872. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  2873. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  2874. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  2875. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  2876. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  2877. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  2878. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  2879. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  2880. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  2881. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  2882. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  2883. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  2884. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  2885. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  2886. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  2887. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  2888. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2889. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2890. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2891. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2892. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2893. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2894. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2895. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2896. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2897. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2898. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2899. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  2900. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  2901. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  2902. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  2903. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  2904. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  2905. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  2906. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  2907. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  2908. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  2909. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  2910. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  2911. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  2912. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  2913. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  2914. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  2915. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  2916. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  2917. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  2918. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  2919. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2920. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2921. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  2922. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  2923. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  2924. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2925. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  2926. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  2927. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  2928. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  2929. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  2930. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  2931. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  2932. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  2933. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  2934. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  2935. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  2936. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  2937. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  2938. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  2939. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  2940. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  2941. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  2942. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  2943. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  2944. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  2945. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  2946. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  2947. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  2948. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  2949. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  2950. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  2951. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  2952. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  2953. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  2954. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  2955. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  2956. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  2957. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  2958. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  2959. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  2960. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  2961. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  2962. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  2963. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  2964. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  2965. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  2966. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  2967. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  2968. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  2969. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  2970. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  2971. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  2972. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  2973. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  2974. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  2975. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  2976. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  2977. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  2978. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  2979. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  2980. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  2981. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  2982. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  2983. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  2984. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  2985. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  2986. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  2987. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2988. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  2989. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  2990. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  2991. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  2992. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2993. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  2994. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  2995. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  2996. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  2997. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  2998. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  2999. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3000. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3001. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3002. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3003. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3004. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3005. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3006. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3007. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3008. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3009. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3010. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3011. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3012. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3013. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3014. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3015. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3016. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3017. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3018. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3019. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3020. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3021. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3022. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3023. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3024. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3025. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3026. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3027. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3028. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3029. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3030. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3031. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3032. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3033. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3034. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3035. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3036. };
  3037. OcspEntry entry[1];
  3038. CertStatus status[1];
  3039. OcspRequest* request = NULL;
  3040. #ifndef NO_FILESYSTEM
  3041. const char* ca_cert = "./certs/ca-cert.pem";
  3042. #endif
  3043. byte serial[] = {0x05};
  3044. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3045. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3046. XMEMSET(entry, 0, sizeof(OcspEntry));
  3047. XMEMSET(status, 0, sizeof(CertStatus));
  3048. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3049. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3050. DYNAMIC_TYPE_OCSP_REQUEST));
  3051. if ((request != NULL) && (request->serial != NULL)) {
  3052. request->serialSz = sizeof(serial);
  3053. XMEMCPY(request->serial, serial, sizeof(serial));
  3054. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3055. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3056. }
  3057. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3058. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3059. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3060. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3061. /* Response should be valid. */
  3062. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3063. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3064. /* Flip a byte in the request serial number, response should be invalid
  3065. * now. */
  3066. if ((request != NULL) && (request->serial != NULL))
  3067. request->serial[0] ^= request->serial[0];
  3068. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3069. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3070. #ifndef NO_FILESYSTEM
  3071. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3072. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3073. ExpectIntEQ(WOLFSSL_SUCCESS,
  3074. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3075. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3076. sizeof(server_cert_der_2048)), 1);
  3077. #endif
  3078. wolfSSL_OCSP_REQUEST_free(request);
  3079. wolfSSL_CertManagerFree(cm);
  3080. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3081. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3082. #endif /* HAVE_OCSP */
  3083. return EXPECT_RESULT();
  3084. }
  3085. static int test_wolfSSL_CheckOCSPResponse(void)
  3086. {
  3087. EXPECT_DECLS;
  3088. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3089. defined(OPENSSL_ALL)
  3090. const char* responseFile = "./certs/ocsp/test-response.der";
  3091. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3092. const char* responseNoInternFile =
  3093. "./certs/ocsp/test-response-nointern.der";
  3094. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3095. OcspResponse* res = NULL;
  3096. byte data[4096];
  3097. const unsigned char* pt;
  3098. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3099. * gcc sanitizer with --enable-heapmath.
  3100. */
  3101. XFILE f = XBADFILE;
  3102. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3103. WOLFSSL_X509_STORE* st = NULL;
  3104. WOLFSSL_X509* issuer = NULL;
  3105. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3106. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3107. if (f != XBADFILE) {
  3108. XFCLOSE(f);
  3109. f = XBADFILE;
  3110. }
  3111. pt = data;
  3112. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3113. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3114. SSL_FILETYPE_PEM));
  3115. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3116. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3117. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3118. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3119. wolfSSL_OCSP_BASICRESP_free(bs);
  3120. bs = NULL;
  3121. wolfSSL_OCSP_RESPONSE_free(res);
  3122. res = NULL;
  3123. wolfSSL_X509_STORE_free(st);
  3124. st = NULL;
  3125. wolfSSL_X509_free(issuer);
  3126. issuer = NULL;
  3127. /* check loading a response with optional certs */
  3128. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3129. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3130. if (f != XBADFILE)
  3131. XFCLOSE(f);
  3132. f = XBADFILE;
  3133. pt = data;
  3134. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3135. wolfSSL_OCSP_RESPONSE_free(res);
  3136. res = NULL;
  3137. /* check loading a response with multiple certs */
  3138. {
  3139. WOLFSSL_CERT_MANAGER* cm = NULL;
  3140. OcspEntry *entry = NULL;
  3141. CertStatus* status = NULL;
  3142. OcspRequest* request = NULL;
  3143. byte serial1[] = {0x01};
  3144. byte serial[] = {0x02};
  3145. byte issuerHash[] = {
  3146. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3147. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3148. 0x37, 0x15, 0x8A, 0x88
  3149. };
  3150. byte issuerKeyHash[] = {
  3151. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3152. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3153. 0x7E, 0x72, 0x15, 0x21
  3154. };
  3155. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3156. DYNAMIC_TYPE_OPENSSL));
  3157. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3158. DYNAMIC_TYPE_OPENSSL));
  3159. if (entry != NULL)
  3160. XMEMSET(entry, 0, sizeof(OcspEntry));
  3161. if (status != NULL)
  3162. XMEMSET(status, 0, sizeof(CertStatus));
  3163. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3164. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3165. DYNAMIC_TYPE_OCSP_REQUEST));
  3166. if (request != NULL && request->serial != NULL) {
  3167. request->serialSz = sizeof(serial);
  3168. XMEMCPY(request->serial, serial, sizeof(serial));
  3169. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3170. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3171. sizeof(issuerKeyHash));
  3172. }
  3173. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3174. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3175. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3176. WOLFSSL_SUCCESS);
  3177. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3178. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3179. if (f != XBADFILE)
  3180. XFCLOSE(f);
  3181. f = XBADFILE;
  3182. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3183. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3184. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3185. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3186. ExpectNotNull(entry->status);
  3187. if (request != NULL && request->serial != NULL)
  3188. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3189. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3190. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3191. /* store both status's in the entry to check that "next" is not
  3192. * overwritten */
  3193. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3194. status->next = entry->status;
  3195. entry->status = status;
  3196. }
  3197. if (request != NULL && request->serial != NULL)
  3198. XMEMCPY(request->serial, serial, sizeof(serial));
  3199. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3200. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3201. ExpectNotNull(entry->status->next);
  3202. /* compare the status found */
  3203. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3204. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3205. status->serialSz), 0);
  3206. if (status != NULL && entry != NULL && entry->status != status) {
  3207. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3208. }
  3209. wolfSSL_OCSP_CERTID_free(entry);
  3210. wolfSSL_OCSP_REQUEST_free(request);
  3211. wolfSSL_CertManagerFree(cm);
  3212. }
  3213. #if defined(WC_RSA_PSS)
  3214. {
  3215. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3216. /* check loading a response with RSA-PSS signature */
  3217. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3218. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3219. if (f != XBADFILE)
  3220. XFCLOSE(f);
  3221. pt = data;
  3222. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3223. /* try to verify the response */
  3224. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3225. SSL_FILETYPE_PEM));
  3226. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3227. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3228. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3229. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3230. WOLFSSL_SUCCESS);
  3231. wolfSSL_OCSP_BASICRESP_free(bs);
  3232. wolfSSL_OCSP_RESPONSE_free(res);
  3233. wolfSSL_X509_STORE_free(st);
  3234. wolfSSL_X509_free(issuer);
  3235. }
  3236. #endif
  3237. #endif /* HAVE_OCSP */
  3238. return EXPECT_RESULT();
  3239. }
  3240. static int test_wolfSSL_FPKI(void)
  3241. {
  3242. EXPECT_DECLS;
  3243. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3244. XFILE f = XBADFILE;
  3245. const char* fpkiCert = "./certs/fpki-cert.der";
  3246. DecodedCert cert;
  3247. byte buf[4096];
  3248. byte* uuid = NULL;
  3249. byte* fascn = NULL;
  3250. word32 fascnSz;
  3251. word32 uuidSz;
  3252. int bytes = 0;
  3253. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3254. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3255. if (f != XBADFILE)
  3256. XFCLOSE(f);
  3257. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3258. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3259. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3260. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3261. DYNAMIC_TYPE_TMP_BUFFER));
  3262. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3263. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3264. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3265. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3266. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3267. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3268. wc_FreeDecodedCert(&cert);
  3269. #endif
  3270. return EXPECT_RESULT();
  3271. }
  3272. /* use RID in confuncture with other names to test parsing of unknown other
  3273. * names */
  3274. static int test_wolfSSL_OtherName(void)
  3275. {
  3276. EXPECT_DECLS;
  3277. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3278. XFILE f = XBADFILE;
  3279. const char* ridCert = "./certs/rid-cert.der";
  3280. DecodedCert cert;
  3281. byte buf[4096];
  3282. int bytes = 0;
  3283. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3284. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3285. if (f != XBADFILE)
  3286. XFCLOSE(f);
  3287. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3288. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3289. wc_FreeDecodedCert(&cert);
  3290. #endif
  3291. return EXPECT_RESULT();
  3292. }
  3293. static int test_wolfSSL_CertRsaPss(void)
  3294. {
  3295. EXPECT_DECLS;
  3296. /* FIPS v2 and below don't support long salts. */
  3297. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3298. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3299. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3300. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3301. XFILE f = XBADFILE;
  3302. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3303. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3304. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3305. RSA_MAX_SIZE >= 3072
  3306. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3307. #endif
  3308. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3309. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3310. #endif
  3311. DecodedCert cert;
  3312. byte buf[4096];
  3313. int bytes = 0;
  3314. WOLFSSL_CERT_MANAGER* cm = NULL;
  3315. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3316. ExpectIntEQ(WOLFSSL_SUCCESS,
  3317. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3318. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3319. ExpectIntEQ(WOLFSSL_SUCCESS,
  3320. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3321. #endif
  3322. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3323. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3324. if (f != XBADFILE) {
  3325. XFCLOSE(f);
  3326. f = XBADFILE;
  3327. }
  3328. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3329. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3330. wc_FreeDecodedCert(&cert);
  3331. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3332. RSA_MAX_SIZE >= 3072
  3333. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3334. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3335. if (f != XBADFILE)
  3336. XFCLOSE(f);
  3337. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3338. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3339. wc_FreeDecodedCert(&cert);
  3340. #endif
  3341. wolfSSL_CertManagerFree(cm);
  3342. #endif
  3343. return EXPECT_RESULT();
  3344. }
  3345. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3346. {
  3347. EXPECT_DECLS;
  3348. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3349. !defined(NO_WOLFSSL_CLIENT)
  3350. WOLFSSL_CTX* ctx = NULL;
  3351. const char* ca_cert = "./certs/ca-cert.pem";
  3352. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3353. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3354. /* test good CA */
  3355. ExpectTrue(WOLFSSL_SUCCESS ==
  3356. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3357. WOLFSSL_LOAD_FLAG_NONE));
  3358. /* test expired CA */
  3359. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3360. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3361. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3362. #else
  3363. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3364. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3365. #endif
  3366. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3367. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3368. wolfSSL_CTX_free(ctx);
  3369. #endif
  3370. return EXPECT_RESULT();
  3371. }
  3372. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3373. {
  3374. EXPECT_DECLS;
  3375. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3376. defined(USE_CERT_BUFFERS_2048)
  3377. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3378. WOLFSSL_CTX* ctx;
  3379. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3380. byte ca_expired_cert[TWOK_BUF];
  3381. word32 sizeof_ca_expired_cert = 0;
  3382. XFILE fp = XBADFILE;
  3383. #ifndef NO_WOLFSSL_CLIENT
  3384. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3385. #else
  3386. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3387. #endif
  3388. ExpectNotNull(ctx);
  3389. /* test good CA */
  3390. ExpectTrue(WOLFSSL_SUCCESS ==
  3391. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3392. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3393. WOLFSSL_LOAD_FLAG_NONE));
  3394. /* load expired CA */
  3395. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3396. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3397. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3398. sizeof(ca_expired_cert), fp), 0);
  3399. if (fp != XBADFILE)
  3400. XFCLOSE(fp);
  3401. /* test expired CA failure */
  3402. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3403. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3404. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3405. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3406. #else
  3407. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3408. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3409. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3410. #endif
  3411. /* test expired CA success */
  3412. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3413. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3414. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3415. wolfSSL_CTX_free(ctx);
  3416. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3417. #endif
  3418. return EXPECT_RESULT();
  3419. }
  3420. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3421. {
  3422. EXPECT_DECLS;
  3423. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3424. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3425. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3426. WOLFSSL_CTX* ctx = NULL;
  3427. #ifndef NO_WOLFSSL_CLIENT
  3428. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3429. #else
  3430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3431. #endif
  3432. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3433. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3434. WOLFSSL_FILETYPE_ASN1));
  3435. wolfSSL_CTX_free(ctx);
  3436. #endif
  3437. return EXPECT_RESULT();
  3438. }
  3439. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3440. {
  3441. EXPECT_DECLS;
  3442. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3443. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3444. WOLFSSL_CTX* ctx;
  3445. WOLFSSL* ssl = NULL;
  3446. const char *certChain[] = {
  3447. "./certs/intermediate/client-int-cert.pem",
  3448. "./certs/intermediate/ca-int2-cert.pem",
  3449. "./certs/intermediate/ca-int-cert.pem",
  3450. "./certs/ca-cert.pem",
  3451. NULL
  3452. };
  3453. const char** cert;
  3454. WOLFSSL_X509* x509 = NULL;
  3455. WOLF_STACK_OF(X509)* chain = NULL;
  3456. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3457. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3458. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3459. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3460. WOLFSSL_FILETYPE_PEM));
  3461. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3462. X509_free(x509);
  3463. x509 = NULL;
  3464. }
  3465. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3466. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3467. WOLFSSL_FILETYPE_PEM));
  3468. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3469. X509_free(x509);
  3470. x509 = NULL;
  3471. }
  3472. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3473. ExpectIntEQ(sk_X509_num(chain), 3);
  3474. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3475. ExpectIntEQ(sk_X509_num(chain), 3);
  3476. SSL_free(ssl);
  3477. SSL_CTX_free(ctx);
  3478. #endif
  3479. return EXPECT_RESULT();
  3480. }
  3481. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3482. {
  3483. EXPECT_DECLS;
  3484. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3485. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3486. const char* server_chain_der = "./certs/server-cert-chain.der";
  3487. const char* client_single_pem = "./certs/client-cert.pem";
  3488. WOLFSSL_CTX* ctx;
  3489. (void)server_chain_der;
  3490. (void)client_single_pem;
  3491. (void)ctx;
  3492. #ifndef NO_WOLFSSL_CLIENT
  3493. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3494. #else
  3495. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3496. #endif
  3497. ExpectNotNull(ctx);
  3498. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3499. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3500. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3501. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3502. wolfSSL_CTX_free(ctx);
  3503. #endif
  3504. return EXPECT_RESULT();
  3505. }
  3506. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3507. {
  3508. EXPECT_DECLS;
  3509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3510. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3511. WOLFSSL_CTX *ctx = NULL;
  3512. (void)ctx;
  3513. #ifndef NO_WOLFSSL_CLIENT
  3514. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3515. #else
  3516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3517. #endif
  3518. /* invalid context */
  3519. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3520. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3521. /* invalid dhParamFile file */
  3522. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3523. NULL, WOLFSSL_FILETYPE_PEM));
  3524. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3525. bogusFile, WOLFSSL_FILETYPE_PEM));
  3526. /* success */
  3527. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3528. WOLFSSL_FILETYPE_PEM));
  3529. wolfSSL_CTX_free(ctx);
  3530. #endif
  3531. return EXPECT_RESULT();
  3532. }
  3533. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3534. {
  3535. EXPECT_DECLS;
  3536. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3537. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3538. WOLFSSL_CTX *ctx = NULL;
  3539. #ifndef NO_WOLFSSL_CLIENT
  3540. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3541. #else
  3542. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3543. #endif
  3544. /* invalid context */
  3545. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3546. dh_key_der_2048, sizeof_dh_key_der_2048,
  3547. WOLFSSL_FILETYPE_ASN1));
  3548. /* invalid dhParamFile file */
  3549. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3550. 0, WOLFSSL_FILETYPE_ASN1));
  3551. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3552. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3553. WOLFSSL_FILETYPE_ASN1));
  3554. /* success */
  3555. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3556. dh_key_der_2048, sizeof_dh_key_der_2048,
  3557. WOLFSSL_FILETYPE_ASN1));
  3558. wolfSSL_CTX_free(ctx);
  3559. #endif
  3560. return EXPECT_RESULT();
  3561. }
  3562. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3563. {
  3564. EXPECT_DECLS;
  3565. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3566. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3567. WOLFSSL_CTX *ctx;
  3568. (void)ctx;
  3569. #ifndef NO_WOLFSSL_CLIENT
  3570. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3571. #else
  3572. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3573. #endif
  3574. ExpectNotNull(ctx);
  3575. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3576. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3577. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3578. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3579. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3580. dh_key_der_2048, sizeof_dh_key_der_2048,
  3581. WOLFSSL_FILETYPE_ASN1));
  3582. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3583. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3584. dh_key_der_2048, sizeof_dh_key_der_2048,
  3585. WOLFSSL_FILETYPE_ASN1));
  3586. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3587. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3588. dh_key_der_2048, sizeof_dh_key_der_2048,
  3589. WOLFSSL_FILETYPE_ASN1));
  3590. wolfSSL_CTX_free(ctx);
  3591. #endif
  3592. return EXPECT_RESULT();
  3593. }
  3594. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3595. {
  3596. EXPECT_DECLS;
  3597. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3598. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3599. WOLFSSL_CTX* ctx = NULL;
  3600. const char* derCert = "./certs/server-cert.der";
  3601. const char* nullPath = NULL;
  3602. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3603. const char* emptyPath = "";
  3604. /* der load Case 1 ctx NULL */
  3605. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3606. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3607. #ifndef NO_WOLFSSL_CLIENT
  3608. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3609. #else
  3610. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3611. #endif
  3612. /* Case 2 filePath NULL */
  3613. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3615. /* Case 3 invalid format */
  3616. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3617. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3618. /* Case 4 filePath not valid */
  3619. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3620. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3621. /* Case 5 filePath empty */
  3622. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3623. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3624. #ifndef NO_RSA
  3625. /* Case 6 success case */
  3626. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3627. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3628. #endif
  3629. wolfSSL_CTX_free(ctx);
  3630. #endif
  3631. return EXPECT_RESULT();
  3632. }
  3633. static int test_wolfSSL_CTX_enable_disable(void)
  3634. {
  3635. EXPECT_DECLS;
  3636. #ifndef NO_CERTS
  3637. WOLFSSL_CTX* ctx = NULL;
  3638. #ifdef HAVE_CRL
  3639. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3640. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3641. #endif
  3642. #ifdef HAVE_OCSP
  3643. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3644. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3645. #endif
  3646. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3647. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3648. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3649. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3650. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3651. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3652. #endif
  3653. #ifndef NO_WOLFSSL_CLIENT
  3654. #ifdef HAVE_EXTENDED_MASTER
  3655. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3656. #endif
  3657. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3658. #ifdef HAVE_EXTENDED_MASTER
  3659. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3660. #endif
  3661. #elif !defined(NO_WOLFSSL_SERVER)
  3662. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3663. #endif
  3664. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3665. #ifdef HAVE_CRL
  3666. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3667. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3668. #endif
  3669. #ifdef HAVE_OCSP
  3670. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3671. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3672. WOLFSSL_SUCCESS);
  3673. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3674. WOLFSSL_SUCCESS);
  3675. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3676. WOLFSSL_SUCCESS);
  3677. #endif
  3678. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3679. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3680. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3681. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3682. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3683. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3684. #endif
  3685. wolfSSL_CTX_free(ctx);
  3686. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3687. #endif /* NO_CERTS */
  3688. return EXPECT_RESULT();
  3689. }
  3690. static int test_wolfSSL_CTX_ticket_API(void)
  3691. {
  3692. EXPECT_DECLS;
  3693. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3694. WOLFSSL_CTX* ctx = NULL;
  3695. void *userCtx = (void*)"this is my ctx";
  3696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3697. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3698. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3699. wolfSSL_CTX_free(ctx);
  3700. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3701. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3702. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3703. return EXPECT_RESULT();
  3704. }
  3705. static int test_wolfSSL_set_minmax_proto_version(void)
  3706. {
  3707. EXPECT_DECLS;
  3708. #ifdef OPENSSL_EXTRA
  3709. WOLFSSL_CTX *ctx = NULL;
  3710. WOLFSSL *ssl = NULL;
  3711. (void)ssl;
  3712. #ifndef NO_WOLFSSL_CLIENT
  3713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3714. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3715. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3716. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3717. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3718. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3719. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3720. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3721. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3722. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3723. wolfSSL_free(ssl);
  3724. wolfSSL_CTX_free(ctx);
  3725. ctx = NULL;
  3726. #endif
  3727. #ifndef NO_WOLFSSL_SERVER
  3728. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3729. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3730. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3731. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3732. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3733. wolfSSL_CTX_free(ctx);
  3734. #endif
  3735. #endif
  3736. return EXPECT_RESULT();
  3737. }
  3738. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3739. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3740. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3741. {
  3742. EXPECT_DECLS;
  3743. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3744. return EXPECT_RESULT();
  3745. }
  3746. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3747. {
  3748. EXPECT_DECLS;
  3749. /* Set TLS 1.2 */
  3750. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3751. WOLFSSL_SUCCESS);
  3752. return EXPECT_RESULT();
  3753. }
  3754. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3755. * what was set at ctx creation. */
  3756. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3757. {
  3758. EXPECT_DECLS;
  3759. test_ssl_cbf client_cbs;
  3760. test_ssl_cbf server_cbs;
  3761. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3762. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3763. client_cbs.method = wolfTLS_client_method;
  3764. server_cbs.method = wolfTLS_server_method;
  3765. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3766. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3767. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3768. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3769. &server_cbs, NULL), TEST_SUCCESS);
  3770. return EXPECT_RESULT();
  3771. }
  3772. #else
  3773. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3774. {
  3775. return TEST_SKIPPED;
  3776. }
  3777. #endif
  3778. /*----------------------------------------------------------------------------*
  3779. | SSL
  3780. *----------------------------------------------------------------------------*/
  3781. static int test_server_wolfSSL_new(void)
  3782. {
  3783. EXPECT_DECLS;
  3784. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3785. !defined(NO_WOLFSSL_SERVER)
  3786. WOLFSSL_CTX *ctx = NULL;
  3787. WOLFSSL_CTX *ctx_nocert = NULL;
  3788. WOLFSSL *ssl = NULL;
  3789. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3790. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3791. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3792. WOLFSSL_FILETYPE_PEM));
  3793. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3794. WOLFSSL_FILETYPE_PEM));
  3795. /* invalid context */
  3796. ExpectNull(ssl = wolfSSL_new(NULL));
  3797. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3798. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  3799. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3800. #endif
  3801. /* success */
  3802. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3803. wolfSSL_free(ssl);
  3804. wolfSSL_CTX_free(ctx);
  3805. wolfSSL_CTX_free(ctx_nocert);
  3806. #endif
  3807. return EXPECT_RESULT();
  3808. }
  3809. static int test_client_wolfSSL_new(void)
  3810. {
  3811. EXPECT_DECLS;
  3812. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3813. !defined(NO_WOLFSSL_CLIENT)
  3814. WOLFSSL_CTX *ctx = NULL;
  3815. WOLFSSL_CTX *ctx_nocert = NULL;
  3816. WOLFSSL *ssl = NULL;
  3817. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3818. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3819. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3820. /* invalid context */
  3821. ExpectNull(ssl = wolfSSL_new(NULL));
  3822. /* success */
  3823. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3824. wolfSSL_free(ssl);
  3825. ssl = NULL;
  3826. /* success */
  3827. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3828. wolfSSL_free(ssl);
  3829. wolfSSL_CTX_free(ctx);
  3830. wolfSSL_CTX_free(ctx_nocert);
  3831. #endif
  3832. return EXPECT_RESULT();
  3833. }
  3834. static int test_wolfSSL_SetTmpDH_file(void)
  3835. {
  3836. EXPECT_DECLS;
  3837. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3838. !defined(NO_WOLFSSL_SERVER)
  3839. WOLFSSL_CTX *ctx = NULL;
  3840. WOLFSSL *ssl = NULL;
  3841. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3842. #ifndef NO_RSA
  3843. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3844. WOLFSSL_FILETYPE_PEM));
  3845. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3846. WOLFSSL_FILETYPE_PEM));
  3847. #elif defined(HAVE_ECC)
  3848. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3849. WOLFSSL_FILETYPE_PEM));
  3850. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3851. WOLFSSL_FILETYPE_PEM));
  3852. #elif defined(HAVE_ED25519)
  3853. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3854. WOLFSSL_FILETYPE_PEM));
  3855. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3856. WOLFSSL_FILETYPE_PEM));
  3857. #elif defined(HAVE_ED448)
  3858. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3859. WOLFSSL_FILETYPE_PEM));
  3860. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3861. WOLFSSL_FILETYPE_PEM));
  3862. #endif
  3863. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3864. /* invalid ssl */
  3865. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3866. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3867. /* invalid dhParamFile file */
  3868. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3869. NULL, WOLFSSL_FILETYPE_PEM));
  3870. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3871. bogusFile, WOLFSSL_FILETYPE_PEM));
  3872. /* success */
  3873. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3874. WOLFSSL_FILETYPE_PEM));
  3875. wolfSSL_free(ssl);
  3876. wolfSSL_CTX_free(ctx);
  3877. #endif
  3878. return EXPECT_RESULT();
  3879. }
  3880. static int test_wolfSSL_SetTmpDH_buffer(void)
  3881. {
  3882. EXPECT_DECLS;
  3883. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3884. WOLFSSL_CTX *ctx = NULL;
  3885. WOLFSSL *ssl = NULL;
  3886. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3887. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3888. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3889. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3890. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3891. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3892. /* invalid ssl */
  3893. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3894. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3895. /* invalid dhParamFile file */
  3896. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3897. 0, WOLFSSL_FILETYPE_ASN1));
  3898. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3899. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3900. /* success */
  3901. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3902. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3903. wolfSSL_free(ssl);
  3904. wolfSSL_CTX_free(ctx);
  3905. #endif
  3906. return EXPECT_RESULT();
  3907. }
  3908. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3909. {
  3910. EXPECT_DECLS;
  3911. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3912. WOLFSSL_CTX *ctx = NULL;
  3913. WOLFSSL_CTX *ctx2 = NULL;
  3914. WOLFSSL *ssl = NULL;
  3915. WOLFSSL *ssl2 = NULL;
  3916. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3917. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3918. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3919. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3920. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3921. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3922. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3923. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3924. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3925. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3926. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3927. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3928. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3929. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3930. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3931. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3932. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3933. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3934. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3935. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3936. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3937. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3938. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3939. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3940. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3941. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3942. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3943. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3944. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3945. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3946. wolfSSL_free(ssl2);
  3947. wolfSSL_CTX_free(ctx2);
  3948. wolfSSL_free(ssl);
  3949. wolfSSL_CTX_free(ctx);
  3950. #endif
  3951. return EXPECT_RESULT();
  3952. }
  3953. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3954. * allowed.
  3955. * POST: return 1 on success.
  3956. */
  3957. static int test_wolfSSL_SetMinVersion(void)
  3958. {
  3959. int res = TEST_SKIPPED;
  3960. #ifndef NO_WOLFSSL_CLIENT
  3961. int failFlag = WOLFSSL_SUCCESS;
  3962. WOLFSSL_CTX* ctx = NULL;
  3963. WOLFSSL* ssl = NULL;
  3964. int itr;
  3965. #ifndef NO_OLD_TLS
  3966. const int versions[] = {
  3967. #ifdef WOLFSSL_ALLOW_TLSV10
  3968. WOLFSSL_TLSV1,
  3969. #endif
  3970. WOLFSSL_TLSV1_1,
  3971. WOLFSSL_TLSV1_2};
  3972. #elif !defined(WOLFSSL_NO_TLS12)
  3973. const int versions[] = { WOLFSSL_TLSV1_2 };
  3974. #else
  3975. const int versions[] = { WOLFSSL_TLSV1_3 };
  3976. #endif
  3977. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3978. ssl = wolfSSL_new(ctx);
  3979. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3980. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3981. failFlag = WOLFSSL_FAILURE;
  3982. }
  3983. }
  3984. wolfSSL_free(ssl);
  3985. wolfSSL_CTX_free(ctx);
  3986. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3987. #endif
  3988. return res;
  3989. } /* END test_wolfSSL_SetMinVersion */
  3990. #ifdef OPENSSL_EXTRA
  3991. static int test_ED25519(void)
  3992. {
  3993. EXPECT_DECLS;
  3994. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3995. defined(WOLFSSL_KEY_GEN)
  3996. byte priv[ED25519_PRV_KEY_SIZE];
  3997. unsigned int privSz = (unsigned int)sizeof(priv);
  3998. byte pub[ED25519_PUB_KEY_SIZE];
  3999. unsigned int pubSz = (unsigned int)sizeof(pub);
  4000. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4001. const char* msg = TEST_STRING;
  4002. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4003. byte sig[ED25519_SIG_SIZE];
  4004. unsigned int sigSz = (unsigned int)sizeof(sig);
  4005. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4006. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4007. WOLFSSL_SUCCESS);
  4008. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4009. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4010. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4011. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4012. &sigSz), WOLFSSL_SUCCESS);
  4013. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4014. #ifdef HAVE_ED25519_VERIFY
  4015. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4016. sigSz), WOLFSSL_SUCCESS);
  4017. #endif /* HAVE_ED25519_VERIFY */
  4018. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4019. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4020. return EXPECT_RESULT();
  4021. }
  4022. static int test_ED448(void)
  4023. {
  4024. EXPECT_DECLS;
  4025. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4026. defined(WOLFSSL_KEY_GEN)
  4027. byte priv[ED448_PRV_KEY_SIZE];
  4028. unsigned int privSz = (unsigned int)sizeof(priv);
  4029. byte pub[ED448_PUB_KEY_SIZE];
  4030. unsigned int pubSz = (unsigned int)sizeof(pub);
  4031. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4032. const char* msg = TEST_STRING;
  4033. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4034. byte sig[ED448_SIG_SIZE];
  4035. unsigned int sigSz = (unsigned int)sizeof(sig);
  4036. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4037. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4038. WOLFSSL_SUCCESS);
  4039. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4040. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4041. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4042. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4043. &sigSz), WOLFSSL_SUCCESS);
  4044. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4045. #ifdef HAVE_ED448_VERIFY
  4046. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4047. sigSz), WOLFSSL_SUCCESS);
  4048. #endif /* HAVE_ED448_VERIFY */
  4049. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4050. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4051. return EXPECT_RESULT();
  4052. }
  4053. #endif /* OPENSSL_EXTRA */
  4054. #include <wolfssl/openssl/pem.h>
  4055. /*----------------------------------------------------------------------------*
  4056. | EVP
  4057. *----------------------------------------------------------------------------*/
  4058. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4059. {
  4060. EXPECT_DECLS;
  4061. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4062. WOLFSSL_BIO* rbio = NULL;
  4063. WOLFSSL_BIO* wbio = NULL;
  4064. WOLFSSL_EVP_PKEY* pkey = NULL;
  4065. char line[256] = { 0 };
  4066. char line1[256] = { 0 };
  4067. int i;
  4068. /* test error cases */
  4069. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4070. /*
  4071. * test RSA public key print
  4072. * in this test, pass '3' for indent
  4073. */
  4074. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4075. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4076. sizeof_client_keypub_der_1024));
  4077. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4078. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4079. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4080. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4081. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4082. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4083. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4084. strcpy(line1, " Modulus:\n");
  4085. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4086. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4087. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4088. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4089. /* skip to the end of modulus element*/
  4090. for (i = 0; i < 8 ;i++) {
  4091. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4092. }
  4093. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4094. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4095. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4096. /* should reach EOF */
  4097. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4098. EVP_PKEY_free(pkey);
  4099. pkey = NULL;
  4100. BIO_free(rbio);
  4101. BIO_free(wbio);
  4102. rbio = NULL;
  4103. wbio = NULL;
  4104. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4105. /*
  4106. * test DSA public key print
  4107. */
  4108. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4109. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4110. sizeof_dsa_pub_key_der_2048));
  4111. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4112. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4113. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4114. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4115. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4116. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4117. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4118. strcpy(line1, "pub:\n");
  4119. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4120. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4121. strcpy(line1,
  4122. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4123. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4124. /* skip to the end of pub element*/
  4125. for (i = 0; i < 17 ;i++) {
  4126. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4127. }
  4128. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4129. strcpy(line1, "P:\n");
  4130. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4131. /* skip to the end of P element*/
  4132. for (i = 0; i < 18 ;i++) {
  4133. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4134. }
  4135. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4136. strcpy(line1, "Q:\n");
  4137. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4138. /* skip to the end of Q element*/
  4139. for (i = 0; i < 3 ;i++) {
  4140. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4141. }
  4142. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4143. strcpy(line1, "G:\n");
  4144. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4145. /* skip to the end of G element*/
  4146. for (i = 0; i < 18 ;i++) {
  4147. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4148. }
  4149. /* should reach EOF */
  4150. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4151. EVP_PKEY_free(pkey);
  4152. pkey = NULL;
  4153. BIO_free(rbio);
  4154. BIO_free(wbio);
  4155. rbio = NULL;
  4156. wbio = NULL;
  4157. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4158. /*
  4159. * test ECC public key print
  4160. */
  4161. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4162. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4163. sizeof_ecc_clikeypub_der_256));
  4164. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4165. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4166. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4167. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4168. strcpy(line1, "Public-Key: (256 bit)\n");
  4169. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4170. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4171. strcpy(line1, "pub:\n");
  4172. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4173. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4174. strcpy(line1,
  4175. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4176. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4177. /* skip to the end of pub element*/
  4178. for (i = 0; i < 4 ;i++) {
  4179. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4180. }
  4181. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4182. strcpy(line1, "ASN1 OID: prime256v1\n");
  4183. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4184. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4185. strcpy(line1, "NIST CURVE: P-256\n");
  4186. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4187. /* should reach EOF */
  4188. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4189. EVP_PKEY_free(pkey);
  4190. pkey = NULL;
  4191. BIO_free(rbio);
  4192. BIO_free(wbio);
  4193. rbio = NULL;
  4194. wbio = NULL;
  4195. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4196. /*
  4197. * test DH public key print
  4198. */
  4199. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4200. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4201. sizeof_dh_pub_key_der_2048));
  4202. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4203. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4204. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4205. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4206. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4207. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4208. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4209. strcpy(line1, "public-key:\n");
  4210. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4211. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4212. strcpy(line1,
  4213. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4214. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4215. /* skip to the end of public-key element*/
  4216. for (i = 0; i < 17 ;i++) {
  4217. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4218. }
  4219. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4220. strcpy(line1, "prime:\n");
  4221. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4222. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4223. strcpy(line1,
  4224. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4225. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4226. /* skip to the end of prime element*/
  4227. for (i = 0; i < 17 ;i++) {
  4228. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4229. }
  4230. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4231. strcpy(line1, "generator: 2 (0x02)\n");
  4232. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4233. /* should reach EOF */
  4234. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4235. EVP_PKEY_free(pkey);
  4236. pkey = NULL;
  4237. BIO_free(rbio);
  4238. BIO_free(wbio);
  4239. rbio = NULL;
  4240. wbio = NULL;
  4241. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4242. /* to prevent "unused variable" warning */
  4243. (void)pkey;
  4244. (void)wbio;
  4245. (void)rbio;
  4246. (void)line;
  4247. (void)line1;
  4248. (void)i;
  4249. #endif /* OPENSSL_EXTRA */
  4250. return EXPECT_RESULT();
  4251. }
  4252. /* Test functions for base64 encode/decode */
  4253. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4254. {
  4255. EXPECT_DECLS;
  4256. #if defined(OPENSSL_EXTRA) && \
  4257. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4258. EVP_ENCODE_CTX* ctx = NULL;
  4259. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4260. ExpectIntEQ(ctx->remaining,0);
  4261. ExpectIntEQ(ctx->data[0],0);
  4262. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4263. EVP_ENCODE_CTX_free(ctx);
  4264. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4265. return EXPECT_RESULT();
  4266. }
  4267. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4268. {
  4269. EXPECT_DECLS;
  4270. #if defined(OPENSSL_EXTRA) && \
  4271. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4272. EVP_ENCODE_CTX* ctx = NULL;
  4273. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4274. EVP_ENCODE_CTX_free(ctx);
  4275. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4276. return EXPECT_RESULT();
  4277. }
  4278. static int test_wolfSSL_EVP_EncodeInit(void)
  4279. {
  4280. EXPECT_DECLS;
  4281. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4282. EVP_ENCODE_CTX* ctx = NULL;
  4283. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4284. ExpectIntEQ(ctx->remaining, 0);
  4285. ExpectIntEQ(ctx->data[0], 0);
  4286. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4287. if (ctx != NULL) {
  4288. /* make ctx dirty */
  4289. ctx->remaining = 10;
  4290. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4291. }
  4292. EVP_EncodeInit(ctx);
  4293. ExpectIntEQ(ctx->remaining, 0);
  4294. ExpectIntEQ(ctx->data[0], 0);
  4295. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4296. EVP_ENCODE_CTX_free(ctx);
  4297. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4298. return EXPECT_RESULT();
  4299. }
  4300. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4301. {
  4302. EXPECT_DECLS;
  4303. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4304. int outl;
  4305. int total;
  4306. const unsigned char plain0[] = {"Th"};
  4307. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4308. const unsigned char plain2[] = {"This is additional data."};
  4309. const unsigned char encBlock0[] = {"VGg="};
  4310. const unsigned char enc0[] = {"VGg=\n"};
  4311. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4312. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4313. const unsigned char enc2[] =
  4314. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4315. unsigned char encOutBuff[300];
  4316. EVP_ENCODE_CTX* ctx = NULL;
  4317. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4318. EVP_EncodeInit(ctx);
  4319. /* illegal parameter test */
  4320. ExpectIntEQ(
  4321. EVP_EncodeUpdate(
  4322. NULL, /* pass NULL as ctx */
  4323. encOutBuff,
  4324. &outl,
  4325. plain1,
  4326. sizeof(plain1)-1),
  4327. 0 /* expected result code 0: fail */
  4328. );
  4329. ExpectIntEQ(
  4330. EVP_EncodeUpdate(
  4331. ctx,
  4332. NULL, /* pass NULL as out buff */
  4333. &outl,
  4334. plain1,
  4335. sizeof(plain1)-1),
  4336. 0 /* expected result code 0: fail */
  4337. );
  4338. ExpectIntEQ(
  4339. EVP_EncodeUpdate(
  4340. ctx,
  4341. encOutBuff,
  4342. NULL, /* pass NULL as outl */
  4343. plain1,
  4344. sizeof(plain1)-1),
  4345. 0 /* expected result code 0: fail */
  4346. );
  4347. ExpectIntEQ(
  4348. EVP_EncodeUpdate(
  4349. ctx,
  4350. encOutBuff,
  4351. &outl,
  4352. NULL, /* pass NULL as in */
  4353. sizeof(plain1)-1),
  4354. 0 /* expected result code 0: fail */
  4355. );
  4356. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4357. /* meaningless parameter test */
  4358. ExpectIntEQ(
  4359. EVP_EncodeUpdate(
  4360. ctx,
  4361. encOutBuff,
  4362. &outl,
  4363. plain1,
  4364. 0), /* pass zero input */
  4365. 1 /* expected result code 1: success */
  4366. );
  4367. /* very small data encoding test */
  4368. EVP_EncodeInit(ctx);
  4369. ExpectIntEQ(
  4370. EVP_EncodeUpdate(
  4371. ctx,
  4372. encOutBuff,
  4373. &outl,
  4374. plain0,
  4375. sizeof(plain0)-1),
  4376. 1 /* expected result code 1: success */
  4377. );
  4378. ExpectIntEQ(outl,0);
  4379. if (EXPECT_SUCCESS()) {
  4380. EVP_EncodeFinal(
  4381. ctx,
  4382. encOutBuff + outl,
  4383. &outl);
  4384. }
  4385. ExpectIntEQ( outl, sizeof(enc0)-1);
  4386. ExpectIntEQ(
  4387. XSTRNCMP(
  4388. (const char*)encOutBuff,
  4389. (const char*)enc0,sizeof(enc0) ),
  4390. 0);
  4391. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4392. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4393. sizeof(encBlock0)-1);
  4394. ExpectStrEQ(encOutBuff, encBlock0);
  4395. /* pass small size( < 48bytes ) input, then make sure they are not
  4396. * encoded and just stored in ctx
  4397. */
  4398. EVP_EncodeInit(ctx);
  4399. total = 0;
  4400. outl = 0;
  4401. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4402. ExpectIntEQ(
  4403. EVP_EncodeUpdate(
  4404. ctx,
  4405. encOutBuff, /* buffer for output */
  4406. &outl, /* size of output */
  4407. plain1, /* input */
  4408. sizeof(plain1)-1), /* size of input */
  4409. 1); /* expected result code 1:success */
  4410. total += outl;
  4411. ExpectIntEQ(outl, 0); /* no output expected */
  4412. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4413. ExpectTrue(
  4414. XSTRNCMP((const char*)(ctx->data),
  4415. (const char*)plain1,
  4416. ctx->remaining) ==0 );
  4417. ExpectTrue(encOutBuff[0] == 0);
  4418. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4419. * the stored data and the new input together
  4420. */
  4421. ExpectIntEQ(
  4422. EVP_EncodeUpdate(
  4423. ctx,
  4424. encOutBuff + outl, /* buffer for output */
  4425. &outl, /* size of output */
  4426. plain2, /* additional input */
  4427. sizeof(plain2) -1), /* size of additional input */
  4428. 1); /* expected result code 1:success */
  4429. total += outl;
  4430. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4431. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4432. ExpectIntEQ(
  4433. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4434. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4435. EVP_EncodeFinal(
  4436. ctx,
  4437. encOutBuff + outl,
  4438. &outl);
  4439. total += outl;
  4440. ExpectIntNE(total,0);
  4441. ExpectIntNE(outl,0);
  4442. ExpectIntEQ(XSTRNCMP(
  4443. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4444. /* test with illeagal parameters */
  4445. outl = 1;
  4446. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4447. ExpectIntEQ(outl, 0);
  4448. outl = 1;
  4449. EVP_EncodeFinal(ctx, NULL, &outl);
  4450. ExpectIntEQ(outl, 0);
  4451. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4452. EVP_EncodeFinal(NULL, NULL, NULL);
  4453. EVP_ENCODE_CTX_free(ctx);
  4454. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4455. return EXPECT_RESULT();
  4456. }
  4457. static int test_wolfSSL_EVP_EncodeFinal(void)
  4458. {
  4459. int res = TEST_SKIPPED;
  4460. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4461. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4462. * test_wolfSSL_EVP_EncodeUpdate
  4463. */
  4464. res = TEST_SUCCESS;
  4465. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4466. return res;
  4467. }
  4468. static int test_wolfSSL_EVP_DecodeInit(void)
  4469. {
  4470. EXPECT_DECLS;
  4471. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4472. EVP_ENCODE_CTX* ctx = NULL;
  4473. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4474. ExpectIntEQ( ctx->remaining,0);
  4475. ExpectIntEQ( ctx->data[0],0);
  4476. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4477. if (ctx != NULL) {
  4478. /* make ctx dirty */
  4479. ctx->remaining = 10;
  4480. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4481. }
  4482. EVP_DecodeInit(ctx);
  4483. ExpectIntEQ( ctx->remaining,0);
  4484. ExpectIntEQ( ctx->data[0],0);
  4485. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4486. EVP_ENCODE_CTX_free(ctx);
  4487. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4488. return EXPECT_RESULT();
  4489. }
  4490. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4491. {
  4492. EXPECT_DECLS;
  4493. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4494. int outl;
  4495. unsigned char decOutBuff[300];
  4496. EVP_ENCODE_CTX* ctx = NULL;
  4497. static const unsigned char enc1[] =
  4498. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4499. /* const unsigned char plain1[] =
  4500. {"This is a base64 decoding test."} */
  4501. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4502. EVP_DecodeInit(ctx);
  4503. /* illegal parameter tests */
  4504. /* pass NULL as ctx */
  4505. ExpectIntEQ(
  4506. EVP_DecodeUpdate(
  4507. NULL, /* pass NULL as ctx */
  4508. decOutBuff,
  4509. &outl,
  4510. enc1,
  4511. sizeof(enc1)-1),
  4512. -1 /* expected result code -1: fail */
  4513. );
  4514. ExpectIntEQ( outl, 0);
  4515. /* pass NULL as output */
  4516. ExpectIntEQ(
  4517. EVP_DecodeUpdate(
  4518. ctx,
  4519. NULL, /* pass NULL as out buff */
  4520. &outl,
  4521. enc1,
  4522. sizeof(enc1)-1),
  4523. -1 /* expected result code -1: fail */
  4524. );
  4525. ExpectIntEQ( outl, 0);
  4526. /* pass NULL as outl */
  4527. ExpectIntEQ(
  4528. EVP_DecodeUpdate(
  4529. ctx,
  4530. decOutBuff,
  4531. NULL, /* pass NULL as outl */
  4532. enc1,
  4533. sizeof(enc1)-1),
  4534. -1 /* expected result code -1: fail */
  4535. );
  4536. /* pass NULL as input */
  4537. ExpectIntEQ(
  4538. EVP_DecodeUpdate(
  4539. ctx,
  4540. decOutBuff,
  4541. &outl,
  4542. NULL, /* pass NULL as in */
  4543. sizeof(enc1)-1),
  4544. -1 /* expected result code -1: fail */
  4545. );
  4546. ExpectIntEQ( outl, 0);
  4547. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4548. /* pass zero length input */
  4549. ExpectIntEQ(
  4550. EVP_DecodeUpdate(
  4551. ctx,
  4552. decOutBuff,
  4553. &outl,
  4554. enc1,
  4555. 0), /* pass zero as input len */
  4556. 1 /* expected result code 1: success */
  4557. );
  4558. /* decode correct base64 string */
  4559. {
  4560. static const unsigned char enc2[] =
  4561. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4562. static const unsigned char plain2[] =
  4563. {"This is a base64 decoding test."};
  4564. EVP_EncodeInit(ctx);
  4565. ExpectIntEQ(
  4566. EVP_DecodeUpdate(
  4567. ctx,
  4568. decOutBuff,
  4569. &outl,
  4570. enc2,
  4571. sizeof(enc2)-1),
  4572. 0 /* expected result code 0: success */
  4573. );
  4574. ExpectIntEQ(outl,sizeof(plain2) -1);
  4575. ExpectIntEQ(
  4576. EVP_DecodeFinal(
  4577. ctx,
  4578. decOutBuff + outl,
  4579. &outl),
  4580. 1 /* expected result code 1: success */
  4581. );
  4582. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4583. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4584. sizeof(plain2) -1 ),0);
  4585. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4586. sizeof(plain2)-1);
  4587. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4588. sizeof(plain2) -1 ),0);
  4589. }
  4590. /* decode correct base64 string which does not have '\n' in its last*/
  4591. {
  4592. static const unsigned char enc3[] =
  4593. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4594. static const unsigned char plain3[] =
  4595. {"This is a base64 decoding test."}; /* 31 chars */
  4596. EVP_EncodeInit(ctx);
  4597. ExpectIntEQ(
  4598. EVP_DecodeUpdate(
  4599. ctx,
  4600. decOutBuff,
  4601. &outl,
  4602. enc3,
  4603. sizeof(enc3)-1),
  4604. 0 /* expected result code 0: success */
  4605. );
  4606. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4607. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4608. sizeof(plain3) -1 ),0);
  4609. ExpectIntEQ(
  4610. EVP_DecodeFinal(
  4611. ctx,
  4612. decOutBuff + outl,
  4613. &outl),
  4614. 1 /* expected result code 1: success */
  4615. );
  4616. ExpectIntEQ(outl,0 );
  4617. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4618. sizeof(plain3)-1);
  4619. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4620. sizeof(plain3) -1 ),0);
  4621. }
  4622. /* decode string which has a padding char ('=') in the illegal position*/
  4623. {
  4624. static const unsigned char enc4[] =
  4625. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4626. EVP_EncodeInit(ctx);
  4627. ExpectIntEQ(
  4628. EVP_DecodeUpdate(
  4629. ctx,
  4630. decOutBuff,
  4631. &outl,
  4632. enc4,
  4633. sizeof(enc4)-1),
  4634. -1 /* expected result code -1: error */
  4635. );
  4636. ExpectIntEQ(outl,0);
  4637. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4638. }
  4639. /* small data decode test */
  4640. {
  4641. static const unsigned char enc00[] = {"VG"};
  4642. static const unsigned char enc01[] = {"g=\n"};
  4643. static const unsigned char plain4[] = {"Th"};
  4644. EVP_EncodeInit(ctx);
  4645. ExpectIntEQ(
  4646. EVP_DecodeUpdate(
  4647. ctx,
  4648. decOutBuff,
  4649. &outl,
  4650. enc00,
  4651. sizeof(enc00)-1),
  4652. 1 /* expected result code 1: success */
  4653. );
  4654. ExpectIntEQ(outl,0);
  4655. ExpectIntEQ(
  4656. EVP_DecodeUpdate(
  4657. ctx,
  4658. decOutBuff + outl,
  4659. &outl,
  4660. enc01,
  4661. sizeof(enc01)-1),
  4662. 0 /* expected result code 0: success */
  4663. );
  4664. ExpectIntEQ(outl,sizeof(plain4)-1);
  4665. /* test with illegal parameters */
  4666. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4667. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4668. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4669. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4670. if (EXPECT_SUCCESS()) {
  4671. EVP_DecodeFinal(
  4672. ctx,
  4673. decOutBuff + outl,
  4674. &outl);
  4675. }
  4676. ExpectIntEQ( outl, 0);
  4677. ExpectIntEQ(
  4678. XSTRNCMP(
  4679. (const char*)decOutBuff,
  4680. (const char*)plain4,sizeof(plain4)-1 ),
  4681. 0);
  4682. }
  4683. EVP_ENCODE_CTX_free(ctx);
  4684. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4685. return EXPECT_RESULT();
  4686. }
  4687. static int test_wolfSSL_EVP_DecodeFinal(void)
  4688. {
  4689. int res = TEST_SKIPPED;
  4690. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4691. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4692. * test_wolfSSL_EVP_DecodeUpdate
  4693. */
  4694. res = TEST_SUCCESS;
  4695. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4696. return res;
  4697. }
  4698. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4699. */
  4700. #ifdef OPENSSL_EXTRA
  4701. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4702. {
  4703. EXPECT_DECLS;
  4704. #ifndef NO_AES
  4705. const WOLFSSL_EVP_CIPHER* c;
  4706. c = wolfSSL_EVP_get_cipherbynid(419);
  4707. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4708. defined(WOLFSSL_AES_128)
  4709. ExpectNotNull(c);
  4710. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  4711. #else
  4712. ExpectNull(c);
  4713. #endif
  4714. c = wolfSSL_EVP_get_cipherbynid(423);
  4715. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4716. defined(WOLFSSL_AES_192)
  4717. ExpectNotNull(c);
  4718. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  4719. #else
  4720. ExpectNull(c);
  4721. #endif
  4722. c = wolfSSL_EVP_get_cipherbynid(427);
  4723. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4724. defined(WOLFSSL_AES_256)
  4725. ExpectNotNull(c);
  4726. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  4727. #else
  4728. ExpectNull(c);
  4729. #endif
  4730. c = wolfSSL_EVP_get_cipherbynid(904);
  4731. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4732. ExpectNotNull(c);
  4733. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  4734. #else
  4735. ExpectNull(c);
  4736. #endif
  4737. c = wolfSSL_EVP_get_cipherbynid(905);
  4738. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4739. ExpectNotNull(c);
  4740. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  4741. #else
  4742. ExpectNull(c);
  4743. #endif
  4744. c = wolfSSL_EVP_get_cipherbynid(906);
  4745. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4746. ExpectNotNull(c);
  4747. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  4748. #else
  4749. ExpectNull(c);
  4750. #endif
  4751. c = wolfSSL_EVP_get_cipherbynid(418);
  4752. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4753. ExpectNotNull(c);
  4754. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  4755. #else
  4756. ExpectNull(c);
  4757. #endif
  4758. c = wolfSSL_EVP_get_cipherbynid(422);
  4759. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4760. ExpectNotNull(c);
  4761. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  4762. #else
  4763. ExpectNull(c);
  4764. #endif
  4765. c = wolfSSL_EVP_get_cipherbynid(426);
  4766. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4767. ExpectNotNull(c);
  4768. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  4769. #else
  4770. ExpectNull(c);
  4771. #endif
  4772. #endif /* !NO_AES */
  4773. #ifndef NO_DES3
  4774. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4775. #ifdef WOLFSSL_DES_ECB
  4776. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4777. #endif
  4778. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4779. #ifdef WOLFSSL_DES_ECB
  4780. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4781. #endif
  4782. #endif /* !NO_DES3 */
  4783. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4784. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4785. #endif
  4786. /* test for nid is out of range */
  4787. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  4788. return EXPECT_RESULT();
  4789. }
  4790. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4791. {
  4792. EXPECT_DECLS;
  4793. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4794. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4795. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4796. const EVP_CIPHER *test;
  4797. byte key[AES_BLOCK_SIZE] = {0};
  4798. byte iv[AES_BLOCK_SIZE] = {0};
  4799. ExpectNotNull(ctx);
  4800. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4801. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4802. test = EVP_CIPHER_CTX_cipher(ctx);
  4803. ExpectTrue(init == test);
  4804. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4805. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4806. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4807. EVP_CIPHER_CTX_free(ctx);
  4808. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4809. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4810. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4811. return EXPECT_RESULT();
  4812. }
  4813. #endif /* OPENSSL_EXTRA */
  4814. /*----------------------------------------------------------------------------*
  4815. | IO
  4816. *----------------------------------------------------------------------------*/
  4817. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4818. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4819. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4820. #ifdef WC_SHA512_DIGEST_SIZE
  4821. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4822. #else
  4823. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4824. #endif
  4825. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4826. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4827. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4828. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4829. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4830. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4831. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4832. defined(HAVE_AES_CBC)
  4833. typedef struct openssl_key_ctx {
  4834. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4835. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4836. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4837. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4838. } openssl_key_ctx;
  4839. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4840. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4841. static WC_INLINE int OpenSSLTicketInit(void)
  4842. {
  4843. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4844. if (ret != 0) return ret;
  4845. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4846. sizeof(myOpenSSLKey_ctx.name));
  4847. if (ret != 0) return ret;
  4848. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4849. sizeof(myOpenSSLKey_ctx.key));
  4850. if (ret != 0) return ret;
  4851. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4852. sizeof(myOpenSSLKey_ctx.hmacKey));
  4853. if (ret != 0) return ret;
  4854. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4855. sizeof(myOpenSSLKey_ctx.iv));
  4856. if (ret != 0) return ret;
  4857. return 0;
  4858. }
  4859. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4860. byte name[WOLFSSL_TICKET_NAME_SZ],
  4861. byte iv[WOLFSSL_TICKET_IV_SZ],
  4862. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4863. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4864. (void)ssl;
  4865. if (enc) {
  4866. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4867. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4868. }
  4869. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4870. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4871. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4872. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4873. return 0;
  4874. }
  4875. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4876. if (enc)
  4877. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4878. else
  4879. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4880. return 1;
  4881. }
  4882. static WC_INLINE void OpenSSLTicketCleanup(void)
  4883. {
  4884. wc_FreeRng(&myOpenSSLKey_rng);
  4885. }
  4886. #endif
  4887. #endif
  4888. /* helper functions */
  4889. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4890. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4891. void *ctx)
  4892. {
  4893. struct test_ssl_memio_ctx *test_ctx;
  4894. byte *buf;
  4895. int *len;
  4896. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4897. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4898. buf = test_ctx->c_buff;
  4899. len = &test_ctx->c_len;
  4900. }
  4901. else {
  4902. buf = test_ctx->s_buff;
  4903. len = &test_ctx->s_len;
  4904. }
  4905. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4906. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4907. XMEMCPY(buf + *len, data, sz);
  4908. *len += sz;
  4909. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  4910. {
  4911. /* This can be imported into Wireshark by transforming the file with
  4912. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  4913. * And then loading test_output.dump.hex into Wireshark using the
  4914. * "Import from Hex Dump..." option ion and selecting the TCP
  4915. * encapsulation option. */
  4916. char dump_file_name[64];
  4917. WOLFSSL_BIO *dump_file;
  4918. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  4919. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  4920. if (dump_file != NULL) {
  4921. (void)wolfSSL_BIO_write(dump_file, data, sz);
  4922. wolfSSL_BIO_free(dump_file);
  4923. }
  4924. }
  4925. #endif
  4926. return sz;
  4927. }
  4928. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4929. void *ctx)
  4930. {
  4931. struct test_ssl_memio_ctx *test_ctx;
  4932. int read_sz;
  4933. byte *buf;
  4934. int *len;
  4935. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4936. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4937. buf = test_ctx->s_buff;
  4938. len = &test_ctx->s_len;
  4939. }
  4940. else {
  4941. buf = test_ctx->c_buff;
  4942. len = &test_ctx->c_len;
  4943. }
  4944. if (*len == 0)
  4945. return WOLFSSL_CBIO_ERR_WANT_READ;
  4946. read_sz = sz < *len ? sz : *len;
  4947. XMEMCPY(data, buf, read_sz);
  4948. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4949. *len -= read_sz;
  4950. return read_sz;
  4951. }
  4952. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4953. {
  4954. EXPECT_DECLS;
  4955. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4956. int c_sharedCtx = 0;
  4957. int s_sharedCtx = 0;
  4958. #endif
  4959. const char* certFile = svrCertFile;
  4960. const char* keyFile = svrKeyFile;
  4961. /********************************
  4962. * Create WOLFSSL_CTX for client.
  4963. ********************************/
  4964. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4965. if (ctx->c_ctx != NULL) {
  4966. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4967. }
  4968. else
  4969. #endif
  4970. {
  4971. WOLFSSL_METHOD* method = NULL;
  4972. if (ctx->c_cb.method != NULL) {
  4973. method = ctx->c_cb.method();
  4974. }
  4975. else {
  4976. method = wolfSSLv23_client_method();
  4977. }
  4978. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4979. }
  4980. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4981. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4982. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4983. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4984. #endif
  4985. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx, caCertFile, 0),
  4986. WOLFSSL_SUCCESS);
  4987. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4988. if (!c_sharedCtx)
  4989. #endif
  4990. {
  4991. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  4992. cliCertFile), WOLFSSL_SUCCESS);
  4993. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  4994. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4995. }
  4996. #ifdef HAVE_CRL
  4997. if (ctx->c_cb.crlPemFile != NULL) {
  4998. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  4999. WOLFSSL_SUCCESS);
  5000. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5001. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5002. }
  5003. #endif
  5004. if (ctx->c_ciphers != NULL) {
  5005. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5006. WOLFSSL_SUCCESS);
  5007. }
  5008. if (ctx->c_cb.ctx_ready != NULL) {
  5009. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5010. }
  5011. /********************************
  5012. * Create WOLFSSL_CTX for server.
  5013. ********************************/
  5014. if (ctx->s_ctx != NULL) {
  5015. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5016. s_sharedCtx = 1;
  5017. #endif
  5018. ctx->s_cb.isSharedCtx = 1;
  5019. }
  5020. else
  5021. {
  5022. WOLFSSL_METHOD* method = NULL;
  5023. if (ctx->s_cb.method != NULL) {
  5024. method = ctx->s_cb.method();
  5025. }
  5026. else {
  5027. method = wolfSSLv23_server_method();
  5028. }
  5029. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5030. ctx->s_cb.isSharedCtx = 0;
  5031. }
  5032. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5033. #if defined(HAVE_SESSION_TICKET) && \
  5034. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5035. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5036. OpenSSLTicketInit();
  5037. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5038. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5039. TicketInit();
  5040. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5041. #endif
  5042. #endif
  5043. }
  5044. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5045. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5046. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5047. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5048. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx, cliCertFile, 0),
  5049. WOLFSSL_SUCCESS);
  5050. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5051. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5052. #endif
  5053. if (ctx->s_cb.certPemFile != NULL) {
  5054. certFile = ctx->s_cb.certPemFile;
  5055. }
  5056. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5057. if (!s_sharedCtx)
  5058. #endif
  5059. {
  5060. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5061. certFile), WOLFSSL_SUCCESS);
  5062. }
  5063. if (ctx->s_cb.keyPemFile != NULL) {
  5064. keyFile = ctx->s_cb.keyPemFile;
  5065. }
  5066. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5067. if (!s_sharedCtx)
  5068. #endif
  5069. {
  5070. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  5071. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5072. }
  5073. if (ctx->s_ciphers != NULL) {
  5074. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5075. WOLFSSL_SUCCESS);
  5076. }
  5077. if (ctx->s_cb.ctx_ready != NULL) {
  5078. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5079. }
  5080. /****************************
  5081. * Create WOLFSSL for client.
  5082. ****************************/
  5083. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5084. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5085. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5086. if (0
  5087. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5088. || c_sharedCtx
  5089. #endif
  5090. )
  5091. {
  5092. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl, cliCertFile),
  5093. WOLFSSL_SUCCESS);
  5094. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  5095. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5096. }
  5097. if (ctx->c_cb.ssl_ready != NULL) {
  5098. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5099. }
  5100. /****************************
  5101. * Create WOLFSSL for server.
  5102. ****************************/
  5103. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5104. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5105. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5106. if (0
  5107. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5108. || s_sharedCtx
  5109. #endif
  5110. )
  5111. {
  5112. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl, certFile),
  5113. WOLFSSL_SUCCESS);
  5114. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  5115. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5116. }
  5117. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5118. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5119. #elif !defined(NO_DH)
  5120. /* will repick suites with DHE, higher priority than PSK */
  5121. SetDH(ctx->s_ssl);
  5122. #endif
  5123. if (ctx->s_cb.ssl_ready != NULL) {
  5124. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5125. }
  5126. return EXPECT_RESULT();
  5127. }
  5128. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5129. int* rounds)
  5130. {
  5131. int handshake_complete = 0;
  5132. int hs_c = 0;
  5133. int hs_s = 0;
  5134. int failing_s = 0;
  5135. int failing_c = 0;
  5136. int ret;
  5137. int err;
  5138. if (rounds != NULL) {
  5139. *rounds = 0;
  5140. }
  5141. while ((!handshake_complete) && (max_rounds > 0)) {
  5142. if (!hs_c) {
  5143. wolfSSL_SetLoggingPrefix("client");
  5144. ret = wolfSSL_connect(ctx->c_ssl);
  5145. wolfSSL_SetLoggingPrefix(NULL);
  5146. if (ret == WOLFSSL_SUCCESS) {
  5147. hs_c = 1;
  5148. }
  5149. else {
  5150. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5151. if (err != WOLFSSL_ERROR_WANT_READ &&
  5152. err != WOLFSSL_ERROR_WANT_WRITE) {
  5153. failing_c = 1;
  5154. hs_c = 1;
  5155. if (failing_c && failing_s) {
  5156. break;
  5157. }
  5158. }
  5159. }
  5160. }
  5161. if (!hs_s) {
  5162. wolfSSL_SetLoggingPrefix("server");
  5163. ret = wolfSSL_accept(ctx->s_ssl);
  5164. wolfSSL_SetLoggingPrefix(NULL);
  5165. if (ret == WOLFSSL_SUCCESS) {
  5166. hs_s = 1;
  5167. }
  5168. else {
  5169. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5170. if (err != WOLFSSL_ERROR_WANT_READ &&
  5171. err != WOLFSSL_ERROR_WANT_WRITE) {
  5172. failing_s = 1;
  5173. hs_s = 1;
  5174. if (failing_c && failing_s) {
  5175. break;
  5176. }
  5177. }
  5178. }
  5179. }
  5180. handshake_complete = hs_c && hs_s;
  5181. max_rounds--;
  5182. if (rounds != NULL) {
  5183. *rounds += 1;
  5184. }
  5185. }
  5186. if (!handshake_complete) {
  5187. return TEST_FAIL;
  5188. }
  5189. return TEST_SUCCESS;
  5190. }
  5191. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5192. {
  5193. EXPECT_DECLS;
  5194. char input[1024];
  5195. int idx = 0;
  5196. const char* msg_c = "hello wolfssl!";
  5197. int msglen_c = (int)XSTRLEN(msg_c);
  5198. const char* msg_s = "I hear you fa shizzle!";
  5199. int msglen_s = (int)XSTRLEN(msg_s);
  5200. if (ctx->c_msg != NULL) {
  5201. msg_c = ctx->c_msg;
  5202. msglen_c = ctx->c_msglen;
  5203. }
  5204. if (ctx->s_msg != NULL) {
  5205. msg_s = ctx->s_msg;
  5206. msglen_s = ctx->s_msglen;
  5207. }
  5208. wolfSSL_SetLoggingPrefix("client");
  5209. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5210. wolfSSL_SetLoggingPrefix("server");
  5211. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5212. if (idx >= 0) {
  5213. input[idx] = '\0';
  5214. }
  5215. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5216. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5217. ctx->s_cb.return_code = EXPECT_RESULT();
  5218. wolfSSL_SetLoggingPrefix("client");
  5219. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5220. wolfSSL_SetLoggingPrefix(NULL);
  5221. if (idx >= 0) {
  5222. input[idx] = '\0';
  5223. }
  5224. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5225. ctx->c_cb.return_code = EXPECT_RESULT();
  5226. if (ctx->c_cb.on_result != NULL) {
  5227. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5228. }
  5229. if (ctx->s_cb.on_result != NULL) {
  5230. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5231. }
  5232. return EXPECT_RESULT();
  5233. }
  5234. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5235. {
  5236. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5237. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5238. if (ctx->c_cb.on_cleanup != NULL) {
  5239. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5240. }
  5241. if (ctx->s_cb.on_cleanup != NULL) {
  5242. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5243. }
  5244. wolfSSL_shutdown(ctx->s_ssl);
  5245. wolfSSL_shutdown(ctx->c_ssl);
  5246. wolfSSL_free(ctx->s_ssl);
  5247. wolfSSL_free(ctx->c_ssl);
  5248. if (!ctx->s_cb.isSharedCtx) {
  5249. wolfSSL_CTX_free(ctx->s_ctx);
  5250. ctx->s_ctx = NULL;
  5251. }
  5252. if (!ctx->c_cb.isSharedCtx) {
  5253. wolfSSL_CTX_free(ctx->c_ctx);
  5254. ctx->c_ctx = NULL;
  5255. }
  5256. if (!ctx->s_cb.ticNoInit) {
  5257. #if defined(HAVE_SESSION_TICKET) && \
  5258. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5259. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5260. OpenSSLTicketCleanup();
  5261. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5262. TicketCleanup();
  5263. #endif
  5264. #endif
  5265. }
  5266. }
  5267. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5268. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5269. {
  5270. EXPECT_DECLS;
  5271. struct test_ssl_memio_ctx test_ctx;
  5272. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5273. size_t msg_len;
  5274. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5275. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5276. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5277. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5278. test_ctx.c_ctx = client_cb->ctx;
  5279. test_ctx.s_ctx = server_cb->ctx;
  5280. test_ctx.c_cb.return_code = TEST_FAIL;
  5281. test_ctx.s_cb.return_code = TEST_FAIL;
  5282. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5283. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5284. if (client_on_handshake != NULL) {
  5285. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5286. TEST_SUCCESS);
  5287. }
  5288. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5289. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5290. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5291. MD_MAX_SIZE);
  5292. ExpectIntGE(msg_len, 0);
  5293. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5294. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5295. MD_MAX_SIZE);
  5296. ExpectIntGE(msg_len, 0);
  5297. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5298. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5299. test_ssl_memio_cleanup(&test_ctx);
  5300. client_cb->return_code = test_ctx.c_cb.return_code;
  5301. client_cb->last_err = test_ctx.c_cb.last_err;
  5302. server_cb->return_code = test_ctx.s_cb.return_code;
  5303. server_cb->last_err = test_ctx.s_cb.last_err;
  5304. return EXPECT_RESULT();
  5305. }
  5306. #endif
  5307. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5308. #ifdef WOLFSSL_SESSION_EXPORT
  5309. #ifdef WOLFSSL_DTLS
  5310. /* set up function for sending session information */
  5311. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5312. {
  5313. WOLFSSL_CTX* ctx = NULL;
  5314. WOLFSSL* ssl = NULL;
  5315. AssertNotNull(inSsl);
  5316. AssertNotNull(buf);
  5317. AssertIntNE(0, sz);
  5318. /* Set ctx to DTLS 1.2 */
  5319. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5320. AssertNotNull(ctx);
  5321. ssl = wolfSSL_new(ctx);
  5322. AssertNotNull(ssl);
  5323. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5324. wolfSSL_free(ssl);
  5325. wolfSSL_CTX_free(ctx);
  5326. (void)userCtx;
  5327. return 0;
  5328. }
  5329. #endif
  5330. /* returns negative value on fail and positive (including 0) on success */
  5331. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5332. {
  5333. int ret, err, loop_count, count, timeout = 10;
  5334. char msg[] = "I hear you fa shizzle!";
  5335. char input[1024];
  5336. loop_count = ((func_args*)args)->argc;
  5337. #ifdef WOLFSSL_ASYNC_CRYPT
  5338. err = 0; /* Reset error */
  5339. #endif
  5340. do {
  5341. #ifdef WOLFSSL_ASYNC_CRYPT
  5342. if (err == WC_PENDING_E) {
  5343. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5344. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5345. }
  5346. #endif
  5347. ret = wolfSSL_accept(ssl);
  5348. err = wolfSSL_get_error(ssl, 0);
  5349. if (err == WOLFSSL_ERROR_WANT_READ ||
  5350. err == WOLFSSL_ERROR_WANT_WRITE) {
  5351. int select_ret;
  5352. err = WC_PENDING_E;
  5353. select_ret = tcp_select(*sockfd, timeout);
  5354. if (select_ret == TEST_TIMEOUT) {
  5355. return WOLFSSL_FATAL_ERROR;
  5356. }
  5357. }
  5358. } while (err == WC_PENDING_E);
  5359. if (ret != WOLFSSL_SUCCESS) {
  5360. char buff[WOLFSSL_MAX_ERROR_SZ];
  5361. fprintf(stderr, "error = %d, %s\n", err,
  5362. wolfSSL_ERR_error_string(err, buff));
  5363. return ret;
  5364. }
  5365. for (count = 0; count < loop_count; count++) {
  5366. int select_ret;
  5367. select_ret = tcp_select(*sockfd, timeout);
  5368. if (select_ret == TEST_TIMEOUT) {
  5369. ret = WOLFSSL_FATAL_ERROR;
  5370. break;
  5371. }
  5372. do {
  5373. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5374. if (ret > 0) {
  5375. input[ret] = '\0';
  5376. fprintf(stderr, "Client message: %s\n", input);
  5377. }
  5378. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5379. do {
  5380. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5381. return WOLFSSL_FATAL_ERROR;
  5382. }
  5383. err = wolfSSL_get_error(ssl, ret);
  5384. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5385. }
  5386. return ret;
  5387. }
  5388. #endif /* WOLFSSL_SESSION_EXPORT */
  5389. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5390. {
  5391. SOCKET_T sockfd = 0;
  5392. SOCKET_T clientfd = 0;
  5393. word16 port;
  5394. callback_functions* cbf;
  5395. WOLFSSL_CTX* ctx = NULL;
  5396. WOLFSSL* ssl = NULL;
  5397. func_args* opts = (func_args*)args;
  5398. char msg[] = "I hear you fa shizzle!";
  5399. char input[1024];
  5400. int idx;
  5401. int ret, err = 0;
  5402. int sharedCtx = 0;
  5403. int doUdp = 0;
  5404. SOCKADDR_IN_T cliAddr;
  5405. socklen_t cliLen;
  5406. const char* certFile = svrCertFile;
  5407. const char* keyFile = svrKeyFile;
  5408. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5409. size_t msg_len = 0;
  5410. #endif
  5411. wolfSSL_SetLoggingPrefix("server");
  5412. #ifdef WOLFSSL_TIRTOS
  5413. fdOpenSession(Task_self());
  5414. #endif
  5415. opts->return_code = TEST_FAIL;
  5416. cbf = opts->callbacks;
  5417. if (cbf != NULL && cbf->ctx) {
  5418. ctx = cbf->ctx;
  5419. sharedCtx = 1;
  5420. }
  5421. else
  5422. {
  5423. WOLFSSL_METHOD* method = NULL;
  5424. if (cbf != NULL && cbf->method != NULL) {
  5425. method = cbf->method();
  5426. }
  5427. else {
  5428. method = wolfSSLv23_server_method();
  5429. }
  5430. ctx = wolfSSL_CTX_new(method);
  5431. }
  5432. if (ctx == NULL) {
  5433. /* Release the wait for TCP ready. */
  5434. signal_ready(opts->signal);
  5435. goto done;
  5436. }
  5437. if (cbf == NULL || !cbf->ticNoInit) {
  5438. #if defined(HAVE_SESSION_TICKET) && \
  5439. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5440. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5441. OpenSSLTicketInit();
  5442. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5443. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5444. TicketInit();
  5445. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5446. #endif
  5447. #endif
  5448. }
  5449. #if defined(USE_WINDOWS_API)
  5450. port = opts->signal->port;
  5451. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5452. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5453. /* Let tcp_listen assign port */
  5454. port = 0;
  5455. #else
  5456. /* Use default port */
  5457. port = wolfSSLPort;
  5458. #endif
  5459. if (cbf != NULL)
  5460. doUdp = cbf->doUdp;
  5461. /* do it here to detect failure */
  5462. tcp_accept(
  5463. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5464. if (doUdp) {
  5465. cliLen = sizeof(cliAddr);
  5466. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5467. (struct sockaddr*)&cliAddr, &cliLen);
  5468. AssertIntGT(idx, 0);
  5469. }
  5470. else {
  5471. CloseSocket(sockfd);
  5472. }
  5473. wolfSSL_CTX_set_verify(ctx,
  5474. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5475. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5476. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5477. #endif
  5478. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5479. != WOLFSSL_SUCCESS) {
  5480. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5481. goto done;
  5482. }
  5483. if (cbf != NULL && cbf->certPemFile != NULL)
  5484. certFile = cbf->certPemFile;
  5485. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5486. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5487. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5488. #else
  5489. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5490. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5491. #endif
  5492. /*err_sys("can't load server cert chain file, "
  5493. "Please run from wolfSSL home dir");*/
  5494. goto done;
  5495. }
  5496. if (cbf != NULL && cbf->keyPemFile != NULL)
  5497. keyFile = cbf->keyPemFile;
  5498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5499. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5500. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5501. #else
  5502. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5503. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5504. #endif
  5505. /*err_sys("can't load server key file, "
  5506. "Please run from wolfSSL home dir");*/
  5507. goto done;
  5508. }
  5509. #ifdef HAVE_CRL
  5510. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5511. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5512. goto done;
  5513. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5514. != WOLFSSL_SUCCESS)
  5515. goto done;
  5516. }
  5517. #endif
  5518. /* call ctx setup callback */
  5519. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5520. cbf->ctx_ready(ctx);
  5521. }
  5522. ssl = wolfSSL_new(ctx);
  5523. if (ssl == NULL) {
  5524. goto done;
  5525. }
  5526. if (doUdp) {
  5527. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5528. if (err != WOLFSSL_SUCCESS)
  5529. goto done;
  5530. }
  5531. #ifdef WOLFSSL_SESSION_EXPORT
  5532. /* only add in more complex nonblocking case with session export tests */
  5533. if (args && opts->argc > 0) {
  5534. /* set as nonblock and time out for waiting on read/write */
  5535. tcp_set_nonblocking(&clientfd);
  5536. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5537. }
  5538. #endif
  5539. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5540. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5541. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5542. #else
  5543. if (wolfSSL_use_certificate_file(ssl, certFile,
  5544. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5545. #endif
  5546. /*err_sys("can't load server cert chain file, "
  5547. "Please run from wolfSSL home dir");*/
  5548. goto done;
  5549. }
  5550. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5551. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5552. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5553. #else
  5554. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5555. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5556. #endif
  5557. /*err_sys("can't load server key file, "
  5558. "Please run from wolfSSL home dir");*/
  5559. goto done;
  5560. }
  5561. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5562. /*err_sys("SSL_set_fd failed");*/
  5563. goto done;
  5564. }
  5565. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5566. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5567. #elif !defined(NO_DH)
  5568. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5569. #endif
  5570. /* call ssl setup callback */
  5571. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5572. cbf->ssl_ready(ssl);
  5573. }
  5574. #ifdef WOLFSSL_SESSION_EXPORT
  5575. /* only add in more complex nonblocking case with session export tests */
  5576. if (opts->argc > 0) {
  5577. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5578. if (ret >= 0) {
  5579. opts->return_code = TEST_SUCCESS;
  5580. }
  5581. #ifdef WOLFSSL_TIRTOS
  5582. Task_yield();
  5583. #endif
  5584. goto done;
  5585. }
  5586. #endif
  5587. #ifdef WOLFSSL_ASYNC_CRYPT
  5588. err = 0; /* Reset error */
  5589. #endif
  5590. do {
  5591. #ifdef WOLFSSL_ASYNC_CRYPT
  5592. if (err == WC_PENDING_E) {
  5593. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5594. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5595. }
  5596. #endif
  5597. ret = wolfSSL_negotiate(ssl);
  5598. err = wolfSSL_get_error(ssl, 0);
  5599. } while (err == WC_PENDING_E);
  5600. if (ret != WOLFSSL_SUCCESS) {
  5601. char buff[WOLFSSL_MAX_ERROR_SZ];
  5602. fprintf(stderr, "error = %d, %s\n", err,
  5603. wolfSSL_ERR_error_string(err, buff));
  5604. /*err_sys("SSL_accept failed");*/
  5605. goto done;
  5606. }
  5607. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5608. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5609. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5610. AssertIntGE(msg_len, 0);
  5611. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5612. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5613. AssertIntGE(msg_len, 0);
  5614. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5615. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5616. if (idx > 0) {
  5617. input[idx] = '\0';
  5618. fprintf(stderr, "Client message: %s\n", input);
  5619. }
  5620. else if (idx < 0) {
  5621. goto done;
  5622. }
  5623. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5624. /*err_sys("SSL_write failed");*/
  5625. goto done;
  5626. }
  5627. if (cbf != NULL && cbf->on_result != NULL)
  5628. cbf->on_result(ssl);
  5629. #ifdef WOLFSSL_TIRTOS
  5630. Task_yield();
  5631. #endif
  5632. opts->return_code = TEST_SUCCESS;
  5633. done:
  5634. if (cbf != NULL)
  5635. cbf->last_err = err;
  5636. if (cbf != NULL && cbf->on_cleanup != NULL)
  5637. cbf->on_cleanup(ssl);
  5638. wolfSSL_shutdown(ssl);
  5639. wolfSSL_free(ssl);
  5640. if (!sharedCtx)
  5641. wolfSSL_CTX_free(ctx);
  5642. CloseSocket(clientfd);
  5643. #ifdef WOLFSSL_TIRTOS
  5644. fdCloseSession(Task_self());
  5645. #endif
  5646. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5647. && defined(HAVE_THREAD_LS)
  5648. wc_ecc_fp_free(); /* free per thread cache */
  5649. #endif
  5650. if (cbf == NULL || !cbf->ticNoInit) {
  5651. #if defined(HAVE_SESSION_TICKET) && \
  5652. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5653. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5654. OpenSSLTicketCleanup();
  5655. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5656. TicketCleanup();
  5657. #endif
  5658. #endif
  5659. }
  5660. wolfSSL_SetLoggingPrefix(NULL);
  5661. WOLFSSL_RETURN_FROM_THREAD(0);
  5662. }
  5663. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5664. !defined(WOLFSSL_NO_TLS12)
  5665. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5666. {
  5667. SOCKET_T sockfd = 0;
  5668. SOCKET_T clientfd = 0;
  5669. word16 port;
  5670. callback_functions* cbf;
  5671. WOLFSSL_CTX* ctx = 0;
  5672. WOLFSSL* ssl = 0;
  5673. char msg[] = "I hear you fa shizzle!";
  5674. char input[1024];
  5675. int idx;
  5676. int ret, err = 0;
  5677. int sharedCtx = 0;
  5678. func_args* opts = (func_args*)args;
  5679. int loop_count = opts->argc;
  5680. int count = 0;
  5681. #ifdef WOLFSSL_TIRTOS
  5682. fdOpenSession(Task_self());
  5683. #endif
  5684. opts->return_code = TEST_FAIL;
  5685. cbf = opts->callbacks;
  5686. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5687. if (cbf != NULL && cbf->ctx) {
  5688. ctx = cbf->ctx;
  5689. sharedCtx = 1;
  5690. }
  5691. else
  5692. #endif
  5693. {
  5694. WOLFSSL_METHOD* method = NULL;
  5695. if (cbf != NULL && cbf->method != NULL) {
  5696. method = cbf->method();
  5697. }
  5698. else {
  5699. method = wolfSSLv23_server_method();
  5700. }
  5701. ctx = wolfSSL_CTX_new(method);
  5702. }
  5703. #if defined(USE_WINDOWS_API)
  5704. port = opts->signal->port;
  5705. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5706. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5707. /* Let tcp_listen assign port */
  5708. port = 0;
  5709. #else
  5710. /* Use default port */
  5711. port = wolfSSLPort;
  5712. #endif
  5713. wolfSSL_CTX_set_verify(ctx,
  5714. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5715. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5716. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5717. #endif
  5718. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5719. != WOLFSSL_SUCCESS) {
  5720. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5721. /* Release the wait for TCP ready. */
  5722. signal_ready(opts->signal);
  5723. goto done;
  5724. }
  5725. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5726. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5727. /*err_sys("can't load server cert chain file, "
  5728. "Please run from wolfSSL home dir");*/
  5729. /* Release the wait for TCP ready. */
  5730. signal_ready(opts->signal);
  5731. goto done;
  5732. }
  5733. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5734. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5735. /*err_sys("can't load server key file, "
  5736. "Please run from wolfSSL home dir");*/
  5737. /* Release the wait for TCP ready. */
  5738. signal_ready(opts->signal);
  5739. goto done;
  5740. }
  5741. /* call ctx setup callback */
  5742. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5743. cbf->ctx_ready(ctx);
  5744. }
  5745. while (count != loop_count) {
  5746. ssl = wolfSSL_new(ctx);
  5747. if (ssl == NULL) {
  5748. signal_ready(opts->signal);
  5749. goto done;
  5750. }
  5751. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5753. /*err_sys("can't load server cert chain file, "
  5754. "Please run from wolfSSL home dir");*/
  5755. /* Release the wait for TCP ready. */
  5756. signal_ready(opts->signal);
  5757. goto done;
  5758. }
  5759. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5760. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5761. /*err_sys("can't load server key file, "
  5762. "Please run from wolfSSL home dir");*/
  5763. /* Release the wait for TCP ready. */
  5764. signal_ready(opts->signal);
  5765. goto done;
  5766. }
  5767. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5768. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5769. #elif !defined(NO_DH)
  5770. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5771. #endif
  5772. /* call ssl setup callback */
  5773. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5774. cbf->ssl_ready(ssl);
  5775. }
  5776. /* do it here to detect failure */
  5777. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5778. 0);
  5779. CloseSocket(sockfd);
  5780. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5781. /*err_sys("SSL_set_fd failed");*/
  5782. goto done;
  5783. }
  5784. #ifdef WOLFSSL_ASYNC_CRYPT
  5785. err = 0; /* Reset error */
  5786. #endif
  5787. do {
  5788. #ifdef WOLFSSL_ASYNC_CRYPT
  5789. if (err == WC_PENDING_E) {
  5790. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5791. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5792. }
  5793. #endif
  5794. ret = wolfSSL_accept(ssl);
  5795. err = wolfSSL_get_error(ssl, 0);
  5796. } while (err == WC_PENDING_E);
  5797. if (ret != WOLFSSL_SUCCESS) {
  5798. char buff[WOLFSSL_MAX_ERROR_SZ];
  5799. fprintf(stderr, "error = %d, %s\n", err,
  5800. wolfSSL_ERR_error_string(err, buff));
  5801. /*err_sys("SSL_accept failed");*/
  5802. goto done;
  5803. }
  5804. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5805. if (idx > 0) {
  5806. input[idx] = '\0';
  5807. fprintf(stderr, "Client message: %s\n", input);
  5808. }
  5809. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5810. /*err_sys("SSL_write failed");*/
  5811. goto done;
  5812. }
  5813. /* free ssl for this connection */
  5814. wolfSSL_shutdown(ssl);
  5815. wolfSSL_free(ssl); ssl = NULL;
  5816. CloseSocket(clientfd);
  5817. count++;
  5818. }
  5819. #ifdef WOLFSSL_TIRTOS
  5820. Task_yield();
  5821. #endif
  5822. opts->return_code = TEST_SUCCESS;
  5823. done:
  5824. if (ssl != NULL) {
  5825. wolfSSL_shutdown(ssl);
  5826. wolfSSL_free(ssl);
  5827. }
  5828. if (!sharedCtx)
  5829. wolfSSL_CTX_free(ctx);
  5830. CloseSocket(clientfd);
  5831. #ifdef WOLFSSL_TIRTOS
  5832. fdCloseSession(Task_self());
  5833. #endif
  5834. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5835. && defined(HAVE_THREAD_LS)
  5836. wc_ecc_fp_free(); /* free per thread cache */
  5837. #endif
  5838. WOLFSSL_RETURN_FROM_THREAD(0);
  5839. }
  5840. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5841. static int test_client_nofail(void* args, cbType cb)
  5842. {
  5843. #if !defined(NO_WOLFSSL_CLIENT)
  5844. SOCKET_T sockfd = 0;
  5845. callback_functions* cbf;
  5846. WOLFSSL_CTX* ctx = 0;
  5847. WOLFSSL* ssl = 0;
  5848. WOLFSSL_CIPHER* cipher;
  5849. char msg[64] = "hello wolfssl!";
  5850. char reply[1024];
  5851. int input;
  5852. int msgSz = (int)XSTRLEN(msg);
  5853. int ret, err = 0;
  5854. int cipherSuite;
  5855. int sharedCtx = 0;
  5856. int doUdp = 0;
  5857. const char* cipherName1, *cipherName2;
  5858. wolfSSL_SetLoggingPrefix("client");
  5859. #ifdef WOLFSSL_TIRTOS
  5860. fdOpenSession(Task_self());
  5861. #endif
  5862. ((func_args*)args)->return_code = TEST_FAIL;
  5863. cbf = ((func_args*)args)->callbacks;
  5864. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5865. if (cbf != NULL && cbf->ctx) {
  5866. ctx = cbf->ctx;
  5867. sharedCtx = cbf->isSharedCtx;
  5868. }
  5869. else
  5870. #endif
  5871. {
  5872. WOLFSSL_METHOD* method = NULL;
  5873. if (cbf != NULL && cbf->method != NULL) {
  5874. method = cbf->method();
  5875. }
  5876. else {
  5877. method = wolfSSLv23_client_method();
  5878. }
  5879. ctx = wolfSSL_CTX_new(method);
  5880. }
  5881. if (cbf != NULL)
  5882. doUdp = cbf->doUdp;
  5883. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5884. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5885. #endif
  5886. /* Do connect here so server detects failures */
  5887. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5888. doUdp, 0, NULL);
  5889. /* Connect the socket so that we don't have to set the peer later on */
  5890. if (doUdp)
  5891. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5892. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5893. {
  5894. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5895. goto done;
  5896. }
  5897. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5898. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5899. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5900. #else
  5901. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5902. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5903. #endif
  5904. /*err_sys("can't load client cert file, "
  5905. "Please run from wolfSSL home dir");*/
  5906. goto done;
  5907. }
  5908. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5909. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5910. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5911. #else
  5912. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5913. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5914. #endif
  5915. /*err_sys("can't load client key file, "
  5916. "Please run from wolfSSL home dir");*/
  5917. goto done;
  5918. }
  5919. #ifdef HAVE_CRL
  5920. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5921. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5922. goto done;
  5923. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5924. != WOLFSSL_SUCCESS)
  5925. goto done;
  5926. }
  5927. #endif
  5928. /* call ctx setup callback */
  5929. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5930. cbf->ctx_ready(ctx);
  5931. }
  5932. ssl = wolfSSL_new(ctx);
  5933. if (ssl == NULL) {
  5934. goto done;
  5935. }
  5936. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5937. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5938. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5939. #else
  5940. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5941. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5942. #endif
  5943. /*err_sys("can't load client cert file, "
  5944. "Please run from wolfSSL home dir");*/
  5945. goto done;
  5946. }
  5947. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5948. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5949. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5950. #else
  5951. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5953. #endif
  5954. /*err_sys("can't load client key file, "
  5955. "Please run from wolfSSL home dir");*/
  5956. goto done;
  5957. }
  5958. if (!doUdp) {
  5959. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5960. /*err_sys("SSL_set_fd failed");*/
  5961. goto done;
  5962. }
  5963. }
  5964. else {
  5965. #ifdef WOLFSSL_DTLS
  5966. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5967. /*err_sys("SSL_set_fd failed");*/
  5968. goto done;
  5969. }
  5970. #else
  5971. goto done;
  5972. #endif
  5973. }
  5974. /* call ssl setup callback */
  5975. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5976. cbf->ssl_ready(ssl);
  5977. }
  5978. #ifdef WOLFSSL_ASYNC_CRYPT
  5979. err = 0; /* Reset error */
  5980. #endif
  5981. do {
  5982. #ifdef WOLFSSL_ASYNC_CRYPT
  5983. if (err == WC_PENDING_E) {
  5984. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5985. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5986. }
  5987. #endif
  5988. ret = wolfSSL_negotiate(ssl);
  5989. err = wolfSSL_get_error(ssl, 0);
  5990. } while (err == WC_PENDING_E);
  5991. if (ret != WOLFSSL_SUCCESS) {
  5992. char buff[WOLFSSL_MAX_ERROR_SZ];
  5993. fprintf(stderr, "error = %d, %s\n", err,
  5994. wolfSSL_ERR_error_string(err, buff));
  5995. /*err_sys("SSL_connect failed");*/
  5996. goto done;
  5997. }
  5998. /* test the various get cipher methods */
  5999. /* Internal cipher suite names */
  6000. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6001. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6002. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6003. (cipherSuite >> 8), cipherSuite & 0xFF);
  6004. AssertStrEQ(cipherName1, cipherName2);
  6005. /* IANA Cipher Suites Names */
  6006. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6007. then it's the internal cipher suite name */
  6008. cipher = wolfSSL_get_current_cipher(ssl);
  6009. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6010. cipherName2 = wolfSSL_get_cipher(ssl);
  6011. AssertStrEQ(cipherName1, cipherName2);
  6012. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6013. !defined(WOLFSSL_QT)
  6014. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6015. (cipherSuite >> 8), cipherSuite & 0xFF);
  6016. AssertStrEQ(cipherName1, cipherName2);
  6017. #endif
  6018. if (cb != NULL)
  6019. (cb)(ctx, ssl);
  6020. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6021. /*err_sys("SSL_write failed");*/
  6022. goto done;
  6023. }
  6024. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6025. if (input > 0) {
  6026. reply[input] = '\0';
  6027. fprintf(stderr, "Server response: %s\n", reply);
  6028. }
  6029. if (cbf != NULL && cbf->on_result != NULL)
  6030. cbf->on_result(ssl);
  6031. ((func_args*)args)->return_code = TEST_SUCCESS;
  6032. done:
  6033. if (cbf != NULL)
  6034. cbf->last_err = err;
  6035. if (cbf != NULL && cbf->on_cleanup != NULL)
  6036. cbf->on_cleanup(ssl);
  6037. wolfSSL_free(ssl);
  6038. if (!sharedCtx)
  6039. wolfSSL_CTX_free(ctx);
  6040. CloseSocket(sockfd);
  6041. #ifdef WOLFSSL_TIRTOS
  6042. fdCloseSession(Task_self());
  6043. #endif
  6044. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6045. && defined(HAVE_THREAD_LS)
  6046. wc_ecc_fp_free(); /* free per thread cache */
  6047. #endif
  6048. #else
  6049. (void)args;
  6050. (void)cb;
  6051. #endif /* !NO_WOLFSSL_CLIENT */
  6052. wolfSSL_SetLoggingPrefix(NULL);
  6053. return 0;
  6054. }
  6055. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6056. callback_functions* server_cb, cbType client_on_handshake)
  6057. {
  6058. func_args client_args;
  6059. func_args server_args;
  6060. tcp_ready ready;
  6061. THREAD_TYPE serverThread;
  6062. XMEMSET(&client_args, 0, sizeof(func_args));
  6063. XMEMSET(&server_args, 0, sizeof(func_args));
  6064. #ifdef WOLFSSL_TIRTOS
  6065. fdOpenSession(Task_self());
  6066. #endif
  6067. StartTCP();
  6068. InitTcpReady(&ready);
  6069. #if defined(USE_WINDOWS_API)
  6070. /* use RNG to get random port if using windows */
  6071. ready.port = GetRandomPort();
  6072. #endif
  6073. server_args.signal = &ready;
  6074. server_args.callbacks = server_cb;
  6075. client_args.signal = &ready;
  6076. client_args.callbacks = client_cb;
  6077. start_thread(test_server_nofail, &server_args, &serverThread);
  6078. wait_tcp_ready(&server_args);
  6079. test_client_nofail(&client_args, client_on_handshake);
  6080. join_thread(serverThread);
  6081. client_cb->return_code = client_args.return_code;
  6082. server_cb->return_code = server_args.return_code;
  6083. FreeTcpReady(&ready);
  6084. #ifdef WOLFSSL_TIRTOS
  6085. fdOpenSession(Task_self());
  6086. #endif
  6087. }
  6088. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6089. callback_functions* server_cb)
  6090. {
  6091. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6092. }
  6093. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6094. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6095. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6096. void* server_args)
  6097. {
  6098. SOCKET_T sockfd = 0;
  6099. callback_functions* cbf;
  6100. WOLFSSL_CTX* ctx = 0;
  6101. WOLFSSL* ssl = 0;
  6102. WOLFSSL_SESSION* session = NULL;
  6103. char msg[64] = "hello wolfssl!";
  6104. char reply[1024];
  6105. int input;
  6106. int msgSz = (int)XSTRLEN(msg);
  6107. int ret, err = 0;
  6108. int sharedCtx = 0;
  6109. #ifdef WOLFSSL_TIRTOS
  6110. fdOpenSession(Task_self());
  6111. #endif
  6112. ((func_args*)args)->return_code = TEST_FAIL;
  6113. cbf = ((func_args*)args)->callbacks;
  6114. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6115. if (cbf != NULL && cbf->ctx) {
  6116. ctx = cbf->ctx;
  6117. sharedCtx = 1;
  6118. }
  6119. else
  6120. #endif
  6121. {
  6122. WOLFSSL_METHOD* method = NULL;
  6123. if (cbf != NULL && cbf->method != NULL) {
  6124. method = cbf->method();
  6125. }
  6126. else {
  6127. method = wolfSSLv23_client_method();
  6128. }
  6129. ctx = wolfSSL_CTX_new(method);
  6130. }
  6131. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6132. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6133. #endif
  6134. /* Do connect here so server detects failures */
  6135. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6136. 0, 0, NULL);
  6137. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6138. WOLFSSL_SUCCESS) {
  6139. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6140. goto done;
  6141. }
  6142. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6143. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6144. /*err_sys("can't load client cert file, "
  6145. "Please run from wolfSSL home dir");*/
  6146. goto done;
  6147. }
  6148. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6149. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6150. /*err_sys("can't load client key file, "
  6151. "Please run from wolfSSL home dir");*/
  6152. goto done;
  6153. }
  6154. /* call ctx setup callback */
  6155. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6156. cbf->ctx_ready(ctx);
  6157. }
  6158. ssl = wolfSSL_new(ctx);
  6159. if (ssl == NULL) {
  6160. goto done;
  6161. }
  6162. /* keep handshake resources for re-using WOLFSSL obj */
  6163. wolfSSL_KeepArrays(ssl);
  6164. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6165. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6166. goto done;
  6167. }
  6168. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6169. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6170. /*err_sys("can't load client cert file, "
  6171. "Please run from wolfSSL home dir");*/
  6172. goto done;
  6173. }
  6174. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6175. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6176. /*err_sys("can't load client key file, "
  6177. "Please run from wolfSSL home dir");*/
  6178. goto done;
  6179. }
  6180. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6181. /*err_sys("SSL_set_fd failed");*/
  6182. goto done;
  6183. }
  6184. /* call ssl setup callback */
  6185. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6186. cbf->ssl_ready(ssl);
  6187. }
  6188. #ifdef WOLFSSL_ASYNC_CRYPT
  6189. err = 0; /* Reset error */
  6190. #endif
  6191. do {
  6192. #ifdef WOLFSSL_ASYNC_CRYPT
  6193. if (err == WC_PENDING_E) {
  6194. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6195. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6196. }
  6197. #endif
  6198. ret = wolfSSL_connect(ssl);
  6199. err = wolfSSL_get_error(ssl, 0);
  6200. } while (err == WC_PENDING_E);
  6201. if (ret != WOLFSSL_SUCCESS) {
  6202. char buff[WOLFSSL_MAX_ERROR_SZ];
  6203. fprintf(stderr, "error = %d, %s\n", err,
  6204. wolfSSL_ERR_error_string(err, buff));
  6205. /*err_sys("SSL_connect failed");*/
  6206. goto done;
  6207. }
  6208. /* Build first session */
  6209. if (cb != NULL)
  6210. cb(ctx, ssl);
  6211. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6212. /*err_sys("SSL_write failed");*/
  6213. goto done;
  6214. }
  6215. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6216. if (input > 0) {
  6217. reply[input] = '\0';
  6218. fprintf(stderr, "Server response: %s\n", reply);
  6219. }
  6220. /* Session Resumption by re-using WOLFSSL object */
  6221. wolfSSL_set_quiet_shutdown(ssl, 1);
  6222. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6223. /* err_sys ("SSL shutdown failed"); */
  6224. goto done;
  6225. }
  6226. session = wolfSSL_get1_session(ssl);
  6227. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6228. wolfSSL_SESSION_free(session);
  6229. /* err_sys ("SSL_clear failed"); */
  6230. goto done;
  6231. }
  6232. wolfSSL_set_session(ssl, session);
  6233. wolfSSL_SESSION_free(session);
  6234. session = NULL;
  6235. /* close socket once */
  6236. CloseSocket(sockfd);
  6237. sockfd = 0;
  6238. /* wait until server ready */
  6239. wait_tcp_ready((func_args*)server_args);
  6240. fprintf(stderr, "session resumption\n");
  6241. /* Do re-connect */
  6242. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6243. 0, 0, NULL);
  6244. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6245. /*err_sys("SSL_set_fd failed");*/
  6246. goto done;
  6247. }
  6248. #ifdef WOLFSSL_ASYNC_CRYPT
  6249. err = 0; /* Reset error */
  6250. #endif
  6251. do {
  6252. #ifdef WOLFSSL_ASYNC_CRYPT
  6253. if (err == WC_PENDING_E) {
  6254. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6255. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6256. }
  6257. #endif
  6258. ret = wolfSSL_connect(ssl);
  6259. err = wolfSSL_get_error(ssl, 0);
  6260. } while (err == WC_PENDING_E);
  6261. if (ret != WOLFSSL_SUCCESS) {
  6262. char buff[WOLFSSL_MAX_ERROR_SZ];
  6263. fprintf(stderr, "error = %d, %s\n", err,
  6264. wolfSSL_ERR_error_string(err, buff));
  6265. /*err_sys("SSL_connect failed");*/
  6266. goto done;
  6267. }
  6268. /* Build first session */
  6269. if (cb != NULL)
  6270. cb(ctx, ssl);
  6271. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6272. /*err_sys("SSL_write failed");*/
  6273. goto done;
  6274. }
  6275. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6276. if (input > 0) {
  6277. reply[input] = '\0';
  6278. fprintf(stderr, "Server response: %s\n", reply);
  6279. }
  6280. ((func_args*)args)->return_code = TEST_SUCCESS;
  6281. done:
  6282. wolfSSL_free(ssl);
  6283. if (!sharedCtx)
  6284. wolfSSL_CTX_free(ctx);
  6285. CloseSocket(sockfd);
  6286. #ifdef WOLFSSL_TIRTOS
  6287. fdCloseSession(Task_self());
  6288. #endif
  6289. return;
  6290. }
  6291. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6292. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6293. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6294. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6295. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6296. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6297. #define HAVE_ALPN_PROTOS_SUPPORT
  6298. #endif
  6299. /* Generic TLS client / server with callbacks for API unit tests
  6300. * Used by SNI / ALPN / crypto callback helper functions */
  6301. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6302. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6303. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6304. #define ENABLE_TLS_CALLBACK_TEST
  6305. #endif
  6306. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6307. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6308. /* TLS server for API unit testing - generic */
  6309. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6310. {
  6311. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6312. WOLFSSL_CTX* ctx = NULL;
  6313. WOLFSSL* ssl = NULL;
  6314. SOCKET_T sfd = 0;
  6315. SOCKET_T cfd = 0;
  6316. word16 port;
  6317. char msg[] = "I hear you fa shizzle!";
  6318. int len = (int) XSTRLEN(msg);
  6319. char input[1024];
  6320. int idx;
  6321. int ret, err = 0;
  6322. ((func_args*)args)->return_code = TEST_FAIL;
  6323. #if defined(USE_WINDOWS_API)
  6324. port = ((func_args*)args)->signal->port;
  6325. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6326. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6327. /* Let tcp_listen assign port */
  6328. port = 0;
  6329. #else
  6330. /* Use default port */
  6331. port = wolfSSLPort;
  6332. #endif
  6333. #ifdef WOLFSSL_DTLS
  6334. if (callbacks->method == wolfDTLS_server_method
  6335. #ifdef WOLFSSL_STATIC_MEMORY
  6336. || callbacks->method_ex == wolfDTLS_server_method_ex
  6337. #endif
  6338. #ifndef NO_OLD_TLS
  6339. || callbacks->method == wolfDTLSv1_server_method
  6340. #ifdef WOLFSSL_STATIC_MEMORY
  6341. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6342. #endif
  6343. #endif
  6344. #ifndef WOLFSSL_NO_TLS12
  6345. || callbacks->method == wolfDTLSv1_2_server_method
  6346. #ifdef WOLFSSL_STATIC_MEMORY
  6347. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6348. #endif
  6349. #endif
  6350. #ifdef WOLFSSL_DTLS13
  6351. || callbacks->method == wolfDTLSv1_3_server_method
  6352. #ifdef WOLFSSL_STATIC_MEMORY
  6353. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6354. #endif
  6355. #endif
  6356. ) {
  6357. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6358. }
  6359. else
  6360. #endif
  6361. {
  6362. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6363. }
  6364. #ifdef WOLFSSL_STATIC_MEMORY
  6365. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6366. callbacks->memSz > 0) {
  6367. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6368. callbacks->mem, callbacks->memSz, 0, 1);
  6369. if (ret != WOLFSSL_SUCCESS) {
  6370. fprintf(stderr, "CTX static new failed %d\n", ret);
  6371. goto cleanup;
  6372. }
  6373. }
  6374. #else
  6375. ctx = wolfSSL_CTX_new(callbacks->method());
  6376. #endif
  6377. if (ctx == NULL) {
  6378. fprintf(stderr, "CTX new failed\n");
  6379. goto cleanup;
  6380. }
  6381. /* set defaults */
  6382. if (callbacks->caPemFile == NULL)
  6383. callbacks->caPemFile = cliCertFile;
  6384. if (callbacks->certPemFile == NULL)
  6385. callbacks->certPemFile = svrCertFile;
  6386. if (callbacks->keyPemFile == NULL)
  6387. callbacks->keyPemFile = svrKeyFile;
  6388. #ifdef WOLFSSL_TIRTOS
  6389. fdOpenSession(Task_self());
  6390. #endif
  6391. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6392. wolfSSL_CTX_set_verify(ctx,
  6393. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6394. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6395. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6396. #endif
  6397. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6398. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6399. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6400. goto cleanup;
  6401. }
  6402. #endif
  6403. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6404. WOLFSSL_SUCCESS) {
  6405. goto cleanup;
  6406. }
  6407. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6408. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6409. goto cleanup;
  6410. }
  6411. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6412. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6413. goto cleanup;
  6414. }
  6415. #ifdef HAVE_CRL
  6416. if (callbacks->crlPemFile != NULL) {
  6417. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6418. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6419. goto cleanup;
  6420. }
  6421. }
  6422. #endif
  6423. if (callbacks->ctx_ready)
  6424. callbacks->ctx_ready(ctx);
  6425. ssl = wolfSSL_new(ctx);
  6426. if (ssl == NULL) {
  6427. fprintf(stderr, "SSL new failed\n");
  6428. goto cleanup;
  6429. }
  6430. if (wolfSSL_dtls(ssl)) {
  6431. SOCKADDR_IN_T cliAddr;
  6432. socklen_t cliLen;
  6433. cliLen = sizeof(cliAddr);
  6434. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6435. (struct sockaddr*)&cliAddr, &cliLen);
  6436. if (idx <= 0) {
  6437. goto cleanup;
  6438. }
  6439. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6440. }
  6441. else {
  6442. CloseSocket(sfd);
  6443. }
  6444. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6445. goto cleanup;
  6446. }
  6447. if (callbacks->loadToSSL) {
  6448. wolfSSL_SetDevId(ssl, callbacks->devId);
  6449. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6450. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6451. goto cleanup;
  6452. }
  6453. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6454. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6455. goto cleanup;
  6456. }
  6457. }
  6458. #ifdef NO_PSK
  6459. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6460. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6461. #elif !defined(NO_DH)
  6462. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6463. #endif
  6464. #endif
  6465. if (callbacks->ssl_ready)
  6466. callbacks->ssl_ready(ssl);
  6467. #ifdef WOLFSSL_ASYNC_CRYPT
  6468. err = 0; /* Reset error */
  6469. #endif
  6470. do {
  6471. #ifdef WOLFSSL_ASYNC_CRYPT
  6472. if (err == WC_PENDING_E) {
  6473. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6474. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6475. }
  6476. #endif
  6477. ret = wolfSSL_accept(ssl);
  6478. err = wolfSSL_get_error(ssl, ret);
  6479. } while (err == WC_PENDING_E);
  6480. if (ret != WOLFSSL_SUCCESS) {
  6481. char buff[WOLFSSL_MAX_ERROR_SZ];
  6482. fprintf(stderr, "accept error = %d, %s\n", err,
  6483. wolfSSL_ERR_error_string(err, buff));
  6484. /*err_sys("SSL_accept failed");*/
  6485. }
  6486. else {
  6487. #ifdef WOLFSSL_ASYNC_CRYPT
  6488. err = 0; /* Reset error */
  6489. #endif
  6490. do {
  6491. #ifdef WOLFSSL_ASYNC_CRYPT
  6492. if (err == WC_PENDING_E) {
  6493. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6494. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6495. }
  6496. #endif
  6497. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6498. err = wolfSSL_get_error(ssl, idx);
  6499. } while (err == WC_PENDING_E);
  6500. if (idx > 0) {
  6501. input[idx] = 0;
  6502. fprintf(stderr, "Client message: %s\n", input);
  6503. }
  6504. #ifdef WOLFSSL_ASYNC_CRYPT
  6505. err = 0; /* Reset error */
  6506. #endif
  6507. do {
  6508. #ifdef WOLFSSL_ASYNC_CRYPT
  6509. if (err == WC_PENDING_E) {
  6510. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6511. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6512. }
  6513. #endif
  6514. ret = wolfSSL_write(ssl, msg, len);
  6515. err = wolfSSL_get_error(ssl, ret);
  6516. } while (err == WC_PENDING_E);
  6517. if (len != ret) {
  6518. goto cleanup;
  6519. }
  6520. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6521. defined(WOLFSSL_DTLS)
  6522. if (wolfSSL_dtls(ssl)) {
  6523. byte* import;
  6524. word32 sz;
  6525. wolfSSL_dtls_export(ssl, NULL, &sz);
  6526. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6527. if (import == NULL) {
  6528. goto cleanup;
  6529. }
  6530. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6531. if (idx < 0) {
  6532. goto cleanup;
  6533. }
  6534. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6535. goto cleanup;
  6536. }
  6537. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6538. }
  6539. #endif
  6540. #ifdef WOLFSSL_TIRTOS
  6541. Task_yield();
  6542. #endif
  6543. ((func_args*)args)->return_code = TEST_SUCCESS;
  6544. }
  6545. if (callbacks->on_result)
  6546. callbacks->on_result(ssl);
  6547. wolfSSL_shutdown(ssl);
  6548. cleanup:
  6549. wolfSSL_free(ssl);
  6550. wolfSSL_CTX_free(ctx);
  6551. CloseSocket(cfd);
  6552. #ifdef WOLFSSL_TIRTOS
  6553. fdCloseSession(Task_self());
  6554. #endif
  6555. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6556. && defined(HAVE_THREAD_LS)
  6557. wc_ecc_fp_free(); /* free per thread cache */
  6558. #endif
  6559. WOLFSSL_RETURN_FROM_THREAD(0);
  6560. }
  6561. /* TLS Client for API unit testing - generic */
  6562. static void run_wolfssl_client(void* args)
  6563. {
  6564. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6565. WOLFSSL_CTX* ctx = NULL;
  6566. WOLFSSL* ssl = NULL;
  6567. SOCKET_T sfd = 0;
  6568. char msg[] = "hello wolfssl server!";
  6569. int len = (int) XSTRLEN(msg);
  6570. char input[1024];
  6571. int ret, err = 0;
  6572. ((func_args*)args)->return_code = TEST_FAIL;
  6573. /* set defaults */
  6574. if (callbacks->caPemFile == NULL)
  6575. callbacks->caPemFile = caCertFile;
  6576. if (callbacks->certPemFile == NULL)
  6577. callbacks->certPemFile = cliCertFile;
  6578. if (callbacks->keyPemFile == NULL)
  6579. callbacks->keyPemFile = cliKeyFile;
  6580. #ifdef WOLFSSL_STATIC_MEMORY
  6581. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6582. callbacks->memSz > 0) {
  6583. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6584. callbacks->mem, callbacks->memSz, 0, 1);
  6585. if (ret != WOLFSSL_SUCCESS) {
  6586. fprintf(stderr, "CTX static new failed %d\n", ret);
  6587. goto cleanup;
  6588. }
  6589. }
  6590. #else
  6591. ctx = wolfSSL_CTX_new(callbacks->method());
  6592. #endif
  6593. if (ctx == NULL) {
  6594. fprintf(stderr, "CTX new failed\n");
  6595. goto cleanup;
  6596. }
  6597. #ifdef WOLFSSL_TIRTOS
  6598. fdOpenSession(Task_self());
  6599. #endif
  6600. if (!callbacks->loadToSSL) {
  6601. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6602. }
  6603. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6604. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6605. #endif
  6606. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6607. WOLFSSL_SUCCESS) {
  6608. goto cleanup;
  6609. }
  6610. if (!callbacks->loadToSSL) {
  6611. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6612. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6613. goto cleanup;
  6614. }
  6615. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6616. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6617. goto cleanup;
  6618. }
  6619. }
  6620. #ifdef HAVE_CRL
  6621. if (callbacks->crlPemFile != NULL) {
  6622. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6623. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6624. goto cleanup;
  6625. }
  6626. }
  6627. #endif
  6628. if (callbacks->ctx_ready)
  6629. callbacks->ctx_ready(ctx);
  6630. ssl = wolfSSL_new(ctx);
  6631. if (wolfSSL_dtls(ssl)) {
  6632. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6633. 1, 0, ssl);
  6634. }
  6635. else {
  6636. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6637. 0, 0, ssl);
  6638. }
  6639. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6640. goto cleanup;
  6641. }
  6642. if (callbacks->loadToSSL) {
  6643. wolfSSL_SetDevId(ssl, callbacks->devId);
  6644. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6645. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6646. goto cleanup;
  6647. }
  6648. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6649. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6650. goto cleanup;
  6651. }
  6652. }
  6653. if (callbacks->ssl_ready)
  6654. callbacks->ssl_ready(ssl);
  6655. #ifdef WOLFSSL_ASYNC_CRYPT
  6656. err = 0; /* Reset error */
  6657. #endif
  6658. do {
  6659. #ifdef WOLFSSL_ASYNC_CRYPT
  6660. if (err == WC_PENDING_E) {
  6661. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6662. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6663. }
  6664. #endif
  6665. ret = wolfSSL_connect(ssl);
  6666. err = wolfSSL_get_error(ssl, ret);
  6667. } while (err == WC_PENDING_E);
  6668. if (ret != WOLFSSL_SUCCESS) {
  6669. char buff[WOLFSSL_MAX_ERROR_SZ];
  6670. fprintf(stderr, "error = %d, %s\n", err,
  6671. wolfSSL_ERR_error_string(err, buff));
  6672. /*err_sys("SSL_connect failed");*/
  6673. }
  6674. else {
  6675. #ifdef WOLFSSL_ASYNC_CRYPT
  6676. err = 0; /* Reset error */
  6677. #endif
  6678. do {
  6679. #ifdef WOLFSSL_ASYNC_CRYPT
  6680. if (err == WC_PENDING_E) {
  6681. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6682. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6683. }
  6684. #endif
  6685. ret = wolfSSL_write(ssl, msg, len);
  6686. err = wolfSSL_get_error(ssl, ret);
  6687. } while (err == WC_PENDING_E);
  6688. if (len != ret)
  6689. goto cleanup;
  6690. #ifdef WOLFSSL_ASYNC_CRYPT
  6691. err = 0; /* Reset error */
  6692. #endif
  6693. do {
  6694. #ifdef WOLFSSL_ASYNC_CRYPT
  6695. if (err == WC_PENDING_E) {
  6696. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6697. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6698. }
  6699. #endif
  6700. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6701. err = wolfSSL_get_error(ssl, ret);
  6702. } while (err == WC_PENDING_E);
  6703. if (ret > 0) {
  6704. input[ret] = '\0'; /* null term */
  6705. fprintf(stderr, "Server response: %s\n", input);
  6706. }
  6707. ((func_args*)args)->return_code = TEST_SUCCESS;
  6708. }
  6709. if (callbacks->on_result)
  6710. callbacks->on_result(ssl);
  6711. cleanup:
  6712. wolfSSL_free(ssl);
  6713. wolfSSL_CTX_free(ctx);
  6714. CloseSocket(sfd);
  6715. #ifdef WOLFSSL_TIRTOS
  6716. fdCloseSession(Task_self());
  6717. #endif
  6718. }
  6719. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6720. static int test_wolfSSL_read_write(void)
  6721. {
  6722. /* The unit testing for read and write shall happen simultaneously, since
  6723. * one can't do anything with one without the other. (Except for a failure
  6724. * test case.) This function will call all the others that will set up,
  6725. * execute, and report their test findings.
  6726. *
  6727. * Set up the success case first. This function will become the template
  6728. * for the other tests. This should eventually be renamed
  6729. *
  6730. * The success case isn't interesting, how can this fail?
  6731. * - Do not give the client context a CA certificate. The connect should
  6732. * fail. Do not need server for this?
  6733. * - Using NULL for the ssl object on server. Do not need client for this.
  6734. * - Using NULL for the ssl object on client. Do not need server for this.
  6735. * - Good ssl objects for client and server. Client write() without server
  6736. * read().
  6737. * - Good ssl objects for client and server. Server write() without client
  6738. * read().
  6739. * - Forgetting the password callback?
  6740. */
  6741. tcp_ready ready;
  6742. func_args client_args;
  6743. func_args server_args;
  6744. THREAD_TYPE serverThread;
  6745. EXPECT_DECLS;
  6746. XMEMSET(&client_args, 0, sizeof(func_args));
  6747. XMEMSET(&server_args, 0, sizeof(func_args));
  6748. #ifdef WOLFSSL_TIRTOS
  6749. fdOpenSession(Task_self());
  6750. #endif
  6751. StartTCP();
  6752. InitTcpReady(&ready);
  6753. #if defined(USE_WINDOWS_API)
  6754. /* use RNG to get random port if using windows */
  6755. ready.port = GetRandomPort();
  6756. #endif
  6757. server_args.signal = &ready;
  6758. client_args.signal = &ready;
  6759. start_thread(test_server_nofail, &server_args, &serverThread);
  6760. wait_tcp_ready(&server_args);
  6761. test_client_nofail(&client_args, NULL);
  6762. join_thread(serverThread);
  6763. ExpectTrue(client_args.return_code);
  6764. ExpectTrue(server_args.return_code);
  6765. FreeTcpReady(&ready);
  6766. #ifdef WOLFSSL_TIRTOS
  6767. fdOpenSession(Task_self());
  6768. #endif
  6769. return EXPECT_RESULT();
  6770. }
  6771. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6772. {
  6773. EXPECT_DECLS;
  6774. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6775. !defined(WOLFSSL_NO_TLS12)
  6776. /* The unit test for session resumption by re-using WOLFSSL object.
  6777. * WOLFSSL object is not cleared after first session. It reuse the object
  6778. * for second connection.
  6779. */
  6780. tcp_ready ready;
  6781. func_args client_args;
  6782. func_args server_args;
  6783. THREAD_TYPE serverThread;
  6784. callback_functions client_cbf;
  6785. callback_functions server_cbf;
  6786. XMEMSET(&client_args, 0, sizeof(func_args));
  6787. XMEMSET(&server_args, 0, sizeof(func_args));
  6788. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6789. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6790. #ifdef WOLFSSL_TIRTOS
  6791. fdOpenSession(Task_self());
  6792. #endif
  6793. StartTCP();
  6794. InitTcpReady(&ready);
  6795. #if defined(USE_WINDOWS_API)
  6796. /* use RNG to get random port if using windows */
  6797. ready.port = GetRandomPort();
  6798. #endif
  6799. client_cbf.method = wolfTLSv1_2_client_method;
  6800. server_cbf.method = wolfTLSv1_2_server_method;
  6801. client_args.callbacks = &client_cbf;
  6802. server_args.callbacks = &server_cbf;
  6803. server_args.signal = &ready;
  6804. client_args.signal = &ready;
  6805. /* the var is used for loop number */
  6806. server_args.argc = 2;
  6807. start_thread(test_server_loop, &server_args, &serverThread);
  6808. wait_tcp_ready(&server_args);
  6809. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6810. join_thread(serverThread);
  6811. ExpectTrue(client_args.return_code);
  6812. ExpectTrue(server_args.return_code);
  6813. FreeTcpReady(&ready);
  6814. #ifdef WOLFSSL_TIRTOS
  6815. fdOpenSession(Task_self());
  6816. #endif
  6817. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6818. * !defined(WOLFSSL_TLS13) */
  6819. return EXPECT_RESULT();
  6820. }
  6821. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6822. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6823. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6824. WOLFSSL_CTX* ctx)
  6825. {
  6826. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6827. myVerifyAction = VERIFY_USE_PREVERFIY;
  6828. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6829. return TEST_SUCCESS;
  6830. }
  6831. #endif
  6832. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6833. {
  6834. EXPECT_DECLS;
  6835. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6836. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6837. test_ssl_cbf client_cbf;
  6838. test_ssl_cbf server_cbf;
  6839. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6840. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6841. #ifdef WOLFSSL_TLS13
  6842. client_cbf.method = wolfTLSv1_3_client_method;
  6843. #endif /* WOLFSSL_TLS13 */
  6844. client_cbf.ctx_ready =
  6845. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6846. /* test case 1 verify depth is equal to peer chain */
  6847. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6848. &server_cbf, NULL), TEST_SUCCESS);
  6849. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6850. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6851. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6852. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6853. return EXPECT_RESULT();
  6854. }
  6855. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6856. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6857. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6858. WOLFSSL_CTX* ctx)
  6859. {
  6860. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6861. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  6862. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6863. return TEST_SUCCESS;
  6864. }
  6865. #endif
  6866. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6867. {
  6868. EXPECT_DECLS;
  6869. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6870. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6871. test_ssl_cbf client_cbf;
  6872. test_ssl_cbf server_cbf;
  6873. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6874. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6875. #ifdef WOLFSSL_TLS13
  6876. client_cbf.method = wolfTLSv1_3_client_method;
  6877. #endif /* WOLFSSL_TLS13 */
  6878. client_cbf.ctx_ready =
  6879. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6880. /* test case 2
  6881. * verify depth is zero, number of peer's chain is 2.
  6882. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6883. * callback.
  6884. */
  6885. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6886. &server_cbf, NULL), TEST_SUCCESS);
  6887. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6888. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6889. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6890. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6891. return EXPECT_RESULT();
  6892. }
  6893. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6894. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6895. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6896. WOLFSSL_CTX* ctx)
  6897. {
  6898. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6899. myVerifyAction = VERIFY_USE_PREVERFIY;
  6900. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6901. return TEST_SUCCESS;
  6902. }
  6903. #endif
  6904. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6905. {
  6906. EXPECT_DECLS;
  6907. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6908. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6909. test_ssl_cbf client_cbf;
  6910. test_ssl_cbf server_cbf;
  6911. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6912. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6913. #ifdef WOLFSSL_TLS13
  6914. client_cbf.method = wolfTLSv1_3_client_method;
  6915. #endif /* WOLFSSL_TLS13 */
  6916. client_cbf.ctx_ready =
  6917. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6918. /* test case 3
  6919. * verify depth is zero, number of peer's chain is 2
  6920. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6921. * therefore, handshake becomes failure.
  6922. */
  6923. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6924. &server_cbf, NULL), TEST_FAIL);
  6925. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  6926. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  6927. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  6928. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  6929. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6930. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6931. return EXPECT_RESULT();
  6932. }
  6933. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6934. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6935. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6936. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6937. {
  6938. EXPECT_DECLS;
  6939. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6940. return EXPECT_RESULT();
  6941. }
  6942. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6943. {
  6944. EXPECT_DECLS;
  6945. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6946. return EXPECT_RESULT();
  6947. }
  6948. #endif
  6949. static int test_wolfSSL_CTX_set_cipher_list(void)
  6950. {
  6951. EXPECT_DECLS;
  6952. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6953. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6954. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6955. WOLFSSL_CTX* ctxClient = NULL;
  6956. WOLFSSL* sslClient = NULL;
  6957. test_ssl_cbf client_cbf;
  6958. test_ssl_cbf server_cbf;
  6959. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6960. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6961. server_cbf.method = wolfTLSv1_2_server_method;
  6962. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6963. client_cbf.method = wolfTLSv1_2_client_method;
  6964. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6965. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6966. &server_cbf, NULL), TEST_SUCCESS);
  6967. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6968. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6969. /* check with cipher string that has '+' */
  6970. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6971. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6972. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6973. /* check for the existence of an ECDHE ECDSA cipher suite */
  6974. if (EXPECT_SUCCESS()) {
  6975. int i = 0;
  6976. int found = 0;
  6977. const char* suite;
  6978. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6979. WOLFSSL_CIPHER* current;
  6980. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6981. do {
  6982. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6983. if (current) {
  6984. suite = wolfSSL_CIPHER_get_name(current);
  6985. if (suite && XSTRSTR(suite, "ECDSA")) {
  6986. found = 1;
  6987. break;
  6988. }
  6989. }
  6990. } while (current);
  6991. ExpectIntEQ(found, 1);
  6992. }
  6993. wolfSSL_free(sslClient);
  6994. wolfSSL_CTX_free(ctxClient);
  6995. #endif
  6996. return EXPECT_RESULT();
  6997. }
  6998. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6999. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7000. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7001. WOLFSSL* ssl)
  7002. {
  7003. EXPECT_DECLS;
  7004. size_t msg_len;
  7005. (void)ctx;
  7006. /* get_finished test */
  7007. /* 1. get own sent message */
  7008. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7009. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7010. ExpectIntGE(msg_len, 0);
  7011. /* 2. get peer message */
  7012. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7013. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7014. ExpectIntGE(msg_len, 0);
  7015. return EXPECT_RESULT();
  7016. }
  7017. #endif
  7018. static int test_wolfSSL_get_finished(void)
  7019. {
  7020. EXPECT_DECLS;
  7021. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7022. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7023. test_ssl_cbf client_cbf;
  7024. test_ssl_cbf server_cbf;
  7025. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7026. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7027. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7028. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7029. TEST_SUCCESS);
  7030. /* test received msg vs sent msg */
  7031. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7032. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7033. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7034. return EXPECT_RESULT();
  7035. }
  7036. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7037. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7038. !defined(NO_SESSION_CACHE)
  7039. /* Sessions to restore/store */
  7040. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7041. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7042. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7043. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7044. {
  7045. /* Don't store sessions. Lookup is still enabled. */
  7046. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7047. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7048. #ifdef OPENSSL_EXTRA
  7049. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7050. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7051. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7052. #endif
  7053. /* Require both peers to provide certs */
  7054. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7055. }
  7056. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7057. {
  7058. WOLFSSL_SESSION** sess;
  7059. if (wolfSSL_is_server(ssl))
  7060. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7061. else
  7062. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7063. if (*sess == NULL) {
  7064. #ifdef NO_SESSION_CACHE_REF
  7065. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7066. #else
  7067. /* Test for backwards compatibility */
  7068. if (wolfSSL_is_server(ssl)) {
  7069. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7070. }
  7071. else {
  7072. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7073. }
  7074. #endif
  7075. /* Now save the session in the internal store to make it available
  7076. * for lookup. For TLS 1.3, we can't save the session without
  7077. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7078. * session from cache. */
  7079. if (wolfSSL_is_server(ssl)
  7080. #ifndef WOLFSSL_TICKET_HAVE_ID
  7081. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7082. #endif
  7083. )
  7084. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7085. *sess), WOLFSSL_SUCCESS);
  7086. }
  7087. else {
  7088. /* If we have a session retrieved then remaining connections should be
  7089. * resuming on that session */
  7090. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7091. }
  7092. /* Save CTX to be able to decrypt tickets */
  7093. if (wolfSSL_is_server(ssl) &&
  7094. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7095. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7096. = wolfSSL_get_SSL_CTX(ssl));
  7097. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7098. WOLFSSL_SUCCESS);
  7099. }
  7100. #ifdef SESSION_CERTS
  7101. #ifndef WOLFSSL_TICKET_HAVE_ID
  7102. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7103. wolfSSL_session_reused(ssl))
  7104. #endif
  7105. {
  7106. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7107. * for all connections. TLS 1.3 only has tickets so if we don't
  7108. * include the session id in the ticket then the certificates
  7109. * will not be available on resumption. */
  7110. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7111. AssertNotNull(peer);
  7112. wolfSSL_X509_free(peer);
  7113. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7114. #ifdef OPENSSL_EXTRA
  7115. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7116. #endif
  7117. }
  7118. #endif /* SESSION_CERTS */
  7119. }
  7120. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7121. {
  7122. /* Set the session to reuse for the client */
  7123. AssertIntEQ(wolfSSL_set_session(ssl,
  7124. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7125. }
  7126. #endif
  7127. static int test_wolfSSL_CTX_add_session(void)
  7128. {
  7129. EXPECT_DECLS;
  7130. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7131. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7132. !defined(NO_SESSION_CACHE)
  7133. tcp_ready ready;
  7134. func_args client_args;
  7135. func_args server_args;
  7136. THREAD_TYPE serverThread;
  7137. callback_functions client_cb;
  7138. callback_functions server_cb;
  7139. method_provider methods[][2] = {
  7140. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7141. !defined(NO_DES3))
  7142. /* Without AES there are almost no ciphersuites available. This leads
  7143. * to no ciphersuites being available and an error. */
  7144. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7145. #endif
  7146. #ifndef WOLFSSL_NO_TLS12
  7147. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7148. #endif
  7149. /* Needs the default ticket callback since it is tied to the
  7150. * connection context and this makes it easy to carry over the ticket
  7151. * crypto context between connections */
  7152. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7153. defined(HAVE_SESSION_TICKET)
  7154. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7155. #endif
  7156. };
  7157. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7158. size_t i, j;
  7159. for (i = 0; i < methodsLen; i++) {
  7160. /* First run creates a connection while the second+ run will attempt
  7161. * to resume the connection. The trick is that the internal cache
  7162. * is turned off. wolfSSL_CTX_add_session should put the session in
  7163. * the cache anyway. */
  7164. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7165. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7166. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7167. #ifdef NO_SESSION_CACHE_REF
  7168. for (j = 0; j < 4; j++) {
  7169. #else
  7170. /* The session may be overwritten in this case. Do only one resumption
  7171. * to stop this test from failing intermittently. */
  7172. for (j = 0; j < 2; j++) {
  7173. #endif
  7174. #ifdef WOLFSSL_TIRTOS
  7175. fdOpenSession(Task_self());
  7176. #endif
  7177. StartTCP();
  7178. InitTcpReady(&ready);
  7179. XMEMSET(&client_args, 0, sizeof(func_args));
  7180. XMEMSET(&server_args, 0, sizeof(func_args));
  7181. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7182. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7183. client_cb.method = methods[i][0];
  7184. server_cb.method = methods[i][1];
  7185. server_args.signal = &ready;
  7186. server_args.callbacks = &server_cb;
  7187. client_args.signal = &ready;
  7188. client_args.callbacks = &client_cb;
  7189. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7190. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7191. server_cb.isSharedCtx = 1;
  7192. }
  7193. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7194. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7195. if (j != 0)
  7196. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7197. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7198. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7199. server_cb.ticNoInit = 1; /* Use default builtin */
  7200. start_thread(test_server_nofail, &server_args, &serverThread);
  7201. wait_tcp_ready(&server_args);
  7202. test_client_nofail(&client_args, NULL);
  7203. join_thread(serverThread);
  7204. ExpectTrue(client_args.return_code);
  7205. ExpectTrue(server_args.return_code);
  7206. FreeTcpReady(&ready);
  7207. if (EXPECT_FAIL())
  7208. break;
  7209. }
  7210. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7211. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7212. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7213. if (EXPECT_FAIL())
  7214. break;
  7215. }
  7216. #endif
  7217. return EXPECT_RESULT();
  7218. }
  7219. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7220. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7221. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7222. defined(HAVE_SESSION_TICKET) && \
  7223. !defined(TITAN_SESSION_CACHE) && \
  7224. !defined(HUGE_SESSION_CACHE) && \
  7225. !defined(BIG_SESSION_CACHE) && \
  7226. !defined(MEDIUM_SESSION_CACHE)
  7227. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7228. /* Sessions to restore/store */
  7229. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7230. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7231. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7232. static int twcase_new_session_called = 0;
  7233. static int twcase_remove_session_called = 0;
  7234. static int twcase_get_session_called = 0;
  7235. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7236. #define SESSION_CACHE_SIZE 33
  7237. typedef struct {
  7238. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7239. WOLFSSL_SESSION* value;
  7240. } hashTable_entry;
  7241. typedef struct {
  7242. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7243. size_t capacity; /* size of entries */
  7244. size_t length; /* number of items in the hash table */
  7245. wolfSSL_Mutex htLock; /* lock */
  7246. }hashTable;
  7247. static hashTable server_sessionCache;
  7248. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7249. {
  7250. int i;
  7251. unsigned int len;
  7252. (void)ssl;
  7253. /*
  7254. * This example uses a hash table.
  7255. * Steps you should take for a non-demo code:
  7256. * - acquire a lock for the file named according to the session id
  7257. * - open the file
  7258. * - encrypt and write the SSL_SESSION object to the file
  7259. * - release the lock
  7260. *
  7261. * Return:
  7262. * 0: The callback does not wish to hold a reference of the sess
  7263. * 1: The callback wants to hold a reference of the sess. The callback is
  7264. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7265. */
  7266. if (sess == NULL)
  7267. return 0;
  7268. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7269. return 0;
  7270. }
  7271. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7272. if (server_sessionCache.entries[i].value == NULL) {
  7273. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7274. server_sessionCache.entries[i].value = sess;
  7275. server_sessionCache.length++;
  7276. break;
  7277. }
  7278. }
  7279. ++twcase_new_session_called;
  7280. wc_UnLockMutex(&server_sessionCache.htLock);
  7281. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7282. twcase_new_session_called);
  7283. return 1;
  7284. }
  7285. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7286. {
  7287. int i;
  7288. (void)ctx;
  7289. (void)sess;
  7290. if (sess == NULL)
  7291. return;
  7292. /*
  7293. * This example uses a hash table.
  7294. * Steps you should take for a non-demo code:
  7295. * - acquire a lock for the file named according to the session id
  7296. * - remove the file
  7297. * - release the lock
  7298. */
  7299. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7300. return;
  7301. }
  7302. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7303. if (server_sessionCache.entries[i].key != NULL &&
  7304. XMEMCMP(server_sessionCache.entries[i].key,
  7305. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7306. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7307. server_sessionCache.entries[i].value = NULL;
  7308. server_sessionCache.entries[i].key = NULL;
  7309. server_sessionCache.length--;
  7310. break;
  7311. }
  7312. }
  7313. ++twcase_remove_session_called;
  7314. wc_UnLockMutex(&server_sessionCache.htLock);
  7315. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7316. twcase_remove_session_called);
  7317. }
  7318. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7319. const unsigned char *id, int len, int *ref)
  7320. {
  7321. int i;
  7322. (void)ssl;
  7323. (void)id;
  7324. (void)len;
  7325. /*
  7326. * This example uses a hash table.
  7327. * Steps you should take for a non-demo code:
  7328. * - acquire a lock for the file named according to the session id in the
  7329. * 2nd arg
  7330. * - read and decrypt contents of file and create a new SSL_SESSION
  7331. * - object release the lock
  7332. * - return the new session object
  7333. */
  7334. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7335. ++twcase_get_session_called);
  7336. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7337. * be responsible for the pointer then set to 0. */
  7338. *ref = 1;
  7339. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7340. if (server_sessionCache.entries[i].key != NULL &&
  7341. XMEMCMP(server_sessionCache.entries[i].key, id,
  7342. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7343. return server_sessionCache.entries[i].value;
  7344. }
  7345. }
  7346. return NULL;
  7347. }
  7348. static int twcase_get_sessionCb_cleanup(void)
  7349. {
  7350. int i;
  7351. int cnt = 0;
  7352. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7353. * for freeing sessions */
  7354. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7355. if (server_sessionCache.entries[i].value != NULL) {
  7356. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7357. cnt++;
  7358. }
  7359. }
  7360. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7361. cnt);
  7362. return TEST_SUCCESS;
  7363. }
  7364. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7365. {
  7366. EXPECT_DECLS;
  7367. /* off - Disable internal cache */
  7368. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7369. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7370. #ifdef OPENSSL_EXTRA
  7371. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7372. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7373. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7374. #endif
  7375. /* off - Do not setup external cache */
  7376. /* Require both peers to provide certs */
  7377. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7378. return EXPECT_RESULT();
  7379. }
  7380. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7381. {
  7382. /* on - internal cache is on by default */
  7383. /* off - Do not setup external cache */
  7384. /* Require both peers to provide certs */
  7385. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7386. return TEST_SUCCESS;
  7387. }
  7388. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7389. {
  7390. EXPECT_DECLS;
  7391. /* off - Disable internal cache */
  7392. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7393. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7394. #ifdef OPENSSL_EXTRA
  7395. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7396. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7397. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7398. #endif
  7399. /* on - Enable external cache */
  7400. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7401. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7402. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7403. /* Require both peers to provide certs */
  7404. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7405. return EXPECT_RESULT();
  7406. }
  7407. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7408. {
  7409. /* on - internal cache is on by default */
  7410. /* on - Enable external cache */
  7411. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7412. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7413. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7414. /* Require both peers to provide certs */
  7415. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7416. return TEST_SUCCESS;
  7417. }
  7418. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7419. {
  7420. /* on - internal cache is on by default */
  7421. /* on - Enable external cache */
  7422. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7423. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7424. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7425. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7426. /* Require both peers to provide certs */
  7427. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7428. return TEST_SUCCESS;
  7429. }
  7430. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7431. {
  7432. EXPECT_DECLS;
  7433. WOLFSSL_SESSION** sess;
  7434. if (wolfSSL_is_server(ssl))
  7435. sess = &twcase_server_first_session_ptr;
  7436. else
  7437. return TEST_SUCCESS;
  7438. if (*sess == NULL) {
  7439. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7440. /* Now save the session in the internal store to make it available
  7441. * for lookup. For TLS 1.3, we can't save the session without
  7442. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7443. * session from cache. */
  7444. if (wolfSSL_is_server(ssl)
  7445. #ifndef WOLFSSL_TICKET_HAVE_ID
  7446. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7447. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7448. #endif
  7449. ) {
  7450. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7451. *sess), WOLFSSL_SUCCESS);
  7452. }
  7453. }
  7454. /* Save CTX to be able to decrypt tickets */
  7455. if (twcase_server_current_ctx_ptr == NULL) {
  7456. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7457. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7458. WOLFSSL_SUCCESS);
  7459. }
  7460. #ifdef SESSION_CERTS
  7461. #ifndef WOLFSSL_TICKET_HAVE_ID
  7462. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7463. wolfSSL_session_reused(ssl))
  7464. #endif
  7465. {
  7466. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7467. * for all connections. TLS 1.3 only has tickets so if we don't
  7468. * include the session id in the ticket then the certificates
  7469. * will not be available on resumption. */
  7470. WOLFSSL_X509* peer = NULL;
  7471. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7472. wolfSSL_X509_free(peer);
  7473. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7474. }
  7475. #endif
  7476. return EXPECT_RESULT();
  7477. }
  7478. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7479. {
  7480. EXPECT_DECLS;
  7481. WOLFSSL_SESSION** sess;
  7482. sess = &twcase_client_first_session_ptr;
  7483. if (*sess == NULL) {
  7484. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7485. }
  7486. else {
  7487. /* If we have a session retrieved then remaining connections should be
  7488. * resuming on that session */
  7489. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7490. }
  7491. #ifdef SESSION_CERTS
  7492. #ifndef WOLFSSL_TICKET_HAVE_ID
  7493. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7494. wolfSSL_session_reused(ssl))
  7495. #endif
  7496. {
  7497. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7498. ExpectNotNull(peer);
  7499. wolfSSL_X509_free(peer);
  7500. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7501. #ifdef OPENSSL_EXTRA
  7502. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7503. #endif
  7504. }
  7505. #endif
  7506. return EXPECT_RESULT();
  7507. }
  7508. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7509. {
  7510. EXPECT_DECLS;
  7511. /* Set the session to reuse for the client */
  7512. ExpectNotNull(ssl);
  7513. ExpectNotNull(twcase_client_first_session_ptr);
  7514. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7515. WOLFSSL_SUCCESS);
  7516. return EXPECT_RESULT();
  7517. }
  7518. struct test_add_session_ext_params {
  7519. method_provider client_meth;
  7520. method_provider server_meth;
  7521. const char* tls_version;
  7522. };
  7523. static int test_wolfSSL_CTX_add_session_ext(
  7524. struct test_add_session_ext_params* param)
  7525. {
  7526. EXPECT_DECLS;
  7527. /* Test the default 33 sessions */
  7528. int j;
  7529. /* Clear cache before starting */
  7530. wolfSSL_CTX_flush_sessions(NULL, -1);
  7531. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7532. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7533. return BAD_MUTEX_E;
  7534. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7535. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7536. for (j = 0; j < 5; j++) {
  7537. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7538. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7539. test_ssl_cbf client_cb;
  7540. test_ssl_cbf server_cb;
  7541. (void)dtls;
  7542. /* Test five cache configurations */
  7543. twcase_client_first_session_ptr = NULL;
  7544. twcase_server_first_session_ptr = NULL;
  7545. twcase_server_current_ctx_ptr = NULL;
  7546. twcase_new_session_called = 0;
  7547. twcase_remove_session_called = 0;
  7548. twcase_get_session_called = 0;
  7549. /* connection 1 - first connection */
  7550. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7551. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7552. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7553. client_cb.method = param->client_meth;
  7554. server_cb.method = param->server_meth;
  7555. if (dtls)
  7556. client_cb.doUdp = server_cb.doUdp = 1;
  7557. /* Setup internal and external cache */
  7558. switch (j) {
  7559. case 0:
  7560. /* SSL_OP_NO_TICKET stateful ticket case */
  7561. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7562. break;
  7563. case 1:
  7564. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7565. break;
  7566. case 2:
  7567. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7568. break;
  7569. case 3:
  7570. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7571. break;
  7572. case 4:
  7573. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7574. break;
  7575. }
  7576. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7577. /* Add session to internal cache and save SSL session for testing */
  7578. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7579. /* Save client SSL session for testing */
  7580. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7581. server_cb.ticNoInit = 1; /* Use default builtin */
  7582. /* Don't free/release ctx */
  7583. server_cb.ctx = twcase_server_current_ctx_ptr;
  7584. server_cb.isSharedCtx = 1;
  7585. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7586. &server_cb, NULL), TEST_SUCCESS);
  7587. ExpectIntEQ(twcase_get_session_called, 0);
  7588. if (EXPECT_FAIL()) {
  7589. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7590. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7591. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7592. break;
  7593. }
  7594. switch (j) {
  7595. case 0:
  7596. case 1:
  7597. case 2:
  7598. /* cache cannot be searched with out a connection */
  7599. /* Add a new session */
  7600. ExpectIntEQ(twcase_new_session_called, 1);
  7601. /* In twcase_server_sess_ctx_pre_shutdown
  7602. * wolfSSL_CTX_add_session which evicts the existing session
  7603. * in cache and adds it back in */
  7604. ExpectIntLE(twcase_remove_session_called, 1);
  7605. break;
  7606. case 3:
  7607. case 4:
  7608. /* no external cache */
  7609. ExpectIntEQ(twcase_new_session_called, 0);
  7610. ExpectIntEQ(twcase_remove_session_called, 0);
  7611. break;
  7612. }
  7613. /* connection 2 - session resume */
  7614. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7615. twcase_new_session_called = 0;
  7616. twcase_remove_session_called = 0;
  7617. twcase_get_session_called = 0;
  7618. server_cb.on_result = 0;
  7619. client_cb.on_result = 0;
  7620. server_cb.ticNoInit = 1; /* Use default builtin */
  7621. server_cb.ctx = twcase_server_current_ctx_ptr;
  7622. /* try session resumption */
  7623. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7624. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7625. &server_cb, NULL), TEST_SUCCESS);
  7626. /* Clear cache before checking */
  7627. wolfSSL_CTX_flush_sessions(NULL, -1);
  7628. switch (j) {
  7629. case 0:
  7630. if (tls13) {
  7631. /* (D)TLSv1.3 stateful case */
  7632. /* cache hit */
  7633. /* DTLS accesses cache once for stateless parsing and
  7634. * once for stateful parsing */
  7635. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7636. /* (D)TLSv1.3 creates a new ticket,
  7637. * updates both internal and external cache */
  7638. ExpectIntEQ(twcase_new_session_called, 1);
  7639. /* A new session ID is created for a new ticket */
  7640. ExpectIntEQ(twcase_remove_session_called, 2);
  7641. }
  7642. else {
  7643. /* non (D)TLSv1.3 case, no update */
  7644. /* DTLS accesses cache once for stateless parsing and
  7645. * once for stateful parsing */
  7646. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7647. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7648. #else
  7649. ExpectIntEQ(twcase_get_session_called, 1);
  7650. #endif
  7651. ExpectIntEQ(twcase_new_session_called, 0);
  7652. /* Called on session added in
  7653. * twcase_server_sess_ctx_pre_shutdown */
  7654. ExpectIntEQ(twcase_remove_session_called, 1);
  7655. }
  7656. break;
  7657. case 1:
  7658. if (tls13) {
  7659. /* (D)TLSv1.3 case */
  7660. /* cache hit */
  7661. ExpectIntEQ(twcase_get_session_called, 1);
  7662. /* (D)TLSv1.3 creates a new ticket,
  7663. * updates both internal and external cache */
  7664. ExpectIntEQ(twcase_new_session_called, 1);
  7665. /* Called on session added in
  7666. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7667. ExpectIntEQ(twcase_remove_session_called, 1);
  7668. }
  7669. else {
  7670. /* non (D)TLSv1.3 case */
  7671. /* cache hit */
  7672. /* DTLS accesses cache once for stateless parsing and
  7673. * once for stateful parsing */
  7674. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7675. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7676. #else
  7677. ExpectIntEQ(twcase_get_session_called, 1);
  7678. #endif
  7679. ExpectIntEQ(twcase_new_session_called, 0);
  7680. /* Called on session added in
  7681. * twcase_server_sess_ctx_pre_shutdown */
  7682. ExpectIntEQ(twcase_remove_session_called, 1);
  7683. }
  7684. break;
  7685. case 2:
  7686. if (tls13) {
  7687. /* (D)TLSv1.3 case */
  7688. /* cache hit */
  7689. ExpectIntEQ(twcase_get_session_called, 1);
  7690. /* (D)TLSv1.3 creates a new ticket,
  7691. * updates both internal and external cache */
  7692. ExpectIntEQ(twcase_new_session_called, 1);
  7693. /* Called on session added in
  7694. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7695. ExpectIntEQ(twcase_remove_session_called, 1);
  7696. }
  7697. else {
  7698. /* non (D)TLSv1.3 case */
  7699. /* cache hit */
  7700. /* DTLS accesses cache once for stateless parsing and
  7701. * once for stateful parsing */
  7702. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7703. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7704. #else
  7705. ExpectIntEQ(twcase_get_session_called, 1);
  7706. #endif
  7707. ExpectIntEQ(twcase_new_session_called, 0);
  7708. /* Called on session added in
  7709. * twcase_server_sess_ctx_pre_shutdown */
  7710. ExpectIntEQ(twcase_remove_session_called, 1);
  7711. }
  7712. break;
  7713. case 3:
  7714. case 4:
  7715. /* no external cache */
  7716. ExpectIntEQ(twcase_get_session_called, 0);
  7717. ExpectIntEQ(twcase_new_session_called, 0);
  7718. ExpectIntEQ(twcase_remove_session_called, 0);
  7719. break;
  7720. }
  7721. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7722. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7723. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7724. if (EXPECT_FAIL())
  7725. break;
  7726. }
  7727. twcase_get_sessionCb_cleanup();
  7728. XMEMSET(&server_sessionCache.entries, 0,
  7729. sizeof(server_sessionCache.entries));
  7730. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7731. wc_FreeMutex(&server_sessionCache.htLock);
  7732. return EXPECT_RESULT();
  7733. }
  7734. #endif
  7735. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7736. {
  7737. EXPECT_DECLS;
  7738. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7739. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7740. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7741. defined(HAVE_SESSION_TICKET) && \
  7742. !defined(TITAN_SESSION_CACHE) && \
  7743. !defined(HUGE_SESSION_CACHE) && \
  7744. !defined(BIG_SESSION_CACHE) && \
  7745. !defined(MEDIUM_SESSION_CACHE)
  7746. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7747. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7748. struct test_add_session_ext_params param[1] = {
  7749. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7750. };
  7751. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7752. #endif
  7753. #endif
  7754. return EXPECT_RESULT();
  7755. }
  7756. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7757. {
  7758. EXPECT_DECLS;
  7759. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7760. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7761. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7762. defined(HAVE_SESSION_TICKET) && \
  7763. !defined(TITAN_SESSION_CACHE) && \
  7764. !defined(HUGE_SESSION_CACHE) && \
  7765. !defined(BIG_SESSION_CACHE) && \
  7766. !defined(MEDIUM_SESSION_CACHE)
  7767. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7768. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7769. #ifdef WOLFSSL_DTLS13
  7770. struct test_add_session_ext_params param[1] = {
  7771. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7772. };
  7773. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7774. #endif
  7775. #endif
  7776. #endif
  7777. return EXPECT_RESULT();
  7778. }
  7779. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  7780. {
  7781. EXPECT_DECLS;
  7782. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7783. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7784. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7785. defined(HAVE_SESSION_TICKET) && \
  7786. !defined(TITAN_SESSION_CACHE) && \
  7787. !defined(HUGE_SESSION_CACHE) && \
  7788. !defined(BIG_SESSION_CACHE) && \
  7789. !defined(MEDIUM_SESSION_CACHE)
  7790. #ifndef WOLFSSL_NO_TLS12
  7791. struct test_add_session_ext_params param[1] = {
  7792. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7793. };
  7794. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7795. #endif
  7796. #endif
  7797. return EXPECT_RESULT();
  7798. }
  7799. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7800. {
  7801. EXPECT_DECLS;
  7802. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7803. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7804. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7805. defined(HAVE_SESSION_TICKET) && \
  7806. !defined(TITAN_SESSION_CACHE) && \
  7807. !defined(HUGE_SESSION_CACHE) && \
  7808. !defined(BIG_SESSION_CACHE) && \
  7809. !defined(MEDIUM_SESSION_CACHE)
  7810. #ifndef WOLFSSL_NO_TLS12
  7811. #ifdef WOLFSSL_DTLS
  7812. struct test_add_session_ext_params param[1] = {
  7813. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7814. };
  7815. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7816. #endif
  7817. #endif
  7818. #endif
  7819. return EXPECT_RESULT();
  7820. }
  7821. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7822. {
  7823. EXPECT_DECLS;
  7824. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7825. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7826. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7827. defined(HAVE_SESSION_TICKET) && \
  7828. !defined(TITAN_SESSION_CACHE) && \
  7829. !defined(HUGE_SESSION_CACHE) && \
  7830. !defined(BIG_SESSION_CACHE) && \
  7831. !defined(MEDIUM_SESSION_CACHE)
  7832. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7833. !defined(NO_DES3))
  7834. struct test_add_session_ext_params param[1] = {
  7835. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7836. };
  7837. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7838. #endif
  7839. #endif
  7840. return EXPECT_RESULT();
  7841. }
  7842. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7843. {
  7844. EXPECT_DECLS;
  7845. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7846. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7847. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7848. defined(HAVE_SESSION_TICKET) && \
  7849. !defined(TITAN_SESSION_CACHE) && \
  7850. !defined(HUGE_SESSION_CACHE) && \
  7851. !defined(BIG_SESSION_CACHE) && \
  7852. !defined(MEDIUM_SESSION_CACHE)
  7853. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7854. !defined(NO_DES3))
  7855. #ifdef WOLFSSL_DTLS
  7856. struct test_add_session_ext_params param[1] = {
  7857. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7858. };
  7859. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7860. #endif
  7861. #endif
  7862. #endif
  7863. return EXPECT_RESULT();
  7864. }
  7865. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7866. /* canned export of a session using older version 3 */
  7867. static unsigned char version_3[] = {
  7868. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7869. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7870. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7871. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7872. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7873. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7874. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7875. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7876. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7877. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7878. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7879. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7880. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7881. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7882. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7883. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7884. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7885. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7886. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7887. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7888. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7889. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7890. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7891. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7892. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7893. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7894. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7895. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7897. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7900. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7901. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7902. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7903. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7904. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7905. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7906. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7907. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7908. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7909. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7910. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7911. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7912. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7913. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7914. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7915. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7916. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7917. 0x2E, 0x31, 0xED, 0x4F
  7918. };
  7919. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7920. static int test_wolfSSL_dtls_export(void)
  7921. {
  7922. EXPECT_DECLS;
  7923. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7924. tcp_ready ready;
  7925. func_args client_args;
  7926. func_args server_args;
  7927. THREAD_TYPE serverThread;
  7928. callback_functions server_cbf;
  7929. callback_functions client_cbf;
  7930. #ifdef WOLFSSL_TIRTOS
  7931. fdOpenSession(Task_self());
  7932. #endif
  7933. InitTcpReady(&ready);
  7934. #if defined(USE_WINDOWS_API)
  7935. /* use RNG to get random port if using windows */
  7936. ready.port = GetRandomPort();
  7937. #endif
  7938. /* set using dtls */
  7939. XMEMSET(&client_args, 0, sizeof(func_args));
  7940. XMEMSET(&server_args, 0, sizeof(func_args));
  7941. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7942. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7943. server_cbf.method = wolfDTLSv1_2_server_method;
  7944. client_cbf.method = wolfDTLSv1_2_client_method;
  7945. server_args.callbacks = &server_cbf;
  7946. client_args.callbacks = &client_cbf;
  7947. server_args.signal = &ready;
  7948. client_args.signal = &ready;
  7949. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7950. wait_tcp_ready(&server_args);
  7951. run_wolfssl_client(&client_args);
  7952. join_thread(serverThread);
  7953. ExpectTrue(client_args.return_code);
  7954. ExpectTrue(server_args.return_code);
  7955. FreeTcpReady(&ready);
  7956. #ifdef WOLFSSL_TIRTOS
  7957. fdOpenSession(Task_self());
  7958. #endif
  7959. if (EXPECT_SUCCESS()) {
  7960. SOCKET_T sockfd = 0;
  7961. WOLFSSL_CTX* ctx = NULL;
  7962. WOLFSSL* ssl = NULL;
  7963. char msg[64] = "hello wolfssl!";
  7964. char reply[1024];
  7965. int msgSz = (int)XSTRLEN(msg);
  7966. byte *session, *window;
  7967. unsigned int sessionSz = 0;
  7968. unsigned int windowSz = 0;
  7969. #ifndef TEST_IPV6
  7970. struct sockaddr_in peerAddr;
  7971. #else
  7972. struct sockaddr_in6 peerAddr;
  7973. #endif /* TEST_IPV6 */
  7974. int i;
  7975. /* Set ctx to DTLS 1.2 */
  7976. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7977. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7978. /* test importing version 3 */
  7979. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7980. /* test importing bad length and bad version */
  7981. version_3[2] += 1;
  7982. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7983. version_3[2] -= 1; version_3[1] = 0XA0;
  7984. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7985. wolfSSL_free(ssl);
  7986. wolfSSL_CTX_free(ctx);
  7987. /* check storing client state after connection and storing window only */
  7988. #ifdef WOLFSSL_TIRTOS
  7989. fdOpenSession(Task_self());
  7990. #endif
  7991. InitTcpReady(&ready);
  7992. #if defined(USE_WINDOWS_API)
  7993. /* use RNG to get random port if using windows */
  7994. ready.port = GetRandomPort();
  7995. #endif
  7996. /* set using dtls */
  7997. XMEMSET(&server_args, 0, sizeof(func_args));
  7998. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7999. server_cbf.method = wolfDTLSv1_2_server_method;
  8000. server_cbf.doUdp = 1;
  8001. server_args.callbacks = &server_cbf;
  8002. server_args.argc = 3; /* set loop_count to 3 */
  8003. server_args.signal = &ready;
  8004. start_thread(test_server_nofail, &server_args, &serverThread);
  8005. wait_tcp_ready(&server_args);
  8006. /* create and connect with client */
  8007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8008. ExpectIntEQ(WOLFSSL_SUCCESS,
  8009. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8010. ExpectIntEQ(WOLFSSL_SUCCESS,
  8011. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8012. ExpectIntEQ(WOLFSSL_SUCCESS,
  8013. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8014. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8015. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8016. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8017. /* store server information connected too */
  8018. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8019. #ifndef TEST_IPV6
  8020. peerAddr.sin_family = AF_INET;
  8021. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8022. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8023. #else
  8024. peerAddr.sin6_family = AF_INET6;
  8025. ExpectIntEQ(
  8026. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8027. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8028. #endif
  8029. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8030. WOLFSSL_SUCCESS);
  8031. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8032. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8033. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8034. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8035. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8036. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8037. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8038. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8039. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8040. wolfSSL_free(ssl);
  8041. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8042. /* restore state */
  8043. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8044. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8045. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8046. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8047. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8048. WOLFSSL_SUCCESS);
  8049. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8050. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8051. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8052. wolfSSL_free(ssl);
  8053. }
  8054. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8055. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8056. wolfSSL_CTX_free(ctx);
  8057. fprintf(stderr, "done and waiting for server\n");
  8058. join_thread(serverThread);
  8059. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8060. FreeTcpReady(&ready);
  8061. #ifdef WOLFSSL_TIRTOS
  8062. fdOpenSession(Task_self());
  8063. #endif
  8064. }
  8065. #endif
  8066. return EXPECT_RESULT();
  8067. }
  8068. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8069. #ifdef WOLFSSL_TLS13
  8070. static const byte canned_client_tls13_session[] = {
  8071. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8072. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8073. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8074. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8075. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8076. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8077. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8078. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8079. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8080. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8081. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8082. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8083. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8084. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8085. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8086. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8087. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8088. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8089. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8090. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8091. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8092. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8093. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8094. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8095. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8096. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8097. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8098. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8099. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8100. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8101. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8102. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8103. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8104. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8105. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8106. 0x00, 0x03
  8107. };
  8108. static const byte canned_server_tls13_session[] = {
  8109. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8110. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8111. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8112. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8115. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8116. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8117. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8118. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8119. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8120. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8121. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8122. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8123. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8124. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8125. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8126. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8127. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8128. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8129. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8130. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8131. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8132. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8133. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8134. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8135. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8136. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8138. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8139. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8140. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8141. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8142. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8143. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8144. 0x00, 0x04
  8145. };
  8146. #endif /* WOLFSSL_TLS13 */
  8147. static const byte canned_client_session[] = {
  8148. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8149. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8150. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8151. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8152. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8153. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8155. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8156. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8157. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8158. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8159. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8160. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8161. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8162. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8163. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8164. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8165. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8166. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8167. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8168. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8169. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8170. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8171. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8172. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8173. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8174. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8175. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8176. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8181. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8182. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8183. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8184. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8185. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8186. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8187. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8188. 0x00, 0x03
  8189. };
  8190. static const byte canned_server_session[] = {
  8191. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8192. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8193. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8194. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8195. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8198. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8199. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8200. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8201. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8202. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8203. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8204. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8205. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8206. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8207. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8208. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8209. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8210. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8211. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8212. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8213. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8214. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8215. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8216. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8217. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8218. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8219. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8224. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8225. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8226. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8227. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8228. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8229. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8230. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8231. 0x00, 0x04
  8232. };
  8233. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8234. {
  8235. SOCKET_T sockfd = 0;
  8236. SOCKET_T clientfd = 0;
  8237. word16 port;
  8238. callback_functions* cbf;
  8239. WOLFSSL_CTX* ctx = 0;
  8240. WOLFSSL* ssl = 0;
  8241. char msg[] = "I hear you fa shizzle!";
  8242. char input[1024];
  8243. int idx;
  8244. #ifdef WOLFSSL_TIRTOS
  8245. fdOpenSession(Task_self());
  8246. #endif
  8247. ((func_args*)args)->return_code = TEST_FAIL;
  8248. cbf = ((func_args*)args)->callbacks;
  8249. #if defined(USE_WINDOWS_API)
  8250. port = ((func_args*)args)->signal->port;
  8251. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8252. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8253. /* Let tcp_listen assign port */
  8254. port = 0;
  8255. #else
  8256. /* Use default port */
  8257. port = wolfSSLPort;
  8258. #endif
  8259. /* do it here to detect failure */
  8260. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8261. CloseSocket(sockfd);
  8262. {
  8263. WOLFSSL_METHOD* method = NULL;
  8264. if (cbf != NULL && cbf->method != NULL) {
  8265. method = cbf->method();
  8266. }
  8267. else {
  8268. method = wolfTLSv1_2_server_method();
  8269. }
  8270. ctx = wolfSSL_CTX_new(method);
  8271. }
  8272. if (ctx == NULL) {
  8273. goto done;
  8274. }
  8275. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8276. /* call ctx setup callback */
  8277. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8278. cbf->ctx_ready(ctx);
  8279. }
  8280. ssl = wolfSSL_new(ctx);
  8281. if (ssl == NULL) {
  8282. goto done;
  8283. }
  8284. wolfSSL_set_fd(ssl, clientfd);
  8285. /* call ssl setup callback */
  8286. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8287. cbf->ssl_ready(ssl);
  8288. }
  8289. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8290. if (idx > 0) {
  8291. input[idx] = '\0';
  8292. fprintf(stderr, "Client message export/import: %s\n", input);
  8293. }
  8294. else {
  8295. fprintf(stderr, "ret = %d error = %d\n", idx,
  8296. wolfSSL_get_error(ssl, idx));
  8297. goto done;
  8298. }
  8299. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8300. /*err_sys("SSL_write failed");*/
  8301. WOLFSSL_RETURN_FROM_THREAD(0);
  8302. }
  8303. #ifdef WOLFSSL_TIRTOS
  8304. Task_yield();
  8305. #endif
  8306. ((func_args*)args)->return_code = TEST_SUCCESS;
  8307. done:
  8308. wolfSSL_shutdown(ssl);
  8309. wolfSSL_free(ssl);
  8310. wolfSSL_CTX_free(ctx);
  8311. CloseSocket(clientfd);
  8312. #ifdef WOLFSSL_TIRTOS
  8313. fdCloseSession(Task_self());
  8314. #endif
  8315. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8316. && defined(HAVE_THREAD_LS)
  8317. wc_ecc_fp_free(); /* free per thread cache */
  8318. #endif
  8319. #if defined(HAVE_SESSION_TICKET) && \
  8320. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8321. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8322. OpenSSLTicketCleanup();
  8323. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8324. TicketCleanup();
  8325. #endif
  8326. #endif
  8327. WOLFSSL_RETURN_FROM_THREAD(0);
  8328. }
  8329. static void load_tls12_canned_server(WOLFSSL* ssl)
  8330. {
  8331. int clientfd = wolfSSL_get_fd(ssl);
  8332. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8333. sizeof(canned_server_session)), sizeof(canned_server_session));
  8334. wolfSSL_set_fd(ssl, clientfd);
  8335. }
  8336. #ifdef WOLFSSL_TLS13
  8337. static void load_tls13_canned_server(WOLFSSL* ssl)
  8338. {
  8339. int clientfd = wolfSSL_get_fd(ssl);
  8340. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8341. sizeof(canned_server_tls13_session)),
  8342. sizeof(canned_server_tls13_session));
  8343. wolfSSL_set_fd(ssl, clientfd);
  8344. }
  8345. #endif
  8346. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8347. static int test_wolfSSL_tls_export_run(int v)
  8348. {
  8349. EXPECT_DECLS;
  8350. SOCKET_T sockfd = 0;
  8351. WOLFSSL_CTX* ctx = 0;
  8352. WOLFSSL* ssl = 0;
  8353. char msg[64] = "hello wolfssl!";
  8354. char reply[1024];
  8355. word32 replySz;
  8356. int msgSz = (int)XSTRLEN(msg);
  8357. const byte* clientSession = NULL;
  8358. int clientSessionSz = 0;
  8359. tcp_ready ready;
  8360. func_args server_args;
  8361. THREAD_TYPE serverThread;
  8362. callback_functions server_cbf;
  8363. #ifdef WOLFSSL_TIRTOS
  8364. fdOpenSession(Task_self());
  8365. #endif
  8366. InitTcpReady(&ready);
  8367. #if defined(USE_WINDOWS_API)
  8368. /* use RNG to get random port if using windows */
  8369. ready.port = GetRandomPort();
  8370. #endif
  8371. XMEMSET(&server_args, 0, sizeof(func_args));
  8372. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8373. switch (v) {
  8374. case WOLFSSL_TLSV1_2:
  8375. server_cbf.method = wolfTLSv1_2_server_method;
  8376. server_cbf.ssl_ready = load_tls12_canned_server;
  8377. /* setup the client side */
  8378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8379. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8380. clientSession = canned_client_session;
  8381. clientSessionSz = sizeof(canned_client_session);
  8382. break;
  8383. #ifdef WOLFSSL_TLS13
  8384. case WOLFSSL_TLSV1_3:
  8385. server_cbf.method = wolfTLSv1_3_server_method;
  8386. server_cbf.ssl_ready = load_tls13_canned_server;
  8387. /* setup the client side */
  8388. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8389. clientSession = canned_client_tls13_session;
  8390. clientSessionSz = sizeof(canned_client_tls13_session);
  8391. break;
  8392. #endif
  8393. }
  8394. server_args.callbacks = &server_cbf;
  8395. server_args.signal = &ready;
  8396. start_thread(tls_export_server, &server_args, &serverThread);
  8397. wait_tcp_ready(&server_args);
  8398. #ifdef WOLFSSL_TIRTOS
  8399. fdOpenSession(Task_self());
  8400. #endif
  8401. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8402. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8403. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8404. clientSessionSz);
  8405. replySz = sizeof(reply);
  8406. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8407. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8408. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8409. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8410. #endif
  8411. wolfSSL_set_fd(ssl, sockfd);
  8412. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8413. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8414. wolfSSL_free(ssl);
  8415. wolfSSL_CTX_free(ctx);
  8416. CloseSocket(sockfd);
  8417. #ifdef WOLFSSL_TIRTOS
  8418. fdCloseSession(Task_self());
  8419. #endif
  8420. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8421. && defined(HAVE_THREAD_LS)
  8422. wc_ecc_fp_free(); /* free per thread cache */
  8423. #endif
  8424. join_thread(serverThread);
  8425. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8426. FreeTcpReady(&ready);
  8427. #ifdef WOLFSSL_TIRTOS
  8428. fdOpenSession(Task_self());
  8429. #endif
  8430. return EXPECT_RESULT();
  8431. }
  8432. #endif
  8433. static int test_wolfSSL_tls_export(void)
  8434. {
  8435. int res = TEST_SKIPPED;
  8436. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8437. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8438. #ifdef WOLFSSL_TLS13
  8439. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8440. #endif
  8441. res = TEST_RES_CHECK(1);
  8442. #endif
  8443. return res;
  8444. }
  8445. /*----------------------------------------------------------------------------*
  8446. | TLS extensions tests
  8447. *----------------------------------------------------------------------------*/
  8448. #ifdef ENABLE_TLS_CALLBACK_TEST
  8449. /* Connection test runner - generic */
  8450. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8451. callback_functions* server_callbacks)
  8452. {
  8453. tcp_ready ready;
  8454. func_args client_args;
  8455. func_args server_args;
  8456. THREAD_TYPE serverThread;
  8457. XMEMSET(&client_args, 0, sizeof(func_args));
  8458. XMEMSET(&server_args, 0, sizeof(func_args));
  8459. StartTCP();
  8460. client_args.callbacks = client_callbacks;
  8461. server_args.callbacks = server_callbacks;
  8462. #ifdef WOLFSSL_TIRTOS
  8463. fdOpenSession(Task_self());
  8464. #endif
  8465. /* RUN Server side */
  8466. InitTcpReady(&ready);
  8467. #if defined(USE_WINDOWS_API)
  8468. /* use RNG to get random port if using windows */
  8469. ready.port = GetRandomPort();
  8470. #endif
  8471. server_args.signal = &ready;
  8472. client_args.signal = &ready;
  8473. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8474. wait_tcp_ready(&server_args);
  8475. /* RUN Client side */
  8476. run_wolfssl_client(&client_args);
  8477. join_thread(serverThread);
  8478. FreeTcpReady(&ready);
  8479. #ifdef WOLFSSL_TIRTOS
  8480. fdCloseSession(Task_self());
  8481. #endif
  8482. client_callbacks->return_code = client_args.return_code;
  8483. server_callbacks->return_code = server_args.return_code;
  8484. }
  8485. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8486. #ifdef HAVE_SNI
  8487. static int test_wolfSSL_UseSNI_params(void)
  8488. {
  8489. EXPECT_DECLS;
  8490. #if !defined(NO_WOLFSSL_CLIENT)
  8491. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8492. WOLFSSL *ssl = wolfSSL_new(ctx);
  8493. ExpectNotNull(ctx);
  8494. ExpectNotNull(ssl);
  8495. /* invalid [ctx|ssl] */
  8496. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8497. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8498. /* invalid type */
  8499. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8500. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8501. /* invalid data */
  8502. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8503. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8504. /* success case */
  8505. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8506. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8507. wolfSSL_free(ssl);
  8508. wolfSSL_CTX_free(ctx);
  8509. #endif /* !NO_WOLFSSL_CLIENT */
  8510. return EXPECT_RESULT();
  8511. }
  8512. /* BEGIN of connection tests callbacks */
  8513. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8514. {
  8515. AssertIntEQ(WOLFSSL_SUCCESS,
  8516. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8517. }
  8518. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8519. {
  8520. AssertIntEQ(WOLFSSL_SUCCESS,
  8521. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8522. }
  8523. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8524. {
  8525. AssertIntEQ(WOLFSSL_SUCCESS,
  8526. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8527. }
  8528. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8529. {
  8530. use_SNI_at_ssl(ssl);
  8531. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8532. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8533. }
  8534. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8535. {
  8536. use_SNI_at_ssl(ssl);
  8537. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8538. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8539. }
  8540. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8541. {
  8542. use_SNI_at_ctx(ctx);
  8543. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8544. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8545. }
  8546. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8547. {
  8548. use_SNI_at_ssl(ssl);
  8549. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8550. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8551. }
  8552. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8553. {
  8554. use_SNI_at_ctx(ctx);
  8555. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8556. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8557. }
  8558. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8559. {
  8560. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8561. }
  8562. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8563. {
  8564. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8565. }
  8566. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8567. {
  8568. byte type = WOLFSSL_SNI_HOST_NAME;
  8569. void* request = (void*) &type; /* to be overwritten */
  8570. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8571. AssertNotNull(request);
  8572. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8573. AssertNull(request);
  8574. }
  8575. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8576. {
  8577. byte type = WOLFSSL_SNI_HOST_NAME;
  8578. void* request = NULL;
  8579. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8580. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8581. AssertNotNull(request);
  8582. AssertStrEQ("www.wolfssl.com", (char*)request);
  8583. }
  8584. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8585. {
  8586. byte type = WOLFSSL_SNI_HOST_NAME;
  8587. void* request = NULL;
  8588. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8589. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8590. AssertNotNull(request);
  8591. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8592. }
  8593. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8594. {
  8595. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8596. }
  8597. /* END of connection tests callbacks */
  8598. static int test_wolfSSL_UseSNI_connection(void)
  8599. {
  8600. int res = TEST_SKIPPED;
  8601. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8602. callback_functions client_cb;
  8603. callback_functions server_cb;
  8604. size_t i;
  8605. #ifdef WOLFSSL_STATIC_MEMORY
  8606. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8607. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8608. #endif
  8609. struct {
  8610. method_provider client_meth;
  8611. method_provider server_meth;
  8612. #ifdef WOLFSSL_STATIC_MEMORY
  8613. wolfSSL_method_func client_meth_ex;
  8614. wolfSSL_method_func server_meth_ex;
  8615. #endif
  8616. } methods[] = {
  8617. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8618. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8619. #ifdef WOLFSSL_STATIC_MEMORY
  8620. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8621. #endif
  8622. },
  8623. #endif
  8624. #ifndef WOLFSSL_NO_TLS12
  8625. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8626. #ifdef WOLFSSL_STATIC_MEMORY
  8627. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8628. #endif
  8629. },
  8630. #endif
  8631. #ifdef WOLFSSL_TLS13
  8632. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8633. #ifdef WOLFSSL_STATIC_MEMORY
  8634. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8635. #endif
  8636. },
  8637. #endif
  8638. };
  8639. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8640. for (i = 0; i < methodsSz; i++) {
  8641. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8642. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8643. client_cb.method = methods[i].client_meth;
  8644. server_cb.method = methods[i].server_meth;
  8645. client_cb.devId = testDevId;
  8646. server_cb.devId = testDevId;
  8647. #ifdef WOLFSSL_STATIC_MEMORY
  8648. client_cb.method_ex = methods[i].client_meth_ex;
  8649. server_cb.method_ex = methods[i].server_meth_ex;
  8650. client_cb.mem = cliMem;
  8651. client_cb.memSz = (word32)sizeof(cliMem);
  8652. server_cb.mem = svrMem;
  8653. server_cb.memSz = (word32)sizeof(svrMem);;
  8654. #endif
  8655. /* success case at ctx */
  8656. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8657. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8658. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8659. test_wolfSSL_client_server(&client_cb, &server_cb);
  8660. /* success case at ssl */
  8661. fprintf(stderr, "\tsuccess case at ssl\n");
  8662. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8663. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8664. test_wolfSSL_client_server(&client_cb, &server_cb);
  8665. /* default mismatch behavior */
  8666. fprintf(stderr, "\tdefault mismatch behavior\n");
  8667. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8668. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8669. test_wolfSSL_client_server(&client_cb, &server_cb);
  8670. /* continue on mismatch */
  8671. fprintf(stderr, "\tcontinue on mismatch\n");
  8672. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8673. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8674. test_wolfSSL_client_server(&client_cb, &server_cb);
  8675. /* fake answer on mismatch */
  8676. fprintf(stderr, "\tfake answer on mismatch\n");
  8677. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8678. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8679. test_wolfSSL_client_server(&client_cb, &server_cb);
  8680. /* sni abort - success */
  8681. fprintf(stderr, "\tsni abort - success\n");
  8682. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8683. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8684. test_wolfSSL_client_server(&client_cb, &server_cb);
  8685. /* sni abort - abort when absent (ctx) */
  8686. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8687. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8688. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8689. test_wolfSSL_client_server(&client_cb, &server_cb);
  8690. /* sni abort - abort when absent (ssl) */
  8691. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8692. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8693. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8694. test_wolfSSL_client_server(&client_cb, &server_cb);
  8695. /* sni abort - success when overwritten */
  8696. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8697. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8698. 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;
  8699. test_wolfSSL_client_server(&client_cb, &server_cb);
  8700. /* sni abort - success when allowing mismatches */
  8701. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8702. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8703. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8704. test_wolfSSL_client_server(&client_cb, &server_cb);
  8705. }
  8706. res = TEST_RES_CHECK(1);
  8707. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8708. return res;
  8709. }
  8710. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8711. {
  8712. EXPECT_DECLS;
  8713. byte buff[] = { /* www.paypal.com */
  8714. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8715. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8716. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8717. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8718. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8719. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8720. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8721. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8722. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8723. };
  8724. byte buff2[] = { /* api.textmate.org */
  8725. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8726. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8727. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8728. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8729. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8730. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8731. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8732. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8733. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8734. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8735. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8736. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8737. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8738. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8739. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8740. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8741. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8742. };
  8743. byte buff3[] = { /* no sni extension */
  8744. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8745. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8746. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8747. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8748. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8749. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8750. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8751. };
  8752. byte buff4[] = { /* last extension has zero size */
  8753. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8754. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8755. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8756. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8757. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8758. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8759. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8760. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8761. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8762. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8763. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8764. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8765. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8766. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8767. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8768. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8769. 0x12, 0x00, 0x00
  8770. };
  8771. byte buff5[] = { /* SSL v2.0 client hello */
  8772. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8773. /* dummy bytes below, just to pass size check */
  8774. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8775. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8776. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8777. };
  8778. byte result[32] = {0};
  8779. word32 length = 32;
  8780. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8781. 0, result, &length));
  8782. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8783. 0, result, &length));
  8784. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8785. 1, result, &length));
  8786. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8787. 0, result, &length));
  8788. buff[0] = 0x16;
  8789. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8790. 0, result, &length));
  8791. buff[1] = 0x03;
  8792. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8793. sizeof(buff), 0, result, &length));
  8794. buff[2] = 0x03;
  8795. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8796. sizeof(buff), 0, result, &length));
  8797. buff[4] = 0x64;
  8798. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8799. 0, result, &length));
  8800. if (EXPECT_SUCCESS())
  8801. result[length] = 0;
  8802. ExpectStrEQ("www.paypal.com", (const char*) result);
  8803. length = 32;
  8804. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8805. 0, result, &length));
  8806. if (EXPECT_SUCCESS())
  8807. result[length] = 0;
  8808. ExpectStrEQ("api.textmate.org", (const char*) result);
  8809. /* SSL v2.0 tests */
  8810. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8811. sizeof(buff5), 0, result, &length));
  8812. buff5[2] = 0x02;
  8813. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8814. sizeof(buff5), 0, result, &length));
  8815. buff5[2] = 0x01; buff5[6] = 0x08;
  8816. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8817. sizeof(buff5), 0, result, &length));
  8818. buff5[6] = 0x09; buff5[8] = 0x01;
  8819. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8820. sizeof(buff5), 0, result, &length));
  8821. return EXPECT_RESULT();
  8822. }
  8823. #endif /* HAVE_SNI */
  8824. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8825. static int test_wolfSSL_UseTrustedCA(void)
  8826. {
  8827. EXPECT_DECLS;
  8828. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8829. && !defined(NO_RSA)
  8830. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8831. WOLFSSL_CTX *ctx = NULL;
  8832. WOLFSSL *ssl = NULL;
  8833. byte id[20];
  8834. #ifndef NO_WOLFSSL_SERVER
  8835. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8836. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8837. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8838. #else
  8839. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8840. #endif
  8841. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8842. XMEMSET(id, 0, sizeof(id));
  8843. /* error cases */
  8844. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8845. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8846. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8847. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8848. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8849. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8850. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8851. #ifdef NO_SHA
  8852. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8853. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8854. #endif
  8855. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8856. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8857. /* success cases */
  8858. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8859. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8860. #ifndef NO_SHA
  8861. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8862. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8863. #endif
  8864. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8865. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8866. wolfSSL_free(ssl);
  8867. wolfSSL_CTX_free(ctx);
  8868. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8869. #endif /* HAVE_TRUSTED_CA */
  8870. return EXPECT_RESULT();
  8871. }
  8872. static int test_wolfSSL_UseMaxFragment(void)
  8873. {
  8874. EXPECT_DECLS;
  8875. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8876. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8877. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8878. #ifndef NO_WOLFSSL_SERVER
  8879. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8880. #else
  8881. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8882. #endif
  8883. WOLFSSL *ssl = NULL;
  8884. #ifdef OPENSSL_EXTRA
  8885. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8886. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8887. #else
  8888. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8889. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8890. #endif
  8891. #ifndef NO_WOLFSSL_SERVER
  8892. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8893. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8894. #endif
  8895. ExpectNotNull(ctx);
  8896. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8897. #ifdef OPENSSL_EXTRA
  8898. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8899. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8900. #else
  8901. UseMaxFragment = wolfSSL_UseMaxFragment;
  8902. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8903. #endif
  8904. /* error cases */
  8905. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8906. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8907. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8908. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8909. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8910. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8911. /* success case */
  8912. #ifdef OPENSSL_EXTRA
  8913. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8914. #else
  8915. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8916. #endif
  8917. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8918. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8919. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8920. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8921. #ifdef OPENSSL_EXTRA
  8922. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8923. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8924. #else
  8925. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8926. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8927. #endif
  8928. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8929. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8930. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8931. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8932. #ifdef OPENSSL_EXTRA
  8933. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8934. #else
  8935. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8936. #endif
  8937. wolfSSL_free(ssl);
  8938. wolfSSL_CTX_free(ctx);
  8939. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8940. #endif
  8941. return EXPECT_RESULT();
  8942. }
  8943. static int test_wolfSSL_UseTruncatedHMAC(void)
  8944. {
  8945. EXPECT_DECLS;
  8946. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8947. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8948. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8949. #ifndef NO_WOLFSSL_SERVER
  8950. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8951. #else
  8952. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8953. #endif
  8954. WOLFSSL *ssl = NULL;
  8955. ExpectNotNull(ctx);
  8956. #ifndef NO_WOLFSSL_SERVER
  8957. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8958. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8959. #endif
  8960. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8961. /* error cases */
  8962. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8963. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8964. /* success case */
  8965. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8966. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8967. wolfSSL_free(ssl);
  8968. wolfSSL_CTX_free(ctx);
  8969. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8970. #endif
  8971. return EXPECT_RESULT();
  8972. }
  8973. static int test_wolfSSL_UseSupportedCurve(void)
  8974. {
  8975. EXPECT_DECLS;
  8976. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8977. !defined(NO_TLS)
  8978. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8979. WOLFSSL *ssl = wolfSSL_new(ctx);
  8980. ExpectNotNull(ctx);
  8981. ExpectNotNull(ssl);
  8982. /* error cases */
  8983. ExpectIntNE(WOLFSSL_SUCCESS,
  8984. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8985. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8986. ExpectIntNE(WOLFSSL_SUCCESS,
  8987. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8988. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8989. /* success case */
  8990. ExpectIntEQ(WOLFSSL_SUCCESS,
  8991. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8992. ExpectIntEQ(WOLFSSL_SUCCESS,
  8993. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8994. wolfSSL_free(ssl);
  8995. wolfSSL_CTX_free(ctx);
  8996. #endif
  8997. return EXPECT_RESULT();
  8998. }
  8999. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9000. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9001. {
  9002. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9003. }
  9004. static void use_ALPN_all(WOLFSSL* ssl)
  9005. {
  9006. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9007. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9008. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9009. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9010. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9011. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9012. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9013. }
  9014. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9015. {
  9016. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9017. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9018. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9019. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9020. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9021. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9022. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9023. }
  9024. static void use_ALPN_one(WOLFSSL* ssl)
  9025. {
  9026. /* spdy/2 */
  9027. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9028. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9029. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9030. }
  9031. static void use_ALPN_unknown(WOLFSSL* ssl)
  9032. {
  9033. /* http/2.0 */
  9034. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9035. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9036. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9037. }
  9038. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9039. {
  9040. /* http/2.0 */
  9041. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9042. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9043. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9044. }
  9045. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9046. {
  9047. /* spdy/3 */
  9048. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9049. char *proto = NULL;
  9050. word16 protoSz = 0;
  9051. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9052. /* check value */
  9053. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9054. if (proto) {
  9055. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9056. }
  9057. }
  9058. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9059. {
  9060. char *proto = NULL;
  9061. word16 protoSz = 0;
  9062. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9063. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9064. /* check value */
  9065. AssertIntEQ(1, (0 == protoSz));
  9066. AssertIntEQ(1, (NULL == proto));
  9067. }
  9068. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9069. {
  9070. /* http/1.1 */
  9071. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9072. char *proto;
  9073. word16 protoSz = 0;
  9074. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9075. /* check value */
  9076. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9077. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9078. }
  9079. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9080. {
  9081. /* spdy/2 */
  9082. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9083. char *proto;
  9084. word16 protoSz = 0;
  9085. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9086. /* check value */
  9087. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9088. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9089. }
  9090. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9091. {
  9092. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9093. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9094. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9095. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9096. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9097. char *clist = NULL;
  9098. word16 clistSz = 0;
  9099. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9100. &clistSz));
  9101. /* check value */
  9102. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9103. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9104. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9105. }
  9106. static int test_wolfSSL_UseALPN_connection(void)
  9107. {
  9108. int res = TEST_SKIPPED;
  9109. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9110. callback_functions client_cb;
  9111. callback_functions server_cb;
  9112. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9113. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9114. client_cb.method = wolfSSLv23_client_method;
  9115. server_cb.method = wolfSSLv23_server_method;
  9116. client_cb.devId = testDevId;
  9117. server_cb.devId = testDevId;
  9118. /* success case same list */
  9119. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9120. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9121. test_wolfSSL_client_server(&client_cb, &server_cb);
  9122. /* success case only one for server */
  9123. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9124. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9125. test_wolfSSL_client_server(&client_cb, &server_cb);
  9126. /* success case only one for client */
  9127. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9128. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9129. test_wolfSSL_client_server(&client_cb, &server_cb);
  9130. /* success case none for client */
  9131. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9132. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9133. test_wolfSSL_client_server(&client_cb, &server_cb);
  9134. /* success case mismatch behavior but option 'continue' set */
  9135. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9136. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9137. test_wolfSSL_client_server(&client_cb, &server_cb);
  9138. /* success case read protocol send by client */
  9139. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9140. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9141. test_wolfSSL_client_server(&client_cb, &server_cb);
  9142. /* mismatch behavior with same list
  9143. * the first and only this one must be taken */
  9144. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9145. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9146. test_wolfSSL_client_server(&client_cb, &server_cb);
  9147. /* default mismatch behavior */
  9148. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9149. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9150. test_wolfSSL_client_server(&client_cb, &server_cb);
  9151. res = TEST_RES_CHECK(1);
  9152. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9153. return res;
  9154. }
  9155. static int test_wolfSSL_UseALPN_params(void)
  9156. {
  9157. EXPECT_DECLS;
  9158. #ifndef NO_WOLFSSL_CLIENT
  9159. /* "http/1.1" */
  9160. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9161. /* "spdy/1" */
  9162. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9163. /* "spdy/2" */
  9164. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9165. /* "spdy/3" */
  9166. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9167. char buff[256];
  9168. word32 idx;
  9169. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9170. WOLFSSL *ssl = wolfSSL_new(ctx);
  9171. ExpectNotNull(ctx);
  9172. ExpectNotNull(ssl);
  9173. /* error cases */
  9174. ExpectIntNE(WOLFSSL_SUCCESS,
  9175. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9176. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9177. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9178. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9179. /* success case */
  9180. /* http1 only */
  9181. ExpectIntEQ(WOLFSSL_SUCCESS,
  9182. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9183. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9184. /* http1, spdy1 */
  9185. XMEMCPY(buff, http1, sizeof(http1));
  9186. idx = sizeof(http1);
  9187. buff[idx++] = ',';
  9188. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9189. idx += sizeof(spdy1);
  9190. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9191. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9192. /* http1, spdy2, spdy1 */
  9193. XMEMCPY(buff, http1, sizeof(http1));
  9194. idx = sizeof(http1);
  9195. buff[idx++] = ',';
  9196. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9197. idx += sizeof(spdy2);
  9198. buff[idx++] = ',';
  9199. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9200. idx += sizeof(spdy1);
  9201. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9202. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9203. /* spdy3, http1, spdy2, spdy1 */
  9204. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9205. idx = sizeof(spdy3);
  9206. buff[idx++] = ',';
  9207. XMEMCPY(buff+idx, http1, sizeof(http1));
  9208. idx += sizeof(http1);
  9209. buff[idx++] = ',';
  9210. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9211. idx += sizeof(spdy2);
  9212. buff[idx++] = ',';
  9213. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9214. idx += sizeof(spdy1);
  9215. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9216. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9217. wolfSSL_free(ssl);
  9218. wolfSSL_CTX_free(ctx);
  9219. #endif
  9220. return EXPECT_RESULT();
  9221. }
  9222. #endif /* HAVE_ALPN */
  9223. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9224. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9225. {
  9226. unsigned char p[] = {
  9227. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9228. 6, 's', 'p', 'd', 'y', '/', '2',
  9229. 6, 's', 'p', 'd', 'y', '/', '1',
  9230. };
  9231. unsigned char p_len = sizeof(p);
  9232. int ret;
  9233. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9234. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9235. AssertIntEQ(ret, 0);
  9236. #else
  9237. AssertIntEQ(ret, SSL_SUCCESS);
  9238. #endif
  9239. }
  9240. static void set_alpn_protos(SSL* ssl)
  9241. {
  9242. unsigned char p[] = {
  9243. 6, 's', 'p', 'd', 'y', '/', '3',
  9244. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9245. 6, 's', 'p', 'd', 'y', '/', '2',
  9246. 6, 's', 'p', 'd', 'y', '/', '1',
  9247. };
  9248. unsigned char p_len = sizeof(p);
  9249. int ret;
  9250. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9251. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9252. AssertIntEQ(ret, 0);
  9253. #else
  9254. AssertIntEQ(ret, SSL_SUCCESS);
  9255. #endif
  9256. }
  9257. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9258. {
  9259. /* "spdy/3" */
  9260. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9261. const unsigned char *proto;
  9262. unsigned int protoSz = 0;
  9263. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9264. /* check value */
  9265. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9266. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9267. }
  9268. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9269. {
  9270. /* "http/1.1" */
  9271. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9272. const unsigned char *proto;
  9273. unsigned int protoSz = 0;
  9274. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9275. /* check value */
  9276. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9277. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9278. }
  9279. static int test_wolfSSL_set_alpn_protos(void)
  9280. {
  9281. int res = TEST_SKIPPED;
  9282. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9283. callback_functions client_cb;
  9284. callback_functions server_cb;
  9285. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9286. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9287. client_cb.method = wolfSSLv23_client_method;
  9288. server_cb.method = wolfSSLv23_server_method;
  9289. client_cb.devId = testDevId;
  9290. server_cb.devId = testDevId;
  9291. /* use CTX_alpn_protos */
  9292. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9293. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9294. test_wolfSSL_client_server(&client_cb, &server_cb);
  9295. /* use set_alpn_protos */
  9296. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9297. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9298. test_wolfSSL_client_server(&client_cb, &server_cb);
  9299. res = TEST_SUCCESS;
  9300. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9301. return res;
  9302. }
  9303. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9304. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9305. {
  9306. EXPECT_DECLS;
  9307. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9308. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9309. WOLFSSL *ssl = wolfSSL_new(ctx);
  9310. ExpectNotNull(ctx);
  9311. ExpectNotNull(ssl);
  9312. /* error cases */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9315. /* success cases */
  9316. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9317. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9318. wolfSSL_free(ssl);
  9319. wolfSSL_CTX_free(ctx);
  9320. #endif
  9321. return EXPECT_RESULT();
  9322. }
  9323. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9324. {
  9325. EXPECT_DECLS;
  9326. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9327. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9328. WOLFSSL *ssl = wolfSSL_new(ctx);
  9329. ExpectNotNull(ctx);
  9330. ExpectNotNull(ssl);
  9331. /* error cases */
  9332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9333. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9334. /* success cases */
  9335. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9336. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9337. wolfSSL_free(ssl);
  9338. wolfSSL_CTX_free(ctx);
  9339. #endif
  9340. return EXPECT_RESULT();
  9341. }
  9342. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9343. static int test_wolfSSL_SCR_Reconnect(void)
  9344. {
  9345. EXPECT_DECLS;
  9346. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9347. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9348. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9349. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9350. struct test_memio_ctx test_ctx;
  9351. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9352. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9353. byte data;
  9354. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9355. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9356. test_ctx.s_ciphers =
  9357. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9358. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9359. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9360. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9361. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9362. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9363. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9364. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9365. /* WOLFSSL_FATAL_ERROR since it will block */
  9366. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9367. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9368. WOLFSSL_ERROR_WANT_READ);
  9369. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9370. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9371. WOLFSSL_ERROR_WANT_READ);
  9372. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9373. wolfSSL_free(ssl_c);
  9374. ssl_c = NULL;
  9375. wolfSSL_free(ssl_s);
  9376. ssl_s = NULL;
  9377. wolfSSL_CTX_free(ctx_c);
  9378. ctx_c = NULL;
  9379. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9380. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9381. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9382. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9383. wolfSSL_free(ssl_s);
  9384. wolfSSL_free(ssl_c);
  9385. wolfSSL_CTX_free(ctx_s);
  9386. wolfSSL_CTX_free(ctx_c);
  9387. #endif
  9388. return EXPECT_RESULT();
  9389. }
  9390. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9391. (!defined(NO_RSA) || defined(HAVE_ECC))
  9392. /* Called when writing. */
  9393. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9394. {
  9395. (void)ssl;
  9396. (void)buf;
  9397. (void)sz;
  9398. (void)ctx;
  9399. /* Force error return from wolfSSL_accept_TLSv13(). */
  9400. return WANT_WRITE;
  9401. }
  9402. /* Called when reading. */
  9403. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9404. {
  9405. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9406. int len = (int)msg->length;
  9407. (void)ssl;
  9408. (void)sz;
  9409. /* Pass back as much of message as will fit in buffer. */
  9410. if (len > sz)
  9411. len = sz;
  9412. XMEMCPY(buf, msg->buffer, len);
  9413. /* Move over returned data. */
  9414. msg->buffer += len;
  9415. msg->length -= len;
  9416. /* Amount actually copied. */
  9417. return len;
  9418. }
  9419. #endif
  9420. /* Test the detection of duplicate known TLS extensions.
  9421. * Specifically in a ClientHello.
  9422. */
  9423. static int test_tls_ext_duplicate(void)
  9424. {
  9425. EXPECT_DECLS;
  9426. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9427. !defined(NO_FILESYSTEM)
  9428. const unsigned char clientHelloDupTlsExt[] = {
  9429. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9430. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9431. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9432. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9433. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9434. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9435. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9436. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9437. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9438. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9439. 0x00, 0x9e, 0x01, 0x00,
  9440. /* Extensions - duplicate signature algorithms. */
  9441. 0x00, 0x19, 0x00, 0x0d,
  9442. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9443. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9444. /* Supported Versions extension for TLS 1.3. */
  9445. 0x00, 0x2b,
  9446. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9447. };
  9448. WOLFSSL_BUFFER_INFO msg;
  9449. const char* testCertFile;
  9450. const char* testKeyFile;
  9451. WOLFSSL_CTX *ctx = NULL;
  9452. WOLFSSL *ssl = NULL;
  9453. #ifndef NO_RSA
  9454. testCertFile = svrCertFile;
  9455. testKeyFile = svrKeyFile;
  9456. #elif defined(HAVE_ECC)
  9457. testCertFile = eccCertFile;
  9458. testKeyFile = eccKeyFile;
  9459. #endif
  9460. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9461. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9462. WOLFSSL_FILETYPE_PEM));
  9463. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9464. WOLFSSL_FILETYPE_PEM));
  9465. /* Read from 'msg'. */
  9466. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9467. /* No where to send to - dummy sender. */
  9468. wolfSSL_SetIOSend(ctx, DummySend);
  9469. ssl = wolfSSL_new(ctx);
  9470. ExpectNotNull(ssl);
  9471. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9472. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9473. wolfSSL_SetIOReadCtx(ssl, &msg);
  9474. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9475. /* can return duplicate ext error or socket error if the peer closed down
  9476. * while sending alert */
  9477. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9478. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9479. }
  9480. wolfSSL_free(ssl);
  9481. wolfSSL_CTX_free(ctx);
  9482. #endif
  9483. return EXPECT_RESULT();
  9484. }
  9485. /*----------------------------------------------------------------------------*
  9486. | X509 Tests
  9487. *----------------------------------------------------------------------------*/
  9488. static int test_wolfSSL_X509_NAME_get_entry(void)
  9489. {
  9490. EXPECT_DECLS;
  9491. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9492. #if defined(OPENSSL_ALL) || \
  9493. (defined(OPENSSL_EXTRA) && \
  9494. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9495. /* use openssl like name to test mapping */
  9496. X509_NAME_ENTRY* ne;
  9497. X509_NAME* name;
  9498. X509* x509 = NULL;
  9499. #ifndef NO_FILESYSTEM
  9500. ASN1_STRING* asn;
  9501. char* subCN = NULL;
  9502. #endif
  9503. int idx;
  9504. ASN1_OBJECT *object = NULL;
  9505. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9506. defined(WOLFSSL_NGINX)
  9507. #ifndef NO_BIO
  9508. BIO* bio = NULL;
  9509. #endif
  9510. #endif
  9511. #ifndef NO_FILESYSTEM
  9512. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9513. WOLFSSL_FILETYPE_PEM));
  9514. ExpectNotNull(name = X509_get_subject_name(x509));
  9515. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9516. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9517. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9518. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9519. wolfSSL_FreeX509(x509);
  9520. x509 = NULL;
  9521. #endif
  9522. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9523. WOLFSSL_FILETYPE_PEM));
  9524. ExpectNotNull(name = X509_get_subject_name(x509));
  9525. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9526. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9527. defined(WOLFSSL_NGINX)
  9528. #ifndef NO_BIO
  9529. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9530. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9531. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9532. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9533. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9534. BIO_free(bio);
  9535. #endif
  9536. #endif
  9537. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9538. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9539. wolfSSL_FreeX509(x509);
  9540. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9541. #endif /* !NO_CERTS && !NO_RSA */
  9542. return EXPECT_RESULT();
  9543. }
  9544. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9545. static int test_wolfSSL_PKCS12(void)
  9546. {
  9547. EXPECT_DECLS;
  9548. /* .p12 file is encrypted with DES3 */
  9549. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9550. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9551. * Password Key
  9552. */
  9553. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9554. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9555. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9556. byte buf[6000];
  9557. char file[] = "./certs/test-servercert.p12";
  9558. char order[] = "./certs/ecc-rsa-server.p12";
  9559. #ifdef WC_RC2
  9560. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9561. #endif
  9562. char pass[] = "a password";
  9563. const char goodPsw[] = "wolfSSL test";
  9564. const char badPsw[] = "bad";
  9565. #ifdef HAVE_ECC
  9566. WOLFSSL_X509_NAME *subject = NULL;
  9567. WOLFSSL_X509 *x509 = NULL;
  9568. #endif
  9569. XFILE f = XBADFILE;
  9570. int bytes, ret, goodPswLen, badPswLen;
  9571. WOLFSSL_BIO *bio = NULL;
  9572. WOLFSSL_EVP_PKEY *pkey = NULL;
  9573. WC_PKCS12 *pkcs12 = NULL;
  9574. WC_PKCS12 *pkcs12_2 = NULL;
  9575. WOLFSSL_X509 *cert = NULL;
  9576. WOLFSSL_X509 *tmp = NULL;
  9577. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9578. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9579. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9580. WOLFSSL_CTX *ctx = NULL;
  9581. WOLFSSL *ssl = NULL;
  9582. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9583. #endif
  9584. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9585. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9586. if (f != XBADFILE) {
  9587. XFCLOSE(f);
  9588. f = XBADFILE;
  9589. }
  9590. goodPswLen = (int)XSTRLEN(goodPsw);
  9591. badPswLen = (int)XSTRLEN(badPsw);
  9592. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9593. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9594. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9595. ExpectNotNull(pkcs12);
  9596. BIO_free(bio);
  9597. bio = NULL;
  9598. /* check verify MAC directly */
  9599. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9600. /* check verify MAC fail case directly */
  9601. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9602. /* check verify MAC fail case */
  9603. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9604. ExpectNull(pkey);
  9605. ExpectNull(cert);
  9606. /* check parse with no extra certs kept */
  9607. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9608. 1);
  9609. ExpectNotNull(pkey);
  9610. ExpectNotNull(cert);
  9611. wolfSSL_EVP_PKEY_free(pkey);
  9612. pkey = NULL;
  9613. wolfSSL_X509_free(cert);
  9614. cert = NULL;
  9615. /* check parse with extra certs kept */
  9616. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9617. 1);
  9618. ExpectNotNull(pkey);
  9619. ExpectNotNull(cert);
  9620. ExpectNotNull(ca);
  9621. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9622. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9623. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9624. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9625. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9626. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9627. #else
  9628. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9629. #endif
  9630. /* Copy stack structure */
  9631. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9632. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9633. /* CTX now owns the tmp_ca stack structure */
  9634. tmp_ca = NULL;
  9635. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9636. ExpectNotNull(tmp_ca);
  9637. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9638. /* Check that the main cert is also set */
  9639. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9640. ExpectNotNull(ssl = SSL_new(ctx));
  9641. ExpectNotNull(SSL_get_certificate(ssl));
  9642. SSL_free(ssl);
  9643. SSL_CTX_free(ctx);
  9644. ctx = NULL;
  9645. #endif
  9646. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9647. /* should be 2 other certs on stack */
  9648. ExpectNotNull(tmp = sk_X509_pop(ca));
  9649. X509_free(tmp);
  9650. ExpectNotNull(tmp = sk_X509_pop(ca));
  9651. X509_free(tmp);
  9652. ExpectNull(sk_X509_pop(ca));
  9653. EVP_PKEY_free(pkey);
  9654. pkey = NULL;
  9655. X509_free(cert);
  9656. cert = NULL;
  9657. sk_X509_pop_free(ca, X509_free);
  9658. ca = NULL;
  9659. /* check PKCS12_create */
  9660. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9661. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9662. SSL_SUCCESS);
  9663. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9664. -1, -1, 100, -1, 0)));
  9665. EVP_PKEY_free(pkey);
  9666. pkey = NULL;
  9667. X509_free(cert);
  9668. cert = NULL;
  9669. sk_X509_pop_free(ca, NULL);
  9670. ca = NULL;
  9671. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9672. SSL_SUCCESS);
  9673. PKCS12_free(pkcs12_2);
  9674. pkcs12_2 = NULL;
  9675. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9676. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9677. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9678. 2000, 1, 0)));
  9679. EVP_PKEY_free(pkey);
  9680. pkey = NULL;
  9681. X509_free(cert);
  9682. cert = NULL;
  9683. sk_X509_pop_free(ca, NULL);
  9684. ca = NULL;
  9685. /* convert to DER then back and parse */
  9686. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9687. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9688. PKCS12_free(pkcs12_2);
  9689. pkcs12_2 = NULL;
  9690. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9691. BIO_free(bio);
  9692. bio = NULL;
  9693. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9694. SSL_SUCCESS);
  9695. /* should be 2 other certs on stack */
  9696. ExpectNotNull(tmp = sk_X509_pop(ca));
  9697. X509_free(tmp);
  9698. ExpectNotNull(tmp = sk_X509_pop(ca));
  9699. X509_free(tmp);
  9700. ExpectNull(sk_X509_pop(ca));
  9701. #ifndef NO_RC4
  9702. PKCS12_free(pkcs12_2);
  9703. pkcs12_2 = NULL;
  9704. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9705. NID_pbe_WithSHA1And128BitRC4,
  9706. NID_pbe_WithSHA1And128BitRC4,
  9707. 2000, 1, 0)));
  9708. EVP_PKEY_free(pkey);
  9709. pkey = NULL;
  9710. X509_free(cert);
  9711. cert = NULL;
  9712. sk_X509_pop_free(ca, NULL);
  9713. ca = NULL;
  9714. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9715. SSL_SUCCESS);
  9716. #endif /* NO_RC4 */
  9717. EVP_PKEY_free(pkey);
  9718. pkey = NULL;
  9719. X509_free(cert);
  9720. cert = NULL;
  9721. PKCS12_free(pkcs12);
  9722. pkcs12 = NULL;
  9723. PKCS12_free(pkcs12_2);
  9724. pkcs12_2 = NULL;
  9725. sk_X509_pop_free(ca, NULL);
  9726. ca = NULL;
  9727. #ifdef HAVE_ECC
  9728. /* test order of parsing */
  9729. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9730. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9731. if (f != XBADFILE) {
  9732. XFCLOSE(f);
  9733. f = XBADFILE;
  9734. }
  9735. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9736. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9737. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9738. WOLFSSL_SUCCESS);
  9739. /* check use of pkey after parse */
  9740. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9741. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9742. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9743. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9744. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9745. #else
  9746. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9747. #endif
  9748. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9749. SSL_CTX_free(ctx);
  9750. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9751. #endif
  9752. ExpectNotNull(pkey);
  9753. ExpectNotNull(cert);
  9754. ExpectNotNull(ca);
  9755. /* compare subject lines of certificates */
  9756. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9757. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9758. SSL_FILETYPE_PEM));
  9759. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9760. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9761. X509_free(x509);
  9762. x509 = NULL;
  9763. /* test expected fail case */
  9764. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9765. SSL_FILETYPE_PEM));
  9766. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9767. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9768. X509_free(x509);
  9769. x509 = NULL;
  9770. X509_free(cert);
  9771. cert = NULL;
  9772. /* get subject line from ca stack */
  9773. ExpectNotNull(cert = sk_X509_pop(ca));
  9774. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9775. /* compare subject from certificate in ca to expected */
  9776. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9777. SSL_FILETYPE_PEM));
  9778. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9779. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9780. EVP_PKEY_free(pkey);
  9781. pkey = NULL;
  9782. X509_free(x509);
  9783. x509 = NULL;
  9784. X509_free(cert);
  9785. cert = NULL;
  9786. BIO_free(bio);
  9787. bio = NULL;
  9788. PKCS12_free(pkcs12);
  9789. pkcs12 = NULL;
  9790. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9791. ca = NULL;
  9792. /* test order of parsing */
  9793. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9794. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9795. if (f != XBADFILE) {
  9796. XFCLOSE(f);
  9797. f = XBADFILE;
  9798. }
  9799. /* check verify MAC fail case */
  9800. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9801. ExpectNull(pkey);
  9802. ExpectNull(cert);
  9803. /* check parse with no extra certs kept */
  9804. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9805. 1);
  9806. ExpectNotNull(pkey);
  9807. ExpectNotNull(cert);
  9808. wolfSSL_EVP_PKEY_free(pkey);
  9809. pkey = NULL;
  9810. wolfSSL_X509_free(cert);
  9811. cert = NULL;
  9812. /* check parse with extra certs kept */
  9813. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9814. 1);
  9815. ExpectNotNull(pkey);
  9816. ExpectNotNull(cert);
  9817. ExpectNotNull(ca);
  9818. wolfSSL_EVP_PKEY_free(pkey);
  9819. pkey = NULL;
  9820. wolfSSL_X509_free(cert);
  9821. cert = NULL;
  9822. sk_X509_pop_free(ca, NULL);
  9823. ca = NULL;
  9824. PKCS12_free(pkcs12);
  9825. pkcs12 = NULL;
  9826. #endif /* HAVE_ECC */
  9827. #ifdef WC_RC2
  9828. /* test PKCS#12 with RC2 encryption */
  9829. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9830. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9831. if (f != XBADFILE) {
  9832. XFCLOSE(f);
  9833. f = XBADFILE;
  9834. }
  9835. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9836. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9837. /* check verify MAC fail case */
  9838. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9839. ExpectNull(pkey);
  9840. ExpectNull(cert);
  9841. /* check parse with not extra certs kept */
  9842. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9843. WOLFSSL_SUCCESS);
  9844. ExpectNotNull(pkey);
  9845. ExpectNotNull(cert);
  9846. wolfSSL_EVP_PKEY_free(pkey);
  9847. pkey = NULL;
  9848. wolfSSL_X509_free(cert);
  9849. cert = NULL;
  9850. /* check parse with extra certs kept */
  9851. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9852. WOLFSSL_SUCCESS);
  9853. ExpectNotNull(pkey);
  9854. ExpectNotNull(cert);
  9855. ExpectNotNull(ca);
  9856. wolfSSL_EVP_PKEY_free(pkey);
  9857. wolfSSL_X509_free(cert);
  9858. sk_X509_pop_free(ca, NULL);
  9859. BIO_free(bio);
  9860. bio = NULL;
  9861. PKCS12_free(pkcs12);
  9862. pkcs12 = NULL;
  9863. #endif /* WC_RC2 */
  9864. /* Test i2d_PKCS12_bio */
  9865. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9866. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9867. if (f != XBADFILE)
  9868. XFCLOSE(f);
  9869. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9870. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9871. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9872. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9873. PKCS12_free(pkcs12);
  9874. BIO_free(bio);
  9875. (void)order;
  9876. #endif /* OPENSSL_EXTRA */
  9877. #endif /* HAVE_FIPS */
  9878. return EXPECT_RESULT();
  9879. }
  9880. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9881. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9882. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9883. #define TEST_PKCS8_ENC
  9884. #endif
  9885. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9886. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9887. /* used to keep track if FailTestCallback was called */
  9888. static int failTestCallbackCalled = 0;
  9889. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9890. {
  9891. (void)passwd;
  9892. (void)sz;
  9893. (void)rw;
  9894. (void)userdata;
  9895. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9896. failTestCallbackCalled = 1;
  9897. return -1;
  9898. }
  9899. #endif
  9900. static int test_wolfSSL_no_password_cb(void)
  9901. {
  9902. EXPECT_DECLS;
  9903. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9904. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9905. WOLFSSL_CTX* ctx = NULL;
  9906. byte buff[FOURK_BUF];
  9907. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9908. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9909. XFILE f = XBADFILE;
  9910. int bytes = 0;
  9911. #ifndef NO_WOLFSSL_CLIENT
  9912. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9913. #else
  9914. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9915. #endif
  9916. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9917. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9918. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9919. if (f != XBADFILE) {
  9920. XFCLOSE(f);
  9921. f = XBADFILE;
  9922. }
  9923. ExpectIntLE(bytes, sizeof(buff));
  9924. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9926. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9927. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9928. if (f != XBADFILE)
  9929. XFCLOSE(f);
  9930. ExpectIntLE(bytes, sizeof(buff));
  9931. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9932. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9933. wolfSSL_CTX_free(ctx);
  9934. /* Password callback should not be called by default */
  9935. ExpectIntEQ(failTestCallbackCalled, 0);
  9936. #endif
  9937. return EXPECT_RESULT();
  9938. }
  9939. #ifdef TEST_PKCS8_ENC
  9940. /* for PKCS8 test case */
  9941. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9942. {
  9943. int flag = 0;
  9944. (void)rw;
  9945. if (userdata != NULL) {
  9946. flag = *((int*)userdata); /* user set data */
  9947. }
  9948. switch (flag) {
  9949. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9950. * can be anything the user wishes to be passed to the callback
  9951. * associated with the WOLFSSL_CTX */
  9952. XSTRNCPY(passwd, "yassl123", sz);
  9953. return 8;
  9954. default:
  9955. return BAD_FUNC_ARG;
  9956. }
  9957. }
  9958. #endif /* TEST_PKCS8_ENC */
  9959. /* Testing functions dealing with PKCS8 */
  9960. static int test_wolfSSL_PKCS8(void)
  9961. {
  9962. EXPECT_DECLS;
  9963. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9964. !defined(WOLFCRYPT_ONLY)
  9965. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9966. byte buff[FOURK_BUF];
  9967. byte der[FOURK_BUF];
  9968. #ifndef NO_RSA
  9969. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9970. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9971. #endif
  9972. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9973. #ifdef HAVE_ECC
  9974. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9975. #endif
  9976. XFILE f = XBADFILE;
  9977. int bytes = 0;
  9978. WOLFSSL_CTX* ctx = NULL;
  9979. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9980. int ret;
  9981. ecc_key key;
  9982. word32 x = 0;
  9983. #endif
  9984. #ifdef TEST_PKCS8_ENC
  9985. #if !defined(NO_RSA) && !defined(NO_SHA)
  9986. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9987. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9988. #endif
  9989. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9990. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9991. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9992. #endif
  9993. int flag;
  9994. #endif
  9995. (void)der;
  9996. #ifndef NO_WOLFSSL_CLIENT
  9997. #ifndef WOLFSSL_NO_TLS12
  9998. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9999. #else
  10000. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10001. #endif
  10002. #else
  10003. #ifndef WOLFSSL_NO_TLS12
  10004. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10005. #else
  10006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10007. #endif
  10008. #endif
  10009. #ifdef TEST_PKCS8_ENC
  10010. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10011. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10012. flag = 1; /* used by password callback as return code */
  10013. #if !defined(NO_RSA) && !defined(NO_SHA)
  10014. /* test loading PEM PKCS8 encrypted file */
  10015. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10016. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10017. if (f != XBADFILE) {
  10018. XFCLOSE(f);
  10019. f = XBADFILE;
  10020. }
  10021. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10022. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10023. /* this next case should fail because of password callback return code */
  10024. flag = 0; /* used by password callback as return code */
  10025. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10026. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10027. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10028. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10029. "yassl123"), 0);
  10030. /* test that error value is returned with a bad password */
  10031. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10032. "bad"), 0);
  10033. /* test loading PEM PKCS8 encrypted file */
  10034. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10035. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10036. if (f != XBADFILE) {
  10037. XFCLOSE(f);
  10038. f = XBADFILE;
  10039. }
  10040. flag = 1; /* used by password callback as return code */
  10041. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10042. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10043. /* this next case should fail because of password callback return code */
  10044. flag = 0; /* used by password callback as return code */
  10045. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10046. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10047. #endif /* !NO_RSA && !NO_SHA */
  10048. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10049. /* test loading PEM PKCS8 encrypted ECC Key file */
  10050. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10051. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10052. if (f != XBADFILE) {
  10053. XFCLOSE(f);
  10054. f = XBADFILE;
  10055. }
  10056. flag = 1; /* used by password callback as return code */
  10057. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10058. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10059. /* this next case should fail because of password callback return code */
  10060. flag = 0; /* used by password callback as return code */
  10061. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10062. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10063. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10064. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10065. "yassl123"), 0);
  10066. /* test that error value is returned with a bad password */
  10067. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10068. "bad"), 0);
  10069. /* test loading DER PKCS8 encrypted ECC Key file */
  10070. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10071. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10072. if (f != XBADFILE) {
  10073. XFCLOSE(f);
  10074. f = XBADFILE;
  10075. }
  10076. flag = 1; /* used by password callback as return code */
  10077. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10078. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10079. /* this next case should fail because of password callback return code */
  10080. flag = 0; /* used by password callback as return code */
  10081. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10082. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10083. /* leave flag as "okay" */
  10084. flag = 1;
  10085. #endif /* HAVE_ECC && !NO_SHA */
  10086. #endif /* TEST_PKCS8_ENC */
  10087. #ifndef NO_RSA
  10088. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10089. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10090. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10091. if (f != XBADFILE) {
  10092. XFCLOSE(f);
  10093. f = XBADFILE;
  10094. }
  10095. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10096. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10097. /* test loading PEM PKCS8 private key file (not encrypted) */
  10098. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10099. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10100. if (f != XBADFILE) {
  10101. XFCLOSE(f);
  10102. f = XBADFILE;
  10103. }
  10104. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10105. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10106. #endif /* !NO_RSA */
  10107. /* Test PKCS8 PEM ECC key no crypt */
  10108. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10109. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10110. if (f != XBADFILE) {
  10111. XFCLOSE(f);
  10112. f = XBADFILE;
  10113. }
  10114. #ifdef HAVE_ECC
  10115. /* Test PKCS8 PEM ECC key no crypt */
  10116. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10117. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10118. #ifndef NO_CODING
  10119. /* decrypt PKCS8 PEM to key in DER format */
  10120. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10121. (word32)sizeof(der), NULL)), 0);
  10122. ret = wc_ecc_init(&key);
  10123. if (ret == 0) {
  10124. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10125. wc_ecc_free(&key);
  10126. }
  10127. ExpectIntEQ(ret, 0);
  10128. #endif
  10129. /* Test PKCS8 DER ECC key no crypt */
  10130. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10131. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10132. if (f != XBADFILE)
  10133. XFCLOSE(f);
  10134. /* Test using a PKCS8 ECC PEM */
  10135. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10136. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10137. #else
  10138. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10139. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10140. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10141. #endif /* HAVE_ECC */
  10142. wolfSSL_CTX_free(ctx);
  10143. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10144. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10145. return EXPECT_RESULT();
  10146. }
  10147. static int test_wolfSSL_PKCS8_ED25519(void)
  10148. {
  10149. EXPECT_DECLS;
  10150. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10151. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10152. defined(HAVE_ED25519_KEY_IMPORT)
  10153. const byte encPrivKey[] = \
  10154. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10155. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10156. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10157. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10158. "2L9W4v7swXkV+X+a1ww=\n"
  10159. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10160. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10161. byte der[FOURK_BUF];
  10162. WOLFSSL_CTX* ctx = NULL;
  10163. int bytes;
  10164. XMEMSET(der, 0, sizeof(der));
  10165. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10166. (word32)sizeof(der), password)), 0);
  10167. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10168. #ifndef NO_WOLFSSL_SERVER
  10169. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10170. #else
  10171. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10172. #endif
  10173. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10174. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10175. wolfSSL_CTX_free(ctx);
  10176. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10177. #endif
  10178. return EXPECT_RESULT();
  10179. }
  10180. static int test_wolfSSL_PKCS8_ED448(void)
  10181. {
  10182. EXPECT_DECLS;
  10183. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10184. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10185. defined(HAVE_ED448_KEY_IMPORT)
  10186. const byte encPrivKey[] = \
  10187. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10188. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10189. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10190. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10191. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10192. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10193. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10194. byte der[FOURK_BUF];
  10195. WOLFSSL_CTX* ctx = NULL;
  10196. int bytes;
  10197. XMEMSET(der, 0, sizeof(der));
  10198. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10199. (word32)sizeof(der), password)), 0);
  10200. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10201. #ifndef NO_WOLFSSL_SERVER
  10202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10203. #else
  10204. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10205. #endif
  10206. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10207. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10208. wolfSSL_CTX_free(ctx);
  10209. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10210. #endif
  10211. return EXPECT_RESULT();
  10212. }
  10213. /* Testing functions dealing with PKCS5 */
  10214. static int test_wolfSSL_PKCS5(void)
  10215. {
  10216. EXPECT_DECLS;
  10217. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10218. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10219. const char* passwd = "myfipsPa$$W0rd";
  10220. #else
  10221. const char *passwd = "pass1234";
  10222. #endif
  10223. const unsigned char *salt = (unsigned char *)"salt1234";
  10224. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10225. DYNAMIC_TYPE_TMP_BUFFER);
  10226. int ret = 0;
  10227. ExpectNotNull(out);
  10228. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10229. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10230. WOLFSSL_SUCCESS);
  10231. #ifdef WOLFSSL_SHA512
  10232. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10233. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10234. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10235. #endif
  10236. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10237. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10238. return EXPECT_RESULT();
  10239. }
  10240. /* test parsing URI from certificate */
  10241. static int test_wolfSSL_URI(void)
  10242. {
  10243. EXPECT_DECLS;
  10244. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10245. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10246. defined(OPENSSL_EXTRA))
  10247. WOLFSSL_X509* x509 = NULL;
  10248. const char uri[] = "./certs/client-uri-cert.pem";
  10249. const char urn[] = "./certs/client-absolute-urn.pem";
  10250. const char badUri[] = "./certs/client-relative-uri.pem";
  10251. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10252. WOLFSSL_FILETYPE_PEM));
  10253. wolfSSL_FreeX509(x509);
  10254. x509 = NULL;
  10255. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10256. WOLFSSL_FILETYPE_PEM));
  10257. wolfSSL_FreeX509(x509);
  10258. x509 = NULL;
  10259. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10260. && !defined(WOLFSSL_FPKI)
  10261. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10262. WOLFSSL_FILETYPE_PEM));
  10263. #else
  10264. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10265. WOLFSSL_FILETYPE_PEM));
  10266. #endif
  10267. wolfSSL_FreeX509(x509);
  10268. #endif
  10269. return EXPECT_RESULT();
  10270. }
  10271. static int test_wolfSSL_TBS(void)
  10272. {
  10273. EXPECT_DECLS;
  10274. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10275. && defined(OPENSSL_EXTRA)
  10276. WOLFSSL_X509* x509 = NULL;
  10277. const unsigned char* tbs;
  10278. int tbsSz;
  10279. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10280. WOLFSSL_FILETYPE_PEM));
  10281. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10282. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10283. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10284. ExpectIntEQ(tbsSz, 1003);
  10285. wolfSSL_FreeX509(x509);
  10286. #endif
  10287. return EXPECT_RESULT();
  10288. }
  10289. static int test_wolfSSL_X509_verify(void)
  10290. {
  10291. EXPECT_DECLS;
  10292. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10293. defined(OPENSSL_EXTRA)
  10294. WOLFSSL_X509* ca = NULL;
  10295. WOLFSSL_X509* serv = NULL;
  10296. WOLFSSL_EVP_PKEY* pkey = NULL;
  10297. unsigned char buf[2048];
  10298. const unsigned char* pt = NULL;
  10299. int bufSz;
  10300. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10301. WOLFSSL_FILETYPE_PEM));
  10302. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10303. WOLFSSL_SUCCESS);
  10304. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10305. WOLFSSL_SUCCESS);
  10306. ExpectIntEQ(bufSz, 294);
  10307. bufSz = 2048;
  10308. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10309. WOLFSSL_SUCCESS);
  10310. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10311. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10312. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10313. WOLFSSL_FILETYPE_PEM));
  10314. /* success case */
  10315. pt = buf;
  10316. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10317. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10318. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10319. wolfSSL_EVP_PKEY_free(pkey);
  10320. pkey = NULL;
  10321. /* fail case */
  10322. bufSz = 2048;
  10323. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10324. WOLFSSL_SUCCESS);
  10325. pt = buf;
  10326. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10327. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10328. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10329. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10330. wolfSSL_EVP_PKEY_free(pkey);
  10331. wolfSSL_FreeX509(ca);
  10332. wolfSSL_FreeX509(serv);
  10333. #endif
  10334. return EXPECT_RESULT();
  10335. }
  10336. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10337. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10338. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10339. /* create certificate with version 2 */
  10340. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10341. {
  10342. EXPECT_DECLS;
  10343. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10344. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10345. unsigned char *der = NULL, *key = NULL, *pt;
  10346. char *header = NULL, *name = NULL;
  10347. int derSz;
  10348. long keySz;
  10349. XFILE fp = XBADFILE;
  10350. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10351. time_t t;
  10352. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10353. WOLFSSL_FILETYPE_PEM));
  10354. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10355. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10356. WOLFSSL_SUCCESS);
  10357. if (fp != XBADFILE)
  10358. XFCLOSE(fp);
  10359. pt = key;
  10360. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10361. (const unsigned char**)&pt, keySz));
  10362. /* create the version 2 certificate */
  10363. ExpectNotNull(x509v2 = X509_new());
  10364. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10365. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10366. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10367. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10368. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10369. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10370. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10371. t = time(NULL);
  10372. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10373. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10374. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10375. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10376. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10377. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10378. ExpectIntGT(derSz, 0);
  10379. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10380. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10381. /* TODO: Replace with API call */
  10382. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10383. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10384. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10385. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10386. wolfSSL_X509_free(x509);
  10387. wolfSSL_X509_free(x509v2);
  10388. wolfSSL_EVP_PKEY_free(priv);
  10389. wolfSSL_EVP_PKEY_free(pub);
  10390. wolfSSL_ASN1_TIME_free(notBefore);
  10391. wolfSSL_ASN1_TIME_free(notAfter);
  10392. return EXPECT_RESULT();
  10393. }
  10394. /* override certificate version error */
  10395. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10396. {
  10397. EXPECT_DECLS;
  10398. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10399. ExpectIntEQ(store->error, ASN_VERSION_E);
  10400. #else
  10401. ExpectIntEQ(store->error, 0);
  10402. #endif
  10403. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10404. (void)preverify;
  10405. return EXPECT_RESULT() == TEST_SUCCESS;
  10406. }
  10407. /* set verify callback that will override bad certificate version */
  10408. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10409. {
  10410. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10411. return TEST_SUCCESS;
  10412. }
  10413. #endif
  10414. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10415. {
  10416. EXPECT_DECLS;
  10417. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10418. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10419. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10420. test_ssl_cbf func_cb_client;
  10421. test_ssl_cbf func_cb_server;
  10422. /* test server rejects a client certificate that is not version 3 */
  10423. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10424. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10425. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10426. #ifndef WOLFSSL_NO_TLS12
  10427. func_cb_client.method = wolfTLSv1_2_client_method;
  10428. #else
  10429. func_cb_client.method = wolfTLSv1_3_client_method;
  10430. #endif
  10431. #ifndef WOLFSSL_NO_TLS12
  10432. func_cb_server.method = wolfTLSv1_2_server_method;
  10433. #else
  10434. func_cb_server.method = wolfTLSv1_3_server_method;
  10435. #endif
  10436. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10437. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10438. &func_cb_server, NULL), TEST_FAIL);
  10439. #else
  10440. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10441. &func_cb_server, NULL), TEST_SUCCESS);
  10442. #endif
  10443. #endif
  10444. return EXPECT_RESULT();
  10445. }
  10446. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10447. {
  10448. EXPECT_DECLS;
  10449. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10450. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10451. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10452. test_ssl_cbf func_cb_client;
  10453. test_ssl_cbf func_cb_server;
  10454. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10455. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10456. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10457. func_cb_server.ctx_ready = &test_set_override_x509;
  10458. #ifndef WOLFSSL_NO_TLS12
  10459. func_cb_client.method = wolfTLSv1_2_client_method;
  10460. #else
  10461. func_cb_client.method = wolfTLSv1_3_client_method;
  10462. #endif
  10463. #ifndef WOLFSSL_NO_TLS12
  10464. func_cb_server.method = wolfTLSv1_2_server_method;
  10465. #else
  10466. func_cb_server.method = wolfTLSv1_3_server_method;
  10467. #endif
  10468. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10469. &func_cb_server, NULL), TEST_SUCCESS);
  10470. #endif
  10471. return EXPECT_RESULT();
  10472. }
  10473. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10474. * version allowed.
  10475. * POST: 1 on success.
  10476. */
  10477. static int test_wolfSSL_CTX_SetMinVersion(void)
  10478. {
  10479. int res = TEST_SKIPPED;
  10480. #ifndef NO_WOLFSSL_CLIENT
  10481. int failFlag = WOLFSSL_SUCCESS;
  10482. WOLFSSL_CTX* ctx;
  10483. int itr;
  10484. #ifndef NO_OLD_TLS
  10485. const int versions[] = {
  10486. #ifdef WOLFSSL_ALLOW_TLSV10
  10487. WOLFSSL_TLSV1,
  10488. #endif
  10489. WOLFSSL_TLSV1_1,
  10490. WOLFSSL_TLSV1_2 };
  10491. #elif !defined(WOLFSSL_NO_TLS12)
  10492. const int versions[] = { WOLFSSL_TLSV1_2 };
  10493. #elif defined(WOLFSSL_TLS13)
  10494. const int versions[] = { WOLFSSL_TLSV1_3 };
  10495. #else
  10496. const int versions[0];
  10497. #endif
  10498. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10499. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10500. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10501. != WOLFSSL_SUCCESS) {
  10502. failFlag = WOLFSSL_FAILURE;
  10503. }
  10504. }
  10505. wolfSSL_CTX_free(ctx);
  10506. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10507. #endif
  10508. return res;
  10509. } /* END test_wolfSSL_CTX_SetMinVersion */
  10510. /*----------------------------------------------------------------------------*
  10511. | OCSP Stapling
  10512. *----------------------------------------------------------------------------*/
  10513. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10514. * need to contact the CA, lowering the cost of cert revocation checking.
  10515. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10516. * POST: 1 returned for success.
  10517. */
  10518. static int test_wolfSSL_UseOCSPStapling(void)
  10519. {
  10520. EXPECT_DECLS;
  10521. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10522. !defined(NO_WOLFSSL_CLIENT)
  10523. WOLFSSL_CTX* ctx = NULL;
  10524. WOLFSSL* ssl = NULL;
  10525. #ifndef NO_WOLFSSL_CLIENT
  10526. #ifndef WOLFSSL_NO_TLS12
  10527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10528. #else
  10529. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10530. #endif
  10531. #else
  10532. #ifndef WOLFSSL_NO_TLS12
  10533. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10534. #else
  10535. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10536. #endif
  10537. #endif
  10538. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10539. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10540. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10541. #ifndef NO_WOLFSSL_CLIENT
  10542. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10543. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10544. #else
  10545. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10546. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10547. #endif
  10548. wolfSSL_free(ssl);
  10549. wolfSSL_CTX_free(ctx);
  10550. #endif
  10551. return EXPECT_RESULT();
  10552. } /* END test_wolfSSL_UseOCSPStapling */
  10553. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10554. * stapling eliminates the need to contact the CA and lowers cert revocation
  10555. * check.
  10556. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10557. */
  10558. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10559. {
  10560. EXPECT_DECLS;
  10561. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10562. !defined(NO_WOLFSSL_CLIENT)
  10563. WOLFSSL_CTX* ctx = NULL;
  10564. WOLFSSL* ssl = NULL;
  10565. #ifndef NO_WOLFSSL_CLIENT
  10566. #ifndef WOLFSSL_NO_TLS12
  10567. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10568. #else
  10569. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10570. #endif
  10571. #else
  10572. #ifndef WOLFSSL_NO_TLS12
  10573. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10574. #else
  10575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10576. #endif
  10577. #endif
  10578. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10579. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10580. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10581. #ifndef NO_WOLFSSL_CLIENT
  10582. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10583. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10584. #else
  10585. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10586. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10587. #endif
  10588. wolfSSL_free(ssl);
  10589. wolfSSL_CTX_free(ctx);
  10590. #endif
  10591. return EXPECT_RESULT();
  10592. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10593. /*----------------------------------------------------------------------------*
  10594. | Multicast Tests
  10595. *----------------------------------------------------------------------------*/
  10596. static int test_wolfSSL_mcast(void)
  10597. {
  10598. EXPECT_DECLS;
  10599. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10600. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10601. WOLFSSL_CTX* ctx = NULL;
  10602. WOLFSSL* ssl = NULL;
  10603. byte preMasterSecret[512];
  10604. byte clientRandom[32];
  10605. byte serverRandom[32];
  10606. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10607. byte buf[256];
  10608. word16 newId;
  10609. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10610. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10611. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10612. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10613. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10614. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10615. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10616. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10617. WOLFSSL_SUCCESS);
  10618. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10619. ExpectIntLE(newId, 100);
  10620. wolfSSL_free(ssl);
  10621. wolfSSL_CTX_free(ctx);
  10622. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10623. * WOLFSSL_SNIFFER) */
  10624. return EXPECT_RESULT();
  10625. }
  10626. /*----------------------------------------------------------------------------*
  10627. | Wolfcrypt
  10628. *----------------------------------------------------------------------------*/
  10629. /*
  10630. * Unit test for the wc_InitBlake2b()
  10631. */
  10632. static int test_wc_InitBlake2b(void)
  10633. {
  10634. EXPECT_DECLS;
  10635. #ifdef HAVE_BLAKE2
  10636. Blake2b blake;
  10637. /* Test good arg. */
  10638. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10639. /* Test bad arg. */
  10640. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10641. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10642. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10643. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10644. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10645. #endif
  10646. return EXPECT_RESULT();
  10647. } /* END test_wc_InitBlake2b*/
  10648. /*
  10649. * Unit test for the wc_InitBlake2b_WithKey()
  10650. */
  10651. static int test_wc_InitBlake2b_WithKey(void)
  10652. {
  10653. EXPECT_DECLS;
  10654. #ifdef HAVE_BLAKE2
  10655. Blake2b blake;
  10656. word32 digestSz = BLAKE2B_KEYBYTES;
  10657. byte key[BLAKE2B_KEYBYTES];
  10658. word32 keylen = BLAKE2B_KEYBYTES;
  10659. XMEMSET(key, 0, sizeof(key));
  10660. /* Test good arg. */
  10661. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10662. /* Test bad args. */
  10663. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10664. BAD_FUNC_ARG);
  10665. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10666. BAD_FUNC_ARG);
  10667. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10668. #endif
  10669. return EXPECT_RESULT();
  10670. } /* END wc_InitBlake2b_WithKey*/
  10671. /*
  10672. * Unit test for the wc_InitBlake2s_WithKey()
  10673. */
  10674. static int test_wc_InitBlake2s_WithKey(void)
  10675. {
  10676. EXPECT_DECLS;
  10677. #ifdef HAVE_BLAKE2S
  10678. Blake2s blake;
  10679. word32 digestSz = BLAKE2S_KEYBYTES;
  10680. byte *key = (byte*)"01234567890123456789012345678901";
  10681. word32 keylen = BLAKE2S_KEYBYTES;
  10682. /* Test good arg. */
  10683. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10684. /* Test bad args. */
  10685. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10686. BAD_FUNC_ARG);
  10687. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10688. BAD_FUNC_ARG);
  10689. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10690. #endif
  10691. return EXPECT_RESULT();
  10692. } /* END wc_InitBlake2s_WithKey*/
  10693. /*
  10694. * Unit test for the wc_InitMd5()
  10695. */
  10696. static int test_wc_InitMd5(void)
  10697. {
  10698. EXPECT_DECLS;
  10699. #ifndef NO_MD5
  10700. wc_Md5 md5;
  10701. /* Test good arg. */
  10702. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10703. /* Test bad arg. */
  10704. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10705. wc_Md5Free(&md5);
  10706. #endif
  10707. return EXPECT_RESULT();
  10708. } /* END test_wc_InitMd5 */
  10709. /*
  10710. * Testing wc_UpdateMd5()
  10711. */
  10712. static int test_wc_Md5Update(void)
  10713. {
  10714. EXPECT_DECLS;
  10715. #ifndef NO_MD5
  10716. wc_Md5 md5;
  10717. byte hash[WC_MD5_DIGEST_SIZE];
  10718. testVector a, b, c;
  10719. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10720. /* Input */
  10721. a.input = "a";
  10722. a.inLen = XSTRLEN(a.input);
  10723. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10724. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10725. /* Update input. */
  10726. a.input = "abc";
  10727. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10728. "\x72";
  10729. a.inLen = XSTRLEN(a.input);
  10730. a.outLen = XSTRLEN(a.output);
  10731. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10732. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10733. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10734. /* Pass in bad values. */
  10735. b.input = NULL;
  10736. b.inLen = 0;
  10737. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10738. c.input = NULL;
  10739. c.inLen = WC_MD5_DIGEST_SIZE;
  10740. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10741. BAD_FUNC_ARG);
  10742. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10743. BAD_FUNC_ARG);
  10744. wc_Md5Free(&md5);
  10745. #endif
  10746. return EXPECT_RESULT();
  10747. } /* END test_wc_Md5Update() */
  10748. /*
  10749. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10750. */
  10751. static int test_wc_Md5Final(void)
  10752. {
  10753. EXPECT_DECLS;
  10754. #ifndef NO_MD5
  10755. /* Instantiate */
  10756. wc_Md5 md5;
  10757. byte* hash_test[3];
  10758. byte hash1[WC_MD5_DIGEST_SIZE];
  10759. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10760. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10761. int times, i;
  10762. /* Initialize */
  10763. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10764. hash_test[0] = hash1;
  10765. hash_test[1] = hash2;
  10766. hash_test[2] = hash3;
  10767. times = sizeof(hash_test)/sizeof(byte*);
  10768. for (i = 0; i < times; i++) {
  10769. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10770. }
  10771. /* Test bad args. */
  10772. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10773. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10774. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10775. wc_Md5Free(&md5);
  10776. #endif
  10777. return EXPECT_RESULT();
  10778. }
  10779. /*
  10780. * Unit test for the wc_InitSha()
  10781. */
  10782. static int test_wc_InitSha(void)
  10783. {
  10784. EXPECT_DECLS;
  10785. #ifndef NO_SHA
  10786. wc_Sha sha;
  10787. /* Test good arg. */
  10788. ExpectIntEQ(wc_InitSha(&sha), 0);
  10789. /* Test bad arg. */
  10790. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10791. wc_ShaFree(&sha);
  10792. #endif
  10793. return EXPECT_RESULT();
  10794. } /* END test_wc_InitSha */
  10795. /*
  10796. * Tesing wc_ShaUpdate()
  10797. */
  10798. static int test_wc_ShaUpdate(void)
  10799. {
  10800. EXPECT_DECLS;
  10801. #ifndef NO_SHA
  10802. wc_Sha sha;
  10803. byte hash[WC_SHA_DIGEST_SIZE];
  10804. testVector a, b, c;
  10805. ExpectIntEQ(wc_InitSha(&sha), 0);
  10806. /* Input. */
  10807. a.input = "a";
  10808. a.inLen = XSTRLEN(a.input);
  10809. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10810. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10811. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10812. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10813. /* Update input. */
  10814. a.input = "abc";
  10815. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10816. "\x6C\x9C\xD0\xD8\x9D";
  10817. a.inLen = XSTRLEN(a.input);
  10818. a.outLen = XSTRLEN(a.output);
  10819. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10820. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10821. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10822. /* Try passing in bad values. */
  10823. b.input = NULL;
  10824. b.inLen = 0;
  10825. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  10826. c.input = NULL;
  10827. c.inLen = WC_SHA_DIGEST_SIZE;
  10828. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  10829. BAD_FUNC_ARG);
  10830. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  10831. BAD_FUNC_ARG);
  10832. wc_ShaFree(&sha);
  10833. #endif
  10834. return EXPECT_RESULT();
  10835. } /* END test_wc_ShaUpdate() */
  10836. /*
  10837. * Unit test on wc_ShaFinal
  10838. */
  10839. static int test_wc_ShaFinal(void)
  10840. {
  10841. EXPECT_DECLS;
  10842. #ifndef NO_SHA
  10843. wc_Sha sha;
  10844. byte* hash_test[3];
  10845. byte hash1[WC_SHA_DIGEST_SIZE];
  10846. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10847. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10848. int times, i;
  10849. /* Initialize*/
  10850. ExpectIntEQ(wc_InitSha(&sha), 0);
  10851. hash_test[0] = hash1;
  10852. hash_test[1] = hash2;
  10853. hash_test[2] = hash3;
  10854. times = sizeof(hash_test)/sizeof(byte*);
  10855. for (i = 0; i < times; i++) {
  10856. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  10857. }
  10858. /* Test bad args. */
  10859. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  10860. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  10861. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  10862. wc_ShaFree(&sha);
  10863. #endif
  10864. return EXPECT_RESULT();
  10865. } /* END test_wc_ShaFinal */
  10866. /*
  10867. * Unit test for wc_InitSha256()
  10868. */
  10869. static int test_wc_InitSha256(void)
  10870. {
  10871. EXPECT_DECLS;
  10872. #ifndef NO_SHA256
  10873. wc_Sha256 sha256;
  10874. /* Test good arg. */
  10875. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10876. /* Test bad arg. */
  10877. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  10878. wc_Sha256Free(&sha256);
  10879. #endif
  10880. return EXPECT_RESULT();
  10881. } /* END test_wc_InitSha256 */
  10882. /*
  10883. * Unit test for wc_Sha256Update()
  10884. */
  10885. static int test_wc_Sha256Update(void)
  10886. {
  10887. EXPECT_DECLS;
  10888. #ifndef NO_SHA256
  10889. wc_Sha256 sha256;
  10890. byte hash[WC_SHA256_DIGEST_SIZE];
  10891. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  10892. testVector a, b, c;
  10893. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10894. /* Input. */
  10895. a.input = "a";
  10896. a.inLen = XSTRLEN(a.input);
  10897. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  10898. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  10899. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10900. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10901. /* Update input. */
  10902. a.input = "abc";
  10903. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10904. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10905. "\x15\xAD";
  10906. a.inLen = XSTRLEN(a.input);
  10907. a.outLen = XSTRLEN(a.output);
  10908. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10909. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10910. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  10911. /* Unaligned check. */
  10912. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  10913. 0);
  10914. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  10915. /* Try passing in bad values */
  10916. b.input = NULL;
  10917. b.inLen = 0;
  10918. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  10919. c.input = NULL;
  10920. c.inLen = WC_SHA256_DIGEST_SIZE;
  10921. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  10922. BAD_FUNC_ARG);
  10923. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  10924. BAD_FUNC_ARG);
  10925. wc_Sha256Free(&sha256);
  10926. #endif
  10927. return EXPECT_RESULT();
  10928. } /* END test_wc_Sha256Update */
  10929. /*
  10930. * Unit test function for wc_Sha256Final()
  10931. */
  10932. static int test_wc_Sha256Final(void)
  10933. {
  10934. EXPECT_DECLS;
  10935. #ifndef NO_SHA256
  10936. wc_Sha256 sha256;
  10937. byte* hash_test[3];
  10938. byte hash1[WC_SHA256_DIGEST_SIZE];
  10939. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10940. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10941. int times, i;
  10942. /* Initialize */
  10943. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10944. hash_test[0] = hash1;
  10945. hash_test[1] = hash2;
  10946. hash_test[2] = hash3;
  10947. times = sizeof(hash_test) / sizeof(byte*);
  10948. for (i = 0; i < times; i++) {
  10949. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  10950. }
  10951. /* Test bad args. */
  10952. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  10953. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  10954. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  10955. wc_Sha256Free(&sha256);
  10956. #endif
  10957. return EXPECT_RESULT();
  10958. } /* END test_wc_Sha256Final */
  10959. /*
  10960. * Unit test function for wc_Sha256FinalRaw()
  10961. */
  10962. static int test_wc_Sha256FinalRaw(void)
  10963. {
  10964. EXPECT_DECLS;
  10965. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10966. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10967. !defined(WOLFSSL_NO_HASH_RAW)
  10968. wc_Sha256 sha256;
  10969. byte* hash_test[3];
  10970. byte hash1[WC_SHA256_DIGEST_SIZE];
  10971. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10972. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10973. int times, i;
  10974. /* Initialize */
  10975. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10976. hash_test[0] = hash1;
  10977. hash_test[1] = hash2;
  10978. hash_test[2] = hash3;
  10979. times = sizeof(hash_test) / sizeof(byte*);
  10980. for (i = 0; i < times; i++) {
  10981. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  10982. }
  10983. /* Test bad args. */
  10984. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  10985. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  10986. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  10987. wc_Sha256Free(&sha256);
  10988. #endif
  10989. return EXPECT_RESULT();
  10990. } /* END test_wc_Sha256FinalRaw */
  10991. /*
  10992. * Unit test function for wc_Sha256GetFlags()
  10993. */
  10994. static int test_wc_Sha256GetFlags(void)
  10995. {
  10996. EXPECT_DECLS;
  10997. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10998. wc_Sha256 sha256;
  10999. word32 flags = 0;
  11000. /* Initialize */
  11001. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11002. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11003. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11004. wc_Sha256Free(&sha256);
  11005. #endif
  11006. return EXPECT_RESULT();
  11007. } /* END test_wc_Sha256GetFlags */
  11008. /*
  11009. * Unit test function for wc_Sha256Free()
  11010. */
  11011. static int test_wc_Sha256Free(void)
  11012. {
  11013. EXPECT_DECLS;
  11014. #ifndef NO_SHA256
  11015. wc_Sha256Free(NULL);
  11016. /* Set result to SUCCESS. */
  11017. ExpectTrue(1);
  11018. #endif
  11019. return EXPECT_RESULT();
  11020. } /* END test_wc_Sha256Free */
  11021. /*
  11022. * Unit test function for wc_Sha256GetHash()
  11023. */
  11024. static int test_wc_Sha256GetHash(void)
  11025. {
  11026. EXPECT_DECLS;
  11027. #ifndef NO_SHA256
  11028. wc_Sha256 sha256;
  11029. byte hash1[WC_SHA256_DIGEST_SIZE];
  11030. /* Initialize */
  11031. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11032. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11033. /* test bad arguments*/
  11034. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11035. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11036. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11037. wc_Sha256Free(&sha256);
  11038. #endif
  11039. return EXPECT_RESULT();
  11040. } /* END test_wc_Sha256GetHash */
  11041. /*
  11042. * Unit test function for wc_Sha256Copy()
  11043. */
  11044. static int test_wc_Sha256Copy(void)
  11045. {
  11046. EXPECT_DECLS;
  11047. #ifndef NO_SHA256
  11048. wc_Sha256 sha256;
  11049. wc_Sha256 temp;
  11050. XMEMSET(&sha256, 0, sizeof(sha256));
  11051. XMEMSET(&temp, 0, sizeof(temp));
  11052. /* Initialize */
  11053. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11054. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11055. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11056. /* test bad arguments*/
  11057. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11058. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11059. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11060. wc_Sha256Free(&sha256);
  11061. wc_Sha256Free(&temp);
  11062. #endif
  11063. return EXPECT_RESULT();
  11064. } /* END test_wc_Sha256Copy */
  11065. /*
  11066. * Testing wc_InitSha512()
  11067. */
  11068. static int test_wc_InitSha512(void)
  11069. {
  11070. EXPECT_DECLS;
  11071. #ifdef WOLFSSL_SHA512
  11072. wc_Sha512 sha512;
  11073. /* Test good arg. */
  11074. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11075. /* Test bad arg. */
  11076. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11077. wc_Sha512Free(&sha512);
  11078. #endif
  11079. return EXPECT_RESULT();
  11080. } /* END test_wc_InitSha512 */
  11081. /*
  11082. * wc_Sha512Update() test.
  11083. */
  11084. static int test_wc_Sha512Update(void)
  11085. {
  11086. EXPECT_DECLS;
  11087. #ifdef WOLFSSL_SHA512
  11088. wc_Sha512 sha512;
  11089. byte hash[WC_SHA512_DIGEST_SIZE];
  11090. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11091. testVector a, b, c;
  11092. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11093. /* Input. */
  11094. a.input = "a";
  11095. a.inLen = XSTRLEN(a.input);
  11096. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11097. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11098. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11099. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11100. /* Update input. */
  11101. a.input = "abc";
  11102. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11103. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11104. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11105. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11106. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11107. a.inLen = XSTRLEN(a.input);
  11108. a.outLen = XSTRLEN(a.output);
  11109. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11110. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11111. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11112. /* Unaligned check. */
  11113. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11114. 0);
  11115. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11116. /* Try passing in bad values */
  11117. b.input = NULL;
  11118. b.inLen = 0;
  11119. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11120. c.input = NULL;
  11121. c.inLen = WC_SHA512_DIGEST_SIZE;
  11122. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11123. BAD_FUNC_ARG);
  11124. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11125. BAD_FUNC_ARG);
  11126. wc_Sha512Free(&sha512);
  11127. #endif
  11128. return EXPECT_RESULT();
  11129. } /* END test_wc_Sha512Update */
  11130. #ifdef WOLFSSL_SHA512
  11131. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11132. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11133. /* Performs test for
  11134. * - wc_Sha512Final/wc_Sha512FinalRaw
  11135. * - wc_Sha512_224Final/wc_Sha512_224Final
  11136. * - wc_Sha512_256Final/wc_Sha512_256Final
  11137. * parameter:
  11138. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11139. * WC_HASH_TYPE_SHA512_256
  11140. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11141. *return 0 on success
  11142. */
  11143. static int test_Sha512_Family_Final(int type, int isRaw)
  11144. {
  11145. EXPECT_DECLS;
  11146. wc_Sha512 sha512;
  11147. byte* hash_test[3];
  11148. byte hash1[WC_SHA512_DIGEST_SIZE];
  11149. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11150. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11151. int times, i;
  11152. int(*initFp)(wc_Sha512*);
  11153. int(*finalFp)(wc_Sha512*, byte*);
  11154. void(*freeFp)(wc_Sha512*);
  11155. if (type == WC_HASH_TYPE_SHA512) {
  11156. initFp = wc_InitSha512;
  11157. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11158. !defined(WOLFSSL_NO_HASH_RAW)
  11159. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11160. #else
  11161. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11162. #endif
  11163. freeFp = wc_Sha512Free;
  11164. }
  11165. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11166. #if !defined(WOLFSSL_NOSHA512_224)
  11167. else if (type == WC_HASH_TYPE_SHA512_224) {
  11168. initFp = wc_InitSha512_224;
  11169. #if !defined(WOLFSSL_NO_HASH_RAW)
  11170. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11171. #else
  11172. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11173. #endif
  11174. freeFp = wc_Sha512_224Free;
  11175. }
  11176. #endif
  11177. #if !defined(WOLFSSL_NOSHA512_256)
  11178. else if (type == WC_HASH_TYPE_SHA512_256) {
  11179. initFp = wc_InitSha512_256;
  11180. #if !defined(WOLFSSL_NO_HASH_RAW)
  11181. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11182. #else
  11183. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11184. #endif
  11185. freeFp = wc_Sha512_256Free;
  11186. }
  11187. #endif
  11188. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11189. else
  11190. return TEST_FAIL;
  11191. /* Initialize */
  11192. ExpectIntEQ(initFp(&sha512), 0);
  11193. hash_test[0] = hash1;
  11194. hash_test[1] = hash2;
  11195. hash_test[2] = hash3;
  11196. times = sizeof(hash_test) / sizeof(byte *);
  11197. /* Good test args. */
  11198. for (i = 0; i < times; i++) {
  11199. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11200. }
  11201. /* Test bad args. */
  11202. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11203. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11204. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11205. freeFp(&sha512);
  11206. return EXPECT_RESULT();
  11207. }
  11208. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11209. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11210. #endif /* WOLFSSL_SHA512 */
  11211. /*
  11212. * Unit test function for wc_Sha512Final()
  11213. */
  11214. static int test_wc_Sha512Final(void)
  11215. {
  11216. EXPECT_DECLS;
  11217. #ifdef WOLFSSL_SHA512
  11218. wc_Sha512 sha512;
  11219. byte* hash_test[3];
  11220. byte hash1[WC_SHA512_DIGEST_SIZE];
  11221. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11222. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11223. int times, i;
  11224. /* Initialize */
  11225. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11226. hash_test[0] = hash1;
  11227. hash_test[1] = hash2;
  11228. hash_test[2] = hash3;
  11229. times = sizeof(hash_test) / sizeof(byte *);
  11230. for (i = 0; i < times; i++) {
  11231. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11232. }
  11233. /* Test bad args. */
  11234. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11235. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11236. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11237. wc_Sha512Free(&sha512);
  11238. #endif
  11239. return EXPECT_RESULT();
  11240. } /* END test_wc_Sha512Final */
  11241. /*
  11242. * Unit test function for wc_Sha512GetFlags()
  11243. */
  11244. static int test_wc_Sha512GetFlags(void)
  11245. {
  11246. EXPECT_DECLS;
  11247. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11248. wc_Sha512 sha512;
  11249. word32 flags = 0;
  11250. /* Initialize */
  11251. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11252. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11253. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11254. wc_Sha512Free(&sha512);
  11255. #endif
  11256. return EXPECT_RESULT();
  11257. } /* END test_wc_Sha512GetFlags */
  11258. /*
  11259. * Unit test function for wc_Sha512FinalRaw()
  11260. */
  11261. static int test_wc_Sha512FinalRaw(void)
  11262. {
  11263. EXPECT_DECLS;
  11264. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11265. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11266. !defined(WOLFSSL_NO_HASH_RAW)
  11267. wc_Sha512 sha512;
  11268. byte* hash_test[3];
  11269. byte hash1[WC_SHA512_DIGEST_SIZE];
  11270. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11271. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11272. int times, i;
  11273. /* Initialize */
  11274. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11275. hash_test[0] = hash1;
  11276. hash_test[1] = hash2;
  11277. hash_test[2] = hash3;
  11278. times = sizeof(hash_test) / sizeof(byte*);
  11279. /* Good test args. */
  11280. for (i = 0; i < times; i++) {
  11281. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11282. }
  11283. /* Test bad args. */
  11284. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11285. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11286. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11287. wc_Sha512Free(&sha512);
  11288. #endif
  11289. return EXPECT_RESULT();
  11290. } /* END test_wc_Sha512FinalRaw */
  11291. /*
  11292. * Unit test function for wc_Sha512Free()
  11293. */
  11294. static int test_wc_Sha512Free(void)
  11295. {
  11296. EXPECT_DECLS;
  11297. #ifdef WOLFSSL_SHA512
  11298. wc_Sha512Free(NULL);
  11299. /* Set result to SUCCESS. */
  11300. ExpectTrue(1);
  11301. #endif
  11302. return EXPECT_RESULT();
  11303. } /* END test_wc_Sha512Free */
  11304. #ifdef WOLFSSL_SHA512
  11305. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11306. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11307. static int test_Sha512_Family_GetHash(int type )
  11308. {
  11309. EXPECT_DECLS;
  11310. int(*initFp)(wc_Sha512*);
  11311. int(*ghashFp)(wc_Sha512*, byte*);
  11312. wc_Sha512 sha512;
  11313. byte hash1[WC_SHA512_DIGEST_SIZE];
  11314. if (type == WC_HASH_TYPE_SHA512) {
  11315. initFp = wc_InitSha512;
  11316. ghashFp = wc_Sha512GetHash;
  11317. }
  11318. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11319. #if !defined(WOLFSSL_NOSHA512_224)
  11320. else if (type == WC_HASH_TYPE_SHA512_224) {
  11321. initFp = wc_InitSha512_224;
  11322. ghashFp = wc_Sha512_224GetHash;
  11323. }
  11324. #endif
  11325. #if !defined(WOLFSSL_NOSHA512_256)
  11326. else if (type == WC_HASH_TYPE_SHA512_256) {
  11327. initFp = wc_InitSha512_256;
  11328. ghashFp = wc_Sha512_256GetHash;
  11329. }
  11330. #endif
  11331. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11332. else {
  11333. initFp = NULL;
  11334. ghashFp = NULL;
  11335. }
  11336. if (initFp == NULL || ghashFp == NULL)
  11337. return TEST_FAIL;
  11338. ExpectIntEQ(initFp(&sha512), 0);
  11339. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11340. /* test bad arguments*/
  11341. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11342. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11343. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11344. wc_Sha512Free(&sha512);
  11345. return EXPECT_RESULT();
  11346. }
  11347. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11348. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11349. #endif /* WOLFSSL_SHA512 */
  11350. /*
  11351. * Unit test function for wc_Sha512GetHash()
  11352. */
  11353. static int test_wc_Sha512GetHash(void)
  11354. {
  11355. EXPECT_DECLS;
  11356. #ifdef WOLFSSL_SHA512
  11357. wc_Sha512 sha512;
  11358. byte hash1[WC_SHA512_DIGEST_SIZE];
  11359. /* Initialize */
  11360. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11361. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11362. /* test bad arguments*/
  11363. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11364. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11365. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11366. wc_Sha512Free(&sha512);
  11367. #endif
  11368. return EXPECT_RESULT();
  11369. } /* END test_wc_Sha512GetHash */
  11370. /*
  11371. * Unit test function for wc_Sha512Copy()
  11372. */
  11373. static int test_wc_Sha512Copy(void)
  11374. {
  11375. EXPECT_DECLS;
  11376. #ifdef WOLFSSL_SHA512
  11377. wc_Sha512 sha512;
  11378. wc_Sha512 temp;
  11379. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11380. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11381. /* Initialize */
  11382. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11383. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11384. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11385. /* test bad arguments*/
  11386. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11387. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11388. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11389. wc_Sha512Free(&sha512);
  11390. wc_Sha512Free(&temp);
  11391. #endif
  11392. return EXPECT_RESULT();
  11393. } /* END test_wc_Sha512Copy */
  11394. static int test_wc_InitSha512_224(void)
  11395. {
  11396. EXPECT_DECLS;
  11397. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11398. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11399. wc_Sha512 sha512;
  11400. /* Test good arg. */
  11401. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11402. /* Test bad arg. */
  11403. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11404. wc_Sha512_224Free(&sha512);
  11405. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11406. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11407. return EXPECT_RESULT();
  11408. }
  11409. static int test_wc_Sha512_224Update(void)
  11410. {
  11411. EXPECT_DECLS;
  11412. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11413. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11414. wc_Sha512 sha512;
  11415. byte hash[WC_SHA512_DIGEST_SIZE];
  11416. testVector a, c;
  11417. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11418. /* Input. */
  11419. a.input = "a";
  11420. a.inLen = XSTRLEN(a.input);
  11421. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11422. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11423. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11424. 0);
  11425. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11426. /* Update input. */
  11427. a.input = "abc";
  11428. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11429. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11430. a.inLen = XSTRLEN(a.input);
  11431. a.outLen = XSTRLEN(a.output);
  11432. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11433. 0);
  11434. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11435. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11436. c.input = NULL;
  11437. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11438. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11439. BAD_FUNC_ARG);
  11440. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11441. BAD_FUNC_ARG);
  11442. wc_Sha512_224Free(&sha512);
  11443. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11444. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11445. return EXPECT_RESULT();
  11446. }
  11447. static int test_wc_Sha512_224Final(void)
  11448. {
  11449. EXPECT_DECLS;
  11450. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11451. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11452. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11453. TEST_SUCCESS);
  11454. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11455. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11456. return EXPECT_RESULT();
  11457. }
  11458. static int test_wc_Sha512_224GetFlags(void)
  11459. {
  11460. EXPECT_DECLS;
  11461. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11462. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11463. wc_Sha512 sha512;
  11464. wc_Sha512 copy;
  11465. word32 flags = 0;
  11466. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11467. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11468. /* Initialize */
  11469. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11470. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11471. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11472. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11473. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11474. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11475. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11476. wc_Sha512_224Free(&copy);
  11477. wc_Sha512_224Free(&sha512);
  11478. #endif
  11479. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11480. return EXPECT_RESULT();
  11481. }
  11482. static int test_wc_Sha512_224FinalRaw(void)
  11483. {
  11484. EXPECT_DECLS;
  11485. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11486. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11487. !defined(WOLFSSL_NO_HASH_RAW)
  11488. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11489. TEST_SUCCESS);
  11490. #endif
  11491. return EXPECT_RESULT();
  11492. }
  11493. static int test_wc_Sha512_224Free(void)
  11494. {
  11495. EXPECT_DECLS;
  11496. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11497. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11498. wc_Sha512_224Free(NULL);
  11499. /* Set result to SUCCESS. */
  11500. ExpectTrue(1);
  11501. #endif
  11502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11503. return EXPECT_RESULT();
  11504. }
  11505. static int test_wc_Sha512_224GetHash(void)
  11506. {
  11507. EXPECT_DECLS;
  11508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11509. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11510. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11511. TEST_SUCCESS);
  11512. #endif
  11513. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11514. return EXPECT_RESULT();
  11515. }
  11516. static int test_wc_Sha512_224Copy(void)
  11517. {
  11518. EXPECT_DECLS;
  11519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11521. wc_Sha512 sha512;
  11522. wc_Sha512 temp;
  11523. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11524. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11525. /* Initialize */
  11526. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11527. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11528. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11529. /* test bad arguments*/
  11530. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11531. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11532. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11533. wc_Sha512_224Free(&sha512);
  11534. wc_Sha512_224Free(&temp);
  11535. #endif
  11536. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11537. return EXPECT_RESULT();
  11538. }
  11539. static int test_wc_InitSha512_256(void)
  11540. {
  11541. EXPECT_DECLS;
  11542. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11543. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11544. wc_Sha512 sha512;
  11545. /* Test good arg. */
  11546. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11547. /* Test bad arg. */
  11548. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11549. wc_Sha512_256Free(&sha512);
  11550. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11551. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11552. return EXPECT_RESULT();
  11553. }
  11554. static int test_wc_Sha512_256Update(void)
  11555. {
  11556. EXPECT_DECLS;
  11557. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11558. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11559. wc_Sha512 sha512;
  11560. byte hash[WC_SHA512_DIGEST_SIZE];
  11561. testVector a, c;
  11562. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11563. /* Input. */
  11564. a.input = "a";
  11565. a.inLen = XSTRLEN(a.input);
  11566. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11567. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11568. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11569. 0);
  11570. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11571. /* Update input. */
  11572. a.input = "abc";
  11573. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11574. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11575. "\x07\xe7\xaf\x23";
  11576. a.inLen = XSTRLEN(a.input);
  11577. a.outLen = XSTRLEN(a.output);
  11578. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11579. 0);
  11580. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11581. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11582. c.input = NULL;
  11583. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11584. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11585. BAD_FUNC_ARG);
  11586. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11587. BAD_FUNC_ARG);
  11588. wc_Sha512_256Free(&sha512);
  11589. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11590. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11591. return EXPECT_RESULT();
  11592. }
  11593. static int test_wc_Sha512_256Final(void)
  11594. {
  11595. EXPECT_DECLS;
  11596. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11597. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11598. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11599. TEST_SUCCESS);
  11600. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11601. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11602. return EXPECT_RESULT();
  11603. }
  11604. static int test_wc_Sha512_256GetFlags(void)
  11605. {
  11606. EXPECT_DECLS;
  11607. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11608. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11609. wc_Sha512 sha512, copy;
  11610. word32 flags = 0;
  11611. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11612. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11613. /* Initialize */
  11614. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11615. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11616. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11617. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11618. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11619. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11620. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11621. wc_Sha512_256Free(&sha512);
  11622. #endif
  11623. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11624. return EXPECT_RESULT();
  11625. }
  11626. static int test_wc_Sha512_256FinalRaw(void)
  11627. {
  11628. EXPECT_DECLS;
  11629. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11630. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11631. !defined(WOLFSSL_NO_HASH_RAW)
  11632. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11633. TEST_SUCCESS);
  11634. #endif
  11635. return EXPECT_RESULT();
  11636. }
  11637. static int test_wc_Sha512_256Free(void)
  11638. {
  11639. EXPECT_DECLS;
  11640. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11641. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11642. wc_Sha512_256Free(NULL);
  11643. /* Set result to SUCCESS. */
  11644. ExpectTrue(1);
  11645. #endif
  11646. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11647. return EXPECT_RESULT();
  11648. }
  11649. static int test_wc_Sha512_256GetHash(void)
  11650. {
  11651. EXPECT_DECLS;
  11652. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11653. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11654. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11655. TEST_SUCCESS);
  11656. #endif
  11657. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11658. return EXPECT_RESULT();
  11659. }
  11660. static int test_wc_Sha512_256Copy(void)
  11661. {
  11662. EXPECT_DECLS;
  11663. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11664. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11665. wc_Sha512 sha512;
  11666. wc_Sha512 temp;
  11667. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11668. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11669. /* Initialize */
  11670. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11671. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11672. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11673. /* test bad arguments*/
  11674. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11675. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11676. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11677. wc_Sha512_256Free(&sha512);
  11678. wc_Sha512_256Free(&temp);
  11679. #endif
  11680. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11681. return EXPECT_RESULT();
  11682. }
  11683. /*
  11684. * Testing wc_InitSha384()
  11685. */
  11686. static int test_wc_InitSha384(void)
  11687. {
  11688. EXPECT_DECLS;
  11689. #ifdef WOLFSSL_SHA384
  11690. wc_Sha384 sha384;
  11691. /* Test good arg. */
  11692. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11693. /* Test bad arg. */
  11694. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11695. wc_Sha384Free(&sha384);
  11696. #endif
  11697. return EXPECT_RESULT();
  11698. } /* END test_wc_InitSha384 */
  11699. /*
  11700. * test wc_Sha384Update()
  11701. */
  11702. static int test_wc_Sha384Update(void)
  11703. {
  11704. EXPECT_DECLS;
  11705. #ifdef WOLFSSL_SHA384
  11706. wc_Sha384 sha384;
  11707. byte hash[WC_SHA384_DIGEST_SIZE];
  11708. testVector a, b, c;
  11709. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11710. /* Input */
  11711. a.input = "a";
  11712. a.inLen = XSTRLEN(a.input);
  11713. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11714. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11715. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11716. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11717. /* Update input. */
  11718. a.input = "abc";
  11719. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11720. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11721. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11722. "\xc8\x25\xa7";
  11723. a.inLen = XSTRLEN(a.input);
  11724. a.outLen = XSTRLEN(a.output);
  11725. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11726. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11727. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11728. /* Pass in bad values. */
  11729. b.input = NULL;
  11730. b.inLen = 0;
  11731. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11732. c.input = NULL;
  11733. c.inLen = WC_SHA384_DIGEST_SIZE;
  11734. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11735. BAD_FUNC_ARG);
  11736. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11737. BAD_FUNC_ARG);
  11738. wc_Sha384Free(&sha384);
  11739. #endif
  11740. return EXPECT_RESULT();
  11741. } /* END test_wc_Sha384Update */
  11742. /*
  11743. * Unit test function for wc_Sha384Final();
  11744. */
  11745. static int test_wc_Sha384Final(void)
  11746. {
  11747. EXPECT_DECLS;
  11748. #ifdef WOLFSSL_SHA384
  11749. wc_Sha384 sha384;
  11750. byte* hash_test[3];
  11751. byte hash1[WC_SHA384_DIGEST_SIZE];
  11752. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11753. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11754. int times, i;
  11755. /* Initialize */
  11756. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11757. hash_test[0] = hash1;
  11758. hash_test[1] = hash2;
  11759. hash_test[2] = hash3;
  11760. times = sizeof(hash_test) / sizeof(byte*);
  11761. /* Good test args. */
  11762. for (i = 0; i < times; i++) {
  11763. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11764. }
  11765. /* Test bad args. */
  11766. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11767. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11768. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11769. wc_Sha384Free(&sha384);
  11770. #endif
  11771. return EXPECT_RESULT();
  11772. } /* END test_wc_Sha384Final */
  11773. /*
  11774. * Unit test function for wc_Sha384GetFlags()
  11775. */
  11776. static int test_wc_Sha384GetFlags(void)
  11777. {
  11778. EXPECT_DECLS;
  11779. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11780. wc_Sha384 sha384;
  11781. word32 flags = 0;
  11782. /* Initialize */
  11783. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11784. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11785. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11786. wc_Sha384Free(&sha384);
  11787. #endif
  11788. return EXPECT_RESULT();
  11789. } /* END test_wc_Sha384GetFlags */
  11790. /*
  11791. * Unit test function for wc_Sha384FinalRaw()
  11792. */
  11793. static int test_wc_Sha384FinalRaw(void)
  11794. {
  11795. EXPECT_DECLS;
  11796. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11797. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11798. !defined(WOLFSSL_NO_HASH_RAW)
  11799. wc_Sha384 sha384;
  11800. byte* hash_test[3];
  11801. byte hash1[WC_SHA384_DIGEST_SIZE];
  11802. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11803. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11804. int times, i;
  11805. /* Initialize */
  11806. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11807. hash_test[0] = hash1;
  11808. hash_test[1] = hash2;
  11809. hash_test[2] = hash3;
  11810. times = sizeof(hash_test) / sizeof(byte*);
  11811. /* Good test args. */
  11812. for (i = 0; i < times; i++) {
  11813. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11814. }
  11815. /* Test bad args. */
  11816. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11817. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11818. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11819. wc_Sha384Free(&sha384);
  11820. #endif
  11821. return EXPECT_RESULT();
  11822. } /* END test_wc_Sha384FinalRaw */
  11823. /*
  11824. * Unit test function for wc_Sha384Free()
  11825. */
  11826. static int test_wc_Sha384Free(void)
  11827. {
  11828. EXPECT_DECLS;
  11829. #ifdef WOLFSSL_SHA384
  11830. wc_Sha384Free(NULL);
  11831. /* Set result to SUCCESS. */
  11832. ExpectTrue(1);
  11833. #endif
  11834. return EXPECT_RESULT();
  11835. } /* END test_wc_Sha384Free */
  11836. /*
  11837. * Unit test function for wc_Sha384GetHash()
  11838. */
  11839. static int test_wc_Sha384GetHash(void)
  11840. {
  11841. EXPECT_DECLS;
  11842. #ifdef WOLFSSL_SHA384
  11843. wc_Sha384 sha384;
  11844. byte hash1[WC_SHA384_DIGEST_SIZE];
  11845. /* Initialize */
  11846. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11847. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  11848. /* test bad arguments*/
  11849. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  11850. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  11851. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  11852. wc_Sha384Free(&sha384);
  11853. #endif
  11854. return EXPECT_RESULT();
  11855. } /* END test_wc_Sha384GetHash */
  11856. /*
  11857. * Unit test function for wc_Sha384Copy()
  11858. */
  11859. static int test_wc_Sha384Copy(void)
  11860. {
  11861. EXPECT_DECLS;
  11862. #ifdef WOLFSSL_SHA384
  11863. wc_Sha384 sha384;
  11864. wc_Sha384 temp;
  11865. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  11866. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  11867. /* Initialize */
  11868. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11869. ExpectIntEQ(wc_InitSha384(&temp), 0);
  11870. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  11871. /* test bad arguments*/
  11872. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  11873. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  11874. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  11875. wc_Sha384Free(&sha384);
  11876. wc_Sha384Free(&temp);
  11877. #endif
  11878. return EXPECT_RESULT();
  11879. } /* END test_wc_Sha384Copy */
  11880. /*
  11881. * Testing wc_InitSha224();
  11882. */
  11883. static int test_wc_InitSha224(void)
  11884. {
  11885. EXPECT_DECLS;
  11886. #ifdef WOLFSSL_SHA224
  11887. wc_Sha224 sha224;
  11888. /* Test good arg. */
  11889. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11890. /* Test bad arg. */
  11891. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  11892. wc_Sha224Free(&sha224);
  11893. #endif
  11894. return EXPECT_RESULT();
  11895. } /* END test_wc_InitSha224 */
  11896. /*
  11897. * Unit test on wc_Sha224Update
  11898. */
  11899. static int test_wc_Sha224Update(void)
  11900. {
  11901. EXPECT_DECLS;
  11902. #ifdef WOLFSSL_SHA224
  11903. wc_Sha224 sha224;
  11904. byte hash[WC_SHA224_DIGEST_SIZE];
  11905. testVector a, b, c;
  11906. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11907. /* Input. */
  11908. a.input = "a";
  11909. a.inLen = XSTRLEN(a.input);
  11910. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  11911. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  11912. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11913. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11914. /* Update input. */
  11915. a.input = "abc";
  11916. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11917. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11918. a.inLen = XSTRLEN(a.input);
  11919. a.outLen = XSTRLEN(a.output);
  11920. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11921. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11922. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  11923. /* Pass in bad values. */
  11924. b.input = NULL;
  11925. b.inLen = 0;
  11926. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  11927. c.input = NULL;
  11928. c.inLen = WC_SHA224_DIGEST_SIZE;
  11929. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  11930. BAD_FUNC_ARG);
  11931. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11932. BAD_FUNC_ARG);
  11933. wc_Sha224Free(&sha224);
  11934. #endif
  11935. return EXPECT_RESULT();
  11936. } /* END test_wc_Sha224Update */
  11937. /*
  11938. * Unit test for wc_Sha224Final();
  11939. */
  11940. static int test_wc_Sha224Final(void)
  11941. {
  11942. EXPECT_DECLS;
  11943. #ifdef WOLFSSL_SHA224
  11944. wc_Sha224 sha224;
  11945. byte* hash_test[3];
  11946. byte hash1[WC_SHA224_DIGEST_SIZE];
  11947. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11948. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11949. int times, i;
  11950. /* Initialize */
  11951. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11952. hash_test[0] = hash1;
  11953. hash_test[1] = hash2;
  11954. hash_test[2] = hash3;
  11955. times = sizeof(hash_test) / sizeof(byte*);
  11956. /* Good test args. */
  11957. /* Testing oversized buffers. */
  11958. for (i = 0; i < times; i++) {
  11959. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  11960. }
  11961. /* Test bad args. */
  11962. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  11963. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  11964. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  11965. wc_Sha224Free(&sha224);
  11966. #endif
  11967. return EXPECT_RESULT();
  11968. } /* END test_wc_Sha224Final */
  11969. /*
  11970. * Unit test function for wc_Sha224SetFlags()
  11971. */
  11972. static int test_wc_Sha224SetFlags(void)
  11973. {
  11974. EXPECT_DECLS;
  11975. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11976. wc_Sha224 sha224;
  11977. word32 flags = WC_HASH_FLAG_WILLCOPY;
  11978. /* Initialize */
  11979. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11980. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  11981. flags = 0;
  11982. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11983. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  11984. wc_Sha224Free(&sha224);
  11985. #endif
  11986. return EXPECT_RESULT();
  11987. } /* END test_wc_Sha224SetFlags */
  11988. /*
  11989. * Unit test function for wc_Sha224GetFlags()
  11990. */
  11991. static int test_wc_Sha224GetFlags(void)
  11992. {
  11993. EXPECT_DECLS;
  11994. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11995. wc_Sha224 sha224;
  11996. word32 flags = 0;
  11997. /* Initialize */
  11998. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11999. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12000. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12001. wc_Sha224Free(&sha224);
  12002. #endif
  12003. return EXPECT_RESULT();
  12004. } /* END test_wc_Sha224GetFlags */
  12005. /*
  12006. * Unit test function for wc_Sha224Free()
  12007. */
  12008. static int test_wc_Sha224Free(void)
  12009. {
  12010. EXPECT_DECLS;
  12011. #ifdef WOLFSSL_SHA224
  12012. wc_Sha224Free(NULL);
  12013. /* Set result to SUCCESS. */
  12014. ExpectTrue(1);
  12015. #endif
  12016. return EXPECT_RESULT();
  12017. } /* END test_wc_Sha224Free */
  12018. /*
  12019. * Unit test function for wc_Sha224GetHash()
  12020. */
  12021. static int test_wc_Sha224GetHash(void)
  12022. {
  12023. EXPECT_DECLS;
  12024. #ifdef WOLFSSL_SHA224
  12025. wc_Sha224 sha224;
  12026. byte hash1[WC_SHA224_DIGEST_SIZE];
  12027. /* Initialize */
  12028. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12029. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12030. /* test bad arguments*/
  12031. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12032. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12033. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12034. wc_Sha224Free(&sha224);
  12035. #endif
  12036. return EXPECT_RESULT();
  12037. } /* END test_wc_Sha224GetHash */
  12038. /*
  12039. * Unit test function for wc_Sha224Copy()
  12040. */
  12041. static int test_wc_Sha224Copy(void)
  12042. {
  12043. EXPECT_DECLS;
  12044. #ifdef WOLFSSL_SHA224
  12045. wc_Sha224 sha224;
  12046. wc_Sha224 temp;
  12047. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12048. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12049. /* Initialize */
  12050. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12051. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12052. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12053. /* test bad arguments*/
  12054. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12055. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12056. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12057. wc_Sha224Free(&sha224);
  12058. wc_Sha224Free(&temp);
  12059. #endif
  12060. return EXPECT_RESULT();
  12061. } /* END test_wc_Sha224Copy */
  12062. /*
  12063. * Testing wc_InitRipeMd()
  12064. */
  12065. static int test_wc_InitRipeMd(void)
  12066. {
  12067. EXPECT_DECLS;
  12068. #ifdef WOLFSSL_RIPEMD
  12069. RipeMd ripemd;
  12070. /* Test good arg. */
  12071. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12072. /* Test bad arg. */
  12073. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12074. #endif
  12075. return EXPECT_RESULT();
  12076. } /* END test_wc_InitRipeMd */
  12077. /*
  12078. * Testing wc_RipeMdUpdate()
  12079. */
  12080. static int test_wc_RipeMdUpdate(void)
  12081. {
  12082. EXPECT_DECLS;
  12083. #ifdef WOLFSSL_RIPEMD
  12084. RipeMd ripemd;
  12085. byte hash[RIPEMD_DIGEST_SIZE];
  12086. testVector a, b, c;
  12087. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12088. /* Input */
  12089. a.input = "a";
  12090. a.inLen = XSTRLEN(a.input);
  12091. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12092. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12093. /* Update input. */
  12094. a.input = "abc";
  12095. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12096. "\xb0\x87\xf1\x5a\x0b\xfc";
  12097. a.inLen = XSTRLEN(a.input);
  12098. a.outLen = XSTRLEN(a.output);
  12099. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12100. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12101. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12102. /* Pass in bad values. */
  12103. b.input = NULL;
  12104. b.inLen = 0;
  12105. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12106. c.input = NULL;
  12107. c.inLen = RIPEMD_DIGEST_SIZE;
  12108. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12109. BAD_FUNC_ARG);
  12110. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12111. BAD_FUNC_ARG);
  12112. #endif
  12113. return EXPECT_RESULT();
  12114. } /* END test_wc_RipeMdUdpate */
  12115. /*
  12116. * Unit test function for wc_RipeMdFinal()
  12117. */
  12118. static int test_wc_RipeMdFinal(void)
  12119. {
  12120. EXPECT_DECLS;
  12121. #ifdef WOLFSSL_RIPEMD
  12122. RipeMd ripemd;
  12123. byte* hash_test[3];
  12124. byte hash1[RIPEMD_DIGEST_SIZE];
  12125. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12126. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12127. int times, i;
  12128. /* Initialize */
  12129. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12130. hash_test[0] = hash1;
  12131. hash_test[1] = hash2;
  12132. hash_test[2] = hash3;
  12133. times = sizeof(hash_test) / sizeof(byte*);
  12134. /* Testing oversized buffers. */
  12135. for (i = 0; i < times; i++) {
  12136. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12137. }
  12138. /* Test bad args. */
  12139. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12140. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12141. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12142. #endif
  12143. return EXPECT_RESULT();
  12144. } /* END test_wc_RipeMdFinal */
  12145. /*
  12146. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12147. * wc_InitSha3_512
  12148. */
  12149. static int test_wc_InitSha3(void)
  12150. {
  12151. EXPECT_DECLS;
  12152. #if defined(WOLFSSL_SHA3)
  12153. wc_Sha3 sha3;
  12154. (void)sha3;
  12155. #if !defined(WOLFSSL_NOSHA3_224)
  12156. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12157. /* Test bad args. */
  12158. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12159. wc_Sha3_224_Free(&sha3);
  12160. #endif /* NOSHA3_224 */
  12161. #if !defined(WOLFSSL_NOSHA3_256)
  12162. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12163. /* Test bad args. */
  12164. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12165. wc_Sha3_256_Free(&sha3);
  12166. #endif /* NOSHA3_256 */
  12167. #if !defined(WOLFSSL_NOSHA3_384)
  12168. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12169. /* Test bad args. */
  12170. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12171. wc_Sha3_384_Free(&sha3);
  12172. #endif /* NOSHA3_384 */
  12173. #if !defined(WOLFSSL_NOSHA3_512)
  12174. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12175. /* Test bad args. */
  12176. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12177. wc_Sha3_512_Free(&sha3);
  12178. #endif /* NOSHA3_512 */
  12179. #endif
  12180. return EXPECT_RESULT();
  12181. } /* END test_wc_InitSha3 */
  12182. /*
  12183. * Testing wc_Sha3_Update()
  12184. */
  12185. static int testing_wc_Sha3_Update(void)
  12186. {
  12187. EXPECT_DECLS;
  12188. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12189. !defined(WOLFSSL_AFALG_XILINX)
  12190. wc_Sha3 sha3;
  12191. byte msg[] = "Everybody's working for the weekend.";
  12192. byte msg2[] = "Everybody gets Friday off.";
  12193. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12194. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12195. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12196. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12197. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12198. word32 msglen = sizeof(msg) - 1;
  12199. word32 msg2len = sizeof(msg2);
  12200. word32 msgCmplen = sizeof(msgCmp);
  12201. #if !defined(WOLFSSL_NOSHA3_224)
  12202. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12203. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12204. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12205. ExpectTrue(sha3.i == msglen);
  12206. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12207. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12208. /* Pass bad args. */
  12209. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12210. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12211. wc_Sha3_224_Free(&sha3);
  12212. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12213. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12214. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12215. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12216. wc_Sha3_224_Free(&sha3);
  12217. #endif /* SHA3_224 */
  12218. #if !defined(WOLFSSL_NOSHA3_256)
  12219. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12221. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12222. ExpectTrue(sha3.i == msglen);
  12223. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12224. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12225. /* Pass bad args. */
  12226. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12227. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12228. wc_Sha3_256_Free(&sha3);
  12229. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12230. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12231. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12232. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12233. wc_Sha3_256_Free(&sha3);
  12234. #endif /* SHA3_256 */
  12235. #if !defined(WOLFSSL_NOSHA3_384)
  12236. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12237. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12238. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12239. ExpectTrue(sha3.i == msglen);
  12240. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12241. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12242. /* Pass bad args. */
  12243. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12245. wc_Sha3_384_Free(&sha3);
  12246. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12247. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12248. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12249. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12250. wc_Sha3_384_Free(&sha3);
  12251. #endif /* SHA3_384 */
  12252. #if !defined(WOLFSSL_NOSHA3_512)
  12253. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12254. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12255. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12256. ExpectTrue(sha3.i == msglen);
  12257. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12258. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12259. /* Pass bad args. */
  12260. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12261. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12262. wc_Sha3_512_Free(&sha3);
  12263. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12264. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12265. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12266. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12267. wc_Sha3_512_Free(&sha3);
  12268. #endif /* SHA3_512 */
  12269. #endif /* WOLFSSL_SHA3 */
  12270. return EXPECT_RESULT();
  12271. } /* END testing_wc_Sha3_Update */
  12272. /*
  12273. * Testing wc_Sha3_224_Final()
  12274. */
  12275. static int test_wc_Sha3_224_Final(void)
  12276. {
  12277. EXPECT_DECLS;
  12278. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12279. wc_Sha3 sha3;
  12280. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12281. "nopnopq";
  12282. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12283. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12284. "\x64\xea\xd0\xfc\xce\x33";
  12285. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12286. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12287. /* Init stack variables. */
  12288. XMEMSET(hash, 0, sizeof(hash));
  12289. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12290. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12291. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12292. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12293. /* Test bad args. */
  12294. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12295. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12296. wc_Sha3_224_Free(&sha3);
  12297. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12298. /* Init stack variables. */
  12299. XMEMSET(hash, 0, sizeof(hash));
  12300. XMEMSET(hashRet, 0, sizeof(hashRet));
  12301. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12302. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12303. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12304. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12305. /* Test bad args. */
  12306. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12307. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12308. wc_Sha3_224_Free(&sha3);
  12309. #endif
  12310. return EXPECT_RESULT();
  12311. } /* END test_wc_Sha3_224_Final */
  12312. /*
  12313. * Testing wc_Sha3_256_Final()
  12314. */
  12315. static int test_wc_Sha3_256_Final(void)
  12316. {
  12317. EXPECT_DECLS;
  12318. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12319. wc_Sha3 sha3;
  12320. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12321. "nopnopq";
  12322. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12323. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12324. "\xdd\x97\x49\x6d\x33\x76";
  12325. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12326. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12327. /* Init stack variables. */
  12328. XMEMSET(hash, 0, sizeof(hash));
  12329. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12330. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12331. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12332. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12333. /* Test bad args. */
  12334. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12335. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12336. wc_Sha3_256_Free(&sha3);
  12337. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12338. /* Init stack variables. */
  12339. XMEMSET(hash, 0, sizeof(hash));
  12340. XMEMSET(hashRet, 0, sizeof(hashRet));
  12341. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12342. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12343. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12344. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12345. /* Test bad args. */
  12346. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12348. wc_Sha3_256_Free(&sha3);
  12349. #endif
  12350. return EXPECT_RESULT();
  12351. } /* END test_wc_Sha3_256_Final */
  12352. /*
  12353. * Testing wc_Sha3_384_Final()
  12354. */
  12355. static int test_wc_Sha3_384_Final(void)
  12356. {
  12357. EXPECT_DECLS;
  12358. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12359. wc_Sha3 sha3;
  12360. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12361. "nopnopq";
  12362. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12363. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12364. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12365. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12366. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12367. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12368. /* Init stack variables. */
  12369. XMEMSET(hash, 0, sizeof(hash));
  12370. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12371. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12372. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12373. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12374. /* Test bad args. */
  12375. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12376. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12377. wc_Sha3_384_Free(&sha3);
  12378. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12379. /* Init stack variables. */
  12380. XMEMSET(hash, 0, sizeof(hash));
  12381. XMEMSET(hashRet, 0, sizeof(hashRet));
  12382. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12383. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12384. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12385. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12386. /* Test bad args. */
  12387. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12388. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12389. wc_Sha3_384_Free(&sha3);
  12390. #endif
  12391. return EXPECT_RESULT();
  12392. } /* END test_wc_Sha3_384_Final */
  12393. /*
  12394. * Testing wc_Sha3_512_Final()
  12395. */
  12396. static int test_wc_Sha3_512_Final(void)
  12397. {
  12398. EXPECT_DECLS;
  12399. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12400. !defined(WOLFSSL_NOSHA3_384)
  12401. wc_Sha3 sha3;
  12402. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12403. "nopnopq";
  12404. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12405. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12406. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12407. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12408. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12409. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12410. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12411. /* Init stack variables. */
  12412. XMEMSET(hash, 0, sizeof(hash));
  12413. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12414. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12415. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12416. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12417. /* Test bad args. */
  12418. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12419. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12420. wc_Sha3_512_Free(&sha3);
  12421. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12422. /* Init stack variables. */
  12423. XMEMSET(hash, 0, sizeof(hash));
  12424. XMEMSET(hashRet, 0, sizeof(hashRet));
  12425. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12426. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12427. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12428. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12429. /* Test bad args. */
  12430. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12431. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12432. wc_Sha3_512_Free(&sha3);
  12433. #endif
  12434. return EXPECT_RESULT();
  12435. } /* END test_wc_Sha3_512_Final */
  12436. /*
  12437. * Testing wc_Sha3_224_Copy()
  12438. */
  12439. static int test_wc_Sha3_224_Copy(void)
  12440. {
  12441. EXPECT_DECLS;
  12442. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12443. wc_Sha3 sha3, sha3Cpy;
  12444. const char* msg = TEST_STRING;
  12445. word32 msglen = (word32)TEST_STRING_SZ;
  12446. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12447. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12448. XMEMSET(hash, 0, sizeof(hash));
  12449. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12450. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12451. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12452. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12453. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12454. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12455. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12456. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12457. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12458. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12459. /* Test bad args. */
  12460. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12461. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12462. wc_Sha3_224_Free(&sha3);
  12463. wc_Sha3_224_Free(&sha3Cpy);
  12464. #endif
  12465. return EXPECT_RESULT();
  12466. } /* END test_wc_Sha3_224_Copy */
  12467. /*
  12468. * Testing wc_Sha3_256_Copy()
  12469. */
  12470. static int test_wc_Sha3_256_Copy(void)
  12471. {
  12472. EXPECT_DECLS;
  12473. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12474. wc_Sha3 sha3, sha3Cpy;
  12475. const char* msg = TEST_STRING;
  12476. word32 msglen = (word32)TEST_STRING_SZ;
  12477. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12478. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12479. XMEMSET(hash, 0, sizeof(hash));
  12480. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12481. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12482. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12483. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12484. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12485. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12486. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12487. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12488. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12489. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12490. /* Test bad args. */
  12491. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12492. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12493. wc_Sha3_256_Free(&sha3);
  12494. wc_Sha3_256_Free(&sha3Cpy);
  12495. #endif
  12496. return EXPECT_RESULT();
  12497. } /* END test_wc_Sha3_256_Copy */
  12498. /*
  12499. * Testing wc_Sha3_384_Copy()
  12500. */
  12501. static int test_wc_Sha3_384_Copy(void)
  12502. {
  12503. EXPECT_DECLS;
  12504. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12505. wc_Sha3 sha3, sha3Cpy;
  12506. const char* msg = TEST_STRING;
  12507. word32 msglen = (word32)TEST_STRING_SZ;
  12508. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12509. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12510. XMEMSET(hash, 0, sizeof(hash));
  12511. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12512. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12513. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12514. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12515. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12516. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12517. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12518. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12519. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12520. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12521. /* Test bad args. */
  12522. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12523. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12524. wc_Sha3_384_Free(&sha3);
  12525. wc_Sha3_384_Free(&sha3Cpy);
  12526. #endif
  12527. return EXPECT_RESULT();
  12528. } /* END test_wc_Sha3_384_Copy */
  12529. /*
  12530. * Testing wc_Sha3_512_Copy()
  12531. */
  12532. static int test_wc_Sha3_512_Copy(void)
  12533. {
  12534. EXPECT_DECLS;
  12535. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12536. wc_Sha3 sha3, sha3Cpy;
  12537. const char* msg = TEST_STRING;
  12538. word32 msglen = (word32)TEST_STRING_SZ;
  12539. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12540. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12541. XMEMSET(hash, 0, sizeof(hash));
  12542. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12543. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12544. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12545. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12546. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12547. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12548. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12549. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12550. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12551. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12552. /* Test bad args. */
  12553. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12554. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12555. wc_Sha3_512_Free(&sha3);
  12556. wc_Sha3_512_Free(&sha3Cpy);
  12557. #endif
  12558. return EXPECT_RESULT();
  12559. } /* END test_wc_Sha3_512_Copy */
  12560. /*
  12561. * Unit test function for wc_Sha3_GetFlags()
  12562. */
  12563. static int test_wc_Sha3_GetFlags(void)
  12564. {
  12565. EXPECT_DECLS;
  12566. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12567. wc_Sha3 sha3;
  12568. word32 flags = 0;
  12569. /* Initialize */
  12570. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12571. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12572. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12573. wc_Sha3_224_Free(&sha3);
  12574. #endif
  12575. return EXPECT_RESULT();
  12576. } /* END test_wc_Sha3_GetFlags */
  12577. static int test_wc_InitShake256(void)
  12578. {
  12579. EXPECT_DECLS;
  12580. #ifdef WOLFSSL_SHAKE256
  12581. wc_Shake shake;
  12582. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12583. /* Test bad args. */
  12584. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12585. wc_Shake256_Free(&shake);
  12586. #endif
  12587. return EXPECT_RESULT();
  12588. }
  12589. static int testing_wc_Shake256_Update(void)
  12590. {
  12591. EXPECT_DECLS;
  12592. #ifdef WOLFSSL_SHAKE256
  12593. wc_Shake shake;
  12594. byte msg[] = "Everybody's working for the weekend.";
  12595. byte msg2[] = "Everybody gets Friday off.";
  12596. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12597. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12598. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12599. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12600. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12601. word32 msglen = sizeof(msg) - 1;
  12602. word32 msg2len = sizeof(msg2);
  12603. word32 msgCmplen = sizeof(msgCmp);
  12604. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12605. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12606. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12607. ExpectTrue(shake.i == msglen);
  12608. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12609. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12610. /* Pass bad args. */
  12611. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12612. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12613. wc_Shake256_Free(&shake);
  12614. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12615. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12616. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12617. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12618. wc_Shake256_Free(&shake);
  12619. #endif /* WOLFSSL_SHAKE256 */
  12620. return EXPECT_RESULT();
  12621. }
  12622. static int test_wc_Shake256_Final(void)
  12623. {
  12624. EXPECT_DECLS;
  12625. #ifdef WOLFSSL_SHAKE256
  12626. wc_Shake shake;
  12627. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12628. "nopnopq";
  12629. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12630. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12631. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12632. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12633. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12634. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12635. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12636. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12637. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12638. byte hash[114];
  12639. /* Init stack variables. */
  12640. XMEMSET(hash, 0, sizeof(hash));
  12641. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12642. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12643. 0);
  12644. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12645. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12646. /* Test bad args. */
  12647. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12648. BAD_FUNC_ARG);
  12649. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12650. BAD_FUNC_ARG);
  12651. wc_Shake256_Free(&shake);
  12652. #endif
  12653. return EXPECT_RESULT();
  12654. }
  12655. /*
  12656. * Testing wc_Shake256_Copy()
  12657. */
  12658. static int test_wc_Shake256_Copy(void)
  12659. {
  12660. EXPECT_DECLS;
  12661. #ifdef WOLFSSL_SHAKE256
  12662. wc_Shake shake, shakeCpy;
  12663. const char* msg = TEST_STRING;
  12664. word32 msglen = (word32)TEST_STRING_SZ;
  12665. byte hash[144];
  12666. byte hashCpy[144];
  12667. word32 hashLen = sizeof(hash);
  12668. word32 hashLenCpy = sizeof(hashCpy);
  12669. XMEMSET(hash, 0, sizeof(hash));
  12670. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12671. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12672. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12673. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12674. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12675. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12676. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12677. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12678. /* Test bad args. */
  12679. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12680. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12681. wc_Shake256_Free(&shake);
  12682. wc_Shake256_Free(&shakeCpy);
  12683. #endif
  12684. return EXPECT_RESULT();
  12685. } /* END test_wc_Shake256_Copy */
  12686. /*
  12687. * Unit test function for wc_Shake256Hash()
  12688. */
  12689. static int test_wc_Shake256Hash(void)
  12690. {
  12691. EXPECT_DECLS;
  12692. #ifdef WOLFSSL_SHAKE256
  12693. const byte data[] = { /* Hello World */
  12694. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12695. 0x72,0x6c,0x64
  12696. };
  12697. word32 len = sizeof(data);
  12698. byte hash[144];
  12699. word32 hashLen = sizeof(hash);
  12700. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12701. #endif
  12702. return EXPECT_RESULT();
  12703. } /* END test_wc_Shake256Hash */
  12704. /*
  12705. * Testing wc_InitSm3(), wc_Sm3Free()
  12706. */
  12707. static int test_wc_InitSm3Free(void)
  12708. {
  12709. EXPECT_DECLS;
  12710. #ifdef WOLFSSL_SM3
  12711. wc_Sm3 sm3;
  12712. /* Invalid Parameters */
  12713. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12714. /* Valid Parameters */
  12715. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12716. wc_Sm3Free(NULL);
  12717. wc_Sm3Free(&sm3);
  12718. #endif
  12719. return EXPECT_RESULT();
  12720. } /* END test_wc_InitSm3 */
  12721. /*
  12722. * Testing wc_Sm3Update(), wc_Sm3Final()
  12723. */
  12724. static int test_wc_Sm3UpdateFinal(void)
  12725. {
  12726. EXPECT_DECLS;
  12727. #ifdef WOLFSSL_SM3
  12728. wc_Sm3 sm3;
  12729. byte data[WC_SM3_BLOCK_SIZE * 4];
  12730. byte hash[WC_SM3_DIGEST_SIZE];
  12731. byte calcHash[WC_SM3_DIGEST_SIZE];
  12732. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12733. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12734. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12735. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12736. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12737. };
  12738. word32 chunk;
  12739. word32 i;
  12740. XMEMSET(data, 0, sizeof(data));
  12741. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12742. /* Invalid Parameters */
  12743. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12744. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12745. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12746. /* Valid Parameters */
  12747. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12748. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12749. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12750. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12751. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12752. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12753. /* Ensure too many bytes for lengths. */
  12754. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12755. /* Invalid Parameters */
  12756. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12757. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12758. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12759. /* Valid Parameters */
  12760. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12761. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12762. /* Chunk tests. */
  12763. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12764. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12765. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12766. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12767. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12768. }
  12769. if (i < (word32)sizeof(data)) {
  12770. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12771. 0);
  12772. }
  12773. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12774. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12775. }
  12776. /* Not testing when the low 32-bit length overflows. */
  12777. wc_Sm3Free(&sm3);
  12778. #endif
  12779. return EXPECT_RESULT();
  12780. } /* END test_wc_Sm3Update */
  12781. /*
  12782. * Testing wc_Sm3GetHash()
  12783. */
  12784. static int test_wc_Sm3GetHash(void)
  12785. {
  12786. EXPECT_DECLS;
  12787. #ifdef WOLFSSL_SM3
  12788. wc_Sm3 sm3;
  12789. byte hash[WC_SM3_DIGEST_SIZE];
  12790. byte calcHash[WC_SM3_DIGEST_SIZE];
  12791. byte data[WC_SM3_BLOCK_SIZE];
  12792. XMEMSET(data, 0, sizeof(data));
  12793. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12794. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12795. /* Invalid Parameters */
  12796. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12797. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12798. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12799. /* Valid Parameters */
  12800. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12801. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12802. /* With update. */
  12803. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12804. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12805. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12806. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12807. wc_Sm3Free(&sm3);
  12808. #endif
  12809. return EXPECT_RESULT();
  12810. } /* END test_wc_Sm3Update */
  12811. /*
  12812. * Testing wc_Sm3Copy()
  12813. */
  12814. static int test_wc_Sm3Copy(void)
  12815. {
  12816. EXPECT_DECLS;
  12817. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12818. wc_Sm3 sm3;
  12819. wc_Sm3 sm3Copy;
  12820. byte hash[WC_SM3_DIGEST_SIZE];
  12821. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12822. byte data[WC_SM3_BLOCK_SIZE + 1];
  12823. int i;
  12824. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12825. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12826. /* Invalid Parameters */
  12827. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  12828. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  12829. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  12830. /* Valid Parameters */
  12831. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12832. /* Ensure all parts of data updated during hashing are copied. */
  12833. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  12834. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  12835. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12836. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12837. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  12838. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12839. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  12840. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  12841. }
  12842. wc_Sm3Free(&sm3Copy);
  12843. wc_Sm3Free(&sm3);
  12844. #endif
  12845. return EXPECT_RESULT();
  12846. } /* END test_wc_Sm3Copy */
  12847. /*
  12848. * Testing wc_Sm3FinalRaw()
  12849. */
  12850. static int test_wc_Sm3FinalRaw(void)
  12851. {
  12852. EXPECT_DECLS;
  12853. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  12854. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12855. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12856. !defined(WOLFSSL_NO_HASH_RAW)
  12857. wc_Sm3 sm3;
  12858. byte hash1[WC_SM3_DIGEST_SIZE];
  12859. byte hash2[WC_SM3_DIGEST_SIZE];
  12860. byte hash3[WC_SM3_DIGEST_SIZE];
  12861. byte* hash_test[3] = { hash1, hash2, hash3 };
  12862. int times;
  12863. int i;
  12864. XMEMSET(&sm3, 0, sizeof(sm3));
  12865. /* Initialize */
  12866. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12867. /* Invalid Parameters */
  12868. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12869. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  12870. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12871. times = sizeof(hash_test) / sizeof(byte*);
  12872. for (i = 0; i < times; i++) {
  12873. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  12874. }
  12875. wc_Sm3Free(&sm3);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_Sm3FinalRaw */
  12879. /*
  12880. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  12881. */
  12882. static int test_wc_Sm3GetSetFlags(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12886. wc_Sm3 sm3;
  12887. wc_Sm3 sm3Copy;
  12888. word32 flags = 0;
  12889. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12890. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12891. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12892. ExpectIntEQ(flags, 0);
  12893. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  12894. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12895. ExpectIntEQ(flags, 0);
  12896. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12897. ExpectIntEQ(flags, 0);
  12898. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  12899. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12900. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  12901. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12902. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  12903. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  12904. wc_Sm3Free(&sm3Copy);
  12905. wc_Sm3Free(&sm3);
  12906. #endif
  12907. return EXPECT_RESULT();
  12908. } /* END test_wc_Sm3Update */
  12909. /*
  12910. * Testing wc_Sm3Hash()
  12911. */
  12912. static int test_wc_Sm3Hash(void)
  12913. {
  12914. EXPECT_DECLS;
  12915. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12916. byte data[WC_SM3_BLOCK_SIZE];
  12917. byte hash[WC_SM3_DIGEST_SIZE];
  12918. /* Invalid parameters. */
  12919. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  12920. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  12921. /* Valid parameters. */
  12922. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  12923. #endif
  12924. return EXPECT_RESULT();
  12925. } /* END test_wc_Sm3Hash */
  12926. /*
  12927. * Test function for wc_HmacSetKey
  12928. */
  12929. static int test_wc_Md5HmacSetKey(void)
  12930. {
  12931. EXPECT_DECLS;
  12932. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12933. Hmac hmac;
  12934. int ret, times, itr;
  12935. const char* keys[]=
  12936. {
  12937. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12938. #ifndef HAVE_FIPS
  12939. "Jefe", /* smaller than minimum FIPS key size */
  12940. #endif
  12941. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12942. };
  12943. times = sizeof(keys) / sizeof(char*);
  12944. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12945. for (itr = 0; itr < times; itr++) {
  12946. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12947. (word32)XSTRLEN(keys[itr]));
  12948. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12949. wc_HmacFree(&hmac);
  12950. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12951. #else
  12952. ExpectIntEQ(ret, 0);
  12953. #endif
  12954. }
  12955. /* Bad args. */
  12956. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12957. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12958. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  12959. BAD_FUNC_ARG);
  12960. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12961. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12962. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12963. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12964. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12965. #elif defined(HAVE_FIPS)
  12966. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12967. #else
  12968. ExpectIntEQ(ret, 0);
  12969. #endif
  12970. wc_HmacFree(&hmac);
  12971. #endif
  12972. return EXPECT_RESULT();
  12973. } /* END test_wc_Md5HmacSetKey */
  12974. /*
  12975. * testing wc_HmacSetKey() on wc_Sha hash.
  12976. */
  12977. static int test_wc_ShaHmacSetKey(void)
  12978. {
  12979. EXPECT_DECLS;
  12980. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12981. Hmac hmac;
  12982. int ret, times, itr;
  12983. const char* keys[]=
  12984. {
  12985. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12986. "\x0b\x0b\x0b",
  12987. #ifndef HAVE_FIPS
  12988. "Jefe", /* smaller than minimum FIPS key size */
  12989. #endif
  12990. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12991. "\xAA\xAA\xAA"
  12992. };
  12993. times = sizeof(keys) / sizeof(char*);
  12994. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12995. for (itr = 0; itr < times; itr++) {
  12996. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12997. (word32)XSTRLEN(keys[itr])), 0);
  12998. }
  12999. /* Bad args. */
  13000. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13001. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13002. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13003. BAD_FUNC_ARG);
  13004. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13005. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13006. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13007. #ifdef HAVE_FIPS
  13008. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13009. #else
  13010. ExpectIntEQ(ret, 0);
  13011. #endif
  13012. wc_HmacFree(&hmac);
  13013. #endif
  13014. return EXPECT_RESULT();
  13015. } /* END test_wc_ShaHmacSetKey() */
  13016. /*
  13017. * testing wc_HmacSetKey() on Sha224 hash.
  13018. */
  13019. static int test_wc_Sha224HmacSetKey(void)
  13020. {
  13021. EXPECT_DECLS;
  13022. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13023. Hmac hmac;
  13024. int ret, times, itr;
  13025. const char* keys[]=
  13026. {
  13027. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13028. "\x0b\x0b\x0b",
  13029. #ifndef HAVE_FIPS
  13030. "Jefe", /* smaller than minimum FIPS key size */
  13031. #endif
  13032. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13033. "\xAA\xAA\xAA"
  13034. };
  13035. times = sizeof(keys) / sizeof(char*);
  13036. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13037. for (itr = 0; itr < times; itr++) {
  13038. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13039. (word32)XSTRLEN(keys[itr])), 0);
  13040. }
  13041. /* Bad args. */
  13042. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13043. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13044. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13045. BAD_FUNC_ARG);
  13046. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13047. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13048. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13049. #ifdef HAVE_FIPS
  13050. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13051. #else
  13052. ExpectIntEQ(ret, 0);
  13053. #endif
  13054. wc_HmacFree(&hmac);
  13055. #endif
  13056. return EXPECT_RESULT();
  13057. } /* END test_wc_Sha224HmacSetKey() */
  13058. /*
  13059. * testing wc_HmacSetKey() on Sha256 hash
  13060. */
  13061. static int test_wc_Sha256HmacSetKey(void)
  13062. {
  13063. EXPECT_DECLS;
  13064. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13065. Hmac hmac;
  13066. int ret, times, itr;
  13067. const char* keys[]=
  13068. {
  13069. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13070. "\x0b\x0b\x0b",
  13071. #ifndef HAVE_FIPS
  13072. "Jefe", /* smaller than minimum FIPS key size */
  13073. #endif
  13074. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13075. "\xAA\xAA\xAA"
  13076. };
  13077. times = sizeof(keys) / sizeof(char*);
  13078. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13079. for (itr = 0; itr < times; itr++) {
  13080. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13081. (word32)XSTRLEN(keys[itr])), 0);
  13082. }
  13083. /* Bad args. */
  13084. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13085. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13086. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13087. BAD_FUNC_ARG);
  13088. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13089. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13090. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13091. #ifdef HAVE_FIPS
  13092. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13093. #else
  13094. ExpectIntEQ(ret, 0);
  13095. #endif
  13096. wc_HmacFree(&hmac);
  13097. #endif
  13098. return EXPECT_RESULT();
  13099. } /* END test_wc_Sha256HmacSetKey() */
  13100. /*
  13101. * testing wc_HmacSetKey on Sha384 hash.
  13102. */
  13103. static int test_wc_Sha384HmacSetKey(void)
  13104. {
  13105. EXPECT_DECLS;
  13106. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13107. Hmac hmac;
  13108. int ret, times, itr;
  13109. const char* keys[]=
  13110. {
  13111. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13112. "\x0b\x0b\x0b",
  13113. #ifndef HAVE_FIPS
  13114. "Jefe", /* smaller than minimum FIPS key size */
  13115. #endif
  13116. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13117. "\xAA\xAA\xAA"
  13118. };
  13119. times = sizeof(keys) / sizeof(char*);
  13120. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13121. for (itr = 0; itr < times; itr++) {
  13122. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13123. (word32)XSTRLEN(keys[itr])), 0);
  13124. }
  13125. /* Bad args. */
  13126. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13127. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13128. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13129. BAD_FUNC_ARG);
  13130. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13131. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13132. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13133. #ifdef HAVE_FIPS
  13134. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13135. #else
  13136. ExpectIntEQ(ret, 0);
  13137. #endif
  13138. wc_HmacFree(&hmac);
  13139. #endif
  13140. return EXPECT_RESULT();
  13141. } /* END test_wc_Sha384HmacSetKey() */
  13142. /*
  13143. * testing wc_HmacUpdate on wc_Md5 hash.
  13144. */
  13145. static int test_wc_Md5HmacUpdate(void)
  13146. {
  13147. EXPECT_DECLS;
  13148. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13149. Hmac hmac;
  13150. testVector a, b;
  13151. #ifdef HAVE_FIPS
  13152. const char* keys =
  13153. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13154. #else
  13155. const char* keys = "Jefe";
  13156. #endif
  13157. a.input = "what do ya want for nothing?";
  13158. a.inLen = XSTRLEN(a.input);
  13159. b.input = "Hi There";
  13160. b.inLen = XSTRLEN(b.input);
  13161. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13162. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13163. (word32)XSTRLEN(keys)), 0);
  13164. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13165. /* Update Hmac. */
  13166. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13167. /* Test bad args. */
  13168. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13169. BAD_FUNC_ARG);
  13170. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13171. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13172. wc_HmacFree(&hmac);
  13173. #endif
  13174. return EXPECT_RESULT();
  13175. } /* END test_wc_Md5HmacUpdate */
  13176. /*
  13177. * testing wc_HmacUpdate on SHA hash.
  13178. */
  13179. static int test_wc_ShaHmacUpdate(void)
  13180. {
  13181. EXPECT_DECLS;
  13182. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13183. Hmac hmac;
  13184. testVector a, b;
  13185. #ifdef HAVE_FIPS
  13186. const char* keys =
  13187. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13188. #else
  13189. const char* keys = "Jefe";
  13190. #endif
  13191. a.input = "what do ya want for nothing?";
  13192. a.inLen = XSTRLEN(a.input);
  13193. b.input = "Hi There";
  13194. b.inLen = XSTRLEN(b.input);
  13195. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13196. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13197. (word32)XSTRLEN(keys)), 0);
  13198. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13199. /* Update Hmac. */
  13200. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13201. /* Test bad args. */
  13202. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13203. BAD_FUNC_ARG);
  13204. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13205. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13206. wc_HmacFree(&hmac);
  13207. #endif
  13208. return EXPECT_RESULT();
  13209. } /* END test_wc_ShaHmacUpdate */
  13210. /*
  13211. * testing wc_HmacUpdate on SHA224 hash.
  13212. */
  13213. static int test_wc_Sha224HmacUpdate(void)
  13214. {
  13215. EXPECT_DECLS;
  13216. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13217. Hmac hmac;
  13218. testVector a, b;
  13219. #ifdef HAVE_FIPS
  13220. const char* keys =
  13221. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13222. #else
  13223. const char* keys = "Jefe";
  13224. #endif
  13225. a.input = "what do ya want for nothing?";
  13226. a.inLen = XSTRLEN(a.input);
  13227. b.input = "Hi There";
  13228. b.inLen = XSTRLEN(b.input);
  13229. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13230. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13231. (word32)XSTRLEN(keys)), 0);
  13232. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13233. /* Update Hmac. */
  13234. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13235. /* Test bad args. */
  13236. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13237. BAD_FUNC_ARG);
  13238. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13239. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13240. wc_HmacFree(&hmac);
  13241. #endif
  13242. return EXPECT_RESULT();
  13243. } /* END test_wc_Sha224HmacUpdate */
  13244. /*
  13245. * testing wc_HmacUpdate on SHA256 hash.
  13246. */
  13247. static int test_wc_Sha256HmacUpdate(void)
  13248. {
  13249. EXPECT_DECLS;
  13250. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13251. Hmac hmac;
  13252. testVector a, b;
  13253. #ifdef HAVE_FIPS
  13254. const char* keys =
  13255. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13256. #else
  13257. const char* keys = "Jefe";
  13258. #endif
  13259. a.input = "what do ya want for nothing?";
  13260. a.inLen = XSTRLEN(a.input);
  13261. b.input = "Hi There";
  13262. b.inLen = XSTRLEN(b.input);
  13263. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13264. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13265. (word32)XSTRLEN(keys)), 0);
  13266. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13267. /* Update Hmac. */
  13268. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13269. /* Test bad args. */
  13270. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13271. BAD_FUNC_ARG);
  13272. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13273. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13274. wc_HmacFree(&hmac);
  13275. #endif
  13276. return EXPECT_RESULT();
  13277. } /* END test_wc_Sha256HmacUpdate */
  13278. /*
  13279. * testing wc_HmacUpdate on SHA384 hash.
  13280. */
  13281. static int test_wc_Sha384HmacUpdate(void)
  13282. {
  13283. EXPECT_DECLS;
  13284. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13285. Hmac hmac;
  13286. testVector a, b;
  13287. #ifdef HAVE_FIPS
  13288. const char* keys =
  13289. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13290. #else
  13291. const char* keys = "Jefe";
  13292. #endif
  13293. a.input = "what do ya want for nothing?";
  13294. a.inLen = XSTRLEN(a.input);
  13295. b.input = "Hi There";
  13296. b.inLen = XSTRLEN(b.input);
  13297. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13298. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13299. (word32)XSTRLEN(keys)), 0);
  13300. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13301. /* Update Hmac. */
  13302. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13303. /* Test bad args. */
  13304. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13305. BAD_FUNC_ARG);
  13306. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13307. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13308. wc_HmacFree(&hmac);
  13309. #endif
  13310. return EXPECT_RESULT();
  13311. } /* END test_wc_Sha384HmacUpdate */
  13312. /*
  13313. * Testing wc_HmacFinal() with MD5
  13314. */
  13315. static int test_wc_Md5HmacFinal(void)
  13316. {
  13317. EXPECT_DECLS;
  13318. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13319. Hmac hmac;
  13320. byte hash[WC_MD5_DIGEST_SIZE];
  13321. testVector a;
  13322. const char* key;
  13323. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13324. a.input = "Hi There";
  13325. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13326. "\x9d";
  13327. a.inLen = XSTRLEN(a.input);
  13328. a.outLen = XSTRLEN(a.output);
  13329. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13330. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13331. 0);
  13332. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13333. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13334. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13335. /* Try bad parameters. */
  13336. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13337. #ifndef HAVE_FIPS
  13338. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13339. #endif
  13340. wc_HmacFree(&hmac);
  13341. #endif
  13342. return EXPECT_RESULT();
  13343. } /* END test_wc_Md5HmacFinal */
  13344. /*
  13345. * Testing wc_HmacFinal() with SHA
  13346. */
  13347. static int test_wc_ShaHmacFinal(void)
  13348. {
  13349. EXPECT_DECLS;
  13350. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13351. Hmac hmac;
  13352. byte hash[WC_SHA_DIGEST_SIZE];
  13353. testVector a;
  13354. const char* key;
  13355. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13356. "\x0b\x0b\x0b";
  13357. a.input = "Hi There";
  13358. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13359. "\x8e\xf1\x46\xbe\x00";
  13360. a.inLen = XSTRLEN(a.input);
  13361. a.outLen = XSTRLEN(a.output);
  13362. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13363. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13364. 0);
  13365. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13366. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13367. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13368. /* Try bad parameters. */
  13369. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13370. #ifndef HAVE_FIPS
  13371. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13372. #endif
  13373. wc_HmacFree(&hmac);
  13374. #endif
  13375. return EXPECT_RESULT();
  13376. } /* END test_wc_ShaHmacFinal */
  13377. /*
  13378. * Testing wc_HmacFinal() with SHA224
  13379. */
  13380. static int test_wc_Sha224HmacFinal(void)
  13381. {
  13382. EXPECT_DECLS;
  13383. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13384. Hmac hmac;
  13385. byte hash[WC_SHA224_DIGEST_SIZE];
  13386. testVector a;
  13387. const char* key;
  13388. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13389. "\x0b\x0b\x0b";
  13390. a.input = "Hi There";
  13391. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13392. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13393. a.inLen = XSTRLEN(a.input);
  13394. a.outLen = XSTRLEN(a.output);
  13395. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13396. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13397. (word32)XSTRLEN(key)), 0);
  13398. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13399. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13400. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13401. /* Try bad parameters. */
  13402. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13403. #ifndef HAVE_FIPS
  13404. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13405. #endif
  13406. wc_HmacFree(&hmac);
  13407. #endif
  13408. return EXPECT_RESULT();
  13409. } /* END test_wc_Sha224HmacFinal */
  13410. /*
  13411. * Testing wc_HmacFinal() with SHA256
  13412. */
  13413. static int test_wc_Sha256HmacFinal(void)
  13414. {
  13415. EXPECT_DECLS;
  13416. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13417. Hmac hmac;
  13418. byte hash[WC_SHA256_DIGEST_SIZE];
  13419. testVector a;
  13420. const char* key;
  13421. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13422. "\x0b\x0b\x0b";
  13423. a.input = "Hi There";
  13424. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13425. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13426. "\xcf\xf7";
  13427. a.inLen = XSTRLEN(a.input);
  13428. a.outLen = XSTRLEN(a.output);
  13429. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13430. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13431. (word32)XSTRLEN(key)), 0);
  13432. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13433. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13434. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13435. /* Try bad parameters. */
  13436. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13437. #ifndef HAVE_FIPS
  13438. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13439. #endif
  13440. wc_HmacFree(&hmac);
  13441. #endif
  13442. return EXPECT_RESULT();
  13443. } /* END test_wc_Sha256HmacFinal */
  13444. /*
  13445. * Testing wc_HmacFinal() with SHA384
  13446. */
  13447. static int test_wc_Sha384HmacFinal(void)
  13448. {
  13449. EXPECT_DECLS;
  13450. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13451. Hmac hmac;
  13452. byte hash[WC_SHA384_DIGEST_SIZE];
  13453. testVector a;
  13454. const char* key;
  13455. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13456. "\x0b\x0b\x0b";
  13457. a.input = "Hi There";
  13458. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13459. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13460. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13461. "\xfa\x9c\xb6";
  13462. a.inLen = XSTRLEN(a.input);
  13463. a.outLen = XSTRLEN(a.output);
  13464. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13465. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13466. (word32)XSTRLEN(key)), 0);
  13467. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13468. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13469. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13470. /* Try bad parameters. */
  13471. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13472. #ifndef HAVE_FIPS
  13473. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13474. #endif
  13475. wc_HmacFree(&hmac);
  13476. #endif
  13477. return EXPECT_RESULT();
  13478. } /* END test_wc_Sha384HmacFinal */
  13479. /*
  13480. * Testing wc_InitCmac()
  13481. */
  13482. static int test_wc_InitCmac(void)
  13483. {
  13484. EXPECT_DECLS;
  13485. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13486. Cmac cmac1;
  13487. Cmac cmac2;
  13488. Cmac cmac3;
  13489. /* AES 128 key. */
  13490. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13491. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13492. /* AES 192 key. */
  13493. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13494. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13495. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13496. /* AES 256 key. */
  13497. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13498. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13499. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13500. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13501. word32 key1Sz = (word32)sizeof(key1) - 1;
  13502. word32 key2Sz = (word32)sizeof(key2) - 1;
  13503. word32 key3Sz = (word32)sizeof(key3) - 1;
  13504. int type = WC_CMAC_AES;
  13505. (void)key1;
  13506. (void)key1Sz;
  13507. (void)key2;
  13508. (void)key2Sz;
  13509. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13510. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13511. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13512. #ifdef WOLFSSL_AES_128
  13513. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13514. #endif
  13515. #ifdef WOLFSSL_AES_192
  13516. wc_AesFree(&cmac1.aes);
  13517. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13518. #endif
  13519. #ifdef WOLFSSL_AES_256
  13520. wc_AesFree(&cmac2.aes);
  13521. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13522. #endif
  13523. wc_AesFree(&cmac3.aes);
  13524. /* Test bad args. */
  13525. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13526. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13527. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13529. #endif
  13530. return EXPECT_RESULT();
  13531. } /* END test_wc_InitCmac */
  13532. /*
  13533. * Testing wc_CmacUpdate()
  13534. */
  13535. static int test_wc_CmacUpdate(void)
  13536. {
  13537. EXPECT_DECLS;
  13538. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13539. Cmac cmac;
  13540. byte key[] = {
  13541. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13542. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13543. };
  13544. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13545. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13546. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13547. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13548. "\xf4";
  13549. word32 inSz = (word32)sizeof(in) - 1;
  13550. word32 keySz = (word32)sizeof(key);
  13551. int type = WC_CMAC_AES;
  13552. XMEMSET(&cmac, 0, sizeof(Cmac));
  13553. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13554. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13555. /* Test bad args. */
  13556. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13557. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13558. wc_AesFree(&cmac.aes);
  13559. #endif
  13560. return EXPECT_RESULT();
  13561. } /* END test_wc_CmacUpdate */
  13562. /*
  13563. * Testing wc_CmacFinal()
  13564. */
  13565. static int test_wc_CmacFinal(void)
  13566. {
  13567. EXPECT_DECLS;
  13568. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13569. Cmac cmac;
  13570. byte key[] = {
  13571. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13572. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13573. };
  13574. byte msg[] = {
  13575. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13576. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13577. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13578. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13579. 0xf4
  13580. };
  13581. /* Test vectors from CMACGenAES128.rsp from
  13582. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13583. * Per RFC4493 truncation of lsb is possible.
  13584. */
  13585. byte expMac[] = {
  13586. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13587. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13588. };
  13589. byte mac[AES_BLOCK_SIZE];
  13590. word32 msgSz = (word32)sizeof(msg);
  13591. word32 keySz = (word32)sizeof(key);
  13592. word32 macSz = sizeof(mac);
  13593. word32 badMacSz = 17;
  13594. int expMacSz = sizeof(expMac);
  13595. int type = WC_CMAC_AES;
  13596. XMEMSET(&cmac, 0, sizeof(Cmac));
  13597. XMEMSET(mac, 0, macSz);
  13598. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13599. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13600. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  13601. /* Pass in bad args. */
  13602. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  13603. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13604. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  13605. /* For the last call, use the API with implicit wc_CmacFree(). */
  13606. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13607. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13608. #else /* !HAVE_FIPS || FIPS>=5.3 */
  13609. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13610. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13611. /* Pass in bad args. */
  13612. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13613. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13614. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13615. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  13616. #endif
  13617. return EXPECT_RESULT();
  13618. } /* END test_wc_CmacFinal */
  13619. /*
  13620. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13621. */
  13622. static int test_wc_AesCmacGenerate(void)
  13623. {
  13624. EXPECT_DECLS;
  13625. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13626. byte key[] = {
  13627. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13628. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13629. };
  13630. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13631. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13632. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13633. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13634. byte mac[AES_BLOCK_SIZE];
  13635. word32 keySz = sizeof(key);
  13636. word32 macSz = sizeof(mac);
  13637. word32 msgSz = sizeof(msg) - 1;
  13638. word32 expMacSz = sizeof(expMac) - 1;
  13639. XMEMSET(mac, 0, macSz);
  13640. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13641. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13642. /* Pass in bad args. */
  13643. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13644. BAD_FUNC_ARG);
  13645. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13646. BAD_FUNC_ARG);
  13647. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13648. BAD_FUNC_ARG);
  13649. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13650. BAD_FUNC_ARG);
  13651. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13652. /* Test bad args. */
  13653. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13654. BAD_FUNC_ARG);
  13655. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13656. BAD_FUNC_ARG);
  13657. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13658. BAD_FUNC_ARG);
  13659. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13660. BAD_FUNC_ARG);
  13661. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13662. BAD_FUNC_ARG);
  13663. #endif
  13664. return EXPECT_RESULT();
  13665. } /* END test_wc_AesCmacGenerate */
  13666. /*
  13667. * Testing streaming AES-GCM API.
  13668. */
  13669. static int test_wc_AesGcmStream(void)
  13670. {
  13671. EXPECT_DECLS;
  13672. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13673. defined(WOLFSSL_AESGCM_STREAM)
  13674. int i;
  13675. WC_RNG rng[1];
  13676. Aes aesEnc[1];
  13677. Aes aesDec[1];
  13678. byte tag[AES_BLOCK_SIZE];
  13679. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13680. byte out[AES_BLOCK_SIZE * 3 + 2];
  13681. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13682. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13683. byte key[AES_128_KEY_SIZE] = { 0, };
  13684. byte iv[AES_IV_SIZE] = { 1, };
  13685. byte ivOut[AES_IV_SIZE];
  13686. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13687. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13688. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13689. };
  13690. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13691. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13692. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13693. };
  13694. static const byte expTag[AES_BLOCK_SIZE] = {
  13695. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13696. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13697. };
  13698. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13699. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13700. XMEMSET(&aesDec, 0, sizeof(Aes));
  13701. /* Create a random for generating IV/nonce. */
  13702. ExpectIntEQ(wc_InitRng(rng), 0);
  13703. /* Initialize data structures. */
  13704. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13705. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13706. /* BadParameters to streaming init. */
  13707. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13708. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13709. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13710. BAD_FUNC_ARG);
  13711. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13712. BAD_FUNC_ARG);
  13713. /* Bad parameters to encrypt update. */
  13714. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13715. BAD_FUNC_ARG);
  13716. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13717. BAD_FUNC_ARG);
  13718. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13719. BAD_FUNC_ARG);
  13720. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13721. BAD_FUNC_ARG);
  13722. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13723. BAD_FUNC_ARG);
  13724. /* Bad parameters to decrypt update. */
  13725. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13726. BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13728. BAD_FUNC_ARG);
  13729. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13730. BAD_FUNC_ARG);
  13731. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13732. BAD_FUNC_ARG);
  13733. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13734. BAD_FUNC_ARG);
  13735. /* Bad parameters to encrypt final. */
  13736. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13737. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13738. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13739. BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13742. BAD_FUNC_ARG);
  13743. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13744. BAD_FUNC_ARG);
  13745. /* Bad parameters to decrypt final. */
  13746. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13747. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13748. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13749. BAD_FUNC_ARG);
  13750. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13751. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13752. BAD_FUNC_ARG);
  13753. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13754. BAD_FUNC_ARG);
  13755. /* Check calling final before setting key fails. */
  13756. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13757. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13758. /* Check calling update before setting key else fails. */
  13759. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13760. MISSING_KEY);
  13761. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13762. MISSING_KEY);
  13763. /* Set key but not IV. */
  13764. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13765. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13766. /* Check calling final before setting IV fails. */
  13767. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13768. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13769. /* Check calling update before setting IV else fails. */
  13770. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13771. MISSING_IV);
  13772. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13773. MISSING_IV);
  13774. /* Set IV using fixed part IV and external IV APIs. */
  13775. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13776. rng), 0);
  13777. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13778. GCM_NONCE_MID_SZ), 0);
  13779. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13780. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13781. /* Encrypt and decrypt data. */
  13782. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13783. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13784. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13785. /* Finalize and check tag matches. */
  13786. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13787. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13788. /* Set key and IV through streaming init API. */
  13789. wc_AesFree(aesEnc);
  13790. wc_AesFree(aesDec);
  13791. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13792. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13793. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13794. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13795. /* Encrypt/decrypt one block and AAD of one block. */
  13796. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13797. AES_BLOCK_SIZE), 0);
  13798. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13799. AES_BLOCK_SIZE), 0);
  13800. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13801. /* Finalize and check tag matches. */
  13802. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13803. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13804. /* Set key and IV through streaming init API. */
  13805. wc_AesFree(aesEnc);
  13806. wc_AesFree(aesDec);
  13807. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13808. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13809. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13810. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13811. /* No data to encrypt/decrypt one byte of AAD. */
  13812. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13813. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13814. /* Finalize and check tag matches. */
  13815. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13816. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13817. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13818. /* Set key and IV through streaming init API. */
  13819. wc_AesFree(aesEnc);
  13820. wc_AesFree(aesDec);
  13821. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13822. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13823. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13824. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13825. /* Encrypt/decrypt one byte and no AAD. */
  13826. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13827. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13828. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13829. /* Finalize and check tag matches. */
  13830. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13831. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13832. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13833. /* Set key and IV through streaming init API. */
  13834. wc_AesFree(aesEnc);
  13835. wc_AesFree(aesDec);
  13836. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13837. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13838. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13839. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13840. /* Encryption AES is one byte at a time */
  13841. for (i = 0; i < (int)sizeof(aad); i++) {
  13842. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13843. 0);
  13844. }
  13845. for (i = 0; i < (int)sizeof(in); i++) {
  13846. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13847. 0);
  13848. }
  13849. /* Decryption AES is two bytes at a time */
  13850. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13851. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13852. 0);
  13853. }
  13854. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13855. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13856. 0), 0);
  13857. }
  13858. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13859. /* Finalize and check tag matches. */
  13860. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13861. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13862. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13863. /* Check streaming encryption can be decrypted with one shot. */
  13864. wc_AesFree(aesDec);
  13865. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13866. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13867. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13868. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13869. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13870. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13871. wc_AesFree(aesEnc);
  13872. wc_AesFree(aesDec);
  13873. wc_FreeRng(rng);
  13874. #endif
  13875. return EXPECT_RESULT();
  13876. } /* END test_wc_AesGcmStream */
  13877. /*
  13878. * Testing streaming SM4 API.
  13879. */
  13880. static int test_wc_Sm4(void)
  13881. {
  13882. int res = TEST_SKIPPED;
  13883. #ifdef WOLFSSL_SM4
  13884. EXPECT_DECLS;
  13885. wc_Sm4 sm4;
  13886. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13887. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13888. unsigned char key[SM4_KEY_SIZE];
  13889. #endif
  13890. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13891. unsigned char iv[SM4_IV_SIZE];
  13892. #endif
  13893. /* Invalid parameters - wc_Sm4Init */
  13894. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13895. /* Valid cases - wc_Sm4Init */
  13896. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13897. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13898. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13899. XMEMSET(key, 0, sizeof(key));
  13900. /* Invalid parameters - wc_Sm4SetKey. */
  13901. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  13902. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  13903. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  13904. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13905. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  13906. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13907. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13908. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  13909. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  13910. /* Valid cases - wc_Sm4SetKey. */
  13911. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13912. #endif
  13913. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13914. XMEMSET(iv, 0, sizeof(iv));
  13915. /* Invalid parameters - wc_Sm4SetIV. */
  13916. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  13917. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  13918. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  13919. /* Valid cases - wc_Sm4SetIV. */
  13920. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13921. #endif
  13922. /* Valid cases - wc_Sm4Free */
  13923. wc_Sm4Free(NULL);
  13924. wc_Sm4Free(&sm4);
  13925. res = EXPECT_RESULT();
  13926. #endif
  13927. return res;
  13928. } /* END test_wc_Sm4 */
  13929. /*
  13930. * Testing block based SM4-ECB API.
  13931. */
  13932. static int test_wc_Sm4Ecb(void)
  13933. {
  13934. int res = TEST_SKIPPED;
  13935. #ifdef WOLFSSL_SM4_ECB
  13936. EXPECT_DECLS;
  13937. wc_Sm4 sm4;
  13938. unsigned char key[SM4_KEY_SIZE];
  13939. unsigned char in[SM4_BLOCK_SIZE * 2];
  13940. unsigned char out[SM4_BLOCK_SIZE * 2];
  13941. unsigned char out2[SM4_BLOCK_SIZE];
  13942. XMEMSET(key, 0, sizeof(key));
  13943. XMEMSET(in, 0, sizeof(in));
  13944. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13945. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13946. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13947. /* Tested in test_wc_Sm4. */
  13948. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13949. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  13950. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13951. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13952. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13953. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13954. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13955. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13956. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13957. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13958. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13959. /* Valid cases - wc_Sm4EcbEncrypt. */
  13960. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  13961. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13962. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13963. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13964. /* In and out are same pointer. */
  13965. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13966. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13967. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  13968. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13969. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13970. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13971. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13972. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13973. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13974. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13975. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13976. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13977. /* Valid cases - wc_Sm4EcbDecrypt. */
  13978. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  13979. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13980. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13981. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13982. /* In and out are same pointer. */
  13983. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13984. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13985. wc_Sm4Free(&sm4);
  13986. res = EXPECT_RESULT();
  13987. #endif
  13988. return res;
  13989. } /* END test_wc_Sm4Ecb */
  13990. /*
  13991. * Testing block based SM4-CBC API.
  13992. */
  13993. static int test_wc_Sm4Cbc(void)
  13994. {
  13995. int res = TEST_SKIPPED;
  13996. #ifdef WOLFSSL_SM4_CBC
  13997. EXPECT_DECLS;
  13998. wc_Sm4 sm4;
  13999. unsigned char key[SM4_KEY_SIZE];
  14000. unsigned char iv[SM4_IV_SIZE];
  14001. unsigned char in[SM4_BLOCK_SIZE * 2];
  14002. unsigned char out[SM4_BLOCK_SIZE * 2];
  14003. unsigned char out2[SM4_BLOCK_SIZE];
  14004. XMEMSET(key, 0, sizeof(key));
  14005. XMEMSET(iv, 0, sizeof(iv));
  14006. XMEMSET(in, 0, sizeof(in));
  14007. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14008. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14009. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14010. /* Tested in test_wc_Sm4. */
  14011. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14012. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14013. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14014. /* Tested in test_wc_Sm4. */
  14015. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14016. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14017. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14018. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14019. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14020. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14021. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14022. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14023. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14024. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14025. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14026. /* Valid cases - wc_Sm4CbcEncrypt. */
  14027. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14028. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14029. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14030. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14031. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14032. /* In and out are same pointer. */
  14033. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14034. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14035. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14036. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14037. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14038. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14039. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14040. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14041. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14042. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14043. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14044. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14045. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14046. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14047. /* Valid cases - wc_Sm4CbcDecrypt. */
  14048. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14049. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14050. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14051. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14052. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14053. /* In and out are same pointer. */
  14054. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14055. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14056. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14057. wc_Sm4Free(&sm4);
  14058. res = EXPECT_RESULT();
  14059. #endif
  14060. return res;
  14061. } /* END test_wc_Sm4Cbc */
  14062. /*
  14063. * Testing streaming SM4-CTR API.
  14064. */
  14065. static int test_wc_Sm4Ctr(void)
  14066. {
  14067. int res = TEST_SKIPPED;
  14068. #ifdef WOLFSSL_SM4_CTR
  14069. EXPECT_DECLS;
  14070. wc_Sm4 sm4;
  14071. unsigned char key[SM4_KEY_SIZE];
  14072. unsigned char iv[SM4_IV_SIZE];
  14073. unsigned char in[SM4_BLOCK_SIZE * 4];
  14074. unsigned char out[SM4_BLOCK_SIZE * 4];
  14075. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14076. word32 chunk;
  14077. word32 i;
  14078. XMEMSET(key, 0, sizeof(key));
  14079. XMEMSET(iv, 0, sizeof(iv));
  14080. XMEMSET(in, 0, sizeof(in));
  14081. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14082. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14083. /* Tested in test_wc_Sm4. */
  14084. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14085. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14086. /* Tested in test_wc_Sm4. */
  14087. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14088. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14089. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14090. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14091. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14092. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14093. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14094. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14095. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14096. /* Valid cases - wc_Sm4CtrEncrypt. */
  14097. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14098. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14099. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14100. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14101. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14102. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14103. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14104. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14105. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14106. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14107. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14108. /* In and out are same pointer. Also check encrypt of cipher text produces
  14109. * plaintext.
  14110. */
  14111. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14112. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14113. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14114. /* Chunking tests. */
  14115. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14116. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14117. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14118. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14119. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14120. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14121. }
  14122. if (i < (word32)sizeof(in)) {
  14123. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14124. (word32)sizeof(in) - i), 0);
  14125. }
  14126. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14127. }
  14128. for (i = 0; i < (word32)sizeof(iv); i++) {
  14129. iv[i] = 0xff;
  14130. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14131. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14132. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14133. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14134. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14135. }
  14136. wc_Sm4Free(&sm4);
  14137. res = EXPECT_RESULT();
  14138. #endif
  14139. return res;
  14140. } /* END test_wc_Sm4Ctr */
  14141. /*
  14142. * Testing stream SM4-GCM API.
  14143. */
  14144. static int test_wc_Sm4Gcm(void)
  14145. {
  14146. int res = TEST_SKIPPED;
  14147. #ifdef WOLFSSL_SM4_GCM
  14148. EXPECT_DECLS;
  14149. wc_Sm4 sm4;
  14150. unsigned char key[SM4_KEY_SIZE];
  14151. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14152. unsigned char in[SM4_BLOCK_SIZE * 2];
  14153. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14154. unsigned char out[SM4_BLOCK_SIZE * 2];
  14155. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14156. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14157. unsigned char tag[SM4_BLOCK_SIZE];
  14158. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14159. word32 i;
  14160. XMEMSET(key, 0, sizeof(key));
  14161. XMEMSET(nonce, 0, sizeof(nonce));
  14162. XMEMSET(in, 0, sizeof(in));
  14163. XMEMSET(in2, 0, sizeof(in2));
  14164. XMEMSET(aad, 0, sizeof(aad));
  14165. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14167. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14168. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14169. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14170. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14171. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14172. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14173. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14174. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14175. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14176. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14177. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14178. /* Valid parameters - wc_Sm4GcmSetKey. */
  14179. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14180. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14181. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14182. 0), BAD_FUNC_ARG);
  14183. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14184. 0), BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14186. 0), BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14188. 0), BAD_FUNC_ARG);
  14189. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14190. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14191. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14192. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14193. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14194. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14195. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14196. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14198. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14199. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14200. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14201. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14202. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14203. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14204. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14205. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14206. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14207. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14208. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14209. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14210. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14211. 0), BAD_FUNC_ARG);
  14212. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14213. 0), BAD_FUNC_ARG);
  14214. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14215. 0), BAD_FUNC_ARG);
  14216. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14217. 0), BAD_FUNC_ARG);
  14218. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14219. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14220. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14221. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14223. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14224. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14225. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14226. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14227. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14228. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14229. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14230. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14231. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14232. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14233. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14234. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14235. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14237. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14238. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14239. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14240. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14241. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14242. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14243. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14244. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14245. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14246. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14247. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14248. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14249. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14250. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14251. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14252. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14253. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14254. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14255. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14256. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14258. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14259. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14260. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14261. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14262. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14263. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14264. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14265. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14266. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14267. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14268. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14269. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14270. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14271. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14272. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14273. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14274. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14275. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14276. SM4_GCM_AUTH_E);
  14277. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14278. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14279. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14280. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14281. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14282. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14283. }
  14284. /* Check different in/out sizes. */
  14285. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14286. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14287. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14288. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14289. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14290. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14291. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14292. XMEMCPY(out2, out, i - 1);
  14293. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14294. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14295. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14296. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14297. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14298. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14299. }
  14300. /* Force the counter to roll over in first byte. */
  14301. {
  14302. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14303. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14304. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14305. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14306. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14307. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14308. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14309. }
  14310. wc_Sm4Free(&sm4);
  14311. res = EXPECT_RESULT();
  14312. #endif
  14313. return res;
  14314. } /* END test_wc_Sm4Gcm */
  14315. /*
  14316. * Testing stream SM4-CCM API.
  14317. */
  14318. static int test_wc_Sm4Ccm(void)
  14319. {
  14320. int res = TEST_SKIPPED;
  14321. #ifdef WOLFSSL_SM4_CCM
  14322. EXPECT_DECLS;
  14323. wc_Sm4 sm4;
  14324. unsigned char key[SM4_KEY_SIZE];
  14325. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14326. unsigned char in[SM4_BLOCK_SIZE * 2];
  14327. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14328. unsigned char out[SM4_BLOCK_SIZE * 2];
  14329. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14330. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14331. unsigned char tag[SM4_BLOCK_SIZE];
  14332. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14333. word32 i;
  14334. XMEMSET(key, 0, sizeof(key));
  14335. XMEMSET(nonce, 0, sizeof(nonce));
  14336. XMEMSET(in, 0, sizeof(in));
  14337. XMEMSET(in2, 0, sizeof(in2));
  14338. XMEMSET(aad, 0, sizeof(aad));
  14339. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14340. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14341. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14342. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14343. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14344. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14345. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14346. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14347. 0), BAD_FUNC_ARG);
  14348. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14349. 0), BAD_FUNC_ARG);
  14350. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14351. 0), BAD_FUNC_ARG);
  14352. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14353. 0), BAD_FUNC_ARG);
  14354. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14355. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14356. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14357. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14358. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14359. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14360. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14361. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14362. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14363. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14364. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14365. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14366. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14367. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14368. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14369. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14370. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14371. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14372. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14373. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14374. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14375. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14376. 0), BAD_FUNC_ARG);
  14377. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14378. 0), BAD_FUNC_ARG);
  14379. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14380. 0), BAD_FUNC_ARG);
  14381. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14382. 0), BAD_FUNC_ARG);
  14383. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14384. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14385. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14386. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14387. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14388. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14389. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14390. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14391. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14392. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14394. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14395. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14396. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14397. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14398. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14399. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14400. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14402. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14403. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14404. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14405. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14406. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14407. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14408. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14409. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14410. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14411. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14412. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14413. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14414. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14415. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14416. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14417. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14418. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14419. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14420. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14421. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14422. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14423. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14424. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14425. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14426. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14427. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14428. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14429. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14430. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14431. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14432. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14433. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14435. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14436. }
  14437. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14438. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14439. SM4_CCM_AUTH_E);
  14440. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14441. for (i = 0; i < 4; i++) {
  14442. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14443. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14444. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14445. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14446. }
  14447. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14448. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14449. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14450. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14451. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14452. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14453. }
  14454. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14455. * Even values in range 4..SM4_BLOCK_SIZE.
  14456. */
  14457. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14458. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14459. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14460. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14461. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14462. }
  14463. /* Check different in/out sizes. */
  14464. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14465. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14466. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14467. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14468. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14469. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14470. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14471. XMEMCPY(out2, out, i - 1);
  14472. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14473. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14474. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14475. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14476. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14477. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14478. }
  14479. /* Force the counter to roll over in first byte. */
  14480. {
  14481. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14482. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14483. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14484. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14485. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14486. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14487. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14488. }
  14489. wc_Sm4Free(&sm4);
  14490. res = EXPECT_RESULT();
  14491. #endif
  14492. return res;
  14493. } /* END test_wc_Sm4Ccm */
  14494. /*
  14495. * unit test for wc_Des3_SetIV()
  14496. */
  14497. static int test_wc_Des3_SetIV(void)
  14498. {
  14499. EXPECT_DECLS;
  14500. #ifndef NO_DES3
  14501. Des3 des;
  14502. const byte key[] = {
  14503. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14504. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14505. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14506. };
  14507. const byte iv[] = {
  14508. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14509. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14510. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14511. };
  14512. XMEMSET(&des, 0, sizeof(Des3));
  14513. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14514. /* DES_ENCRYPTION or DES_DECRYPTION */
  14515. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14516. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14517. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14518. /* Test explicitly wc_Des3_SetIV() */
  14519. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14520. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14521. #endif
  14522. wc_Des3Free(&des);
  14523. #endif
  14524. return EXPECT_RESULT();
  14525. } /* END test_wc_Des3_SetIV */
  14526. /*
  14527. * unit test for wc_Des3_SetKey()
  14528. */
  14529. static int test_wc_Des3_SetKey(void)
  14530. {
  14531. EXPECT_DECLS;
  14532. #ifndef NO_DES3
  14533. Des3 des;
  14534. const byte key[] = {
  14535. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14536. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14537. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14538. };
  14539. const byte iv[] = {
  14540. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14541. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14542. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14543. };
  14544. XMEMSET(&des, 0, sizeof(Des3));
  14545. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14546. /* DES_ENCRYPTION or DES_DECRYPTION */
  14547. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14548. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14549. /* Test bad args. */
  14550. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14551. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14552. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14553. /* Default case. Should return 0. */
  14554. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14555. wc_Des3Free(&des);
  14556. #endif
  14557. return EXPECT_RESULT();
  14558. } /* END test_wc_Des3_SetKey */
  14559. /*
  14560. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14561. */
  14562. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14563. {
  14564. EXPECT_DECLS;
  14565. #ifndef NO_DES3
  14566. Des3 des;
  14567. byte cipher[24];
  14568. byte plain[24];
  14569. const byte key[] = {
  14570. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14571. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14572. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14573. };
  14574. const byte iv[] = {
  14575. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14576. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14577. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14578. };
  14579. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14580. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14581. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14582. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14583. };
  14584. XMEMSET(&des, 0, sizeof(Des3));
  14585. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14586. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14587. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14588. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14589. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14590. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14591. /* Pass in bad args. */
  14592. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14593. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14594. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14595. BAD_FUNC_ARG);
  14596. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14598. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14599. wc_Des3Free(&des);
  14600. #endif
  14601. return EXPECT_RESULT();
  14602. } /* END wc_Des3_CbcEncrypt */
  14603. /*
  14604. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14605. */
  14606. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14607. {
  14608. EXPECT_DECLS;
  14609. #ifndef NO_DES3
  14610. word32 vectorSz, cipherSz;
  14611. byte cipher[24];
  14612. byte plain[24];
  14613. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14614. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14615. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14616. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14617. };
  14618. byte key[] = {
  14619. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14620. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14621. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14622. };
  14623. byte iv[] = {
  14624. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14625. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14626. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14627. };
  14628. vectorSz = sizeof(byte) * 24;
  14629. cipherSz = sizeof(byte) * 24;
  14630. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14631. 0);
  14632. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14633. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14634. /* pass in bad args. */
  14635. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14636. BAD_FUNC_ARG);
  14637. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14638. BAD_FUNC_ARG);
  14639. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14640. BAD_FUNC_ARG);
  14641. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14642. 0);
  14643. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14644. BAD_FUNC_ARG);
  14645. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14646. BAD_FUNC_ARG);
  14647. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14648. BAD_FUNC_ARG);
  14649. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14650. 0);
  14651. #endif
  14652. return EXPECT_RESULT();
  14653. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14654. /*
  14655. * Unit test for wc_Des3_EcbEncrypt
  14656. */
  14657. static int test_wc_Des3_EcbEncrypt(void)
  14658. {
  14659. EXPECT_DECLS;
  14660. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14661. Des3 des;
  14662. byte cipher[24];
  14663. word32 cipherSz = sizeof(cipher);
  14664. const byte key[] = {
  14665. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14666. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14667. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14668. };
  14669. const byte iv[] = {
  14670. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14671. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14672. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14673. };
  14674. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14675. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14676. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14677. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14678. };
  14679. XMEMSET(&des, 0, sizeof(Des3));
  14680. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14681. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14682. /* Bad Cases */
  14683. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14684. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14685. BAD_FUNC_ARG);
  14686. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14687. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14688. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14689. /* Good Cases */
  14690. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14691. wc_Des3Free(&des);
  14692. #endif
  14693. return EXPECT_RESULT();
  14694. } /* END test_wc_Des3_EcbEncrypt */
  14695. /*
  14696. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14697. */
  14698. static int test_wc_Chacha_SetKey(void)
  14699. {
  14700. EXPECT_DECLS;
  14701. #ifdef HAVE_CHACHA
  14702. ChaCha ctx;
  14703. const byte key[] = {
  14704. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14705. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14706. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14707. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14708. };
  14709. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14710. byte cipher[128];
  14711. XMEMSET(cipher, 0, sizeof(cipher));
  14712. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14713. /* Test bad args. */
  14714. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14715. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14716. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14717. /* Test bad args. */
  14718. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14719. #endif
  14720. return EXPECT_RESULT();
  14721. } /* END test_wc_Chacha_SetKey */
  14722. /*
  14723. * unit test for wc_Poly1305SetKey()
  14724. */
  14725. static int test_wc_Poly1305SetKey(void)
  14726. {
  14727. EXPECT_DECLS;
  14728. #ifdef HAVE_POLY1305
  14729. Poly1305 ctx;
  14730. const byte key[] =
  14731. {
  14732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14733. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14734. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14735. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14736. };
  14737. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14738. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14739. /* Test bad args. */
  14740. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14741. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14742. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14743. #endif
  14744. return EXPECT_RESULT();
  14745. } /* END test_wc_Poly1305_SetKey() */
  14746. /*
  14747. * Testing wc_Chacha_Process()
  14748. */
  14749. static int test_wc_Chacha_Process(void)
  14750. {
  14751. EXPECT_DECLS;
  14752. #ifdef HAVE_CHACHA
  14753. ChaCha enc, dec;
  14754. byte cipher[128];
  14755. byte plain[128];
  14756. const byte key[] =
  14757. {
  14758. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14759. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14760. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14761. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14762. };
  14763. const char* input = "Everybody gets Friday off.";
  14764. word32 keySz = sizeof(key)/sizeof(byte);
  14765. unsigned long int inlen = XSTRLEN(input);
  14766. /* Initialize stack variables. */
  14767. XMEMSET(cipher, 0, 128);
  14768. XMEMSET(plain, 0, 128);
  14769. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14770. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14771. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14772. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14773. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14774. 0);
  14775. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14776. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14777. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14778. /* test checking and using leftovers, currently just in C code */
  14779. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14780. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14781. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14782. (word32)inlen - 2), 0);
  14783. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14784. (byte*)input + (inlen - 2), 2), 0);
  14785. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14786. (word32)inlen - 2), 0);
  14787. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14788. (byte*)input + (inlen - 2), 2), 0);
  14789. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14790. /* check edge cases with counter increment */
  14791. {
  14792. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14793. const byte expected[] = {
  14794. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14795. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14796. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14797. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14798. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14799. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14800. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14801. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14802. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14803. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14804. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14805. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14806. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14807. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14808. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14809. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14810. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14811. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14812. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14813. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14814. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14815. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14816. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14817. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14818. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14819. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14820. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14821. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14822. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14823. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14824. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14825. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14826. };
  14827. const byte iv2[] = {
  14828. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14829. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14830. };
  14831. byte input2[256];
  14832. int i;
  14833. for (i = 0; i < 256; i++)
  14834. input2[i] = i;
  14835. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  14836. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  14837. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14838. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  14839. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14840. /* partial */
  14841. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  14842. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14843. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  14844. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14845. }
  14846. #endif
  14847. /* Test bad args. */
  14848. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  14849. BAD_FUNC_ARG);
  14850. #endif
  14851. return EXPECT_RESULT();
  14852. } /* END test_wc_Chacha_Process */
  14853. /*
  14854. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14855. */
  14856. static int test_wc_ChaCha20Poly1305_aead(void)
  14857. {
  14858. EXPECT_DECLS;
  14859. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14860. const byte key[] = {
  14861. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14862. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14863. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14864. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14865. };
  14866. const byte plaintext[] = {
  14867. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14868. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14869. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14870. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14871. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14872. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14873. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14874. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14875. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14876. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14877. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14878. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14879. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14880. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14881. 0x74, 0x2e
  14882. };
  14883. const byte iv[] = {
  14884. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14885. 0x44, 0x45, 0x46, 0x47
  14886. };
  14887. const byte aad[] = { /* additional data */
  14888. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14889. 0xc4, 0xc5, 0xc6, 0xc7
  14890. };
  14891. const byte cipher[] = { /* expected output from operation */
  14892. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14893. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14894. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14895. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14896. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14897. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14898. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14899. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14900. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14901. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14902. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14903. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14904. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14905. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14906. 0x61, 0x16
  14907. };
  14908. const byte authTag[] = { /* expected output from operation */
  14909. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14910. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14911. };
  14912. byte generatedCiphertext[272];
  14913. byte generatedPlaintext[272];
  14914. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14915. /* Initialize stack variables. */
  14916. XMEMSET(generatedCiphertext, 0, 272);
  14917. XMEMSET(generatedPlaintext, 0, 272);
  14918. /* Test Encrypt */
  14919. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14920. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14921. 0);
  14922. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  14923. sizeof(cipher)/sizeof(byte)), 0);
  14924. /* Test bad args. */
  14925. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  14926. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14927. BAD_FUNC_ARG);
  14928. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14929. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14930. BAD_FUNC_ARG);
  14931. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14932. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14933. BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14935. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14936. BAD_FUNC_ARG);
  14937. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14938. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  14939. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14940. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  14941. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14942. sizeof(cipher), authTag, generatedPlaintext), 0);
  14943. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  14944. sizeof(plaintext)/sizeof(byte)), 0);
  14945. /* Test bad args. */
  14946. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14947. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14948. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14949. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14950. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14951. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14952. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14953. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  14954. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14955. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  14956. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14957. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14958. #endif
  14959. return EXPECT_RESULT();
  14960. } /* END test_wc_ChaCha20Poly1305_aead */
  14961. /*
  14962. * Testing function for wc_Rc2SetKey().
  14963. */
  14964. static int test_wc_Rc2SetKey(void)
  14965. {
  14966. EXPECT_DECLS;
  14967. #ifdef WC_RC2
  14968. Rc2 rc2;
  14969. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14970. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14971. /* valid key and IV */
  14972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14973. iv, 40), 0);
  14974. /* valid key, no IV */
  14975. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14976. NULL, 40), 0);
  14977. /* bad arguments */
  14978. /* null Rc2 struct */
  14979. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14980. iv, 40), BAD_FUNC_ARG);
  14981. /* null key */
  14982. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14983. iv, 40), BAD_FUNC_ARG);
  14984. /* key size == 0 */
  14985. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  14986. /* key size > 128 */
  14987. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  14988. /* effective bits == 0 */
  14989. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14990. iv, 0), WC_KEY_SIZE_E);
  14991. /* effective bits > 1024 */
  14992. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14993. iv, 1025), WC_KEY_SIZE_E);
  14994. #endif
  14995. return EXPECT_RESULT();
  14996. } /* END test_wc_Rc2SetKey */
  14997. /*
  14998. * Testing function for wc_Rc2SetIV().
  14999. */
  15000. static int test_wc_Rc2SetIV(void)
  15001. {
  15002. EXPECT_DECLS;
  15003. #ifdef WC_RC2
  15004. Rc2 rc2;
  15005. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15006. /* valid IV */
  15007. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15008. /* valid NULL IV */
  15009. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15010. /* bad arguments */
  15011. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15012. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15013. #endif
  15014. return EXPECT_RESULT();
  15015. } /* END test_wc_Rc2SetIV */
  15016. /*
  15017. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15018. */
  15019. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15020. {
  15021. EXPECT_DECLS;
  15022. #ifdef WC_RC2
  15023. Rc2 rc2;
  15024. int effectiveKeyBits = 63;
  15025. byte cipher[RC2_BLOCK_SIZE];
  15026. byte plain[RC2_BLOCK_SIZE];
  15027. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15028. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15029. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15030. XMEMSET(cipher, 0, sizeof(cipher));
  15031. XMEMSET(plain, 0, sizeof(plain));
  15032. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15033. NULL, effectiveKeyBits), 0);
  15034. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15035. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15036. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15037. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15038. /* Rc2EcbEncrypt bad arguments */
  15039. /* null Rc2 struct */
  15040. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15041. BAD_FUNC_ARG);
  15042. /* null out buffer */
  15043. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15044. BAD_FUNC_ARG);
  15045. /* null input buffer */
  15046. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15047. BAD_FUNC_ARG);
  15048. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15049. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15050. /* Rc2EcbDecrypt bad arguments */
  15051. /* null Rc2 struct */
  15052. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15053. BAD_FUNC_ARG);
  15054. /* null out buffer */
  15055. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15056. BAD_FUNC_ARG);
  15057. /* null input buffer */
  15058. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15059. BAD_FUNC_ARG);
  15060. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15061. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15062. #endif
  15063. return EXPECT_RESULT();
  15064. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15065. /*
  15066. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15067. */
  15068. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15069. {
  15070. EXPECT_DECLS;
  15071. #ifdef WC_RC2
  15072. Rc2 rc2;
  15073. int effectiveKeyBits = 63;
  15074. byte cipher[RC2_BLOCK_SIZE*2];
  15075. byte plain[RC2_BLOCK_SIZE*2];
  15076. /* vector taken from test.c */
  15077. byte key[] = {
  15078. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15079. };
  15080. byte iv[] = {
  15081. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15082. };
  15083. byte input[] = {
  15084. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15085. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15086. };
  15087. byte output[] = {
  15088. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15089. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15090. };
  15091. XMEMSET(cipher, 0, sizeof(cipher));
  15092. XMEMSET(plain, 0, sizeof(plain));
  15093. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15094. iv, effectiveKeyBits), 0);
  15095. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15096. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15097. /* reset IV for decrypt */
  15098. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15099. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15100. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15101. /* Rc2CbcEncrypt bad arguments */
  15102. /* null Rc2 struct */
  15103. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15104. BAD_FUNC_ARG);
  15105. /* null out buffer */
  15106. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15107. BAD_FUNC_ARG);
  15108. /* null input buffer */
  15109. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15110. BAD_FUNC_ARG);
  15111. /* Rc2CbcDecrypt bad arguments */
  15112. /* in size is 0 */
  15113. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15114. /* null Rc2 struct */
  15115. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15116. BAD_FUNC_ARG);
  15117. /* null out buffer */
  15118. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15119. BAD_FUNC_ARG);
  15120. /* null input buffer */
  15121. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15122. BAD_FUNC_ARG);
  15123. #endif
  15124. return EXPECT_RESULT();
  15125. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15126. /*
  15127. * Testing function for wc_AesSetIV
  15128. */
  15129. static int test_wc_AesSetIV(void)
  15130. {
  15131. int res = TEST_SKIPPED;
  15132. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15133. Aes aes;
  15134. int ret = 0;
  15135. byte key16[] =
  15136. {
  15137. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15138. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15139. };
  15140. byte iv1[] = "1234567890abcdef";
  15141. byte iv2[] = "0987654321fedcba";
  15142. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15143. if (ret != 0)
  15144. return ret;
  15145. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15146. iv1, AES_ENCRYPTION);
  15147. if (ret == 0) {
  15148. ret = wc_AesSetIV(&aes, iv2);
  15149. }
  15150. /* Test bad args. */
  15151. if (ret == 0) {
  15152. ret = wc_AesSetIV(NULL, iv1);
  15153. if (ret == BAD_FUNC_ARG) {
  15154. /* NULL iv should return 0. */
  15155. ret = wc_AesSetIV(&aes, NULL);
  15156. }
  15157. else {
  15158. ret = WOLFSSL_FATAL_ERROR;
  15159. }
  15160. }
  15161. wc_AesFree(&aes);
  15162. res = TEST_RES_CHECK(ret == 0);
  15163. #endif
  15164. return res;
  15165. } /* test_wc_AesSetIV */
  15166. /*
  15167. * Testing function for wc_AesSetKey().
  15168. */
  15169. static int test_wc_AesSetKey(void)
  15170. {
  15171. EXPECT_DECLS;
  15172. #ifndef NO_AES
  15173. Aes aes;
  15174. byte key16[] = {
  15175. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15176. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15177. };
  15178. #ifdef WOLFSSL_AES_192
  15179. byte key24[] = {
  15180. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15181. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15182. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15183. };
  15184. #endif
  15185. #ifdef WOLFSSL_AES_256
  15186. byte key32[] = {
  15187. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15188. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15189. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15190. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15191. };
  15192. #endif
  15193. byte badKey16[] = {
  15194. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15195. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15196. };
  15197. byte iv[] = "1234567890abcdef";
  15198. XMEMSET(&aes, 0, sizeof(Aes));
  15199. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15200. #ifdef WOLFSSL_AES_128
  15201. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15202. iv, AES_ENCRYPTION), 0);
  15203. #endif
  15204. #ifdef WOLFSSL_AES_192
  15205. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15206. iv, AES_ENCRYPTION), 0);
  15207. #endif
  15208. #ifdef WOLFSSL_AES_256
  15209. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15210. iv, AES_ENCRYPTION), 0);
  15211. #endif
  15212. /* Pass in bad args. */
  15213. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15214. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15216. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15217. BAD_FUNC_ARG);
  15218. wc_AesFree(&aes);
  15219. #endif
  15220. return EXPECT_RESULT();
  15221. } /* END test_wc_AesSetKey */
  15222. /*
  15223. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15224. * and wc_AesCbcDecryptWithKey()
  15225. */
  15226. static int test_wc_AesCbcEncryptDecrypt(void)
  15227. {
  15228. EXPECT_DECLS;
  15229. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15230. defined(WOLFSSL_AES_256)
  15231. Aes aes;
  15232. byte key32[] = {
  15233. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15234. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15235. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15236. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15237. };
  15238. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15239. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15240. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15241. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15242. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15243. };
  15244. byte iv[] = "1234567890abcdef";
  15245. byte enc[sizeof(vector)];
  15246. byte dec[sizeof(vector)];
  15247. byte dec2[sizeof(vector)];
  15248. /* Init stack variables. */
  15249. XMEMSET(&aes, 0, sizeof(Aes));
  15250. XMEMSET(enc, 0, sizeof(enc));
  15251. XMEMSET(dec, 0, sizeof(vector));
  15252. XMEMSET(dec2, 0, sizeof(vector));
  15253. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15254. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15255. AES_ENCRYPTION), 0);
  15256. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15257. /* Re init for decrypt and set flag. */
  15258. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15259. AES_DECRYPTION), 0);
  15260. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15261. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15262. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15263. sizeof(key32)/sizeof(byte), iv), 0);
  15264. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15265. /* Pass in bad args */
  15266. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15267. BAD_FUNC_ARG);
  15268. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15269. BAD_FUNC_ARG);
  15270. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15271. BAD_FUNC_ARG);
  15272. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15273. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15274. BAD_LENGTH_E);
  15275. #endif
  15276. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15277. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15278. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15279. "mode (v2), skip test");
  15280. #else
  15281. /* Test passing in size of 0 */
  15282. XMEMSET(enc, 0, sizeof(enc));
  15283. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15284. /* Check enc was not modified */
  15285. {
  15286. int i;
  15287. for (i = 0; i < (int)sizeof(enc); i++)
  15288. ExpectIntEQ(enc[i], 0);
  15289. }
  15290. #endif
  15291. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15292. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15293. BAD_FUNC_ARG);
  15294. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15295. BAD_FUNC_ARG);
  15296. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15297. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15298. BAD_LENGTH_E);
  15299. #else
  15300. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15301. BAD_FUNC_ARG);
  15302. #endif
  15303. /* Test passing in size of 0 */
  15304. XMEMSET(dec, 0, sizeof(dec));
  15305. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15306. /* Check dec was not modified */
  15307. {
  15308. int i;
  15309. for (i = 0; i < (int)sizeof(dec); i++)
  15310. ExpectIntEQ(dec[i], 0);
  15311. }
  15312. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15313. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15314. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15315. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15316. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15317. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15318. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15319. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15320. wc_AesFree(&aes);
  15321. #endif
  15322. return EXPECT_RESULT();
  15323. } /* END test_wc_AesCbcEncryptDecrypt */
  15324. /*
  15325. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15326. */
  15327. static int test_wc_AesCtrEncryptDecrypt(void)
  15328. {
  15329. EXPECT_DECLS;
  15330. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15331. Aes aesEnc;
  15332. Aes aesDec;
  15333. byte key32[] = {
  15334. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15335. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15336. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15337. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15338. };
  15339. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15340. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15341. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15342. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15343. };
  15344. byte iv[] = "1234567890abcdef";
  15345. byte enc[AES_BLOCK_SIZE * 2];
  15346. byte dec[AES_BLOCK_SIZE * 2];
  15347. /* Init stack variables. */
  15348. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15349. XMEMSET(&aesDec, 0, sizeof(Aes));
  15350. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15351. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15352. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15353. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15354. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15355. AES_ENCRYPTION), 0);
  15356. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15357. sizeof(vector)/sizeof(byte)), 0);
  15358. /* Decrypt with wc_AesCtrEncrypt() */
  15359. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15360. AES_ENCRYPTION), 0);
  15361. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15362. 0);
  15363. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15364. /* Test bad args. */
  15365. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15366. BAD_FUNC_ARG);
  15367. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15368. BAD_FUNC_ARG);
  15369. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15370. BAD_FUNC_ARG);
  15371. wc_AesFree(&aesEnc);
  15372. wc_AesFree(&aesDec);
  15373. #endif
  15374. return EXPECT_RESULT();
  15375. } /* END test_wc_AesCtrEncryptDecrypt */
  15376. /*
  15377. * test function for wc_AesGcmSetKey()
  15378. */
  15379. static int test_wc_AesGcmSetKey(void)
  15380. {
  15381. EXPECT_DECLS;
  15382. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15383. Aes aes;
  15384. #ifdef WOLFSSL_AES_128
  15385. byte key16[] = {
  15386. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15387. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15388. };
  15389. #endif
  15390. #ifdef WOLFSSL_AES_192
  15391. byte key24[] = {
  15392. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15393. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15394. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15395. };
  15396. #endif
  15397. #ifdef WOLFSSL_AES_256
  15398. byte key32[] = {
  15399. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15400. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15401. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15402. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15403. };
  15404. #endif
  15405. byte badKey16[] = {
  15406. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15407. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15408. };
  15409. byte badKey24[] = {
  15410. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15411. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15412. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15413. };
  15414. byte badKey32[] = {
  15415. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15416. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15417. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15418. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15419. };
  15420. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15421. #ifdef WOLFSSL_AES_128
  15422. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15423. #endif
  15424. #ifdef WOLFSSL_AES_192
  15425. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15426. #endif
  15427. #ifdef WOLFSSL_AES_256
  15428. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15429. #endif
  15430. /* Pass in bad args. */
  15431. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15432. BAD_FUNC_ARG);
  15433. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15434. BAD_FUNC_ARG);
  15435. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15436. BAD_FUNC_ARG);
  15437. wc_AesFree(&aes);
  15438. #endif
  15439. return EXPECT_RESULT();
  15440. } /* END test_wc_AesGcmSetKey */
  15441. /*
  15442. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15443. */
  15444. static int test_wc_AesGcmEncryptDecrypt(void)
  15445. {
  15446. EXPECT_DECLS;
  15447. /* WOLFSSL_AFALG requires 12 byte IV */
  15448. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15449. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15450. Aes aes;
  15451. byte key32[] = {
  15452. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15453. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15454. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15455. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15456. };
  15457. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15458. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15459. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15460. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15461. };
  15462. const byte a[] = {
  15463. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15464. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15465. 0xab, 0xad, 0xda, 0xd2
  15466. };
  15467. byte iv[] = "1234567890a";
  15468. byte longIV[] = "1234567890abcdefghij";
  15469. byte enc[sizeof(vector)];
  15470. byte resultT[AES_BLOCK_SIZE];
  15471. byte dec[sizeof(vector)];
  15472. /* Init stack variables. */
  15473. XMEMSET(&aes, 0, sizeof(Aes));
  15474. XMEMSET(enc, 0, sizeof(vector));
  15475. XMEMSET(dec, 0, sizeof(vector));
  15476. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15477. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15478. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15479. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15480. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15481. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15482. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15483. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15484. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15485. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15486. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15487. BAD_FUNC_ARG);
  15488. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15489. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15490. BAD_FUNC_ARG);
  15491. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15492. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15493. BAD_FUNC_ARG);
  15494. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15495. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15496. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15497. /* FIPS does not check the lower bound of ivSz */
  15498. #else
  15499. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15500. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15501. #endif
  15502. /* This case is now considered good. Long IVs are now allowed.
  15503. * Except for the original FIPS release, it still has an upper
  15504. * bound on the IV length. */
  15505. #if (!defined(HAVE_FIPS) || \
  15506. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15507. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15508. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15509. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15510. 0);
  15511. #else
  15512. (void)longIV;
  15513. #endif /* Old FIPS */
  15514. /* END wc_AesGcmEncrypt */
  15515. #ifdef HAVE_AES_DECRYPT
  15516. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15517. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15518. BAD_FUNC_ARG);
  15519. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15520. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15521. BAD_FUNC_ARG);
  15522. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15523. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15524. BAD_FUNC_ARG);
  15525. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15526. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15527. BAD_FUNC_ARG);
  15528. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15529. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15530. BAD_FUNC_ARG);
  15531. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15532. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15533. BAD_FUNC_ARG);
  15534. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15535. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15536. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15537. /* FIPS does not check the lower bound of ivSz */
  15538. #else
  15539. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15540. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15541. #endif
  15542. #endif /* HAVE_AES_DECRYPT */
  15543. wc_AesFree(&aes);
  15544. #endif
  15545. return EXPECT_RESULT();
  15546. } /* END test_wc_AesGcmEncryptDecrypt */
  15547. /*
  15548. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15549. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15550. */
  15551. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15552. {
  15553. EXPECT_DECLS;
  15554. #if (!defined(HAVE_FIPS) || \
  15555. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15556. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15557. const byte key[] = {
  15558. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15559. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15560. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15561. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15562. };
  15563. const byte in[] = {
  15564. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15565. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15566. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15567. };
  15568. const byte aad[] = {
  15569. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15570. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15571. 0xab, 0xad, 0xda, 0xd2
  15572. };
  15573. Aes aesEnc;
  15574. Aes aesDec;
  15575. byte iv[] = "1234567890abcdefghij";
  15576. byte out[sizeof(in)];
  15577. byte plain[sizeof(in)];
  15578. byte tag[AES_BLOCK_SIZE];
  15579. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15580. XMEMSET(&aesDec, 0, sizeof(Aes));
  15581. XMEMSET(out, 0, sizeof(out));
  15582. XMEMSET(plain, 0, sizeof(plain));
  15583. XMEMSET(tag, 0, sizeof(tag));
  15584. /* Perform one-shot encryption using long IV */
  15585. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15586. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15587. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15588. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15589. /* Perform streaming decryption using long IV */
  15590. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15591. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15592. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15593. sizeof(aad)), 0);
  15594. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15595. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15596. /* Free resources */
  15597. wc_AesFree(&aesEnc);
  15598. wc_AesFree(&aesDec);
  15599. #endif
  15600. return EXPECT_RESULT();
  15601. } /* END wc_AesGcmMixedEncDecLongIV */
  15602. /*
  15603. * unit test for wc_GmacSetKey()
  15604. */
  15605. static int test_wc_GmacSetKey(void)
  15606. {
  15607. EXPECT_DECLS;
  15608. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15609. Gmac gmac;
  15610. byte key16[] = {
  15611. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15612. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15613. };
  15614. #ifdef WOLFSSL_AES_192
  15615. byte key24[] = {
  15616. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15617. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15618. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15619. };
  15620. #endif
  15621. #ifdef WOLFSSL_AES_256
  15622. byte key32[] = {
  15623. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15624. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15625. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15626. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15627. };
  15628. #endif
  15629. byte badKey16[] = {
  15630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15632. };
  15633. byte badKey24[] = {
  15634. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15635. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15636. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15637. };
  15638. byte badKey32[] = {
  15639. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15640. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15641. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15642. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15643. };
  15644. XMEMSET(&gmac, 0, sizeof(Gmac));
  15645. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15646. #ifdef WOLFSSL_AES_128
  15647. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15648. #endif
  15649. #ifdef WOLFSSL_AES_192
  15650. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15651. #endif
  15652. #ifdef WOLFSSL_AES_256
  15653. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15654. #endif
  15655. /* Pass in bad args. */
  15656. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15657. BAD_FUNC_ARG);
  15658. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15659. BAD_FUNC_ARG);
  15660. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15661. BAD_FUNC_ARG);
  15662. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15663. BAD_FUNC_ARG);
  15664. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15665. BAD_FUNC_ARG);
  15666. wc_AesFree(&gmac.aes);
  15667. #endif
  15668. return EXPECT_RESULT();
  15669. } /* END test_wc_GmacSetKey */
  15670. /*
  15671. * unit test for wc_GmacUpdate
  15672. */
  15673. static int test_wc_GmacUpdate(void)
  15674. {
  15675. EXPECT_DECLS;
  15676. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15677. Gmac gmac;
  15678. #ifdef WOLFSSL_AES_128
  15679. const byte key16[] = {
  15680. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15681. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15682. };
  15683. #endif
  15684. #ifdef WOLFSSL_AES_192
  15685. byte key24[] = {
  15686. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15687. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15688. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15689. };
  15690. #endif
  15691. #ifdef WOLFSSL_AES_256
  15692. byte key32[] = {
  15693. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15694. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15695. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15696. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15697. };
  15698. #endif
  15699. #ifdef WOLFSSL_AES_128
  15700. const byte authIn[] = {
  15701. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15702. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15703. };
  15704. #endif
  15705. #ifdef WOLFSSL_AES_192
  15706. const byte authIn2[] = {
  15707. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15708. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15709. };
  15710. #endif
  15711. const byte authIn3[] = {
  15712. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15713. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15714. };
  15715. #ifdef WOLFSSL_AES_128
  15716. const byte tag1[] = { /* Known. */
  15717. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15718. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15719. };
  15720. #endif
  15721. #ifdef WOLFSSL_AES_192
  15722. const byte tag2[] = { /* Known */
  15723. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15724. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15725. };
  15726. #endif
  15727. const byte tag3[] = { /* Known */
  15728. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15729. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15730. };
  15731. #ifdef WOLFSSL_AES_128
  15732. const byte iv[] = {
  15733. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15734. 0xe2, 0x8c, 0x8f, 0x16
  15735. };
  15736. #endif
  15737. #ifdef WOLFSSL_AES_192
  15738. const byte iv2[] = {
  15739. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15740. 0x7e, 0x1a, 0x6f, 0xbc
  15741. };
  15742. #endif
  15743. const byte iv3[] = {
  15744. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15745. 0xc3, 0xfb, 0x6c, 0x8a
  15746. };
  15747. byte tagOut[16];
  15748. byte tagOut2[24];
  15749. byte tagOut3[32];
  15750. /* Init stack variables. */
  15751. XMEMSET(&gmac, 0, sizeof(Gmac));
  15752. XMEMSET(tagOut, 0, sizeof(tagOut));
  15753. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15754. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15755. #ifdef WOLFSSL_AES_128
  15756. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15757. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15758. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15759. tagOut, sizeof(tag1)), 0);
  15760. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15761. wc_AesFree(&gmac.aes);
  15762. #endif
  15763. #ifdef WOLFSSL_AES_192
  15764. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15765. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15766. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15767. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15768. tagOut2, sizeof(tag2)), 0);
  15769. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15770. wc_AesFree(&gmac.aes);
  15771. #endif
  15772. #ifdef WOLFSSL_AES_256
  15773. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15774. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15775. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15776. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15777. tagOut3, sizeof(tag3)), 0);
  15778. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15779. wc_AesFree(&gmac.aes);
  15780. #endif
  15781. /* Pass bad args. */
  15782. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15783. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15784. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15785. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15786. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15787. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15788. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15789. wc_AesFree(&gmac.aes);
  15790. #endif
  15791. return EXPECT_RESULT();
  15792. } /* END test_wc_GmacUpdate */
  15793. /*
  15794. * testing wc_CamelliaSetKey
  15795. */
  15796. static int test_wc_CamelliaSetKey(void)
  15797. {
  15798. EXPECT_DECLS;
  15799. #ifdef HAVE_CAMELLIA
  15800. Camellia camellia;
  15801. /*128-bit key*/
  15802. static const byte key16[] = {
  15803. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15804. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15805. };
  15806. /* 192-bit key */
  15807. static const byte key24[] = {
  15808. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15809. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15810. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15811. };
  15812. /* 256-bit key */
  15813. static const byte key32[] = {
  15814. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15815. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15816. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15817. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15818. };
  15819. static const byte iv[] = {
  15820. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15821. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15822. };
  15823. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  15824. 0);
  15825. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  15826. NULL), 0);
  15827. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15828. 0);
  15829. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15830. NULL), 0);
  15831. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  15832. 0);
  15833. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  15834. NULL), 0);
  15835. /* Bad args. */
  15836. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  15837. BAD_FUNC_ARG);
  15838. #endif
  15839. return EXPECT_RESULT();
  15840. } /* END test_wc_CammeliaSetKey */
  15841. /*
  15842. * Testing wc_CamelliaSetIV()
  15843. */
  15844. static int test_wc_CamelliaSetIV(void)
  15845. {
  15846. EXPECT_DECLS;
  15847. #ifdef HAVE_CAMELLIA
  15848. Camellia camellia;
  15849. static const byte iv[] = {
  15850. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15851. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15852. };
  15853. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  15854. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  15855. /* Bad args. */
  15856. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  15857. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  15858. #endif
  15859. return EXPECT_RESULT();
  15860. } /* END test_wc_CamelliaSetIV*/
  15861. /*
  15862. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15863. */
  15864. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15865. {
  15866. EXPECT_DECLS;
  15867. #ifdef HAVE_CAMELLIA
  15868. Camellia camellia;
  15869. static const byte key24[] = {
  15870. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15871. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15872. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15873. };
  15874. static const byte iv[] = {
  15875. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15876. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15877. };
  15878. static const byte plainT[] = {
  15879. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15880. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15881. };
  15882. byte enc[sizeof(plainT)];
  15883. byte dec[sizeof(enc)];
  15884. /* Init stack variables.*/
  15885. XMEMSET(enc, 0, 16);
  15886. XMEMSET(enc, 0, 16);
  15887. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15888. 0);
  15889. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  15890. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  15891. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15892. /* Pass bad args. */
  15893. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  15894. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  15895. BAD_FUNC_ARG);
  15896. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  15897. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  15898. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  15899. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  15900. #endif
  15901. return EXPECT_RESULT();
  15902. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15903. /*
  15904. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15905. */
  15906. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15907. {
  15908. EXPECT_DECLS;
  15909. #ifdef HAVE_CAMELLIA
  15910. Camellia camellia;
  15911. static const byte key24[] = {
  15912. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15913. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15914. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15915. };
  15916. static const byte plainT[] = {
  15917. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15918. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15919. };
  15920. byte enc[CAMELLIA_BLOCK_SIZE];
  15921. byte dec[CAMELLIA_BLOCK_SIZE];
  15922. /* Init stack variables. */
  15923. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15924. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15925. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15926. NULL), 0);
  15927. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  15928. CAMELLIA_BLOCK_SIZE), 0);
  15929. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15930. NULL), 0);
  15931. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  15932. 0);
  15933. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15934. /* Pass in bad args. */
  15935. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  15936. BAD_FUNC_ARG);
  15937. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15938. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15940. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15941. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  15942. BAD_FUNC_ARG);
  15943. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15944. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15945. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15946. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15947. #endif
  15948. return EXPECT_RESULT();
  15949. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15950. /*
  15951. * Testing wc_Arc4SetKey()
  15952. */
  15953. static int test_wc_Arc4SetKey(void)
  15954. {
  15955. EXPECT_DECLS;
  15956. #ifndef NO_RC4
  15957. Arc4 arc;
  15958. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15959. int keyLen = 8;
  15960. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  15961. /* Test bad args. */
  15962. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  15963. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  15964. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  15965. #endif
  15966. return EXPECT_RESULT();
  15967. } /* END test_wc_Arc4SetKey */
  15968. /*
  15969. * Testing wc_Arc4Process for ENC/DEC.
  15970. */
  15971. static int test_wc_Arc4Process(void)
  15972. {
  15973. EXPECT_DECLS;
  15974. #ifndef NO_RC4
  15975. Arc4 enc;
  15976. Arc4 dec;
  15977. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15978. int keyLen = 8;
  15979. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15980. byte cipher[8];
  15981. byte plain[8];
  15982. /* Init stack variables */
  15983. XMEMSET(&enc, 0, sizeof(Arc4));
  15984. XMEMSET(&dec, 0, sizeof(Arc4));
  15985. XMEMSET(cipher, 0, sizeof(cipher));
  15986. XMEMSET(plain, 0, sizeof(plain));
  15987. /* Use for async. */
  15988. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  15989. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  15990. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  15991. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  15992. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  15993. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  15994. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  15995. /* Bad args. */
  15996. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  15997. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  15998. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  15999. wc_Arc4Free(&enc);
  16000. wc_Arc4Free(&dec);
  16001. #endif
  16002. return EXPECT_RESULT();
  16003. } /* END test_wc_Arc4Process */
  16004. /*
  16005. * Testing wc_Init RsaKey()
  16006. */
  16007. static int test_wc_InitRsaKey(void)
  16008. {
  16009. EXPECT_DECLS;
  16010. #ifndef NO_RSA
  16011. RsaKey key;
  16012. XMEMSET(&key, 0, sizeof(RsaKey));
  16013. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16014. /* Test bad args. */
  16015. #ifndef HAVE_USER_RSA
  16016. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16017. #else
  16018. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  16019. #endif
  16020. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16021. #endif
  16022. return EXPECT_RESULT();
  16023. } /* END test_wc_InitRsaKey */
  16024. /*
  16025. * Testing wc_RsaPrivateKeyDecode()
  16026. */
  16027. static int test_wc_RsaPrivateKeyDecode(void)
  16028. {
  16029. EXPECT_DECLS;
  16030. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16031. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16032. RsaKey key;
  16033. byte* tmp = NULL;
  16034. word32 idx = 0;
  16035. int bytes = 0;
  16036. XMEMSET(&key, 0, sizeof(RsaKey));
  16037. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16038. DYNAMIC_TYPE_TMP_BUFFER));
  16039. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16040. if (tmp != NULL) {
  16041. #ifdef USE_CERT_BUFFERS_1024
  16042. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16043. bytes = sizeof_client_key_der_1024;
  16044. #else
  16045. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16046. bytes = sizeof_client_key_der_2048;
  16047. #endif /* Use cert buffers. */
  16048. }
  16049. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16050. #ifndef HAVE_USER_RSA
  16051. /* Test bad args. */
  16052. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16053. BAD_FUNC_ARG);
  16054. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16055. BAD_FUNC_ARG);
  16056. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16057. BAD_FUNC_ARG);
  16058. #else
  16059. /* Test bad args. User RSA. */
  16060. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16061. USER_CRYPTO_ERROR);
  16062. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16063. USER_CRYPTO_ERROR);
  16064. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16065. USER_CRYPTO_ERROR);
  16066. #endif
  16067. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16068. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16069. #endif
  16070. return EXPECT_RESULT();
  16071. } /* END test_wc_RsaPrivateKeyDecode */
  16072. /*
  16073. * Testing wc_RsaPublicKeyDecode()
  16074. */
  16075. static int test_wc_RsaPublicKeyDecode(void)
  16076. {
  16077. EXPECT_DECLS;
  16078. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16079. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16080. RsaKey keyPub;
  16081. byte* tmp = NULL;
  16082. word32 idx = 0;
  16083. int bytes = 0;
  16084. word32 keySz = 0;
  16085. word32 tstKeySz = 0;
  16086. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16087. XFILE f = XBADFILE;
  16088. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16089. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16090. byte buf[4096];
  16091. #endif
  16092. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16093. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16094. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16095. if (tmp != NULL) {
  16096. #ifdef USE_CERT_BUFFERS_1024
  16097. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16098. bytes = sizeof_client_keypub_der_1024;
  16099. keySz = 1024;
  16100. #else
  16101. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16102. bytes = sizeof_client_keypub_der_2048;
  16103. keySz = 2048;
  16104. #endif
  16105. }
  16106. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16107. #ifndef HAVE_USER_RSA
  16108. /* Pass in bad args. */
  16109. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16110. BAD_FUNC_ARG);
  16111. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16112. BAD_FUNC_ARG);
  16113. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16114. BAD_FUNC_ARG);
  16115. #else
  16116. /* Pass in bad args. */
  16117. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16118. USER_CRYPTO_ERROR);
  16119. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16120. USER_CRYPTO_ERROR);
  16121. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16122. USER_CRYPTO_ERROR);
  16123. #endif
  16124. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16125. /* Test for getting modulus key size */
  16126. idx = 0;
  16127. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16128. &tstKeySz, NULL, NULL), 0);
  16129. ExpectIntEQ(tstKeySz, keySz/8);
  16130. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16131. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16132. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16133. if (f != XBADFILE) {
  16134. XFCLOSE(f);
  16135. f = XBADFILE;
  16136. }
  16137. idx = 0;
  16138. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16139. NULL), 0);
  16140. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16141. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16142. if (f != XBADFILE)
  16143. XFCLOSE(f);
  16144. idx = 0;
  16145. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16146. NULL), 0);
  16147. #endif
  16148. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16149. #endif
  16150. return EXPECT_RESULT();
  16151. } /* END test_wc_RsaPublicKeyDecode */
  16152. /*
  16153. * Testing wc_RsaPublicKeyDecodeRaw()
  16154. */
  16155. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16156. {
  16157. EXPECT_DECLS;
  16158. #if !defined(NO_RSA)
  16159. RsaKey key;
  16160. const byte n = 0x23;
  16161. const byte e = 0x03;
  16162. int nSz = sizeof(n);
  16163. int eSz = sizeof(e);
  16164. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16165. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16166. #ifndef HAVE_USER_RSA
  16167. /* Pass in bad args. */
  16168. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16169. BAD_FUNC_ARG);
  16170. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16171. BAD_FUNC_ARG);
  16172. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16173. BAD_FUNC_ARG);
  16174. #else
  16175. /* Pass in bad args. User RSA. */
  16176. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16177. USER_CRYPTO_ERROR);
  16178. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16179. USER_CRYPTO_ERROR);
  16180. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16181. USER_CRYPTO_ERROR);
  16182. #endif
  16183. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16184. #endif
  16185. return EXPECT_RESULT();
  16186. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16187. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16188. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16189. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16190. * trying until it gets a probable prime. */
  16191. #ifdef HAVE_FIPS
  16192. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16193. {
  16194. int ret;
  16195. for (;;) {
  16196. ret = wc_MakeRsaKey(key, size, e, rng);
  16197. if (ret != PRIME_GEN_E) break;
  16198. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16199. "trying again.\n");
  16200. }
  16201. return ret;
  16202. }
  16203. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16204. #else
  16205. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16206. #endif
  16207. #endif
  16208. /*
  16209. * Testing wc_MakeRsaKey()
  16210. */
  16211. static int test_wc_MakeRsaKey(void)
  16212. {
  16213. EXPECT_DECLS;
  16214. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16215. RsaKey genKey;
  16216. WC_RNG rng;
  16217. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16218. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16219. int bits = 1024;
  16220. #else
  16221. int bits = 2048;
  16222. #endif
  16223. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16224. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16225. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16226. ExpectIntEQ(wc_InitRng(&rng), 0);
  16227. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16228. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16229. #ifndef HAVE_USER_RSA
  16230. /* Test bad args. */
  16231. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16232. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16233. BAD_FUNC_ARG);
  16234. /* e < 3 */
  16235. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16236. /* e & 1 == 0 */
  16237. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16238. #else
  16239. /* Test bad args. */
  16240. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16241. USER_CRYPTO_ERROR);
  16242. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16243. USER_CRYPTO_ERROR);
  16244. /* e < 3 */
  16245. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16246. /* e & 1 == 0 */
  16247. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16248. #endif /* HAVE_USER_RSA */
  16249. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16250. #endif
  16251. return EXPECT_RESULT();
  16252. } /* END test_wc_MakeRsaKey */
  16253. /*
  16254. * Test the bounds checking on the cipher text versus the key modulus.
  16255. * 1. Make a new RSA key.
  16256. * 2. Set c to 1.
  16257. * 3. Decrypt c into k. (error)
  16258. * 4. Copy the key modulus to c and sub 1 from the copy.
  16259. * 5. Decrypt c into k. (error)
  16260. * Valid bounds test cases are covered by all the other RSA tests.
  16261. */
  16262. static int test_RsaDecryptBoundsCheck(void)
  16263. {
  16264. EXPECT_DECLS;
  16265. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16266. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16267. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16268. WC_RNG rng;
  16269. RsaKey key;
  16270. byte flatC[256];
  16271. word32 flatCSz;
  16272. byte out[256];
  16273. word32 outSz = sizeof(out);
  16274. XMEMSET(&key, 0, sizeof(RsaKey));
  16275. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16276. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16277. ExpectIntEQ(wc_InitRng(&rng), 0);
  16278. if (EXPECT_SUCCESS()) {
  16279. const byte* derKey;
  16280. word32 derKeySz;
  16281. word32 idx = 0;
  16282. #ifdef USE_CERT_BUFFERS_1024
  16283. derKey = server_key_der_1024;
  16284. derKeySz = (word32)sizeof_server_key_der_1024;
  16285. flatCSz = 128;
  16286. #else
  16287. derKey = server_key_der_2048;
  16288. derKeySz = (word32)sizeof_server_key_der_2048;
  16289. flatCSz = 256;
  16290. #endif
  16291. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16292. }
  16293. if (EXPECT_SUCCESS()) {
  16294. XMEMSET(flatC, 0, flatCSz);
  16295. flatC[flatCSz-1] = 1;
  16296. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16297. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16298. if (EXPECT_SUCCESS()) {
  16299. mp_int c;
  16300. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16301. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16302. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16303. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16304. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16305. mp_clear(&c);
  16306. }
  16307. }
  16308. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16309. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16310. #endif
  16311. return EXPECT_RESULT();
  16312. } /* END test_wc_RsaDecryptBoundsCheck */
  16313. /*
  16314. * Testing wc_SetKeyUsage()
  16315. */
  16316. static int test_wc_SetKeyUsage(void)
  16317. {
  16318. EXPECT_DECLS;
  16319. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16320. Cert myCert;
  16321. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16322. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16323. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16324. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16325. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16326. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16327. /* Test bad args. */
  16328. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16329. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16330. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16331. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16332. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16333. KEYUSAGE_E);
  16334. #endif
  16335. return EXPECT_RESULT();
  16336. } /* END test_wc_SetKeyUsage */
  16337. /*
  16338. * Testing wc_CheckProbablePrime()
  16339. */
  16340. static int test_wc_CheckProbablePrime(void)
  16341. {
  16342. EXPECT_DECLS;
  16343. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16344. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16345. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16346. RsaKey key;
  16347. WC_RNG rng;
  16348. byte e[3];
  16349. word32 eSz = (word32)sizeof(e);
  16350. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16351. word32 nSz = (word32)sizeof(n);
  16352. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16353. word32 dSz = (word32)sizeof(d);
  16354. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16355. word32 pSz = (word32)sizeof(p);
  16356. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16357. word32 qSz = (word32)sizeof(q);
  16358. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16359. int* isPrime;
  16360. int test[5];
  16361. isPrime = test;
  16362. XMEMSET(&key, 0, sizeof(RsaKey));
  16363. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16364. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16365. ExpectIntEQ(wc_InitRng(&rng), 0);
  16366. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16367. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16368. WC_RSA_EXPONENT, &rng), 0);
  16369. PRIVATE_KEY_UNLOCK();
  16370. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16371. &qSz), 0);
  16372. PRIVATE_KEY_LOCK();
  16373. /* Bad cases */
  16374. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16375. BAD_FUNC_ARG);
  16376. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16377. BAD_FUNC_ARG);
  16378. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16379. BAD_FUNC_ARG);
  16380. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16381. BAD_FUNC_ARG);
  16382. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16383. BAD_FUNC_ARG);
  16384. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16385. BAD_FUNC_ARG);
  16386. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16387. BAD_FUNC_ARG);
  16388. /* Good case */
  16389. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16390. 0);
  16391. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16392. wc_FreeRng(&rng);
  16393. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16394. #endif
  16395. return EXPECT_RESULT();
  16396. } /* END test_wc_CheckProbablePrime */
  16397. /*
  16398. * Testing wc_RsaPSS_Verify()
  16399. */
  16400. static int test_wc_RsaPSS_Verify(void)
  16401. {
  16402. EXPECT_DECLS;
  16403. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16404. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16405. RsaKey key;
  16406. WC_RNG rng;
  16407. int sz = 256;
  16408. const char* szMessage = "This is the string to be signed";
  16409. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16410. unsigned char pDecrypted[2048/8];
  16411. byte* pt = pDecrypted;
  16412. word32 outLen = sizeof(pDecrypted);
  16413. XMEMSET(&key, 0, sizeof(RsaKey));
  16414. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16415. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16416. ExpectIntEQ(wc_InitRng(&rng), 0);
  16417. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16418. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16419. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16420. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16421. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16422. /* Bad cases */
  16423. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16424. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16425. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16426. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16427. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16428. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16429. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16430. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16431. /* Good case */
  16432. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16433. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16434. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16435. wc_FreeRng(&rng);
  16436. #endif
  16437. return EXPECT_RESULT();
  16438. } /* END test_wc_RsaPSS_Verify */
  16439. /*
  16440. * Testing wc_RsaPSS_VerifyCheck()
  16441. */
  16442. static int test_wc_RsaPSS_VerifyCheck(void)
  16443. {
  16444. EXPECT_DECLS;
  16445. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16446. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16447. RsaKey key;
  16448. WC_RNG rng;
  16449. int sz = 256; /* 2048/8 */
  16450. byte digest[32];
  16451. word32 digestSz = sizeof(digest);
  16452. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16453. word32 pSignatureSz = sizeof(pSignature);
  16454. unsigned char pDecrypted[2048/8];
  16455. byte* pt = pDecrypted;
  16456. word32 outLen = sizeof(pDecrypted);
  16457. XMEMSET(&key, 0, sizeof(RsaKey));
  16458. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16459. XMEMSET(digest, 0, sizeof(digest));
  16460. XMEMSET(pSignature, 0, sizeof(pSignature));
  16461. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16462. ExpectIntEQ(wc_InitRng(&rng), 0);
  16463. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16464. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16465. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16466. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16467. 0);
  16468. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16469. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16470. /* Bad cases */
  16471. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16472. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16473. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16474. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16475. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16476. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16477. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16478. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16479. /* Good case */
  16480. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16481. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16482. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16483. wc_FreeRng(&rng);
  16484. #endif
  16485. return EXPECT_RESULT();
  16486. } /* END test_wc_RsaPSS_VerifyCheck */
  16487. /*
  16488. * Testing wc_RsaPSS_VerifyCheckInline()
  16489. */
  16490. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16491. {
  16492. EXPECT_DECLS;
  16493. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16494. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16495. RsaKey key;
  16496. WC_RNG rng;
  16497. int sz = 256;
  16498. byte digest[32];
  16499. word32 digestSz = sizeof(digest);
  16500. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16501. unsigned char pDecrypted[2048/8];
  16502. byte* pt = pDecrypted;
  16503. XMEMSET(&key, 0, sizeof(RsaKey));
  16504. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16505. XMEMSET(digest, 0, sizeof(digest));
  16506. XMEMSET(pSignature, 0, sizeof(pSignature));
  16507. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16508. ExpectIntEQ(wc_InitRng(&rng), 0);
  16509. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16510. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16511. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16512. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16513. 0);
  16514. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16515. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16516. /* Bad Cases */
  16517. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16518. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16519. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16520. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16521. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16522. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16523. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16524. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16525. /* Good case */
  16526. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16527. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16528. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16529. wc_FreeRng(&rng);
  16530. #endif
  16531. return EXPECT_RESULT();
  16532. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16533. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16534. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16535. {
  16536. (void)flag;
  16537. (void)type;
  16538. (void)file;
  16539. (void)line;
  16540. }
  16541. #endif
  16542. /*
  16543. * Testing wc_LockMutex_ex
  16544. */
  16545. static int test_wc_LockMutex_ex(void)
  16546. {
  16547. EXPECT_DECLS;
  16548. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16549. int flag = CRYPTO_LOCK;
  16550. int type = 0;
  16551. const char* file = "./test-LockMutex_ex.txt";
  16552. int line = 0;
  16553. /* without SetMutexCb */
  16554. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16555. /* with SetMutexCb */
  16556. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16557. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16558. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16559. #endif
  16560. return EXPECT_RESULT();
  16561. } /* End test_wc_LockMutex_ex*/
  16562. /*
  16563. * Testing wc_SetMutexCb
  16564. */
  16565. static int test_wc_SetMutexCb(void)
  16566. {
  16567. EXPECT_DECLS;
  16568. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16569. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16570. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16571. #endif
  16572. return EXPECT_RESULT();
  16573. } /* End test_wc_SetMutexCb*/
  16574. /*
  16575. * Testing wc_RsaKeyToDer()
  16576. */
  16577. static int test_wc_RsaKeyToDer(void)
  16578. {
  16579. EXPECT_DECLS;
  16580. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16581. RsaKey genKey;
  16582. WC_RNG rng;
  16583. byte* der = NULL;
  16584. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16585. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16586. int bits = 1024;
  16587. word32 derSz = 611;
  16588. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16589. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16590. #else
  16591. int bits = 2048;
  16592. word32 derSz = 1196;
  16593. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16594. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16595. #endif
  16596. XMEMSET(&rng, 0, sizeof(rng));
  16597. XMEMSET(&genKey, 0, sizeof(genKey));
  16598. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16599. /* Init structures. */
  16600. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16601. ExpectIntEQ(wc_InitRng(&rng), 0);
  16602. /* Make key. */
  16603. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16604. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16605. #ifndef HAVE_USER_RSA
  16606. /* Pass good/bad args. */
  16607. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16608. /* Get just the output length */
  16609. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16610. /* Try Public Key. */
  16611. genKey.type = 0;
  16612. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16613. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16614. /* Put back to Private Key */
  16615. genKey.type = 1;
  16616. #endif
  16617. #else
  16618. /* Pass good/bad args. */
  16619. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16620. /* Get just the output length */
  16621. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16622. /* Try Public Key. */
  16623. genKey.type = 0;
  16624. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16625. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16626. /* Put back to Private Key */
  16627. genKey.type = 1;
  16628. #endif
  16629. #endif
  16630. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16631. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16632. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16633. #endif
  16634. return EXPECT_RESULT();
  16635. } /* END test_wc_RsaKeyToDer */
  16636. /*
  16637. * Testing wc_RsaKeyToPublicDer()
  16638. */
  16639. static int test_wc_RsaKeyToPublicDer(void)
  16640. {
  16641. EXPECT_DECLS;
  16642. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16643. RsaKey key;
  16644. WC_RNG rng;
  16645. byte* der = NULL;
  16646. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16647. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16648. int bits = 1024;
  16649. word32 derLen = 162;
  16650. #else
  16651. int bits = 2048;
  16652. word32 derLen = 294;
  16653. #endif
  16654. #ifndef HAVE_USER_RSA
  16655. int ret;
  16656. #endif
  16657. XMEMSET(&rng, 0, sizeof(rng));
  16658. XMEMSET(&key, 0, sizeof(key));
  16659. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16660. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16661. ExpectIntEQ(wc_InitRng(&rng), 0);
  16662. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16663. /* test getting size only */
  16664. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16665. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16666. /* test getting size only */
  16667. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16668. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16669. #ifndef HAVE_USER_RSA
  16670. /* Pass in bad args. */
  16671. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16672. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16673. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16674. #else
  16675. /* Pass in bad args. */
  16676. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16677. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16678. #endif
  16679. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16680. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16681. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16682. #endif
  16683. return EXPECT_RESULT();
  16684. } /* END test_wc_RsaKeyToPublicDer */
  16685. /*
  16686. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16687. */
  16688. static int test_wc_RsaPublicEncryptDecrypt(void)
  16689. {
  16690. EXPECT_DECLS;
  16691. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16692. RsaKey key;
  16693. WC_RNG rng;
  16694. const char inStr[] = TEST_STRING;
  16695. const word32 plainLen = (word32)TEST_STRING_SZ;
  16696. const word32 inLen = (word32)TEST_STRING_SZ;
  16697. int bits = TEST_RSA_BITS;
  16698. const word32 cipherLen = TEST_RSA_BYTES;
  16699. word32 cipherLenResult = cipherLen;
  16700. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16701. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16702. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16703. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16704. ExpectNotNull(in);
  16705. ExpectNotNull(plain);
  16706. ExpectNotNull(cipher);
  16707. #endif
  16708. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16709. /* Initialize stack structures. */
  16710. XMEMSET(&key, 0, sizeof(RsaKey));
  16711. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16712. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16713. ExpectIntEQ(wc_InitRng(&rng), 0);
  16714. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16715. /* Encrypt. */
  16716. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16717. cipherLen, &key, &rng), 0);
  16718. /* Pass bad args - tested in another testing function.*/
  16719. /* Decrypt */
  16720. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16721. /* Bind rng */
  16722. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16723. #endif
  16724. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16725. &key), 0);
  16726. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16727. /* Pass bad args - tested in another testing function.*/
  16728. WC_FREE_VAR(in, NULL);
  16729. WC_FREE_VAR(plain, NULL);
  16730. WC_FREE_VAR(cipher, NULL);
  16731. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16732. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16733. #endif
  16734. return EXPECT_RESULT();
  16735. } /* END test_wc_RsaPublicEncryptDecrypt */
  16736. /*
  16737. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16738. */
  16739. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16740. {
  16741. EXPECT_DECLS;
  16742. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16743. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16744. && !defined(NO_SHA256)
  16745. RsaKey key;
  16746. WC_RNG rng;
  16747. const char inStr[] = TEST_STRING;
  16748. const word32 inLen = (word32)TEST_STRING_SZ;
  16749. const word32 plainSz = (word32)TEST_STRING_SZ;
  16750. byte* res = NULL;
  16751. int idx = 0;
  16752. int bits = TEST_RSA_BITS;
  16753. const word32 cipherSz = TEST_RSA_BYTES;
  16754. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16755. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16756. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16757. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16758. ExpectNotNull(in);
  16759. ExpectNotNull(plain);
  16760. ExpectNotNull(cipher);
  16761. #endif
  16762. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16763. /* Initialize stack structures. */
  16764. XMEMSET(&key, 0, sizeof(RsaKey));
  16765. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16766. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16767. ExpectIntEQ(wc_InitRng(&rng), 0);
  16768. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16769. /* Encrypt */
  16770. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16771. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16772. /* Pass bad args - tested in another testing function.*/
  16773. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16774. /* Decrypt */
  16775. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16776. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16777. #endif
  16778. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16779. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16780. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16781. /* Pass bad args - tested in another testing function.*/
  16782. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16783. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16784. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16785. #endif
  16786. WC_FREE_VAR(in, NULL);
  16787. WC_FREE_VAR(plain, NULL);
  16788. WC_FREE_VAR(cipher, NULL);
  16789. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16790. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16791. #endif
  16792. return EXPECT_RESULT();
  16793. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16794. /*
  16795. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16796. */
  16797. static int test_wc_RsaSSL_SignVerify(void)
  16798. {
  16799. EXPECT_DECLS;
  16800. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16801. RsaKey key;
  16802. WC_RNG rng;
  16803. const char inStr[] = TEST_STRING;
  16804. const word32 plainSz = (word32)TEST_STRING_SZ;
  16805. const word32 inLen = (word32)TEST_STRING_SZ;
  16806. word32 idx = 0;
  16807. int bits = TEST_RSA_BITS;
  16808. const word32 outSz = TEST_RSA_BYTES;
  16809. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16810. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16811. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16812. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16813. ExpectNotNull(in);
  16814. ExpectNotNull(out);
  16815. ExpectNotNull(plain);
  16816. #endif
  16817. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16818. XMEMSET(&key, 0, sizeof(RsaKey));
  16819. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16820. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16821. ExpectIntEQ(wc_InitRng(&rng), 0);
  16822. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16823. /* Sign. */
  16824. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  16825. idx = (int)outSz;
  16826. #ifndef HAVE_USER_RSA
  16827. /* Test bad args. */
  16828. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16829. BAD_FUNC_ARG);
  16830. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16831. BAD_FUNC_ARG);
  16832. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16833. BAD_FUNC_ARG);
  16834. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16835. BAD_FUNC_ARG);
  16836. #else
  16837. /* Test bad args. */
  16838. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16839. USER_CRYPTO_ERROR);
  16840. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16841. USER_CRYPTO_ERROR);
  16842. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16843. USER_CRYPTO_ERROR);
  16844. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16845. USER_CRYPTO_ERROR);
  16846. #endif
  16847. /* Verify. */
  16848. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  16849. #ifndef HAVE_USER_RSA
  16850. /* Pass bad args. */
  16851. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16852. BAD_FUNC_ARG);
  16853. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16854. BAD_FUNC_ARG);
  16855. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16856. BAD_FUNC_ARG);
  16857. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16858. BAD_FUNC_ARG);
  16859. #else
  16860. /* Pass bad args. */
  16861. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16862. USER_CRYPTO_ERROR);
  16863. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16864. USER_CRYPTO_ERROR);
  16865. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16866. USER_CRYPTO_ERROR);
  16867. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16868. USER_CRYPTO_ERROR);
  16869. #endif
  16870. WC_FREE_VAR(in, NULL);
  16871. WC_FREE_VAR(out, NULL);
  16872. WC_FREE_VAR(plain, NULL);
  16873. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16874. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16875. #endif
  16876. return EXPECT_RESULT();
  16877. } /* END test_wc_RsaSSL_SignVerify */
  16878. /*
  16879. * Testing wc_RsaEncryptSize()
  16880. */
  16881. static int test_wc_RsaEncryptSize(void)
  16882. {
  16883. EXPECT_DECLS;
  16884. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16885. RsaKey key;
  16886. WC_RNG rng;
  16887. XMEMSET(&key, 0, sizeof(RsaKey));
  16888. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16889. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16890. ExpectIntEQ(wc_InitRng(&rng), 0);
  16891. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16892. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16893. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  16894. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  16895. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16896. #endif
  16897. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16898. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  16899. /* Pass in bad arg. */
  16900. #ifndef HAVE_USER_RSA
  16901. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  16902. #else
  16903. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  16904. #endif
  16905. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16906. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16907. #endif
  16908. return EXPECT_RESULT();
  16909. } /* END test_wc_RsaEncryptSize*/
  16910. /*
  16911. * Testing wc_RsaFlattenPublicKey()
  16912. */
  16913. static int test_wc_RsaFlattenPublicKey(void)
  16914. {
  16915. EXPECT_DECLS;
  16916. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16917. RsaKey key;
  16918. WC_RNG rng;
  16919. byte e[256];
  16920. byte n[256];
  16921. word32 eSz = sizeof(e);
  16922. word32 nSz = sizeof(n);
  16923. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16924. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16925. int bits = 1024;
  16926. #else
  16927. int bits = 2048;
  16928. #endif
  16929. XMEMSET(&key, 0, sizeof(RsaKey));
  16930. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16931. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16932. ExpectIntEQ(wc_InitRng(&rng), 0);
  16933. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16934. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  16935. #ifndef HAVE_USER_RSA
  16936. /* Pass bad args. */
  16937. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16938. BAD_FUNC_ARG);
  16939. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16940. BAD_FUNC_ARG);
  16941. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16942. BAD_FUNC_ARG);
  16943. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16944. BAD_FUNC_ARG);
  16945. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16946. BAD_FUNC_ARG);
  16947. #else
  16948. /* Pass bad args. */
  16949. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16950. USER_CRYPTO_ERROR);
  16951. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16952. USER_CRYPTO_ERROR);
  16953. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16954. USER_CRYPTO_ERROR);
  16955. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16956. USER_CRYPTO_ERROR);
  16957. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16958. USER_CRYPTO_ERROR);
  16959. #endif
  16960. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16961. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16962. #endif
  16963. return EXPECT_RESULT();
  16964. } /* END test_wc_RsaFlattenPublicKey */
  16965. /*
  16966. * unit test for wc_AesCcmSetKey
  16967. */
  16968. static int test_wc_AesCcmSetKey(void)
  16969. {
  16970. EXPECT_DECLS;
  16971. #ifdef HAVE_AESCCM
  16972. Aes aes;
  16973. const byte key16[] = {
  16974. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16975. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16976. };
  16977. const byte key24[] = {
  16978. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16979. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16980. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16981. };
  16982. const byte key32[] = {
  16983. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16984. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16985. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16986. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16987. };
  16988. XMEMSET(&aes, 0, sizeof(Aes));
  16989. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16990. #ifdef WOLFSSL_AES_128
  16991. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16992. #endif
  16993. #ifdef WOLFSSL_AES_192
  16994. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  16995. #endif
  16996. #ifdef WOLFSSL_AES_256
  16997. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  16998. #endif
  16999. /* Test bad args. */
  17000. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17001. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17002. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17003. wc_AesFree(&aes);
  17004. #endif
  17005. return EXPECT_RESULT();
  17006. } /* END test_wc_AesCcmSetKey */
  17007. /*
  17008. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17009. */
  17010. static int test_wc_AesCcmEncryptDecrypt(void)
  17011. {
  17012. EXPECT_DECLS;
  17013. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17014. Aes aes;
  17015. const byte key16[] = {
  17016. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17017. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17018. };
  17019. /* plaintext */
  17020. const byte plainT[] = {
  17021. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17022. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17023. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17024. };
  17025. /* nonce */
  17026. const byte iv[] = {
  17027. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17028. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17029. };
  17030. const byte c[] = { /* cipher text. */
  17031. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17032. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17033. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17034. };
  17035. const byte t[] = { /* Auth tag */
  17036. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17037. };
  17038. const byte authIn[] = {
  17039. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17040. };
  17041. byte cipherOut[sizeof(plainT)];
  17042. byte authTag[sizeof(t)];
  17043. #ifdef HAVE_AES_DECRYPT
  17044. byte plainOut[sizeof(cipherOut)];
  17045. #endif
  17046. XMEMSET(&aes, 0, sizeof(Aes));
  17047. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17048. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17049. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17050. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17051. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17052. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17053. #ifdef HAVE_AES_DECRYPT
  17054. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17055. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17056. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17057. #endif
  17058. /* Pass in bad args. Encrypt*/
  17059. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17060. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17061. BAD_FUNC_ARG);
  17062. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17063. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17064. BAD_FUNC_ARG);
  17065. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17066. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17067. BAD_FUNC_ARG);
  17068. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17069. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17070. BAD_FUNC_ARG);
  17071. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17072. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17073. BAD_FUNC_ARG);
  17074. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17075. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17076. BAD_FUNC_ARG);
  17077. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17078. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17079. BAD_FUNC_ARG);
  17080. #ifdef HAVE_AES_DECRYPT
  17081. /* Pass in bad args. Decrypt*/
  17082. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17083. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17084. BAD_FUNC_ARG);
  17085. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17086. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17087. BAD_FUNC_ARG);
  17088. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17089. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17090. BAD_FUNC_ARG);
  17091. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17092. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17093. BAD_FUNC_ARG);
  17094. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17095. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17096. BAD_FUNC_ARG);
  17097. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17098. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17099. BAD_FUNC_ARG);
  17100. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17101. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17102. BAD_FUNC_ARG);
  17103. #endif
  17104. wc_AesFree(&aes);
  17105. #endif /* HAVE_AESCCM */
  17106. return EXPECT_RESULT();
  17107. } /* END test_wc_AesCcmEncryptDecrypt */
  17108. #if defined(WOLFSSL_AES_EAX) && \
  17109. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17110. /*
  17111. * Testing test_wc_AesEaxVectors()
  17112. */
  17113. static int test_wc_AesEaxVectors(void)
  17114. {
  17115. EXPECT_DECLS;
  17116. typedef struct {
  17117. byte key[AES_256_KEY_SIZE];
  17118. int key_length;
  17119. byte iv[AES_BLOCK_SIZE];
  17120. int iv_length;
  17121. byte aad[AES_BLOCK_SIZE * 2];
  17122. int aad_length;
  17123. byte msg[AES_BLOCK_SIZE * 5];
  17124. int msg_length;
  17125. byte ct[AES_BLOCK_SIZE * 5];
  17126. int ct_length;
  17127. byte tag[AES_BLOCK_SIZE];
  17128. int tag_length;
  17129. int valid;
  17130. } AadVector;
  17131. /* Test vectors obtained from Google wycheproof project
  17132. * https://github.com/google/wycheproof
  17133. * from testvectors/aes_eax_test.json
  17134. */
  17135. const AadVector vectors[] = {
  17136. {
  17137. /* key, key length */
  17138. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17139. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17140. /* iv, iv length */
  17141. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17142. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17143. /* aad, aad length */
  17144. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17145. /* msg, msg length */
  17146. {0x00}, 0,
  17147. /* ct, ct length */
  17148. {0x00}, 0,
  17149. /* tag, tag length */
  17150. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17151. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17152. /* valid */
  17153. 1,
  17154. },
  17155. {
  17156. /* key, key length */
  17157. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17158. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17159. /* iv, iv length */
  17160. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17161. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17162. /* aad, aad length */
  17163. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17164. /* msg, msg length */
  17165. {0xf7, 0xfb}, 2,
  17166. /* ct, ct length */
  17167. {0x19, 0xdd}, 2,
  17168. /* tag, tag length */
  17169. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17170. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17171. /* valid */
  17172. 1,
  17173. },
  17174. {
  17175. /* key, key length */
  17176. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17177. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17178. /* iv, iv length */
  17179. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17180. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17181. /* aad, aad length */
  17182. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17183. /* msg, msg length */
  17184. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17185. /* ct, ct length */
  17186. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17187. /* tag, tag length */
  17188. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17189. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17190. /* valid */
  17191. 1,
  17192. },
  17193. {
  17194. /* key, key length */
  17195. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17196. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17197. /* iv, iv length */
  17198. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17199. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17200. /* aad, aad length */
  17201. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17202. /* msg, msg length */
  17203. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17204. /* ct, ct length */
  17205. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17206. /* tag, tag length */
  17207. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17208. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17209. /* valid */
  17210. 1,
  17211. },
  17212. {
  17213. /* key, key length */
  17214. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17215. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17216. /* iv, iv length */
  17217. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17218. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17219. /* aad, aad length */
  17220. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17221. /* msg, msg length */
  17222. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17223. /* ct, ct length */
  17224. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17225. /* tag, tag length */
  17226. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17227. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17228. /* valid */
  17229. 1,
  17230. },
  17231. {
  17232. /* key, key length */
  17233. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17234. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17235. /* iv, iv length */
  17236. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17237. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17238. /* aad, aad length */
  17239. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17240. /* msg, msg length */
  17241. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17242. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17243. /* ct, ct length */
  17244. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17245. 0x0e, 0x72, 0x84, 0x14}, 12,
  17246. /* tag, tag length */
  17247. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17248. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17249. /* valid */
  17250. 1,
  17251. },
  17252. {
  17253. /* key, key length */
  17254. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17255. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17256. /* iv, iv length */
  17257. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17258. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17259. /* aad, aad length */
  17260. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17261. /* msg, msg length */
  17262. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17263. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17264. 0x36}, 17,
  17265. /* ct, ct length */
  17266. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17267. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17268. 0x30}, 17,
  17269. /* tag, tag length */
  17270. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17271. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17272. /* valid */
  17273. 1,
  17274. },
  17275. {
  17276. /* key, key length */
  17277. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17278. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17279. /* iv, iv length */
  17280. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17281. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17282. /* aad, aad length */
  17283. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17284. /* msg, msg length */
  17285. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17286. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17287. 0x2d, 0x56}, 18,
  17288. /* ct, ct length */
  17289. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17290. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17291. 0x8c, 0xc3}, 18,
  17292. /* tag, tag length */
  17293. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17294. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17295. /* valid */
  17296. 1,
  17297. },
  17298. {
  17299. /* key, key length */
  17300. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17301. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17302. /* iv, iv length */
  17303. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17304. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17305. /* aad, aad length */
  17306. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17307. /* msg, msg length */
  17308. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17309. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17310. 0xef, 0x11}, 18,
  17311. /* ct, ct length */
  17312. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17313. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17314. 0xb1, 0xe8}, 18,
  17315. /* tag, tag length */
  17316. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17317. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17318. /* valid */
  17319. 1,
  17320. },
  17321. {
  17322. /* key, key length */
  17323. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17324. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17325. /* iv, iv length */
  17326. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17327. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17328. /* aad, aad length */
  17329. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17330. /* msg, msg length */
  17331. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17332. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17333. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17334. /* ct, ct length */
  17335. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17336. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17337. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17338. /* tag, tag length */
  17339. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17340. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17341. /* valid */
  17342. 1,
  17343. },
  17344. {
  17345. /* key, key length */
  17346. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17347. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17348. /* iv, iv length */
  17349. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17350. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17351. /* aad, aad length */
  17352. {0x00}, 0,
  17353. /* msg, msg length */
  17354. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17355. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17356. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17357. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17358. /* ct, ct length */
  17359. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17360. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17361. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17362. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17363. /* tag, tag length */
  17364. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17365. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17366. /* valid */
  17367. 1,
  17368. },
  17369. {
  17370. /* key, key length */
  17371. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17372. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17373. /* iv, iv length */
  17374. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17375. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17376. /* aad, aad length */
  17377. {0x00}, 0,
  17378. /* msg, msg length */
  17379. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17380. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17381. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17382. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17383. /* ct, ct length */
  17384. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17385. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17386. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17387. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17388. /* tag, tag length */
  17389. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17390. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17391. /* valid */
  17392. 1,
  17393. },
  17394. {
  17395. /* key, key length */
  17396. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17397. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17398. /* iv, iv length */
  17399. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17400. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17401. /* aad, aad length */
  17402. {0x00}, 0,
  17403. /* msg, msg length */
  17404. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17405. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17406. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17407. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17408. /* ct, ct length */
  17409. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17410. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17411. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17412. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17413. /* tag, tag length */
  17414. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17415. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17416. /* valid */
  17417. 1,
  17418. },
  17419. {
  17420. /* key, key length */
  17421. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17422. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17423. /* iv, iv length */
  17424. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17425. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17426. /* aad, aad length */
  17427. {0x00}, 0,
  17428. /* msg, msg length */
  17429. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17430. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17431. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17432. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17433. /* ct, ct length */
  17434. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17435. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17436. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17437. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17438. /* tag, tag length */
  17439. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17440. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17441. /* valid */
  17442. 1,
  17443. },
  17444. {
  17445. /* key, key length */
  17446. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17447. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17448. /* iv, iv length */
  17449. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17450. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17451. /* aad, aad length */
  17452. {0x00}, 0,
  17453. /* msg, msg length */
  17454. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17455. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17456. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17457. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17458. /* ct, ct length */
  17459. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17460. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17461. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17462. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17463. /* tag, tag length */
  17464. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17465. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17466. /* valid */
  17467. 1,
  17468. },
  17469. {
  17470. /* key, key length */
  17471. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17472. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17473. /* iv, iv length */
  17474. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17475. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17476. /* aad, aad length */
  17477. {0x00}, 0,
  17478. /* msg, msg length */
  17479. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17480. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17481. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17482. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17483. /* ct, ct length */
  17484. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17485. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17486. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17487. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17488. /* tag, tag length */
  17489. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17490. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17491. /* valid */
  17492. 1,
  17493. },
  17494. {
  17495. /* key, key length */
  17496. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17497. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17498. /* iv, iv length */
  17499. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17500. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17501. /* aad, aad length */
  17502. {0x00}, 0,
  17503. /* msg, msg length */
  17504. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17506. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17507. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17508. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17509. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17510. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17511. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17512. /* ct, ct length */
  17513. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17514. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17515. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17516. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17517. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17518. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17519. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17520. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17521. /* tag, tag length */
  17522. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17523. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17524. /* valid */
  17525. 1,
  17526. },
  17527. {
  17528. /* key, key length */
  17529. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17530. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17531. /* iv, iv length */
  17532. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17533. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17534. /* aad, aad length */
  17535. {0x00}, 0,
  17536. /* msg, msg length */
  17537. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17538. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17539. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17540. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17541. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17542. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17543. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17544. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17545. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  17546. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  17547. /* ct, ct length */
  17548. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  17549. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  17550. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  17551. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  17552. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  17553. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  17554. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  17555. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  17556. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  17557. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  17558. /* tag, tag length */
  17559. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  17560. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  17561. /* valid */
  17562. 1,
  17563. },
  17564. {
  17565. /* key, key length */
  17566. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  17567. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  17568. /* iv, iv length */
  17569. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  17570. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  17571. /* aad, aad length */
  17572. {0x00}, 0,
  17573. /* msg, msg length */
  17574. {0x00}, 0,
  17575. /* ct, ct length */
  17576. {0x00}, 0,
  17577. /* tag, tag length */
  17578. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  17579. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  17580. /* valid */
  17581. 1,
  17582. },
  17583. {
  17584. /* key, key length */
  17585. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  17586. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  17587. /* iv, iv length */
  17588. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  17589. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  17590. /* aad, aad length */
  17591. {0x00}, 0,
  17592. /* msg, msg length */
  17593. {0x01}, 1,
  17594. /* ct, ct length */
  17595. {0x1d}, 1,
  17596. /* tag, tag length */
  17597. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  17598. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  17599. /* valid */
  17600. 1,
  17601. },
  17602. {
  17603. /* key, key length */
  17604. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  17605. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  17606. /* iv, iv length */
  17607. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  17608. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  17609. /* aad, aad length */
  17610. {0x00}, 0,
  17611. /* msg, msg length */
  17612. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  17613. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  17614. /* ct, ct length */
  17615. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  17616. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  17617. /* tag, tag length */
  17618. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  17619. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  17620. /* valid */
  17621. 1,
  17622. },
  17623. {
  17624. /* key, key length */
  17625. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  17626. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  17627. /* iv, iv length */
  17628. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  17629. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  17630. /* aad, aad length */
  17631. {0x00}, 0,
  17632. /* msg, msg length */
  17633. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  17634. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  17635. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  17636. /* ct, ct length */
  17637. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  17638. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  17639. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  17640. /* tag, tag length */
  17641. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  17642. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  17643. /* valid */
  17644. 1,
  17645. },
  17646. {
  17647. /* key, key length */
  17648. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  17649. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  17650. /* iv, iv length */
  17651. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  17652. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  17653. /* aad, aad length */
  17654. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  17655. /* msg, msg length */
  17656. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  17657. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  17658. /* ct, ct length */
  17659. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  17660. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  17661. /* tag, tag length */
  17662. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  17663. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  17664. /* valid */
  17665. 1,
  17666. },
  17667. {
  17668. /* key, key length */
  17669. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  17670. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  17671. /* iv, iv length */
  17672. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  17673. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  17674. /* aad, aad length */
  17675. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  17676. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  17677. /* msg, msg length */
  17678. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  17679. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  17680. /* ct, ct length */
  17681. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  17682. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  17683. /* tag, tag length */
  17684. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  17685. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  17686. /* valid */
  17687. 1,
  17688. },
  17689. {
  17690. /* key, key length */
  17691. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  17692. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  17693. /* iv, iv length */
  17694. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  17695. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  17696. /* aad, aad length */
  17697. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  17698. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  17699. /* msg, msg length */
  17700. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  17701. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  17702. /* ct, ct length */
  17703. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  17704. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  17705. /* tag, tag length */
  17706. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  17707. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  17708. /* valid */
  17709. 1,
  17710. },
  17711. {
  17712. /* key, key length */
  17713. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  17714. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  17715. /* iv, iv length */
  17716. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  17717. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  17718. /* aad, aad length */
  17719. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  17720. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  17721. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  17722. /* msg, msg length */
  17723. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  17724. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  17725. /* ct, ct length */
  17726. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  17727. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  17728. /* tag, tag length */
  17729. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  17730. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  17731. /* valid */
  17732. 1,
  17733. },
  17734. {
  17735. /* key, key length */
  17736. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17737. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17738. /* iv, iv length */
  17739. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17740. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17741. /* aad, aad length */
  17742. {0x00}, 0,
  17743. /* msg, msg length */
  17744. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17745. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17746. /* ct, ct length */
  17747. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17748. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17749. /* tag, tag length */
  17750. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17751. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17752. /* valid */
  17753. 0,
  17754. },
  17755. {
  17756. /* key, key length */
  17757. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17758. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17759. /* iv, iv length */
  17760. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17761. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17762. /* aad, aad length */
  17763. {0x00}, 0,
  17764. /* msg, msg length */
  17765. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17766. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17767. /* ct, ct length */
  17768. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17769. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17770. /* tag, tag length */
  17771. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17772. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17773. /* valid */
  17774. 0,
  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. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17782. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17783. /* aad, aad length */
  17784. {0x00}, 0,
  17785. /* msg, msg length */
  17786. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17787. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17788. /* ct, ct length */
  17789. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17790. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17791. /* tag, tag length */
  17792. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17793. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17794. /* valid */
  17795. 0,
  17796. },
  17797. {
  17798. /* key, key length */
  17799. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17800. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17801. /* iv, iv length */
  17802. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17803. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17804. /* aad, aad length */
  17805. {0x00}, 0,
  17806. /* msg, msg length */
  17807. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17808. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17809. /* ct, ct length */
  17810. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17811. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17812. /* tag, tag length */
  17813. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17814. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17815. /* valid */
  17816. 0,
  17817. },
  17818. {
  17819. /* key, key length */
  17820. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17821. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17822. /* iv, iv length */
  17823. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17824. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17825. /* aad, aad length */
  17826. {0x00}, 0,
  17827. /* msg, msg length */
  17828. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17829. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17830. /* ct, ct length */
  17831. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17832. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17833. /* tag, tag length */
  17834. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  17835. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17836. /* valid */
  17837. 0,
  17838. },
  17839. {
  17840. /* key, key length */
  17841. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17842. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17843. /* iv, iv length */
  17844. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17845. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17846. /* aad, aad length */
  17847. {0x00}, 0,
  17848. /* msg, msg length */
  17849. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17850. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17851. /* ct, ct length */
  17852. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17853. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17854. /* tag, tag length */
  17855. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  17856. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17857. /* valid */
  17858. 0,
  17859. },
  17860. {
  17861. /* key, key length */
  17862. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17863. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17864. /* iv, iv length */
  17865. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17866. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17867. /* aad, aad length */
  17868. {0x00}, 0,
  17869. /* msg, msg length */
  17870. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17871. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17872. /* ct, ct length */
  17873. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17874. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17875. /* tag, tag length */
  17876. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  17877. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17878. /* valid */
  17879. 0,
  17880. },
  17881. {
  17882. /* key, key length */
  17883. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17884. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17885. /* iv, iv length */
  17886. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17887. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17888. /* aad, aad length */
  17889. {0x00}, 0,
  17890. /* msg, msg length */
  17891. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17892. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17893. /* ct, ct length */
  17894. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17895. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17896. /* tag, tag length */
  17897. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  17898. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17899. /* valid */
  17900. 0,
  17901. },
  17902. {
  17903. /* key, key length */
  17904. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17905. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17906. /* iv, iv length */
  17907. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17908. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17909. /* aad, aad length */
  17910. {0x00}, 0,
  17911. /* msg, msg length */
  17912. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17913. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17914. /* ct, ct length */
  17915. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17916. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17917. /* tag, tag length */
  17918. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17919. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17920. /* valid */
  17921. 0,
  17922. },
  17923. {
  17924. /* key, key length */
  17925. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17926. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17927. /* iv, iv length */
  17928. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17929. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17930. /* aad, aad length */
  17931. {0x00}, 0,
  17932. /* msg, msg length */
  17933. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17934. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17935. /* ct, ct length */
  17936. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17937. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17938. /* tag, tag length */
  17939. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17940. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17941. /* valid */
  17942. 0,
  17943. },
  17944. {
  17945. /* key, key length */
  17946. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17947. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17948. /* iv, iv length */
  17949. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17950. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17951. /* aad, aad length */
  17952. {0x00}, 0,
  17953. /* msg, msg length */
  17954. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17955. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17956. /* ct, ct length */
  17957. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17958. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17959. /* tag, tag length */
  17960. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17961. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17962. /* valid */
  17963. 0,
  17964. },
  17965. {
  17966. /* key, key length */
  17967. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17968. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17969. /* iv, iv length */
  17970. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17971. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17972. /* aad, aad length */
  17973. {0x00}, 0,
  17974. /* msg, msg length */
  17975. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17976. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17977. /* ct, ct length */
  17978. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17979. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17980. /* tag, tag length */
  17981. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17982. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17983. /* valid */
  17984. 0,
  17985. },
  17986. {
  17987. /* key, key length */
  17988. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17989. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17990. /* iv, iv length */
  17991. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17992. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17993. /* aad, aad length */
  17994. {0x00}, 0,
  17995. /* msg, msg length */
  17996. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17997. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17998. /* ct, ct length */
  17999. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18000. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18001. /* tag, tag length */
  18002. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18003. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18004. /* valid */
  18005. 0,
  18006. },
  18007. {
  18008. /* key, key length */
  18009. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18010. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18011. /* iv, iv length */
  18012. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18013. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18014. /* aad, aad length */
  18015. {0x00}, 0,
  18016. /* msg, msg length */
  18017. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18018. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18019. /* ct, ct length */
  18020. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18021. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18022. /* tag, tag length */
  18023. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18024. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18025. /* valid */
  18026. 0,
  18027. },
  18028. {
  18029. /* key, key length */
  18030. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18031. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18032. /* iv, iv length */
  18033. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18034. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18035. /* aad, aad length */
  18036. {0x00}, 0,
  18037. /* msg, msg length */
  18038. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18039. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18040. /* ct, ct length */
  18041. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18042. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18043. /* tag, tag length */
  18044. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18045. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18046. /* valid */
  18047. 0,
  18048. },
  18049. {
  18050. /* key, key length */
  18051. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18052. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18053. /* iv, iv length */
  18054. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18055. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18056. /* aad, aad length */
  18057. {0x00}, 0,
  18058. /* msg, msg length */
  18059. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18060. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18061. /* ct, ct length */
  18062. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18063. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18064. /* tag, tag length */
  18065. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18066. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18067. /* valid */
  18068. 0,
  18069. },
  18070. {
  18071. /* key, key length */
  18072. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18073. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18074. /* iv, iv length */
  18075. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18076. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18077. /* aad, aad length */
  18078. {0x00}, 0,
  18079. /* msg, msg length */
  18080. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18081. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18082. /* ct, ct length */
  18083. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18084. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18085. /* tag, tag length */
  18086. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18087. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18088. /* valid */
  18089. 0,
  18090. },
  18091. {
  18092. /* key, key length */
  18093. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18094. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18095. /* iv, iv length */
  18096. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18097. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18098. /* aad, aad length */
  18099. {0x00}, 0,
  18100. /* msg, msg length */
  18101. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18102. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18103. /* ct, ct length */
  18104. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18105. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18106. /* tag, tag length */
  18107. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18108. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18109. /* valid */
  18110. 0,
  18111. },
  18112. {
  18113. /* key, key length */
  18114. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18115. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18116. /* iv, iv length */
  18117. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18118. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18119. /* aad, aad length */
  18120. {0x00}, 0,
  18121. /* msg, msg length */
  18122. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18123. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18124. /* ct, ct length */
  18125. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18126. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18127. /* tag, tag length */
  18128. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18129. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18130. /* valid */
  18131. 0,
  18132. },
  18133. {
  18134. /* key, key length */
  18135. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18136. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18137. /* iv, iv length */
  18138. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18139. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18140. /* aad, aad length */
  18141. {0x00}, 0,
  18142. /* msg, msg length */
  18143. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18144. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18145. /* ct, ct length */
  18146. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18147. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18148. /* tag, tag length */
  18149. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18150. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18151. /* valid */
  18152. 0,
  18153. },
  18154. {
  18155. /* key, key length */
  18156. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18157. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18158. /* iv, iv length */
  18159. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18160. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18161. /* aad, aad length */
  18162. {0x00}, 0,
  18163. /* msg, msg length */
  18164. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18165. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18166. /* ct, ct length */
  18167. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18168. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18169. /* tag, tag length */
  18170. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18171. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18172. /* valid */
  18173. 0,
  18174. },
  18175. {
  18176. /* key, key length */
  18177. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18178. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18179. /* iv, iv length */
  18180. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18181. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18182. /* aad, aad length */
  18183. {0x00}, 0,
  18184. /* msg, msg length */
  18185. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18186. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18187. /* ct, ct length */
  18188. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18189. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18190. /* tag, tag length */
  18191. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18192. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18193. /* valid */
  18194. 0,
  18195. },
  18196. {
  18197. /* key, key length */
  18198. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18199. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18200. /* iv, iv length */
  18201. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18202. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18203. /* aad, aad length */
  18204. {0x00}, 0,
  18205. /* msg, msg length */
  18206. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18207. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18208. /* ct, ct length */
  18209. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18210. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18211. /* tag, tag length */
  18212. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18213. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18214. /* valid */
  18215. 0,
  18216. },
  18217. {
  18218. /* key, key length */
  18219. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18220. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18221. /* iv, iv length */
  18222. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18223. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18224. /* aad, aad length */
  18225. {0x00}, 0,
  18226. /* msg, msg length */
  18227. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18228. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18229. /* ct, ct length */
  18230. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18231. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18232. /* tag, tag length */
  18233. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18235. /* valid */
  18236. 0,
  18237. },
  18238. {
  18239. /* key, key length */
  18240. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18241. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18242. /* iv, iv length */
  18243. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18244. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18245. /* aad, aad length */
  18246. {0x00}, 0,
  18247. /* msg, msg length */
  18248. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18249. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18250. /* ct, ct length */
  18251. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18252. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18253. /* tag, tag length */
  18254. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18255. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18256. /* valid */
  18257. 0,
  18258. },
  18259. {
  18260. /* key, key length */
  18261. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18262. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18263. /* iv, iv length */
  18264. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18265. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18266. /* aad, aad length */
  18267. {0x00}, 0,
  18268. /* msg, msg length */
  18269. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18270. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18271. /* ct, ct length */
  18272. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18273. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18274. /* tag, tag length */
  18275. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18276. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18277. /* valid */
  18278. 0,
  18279. },
  18280. {
  18281. /* key, key length */
  18282. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18283. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18284. /* iv, iv length */
  18285. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18286. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18287. /* aad, aad length */
  18288. {0x00}, 0,
  18289. /* msg, msg length */
  18290. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18291. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18292. /* ct, ct length */
  18293. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18294. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18295. /* tag, tag length */
  18296. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18297. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18298. /* valid */
  18299. 0,
  18300. },
  18301. };
  18302. byte ciphertext[sizeof(vectors[0].ct)];
  18303. byte authtag[sizeof(vectors[0].tag)];
  18304. int i;
  18305. int len;
  18306. int ret;
  18307. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18308. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18309. len = sizeof(authtag);
  18310. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18311. ciphertext,
  18312. vectors[i].msg, vectors[i].msg_length,
  18313. vectors[i].iv, vectors[i].iv_length,
  18314. authtag, len,
  18315. vectors[i].aad, vectors[i].aad_length),
  18316. 0);
  18317. /* check ciphertext matches vector */
  18318. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18319. 0);
  18320. /* check that computed tag matches vector only for vectors marked as valid */
  18321. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18322. if (vectors[i].valid) {
  18323. ExpectIntEQ(ret, 0);
  18324. }
  18325. else {
  18326. ExpectIntNE(ret, 0);
  18327. }
  18328. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18329. /* Decrypt, checking that the computed auth tags match */
  18330. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18331. ciphertext,
  18332. vectors[i].ct, vectors[i].ct_length,
  18333. vectors[i].iv, vectors[i].iv_length,
  18334. authtag, len,
  18335. vectors[i].aad, vectors[i].aad_length),
  18336. 0);
  18337. /* check decrypted ciphertext matches vector plaintext */
  18338. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18339. 0);
  18340. }
  18341. return EXPECT_RESULT();
  18342. } /* END test_wc_AesEaxVectors */
  18343. /*
  18344. * Testing test_wc_AesEaxEncryptAuth()
  18345. */
  18346. static int test_wc_AesEaxEncryptAuth(void)
  18347. {
  18348. EXPECT_DECLS;
  18349. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18350. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18351. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18352. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18353. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18354. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18355. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18356. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18357. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18358. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18359. byte ciphertext[sizeof(msg)];
  18360. byte authtag[AES_BLOCK_SIZE];
  18361. int i;
  18362. int len;
  18363. len = sizeof(authtag);
  18364. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18365. ciphertext,
  18366. msg, sizeof(msg),
  18367. iv, sizeof(iv),
  18368. authtag, len,
  18369. aad, sizeof(aad)),
  18370. 0);
  18371. /* Test null checking */
  18372. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18373. ciphertext,
  18374. msg, sizeof(msg),
  18375. iv, sizeof(iv),
  18376. authtag, len,
  18377. aad, sizeof(aad)),
  18378. BAD_FUNC_ARG);
  18379. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18380. NULL,
  18381. msg, sizeof(msg),
  18382. iv, sizeof(iv),
  18383. authtag, len,
  18384. aad, sizeof(aad)),
  18385. BAD_FUNC_ARG);
  18386. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18387. ciphertext,
  18388. NULL, sizeof(msg),
  18389. iv, sizeof(iv),
  18390. authtag, len,
  18391. aad, sizeof(aad)),
  18392. BAD_FUNC_ARG);
  18393. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18394. ciphertext,
  18395. msg, sizeof(msg),
  18396. NULL, sizeof(iv),
  18397. authtag, len,
  18398. aad, sizeof(aad)),
  18399. BAD_FUNC_ARG);
  18400. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18401. ciphertext,
  18402. msg, sizeof(msg),
  18403. iv, sizeof(iv),
  18404. NULL, len,
  18405. aad, sizeof(aad)),
  18406. BAD_FUNC_ARG);
  18407. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18408. ciphertext,
  18409. msg, sizeof(msg),
  18410. iv, sizeof(iv),
  18411. authtag, len,
  18412. NULL, sizeof(aad)),
  18413. BAD_FUNC_ARG);
  18414. /* Test bad key lengths */
  18415. for (i = 0; i <= 32; i++) {
  18416. int exp_ret;
  18417. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18418. || i == AES_256_KEY_SIZE) {
  18419. exp_ret = 0;
  18420. }
  18421. else {
  18422. exp_ret = BAD_FUNC_ARG;
  18423. }
  18424. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18425. ciphertext,
  18426. msg, sizeof(msg),
  18427. iv, sizeof(iv),
  18428. authtag, len,
  18429. aad, sizeof(aad)),
  18430. exp_ret);
  18431. }
  18432. /* Test auth tag size out of range */
  18433. len = AES_BLOCK_SIZE + 1;
  18434. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18435. ciphertext,
  18436. msg, sizeof(msg),
  18437. iv, sizeof(iv),
  18438. authtag, len,
  18439. aad, sizeof(aad)),
  18440. BAD_FUNC_ARG);
  18441. return EXPECT_RESULT();
  18442. } /* END test_wc_AesEaxEncryptAuth() */
  18443. /*
  18444. * Testing test_wc_AesEaxDecryptAuth()
  18445. */
  18446. static int test_wc_AesEaxDecryptAuth(void)
  18447. {
  18448. EXPECT_DECLS;
  18449. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18450. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18451. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18452. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18453. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18454. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18455. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18456. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18457. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18458. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18459. /* Garbage tag that should always fail for above aad */
  18460. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18461. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18462. byte plaintext[sizeof(ct)];
  18463. int i;
  18464. int len;
  18465. len = sizeof(tag);
  18466. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18467. plaintext,
  18468. ct, sizeof(ct),
  18469. iv, sizeof(iv),
  18470. tag, len,
  18471. aad, sizeof(aad)),
  18472. AES_EAX_AUTH_E);
  18473. /* Test null checking */
  18474. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18475. plaintext,
  18476. ct, sizeof(ct),
  18477. iv, sizeof(iv),
  18478. tag, len,
  18479. aad, sizeof(aad)),
  18480. BAD_FUNC_ARG);
  18481. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18482. NULL,
  18483. ct, sizeof(ct),
  18484. iv, sizeof(iv),
  18485. tag, len,
  18486. aad, sizeof(aad)),
  18487. BAD_FUNC_ARG);
  18488. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18489. plaintext,
  18490. NULL, sizeof(ct),
  18491. iv, sizeof(iv),
  18492. tag, len,
  18493. aad, sizeof(aad)),
  18494. BAD_FUNC_ARG);
  18495. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18496. plaintext,
  18497. ct, sizeof(ct),
  18498. NULL, sizeof(iv),
  18499. tag, len,
  18500. aad, sizeof(aad)),
  18501. BAD_FUNC_ARG);
  18502. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18503. plaintext,
  18504. ct, sizeof(ct),
  18505. iv, sizeof(iv),
  18506. NULL, len,
  18507. aad, sizeof(aad)),
  18508. BAD_FUNC_ARG);
  18509. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18510. plaintext,
  18511. ct, sizeof(ct),
  18512. iv, sizeof(iv),
  18513. tag, len,
  18514. NULL, sizeof(aad)),
  18515. BAD_FUNC_ARG);
  18516. /* Test bad key lengths */
  18517. for (i = 0; i <= 32; i++) {
  18518. int exp_ret;
  18519. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18520. || i == AES_256_KEY_SIZE) {
  18521. exp_ret = AES_EAX_AUTH_E;
  18522. }
  18523. else {
  18524. exp_ret = BAD_FUNC_ARG;
  18525. }
  18526. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18527. plaintext,
  18528. ct, sizeof(ct),
  18529. iv, sizeof(iv),
  18530. tag, len,
  18531. aad, sizeof(aad)),
  18532. exp_ret);
  18533. }
  18534. /* Test auth tag size out of range */
  18535. len = AES_BLOCK_SIZE + 1;
  18536. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18537. plaintext,
  18538. ct, sizeof(ct),
  18539. iv, sizeof(iv),
  18540. tag, len,
  18541. aad, sizeof(aad)),
  18542. BAD_FUNC_ARG);
  18543. return EXPECT_RESULT();
  18544. } /* END test_wc_AesEaxDecryptAuth() */
  18545. #endif /* WOLFSSL_AES_EAX &&
  18546. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  18547. */
  18548. /*
  18549. * Testing wc_InitDsaKey()
  18550. */
  18551. static int test_wc_InitDsaKey(void)
  18552. {
  18553. EXPECT_DECLS;
  18554. #ifndef NO_DSA
  18555. DsaKey key;
  18556. XMEMSET(&key, 0, sizeof(DsaKey));
  18557. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18558. /* Pass in bad args. */
  18559. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  18560. wc_FreeDsaKey(&key);
  18561. #endif
  18562. return EXPECT_RESULT();
  18563. } /* END test_wc_InitDsaKey */
  18564. /*
  18565. * Testing wc_DsaSign() and wc_DsaVerify()
  18566. */
  18567. static int test_wc_DsaSignVerify(void)
  18568. {
  18569. EXPECT_DECLS;
  18570. #if !defined(NO_DSA)
  18571. DsaKey key;
  18572. WC_RNG rng;
  18573. wc_Sha sha;
  18574. byte signature[DSA_SIG_SIZE];
  18575. byte hash[WC_SHA_DIGEST_SIZE];
  18576. word32 idx = 0;
  18577. word32 bytes;
  18578. int answer;
  18579. #ifdef USE_CERT_BUFFERS_1024
  18580. byte tmp[ONEK_BUF];
  18581. XMEMSET(tmp, 0, sizeof(tmp));
  18582. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18583. bytes = sizeof_dsa_key_der_1024;
  18584. #elif defined(USE_CERT_BUFFERS_2048)
  18585. byte tmp[TWOK_BUF];
  18586. XMEMSET(tmp, 0, sizeof(tmp));
  18587. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18588. bytes = sizeof_dsa_key_der_2048;
  18589. #else
  18590. byte tmp[TWOK_BUF];
  18591. XFILE fp = XBADFILE;
  18592. XMEMSET(tmp, 0, sizeof(tmp));
  18593. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18594. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18595. if (fp != XBADFILE)
  18596. XFCLOSE(fp);
  18597. #endif /* END USE_CERT_BUFFERS_1024 */
  18598. ExpectIntEQ(wc_InitSha(&sha), 0);
  18599. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  18600. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  18601. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18602. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18603. ExpectIntEQ(wc_InitRng(&rng), 0);
  18604. /* Sign. */
  18605. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  18606. /* Test bad args. */
  18607. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  18608. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  18609. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  18610. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18611. /* Verify. */
  18612. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  18613. ExpectIntEQ(answer, 1);
  18614. /* Pass in bad args. */
  18615. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  18616. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  18617. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  18618. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18619. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18620. /* hard set q to 0 and test fail case */
  18621. mp_free(&key.q);
  18622. mp_init(&key.q);
  18623. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18624. mp_set(&key.q, 1);
  18625. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18626. #endif
  18627. DoExpectIntEQ(wc_FreeRng(&rng),0);
  18628. wc_FreeDsaKey(&key);
  18629. wc_ShaFree(&sha);
  18630. #endif
  18631. return EXPECT_RESULT();
  18632. } /* END test_wc_DsaSign */
  18633. /*
  18634. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18635. */
  18636. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18637. {
  18638. EXPECT_DECLS;
  18639. #if !defined(NO_DSA)
  18640. DsaKey key;
  18641. word32 bytes;
  18642. word32 idx = 0;
  18643. int ret;
  18644. #ifdef USE_CERT_BUFFERS_1024
  18645. byte tmp[ONEK_BUF];
  18646. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18647. bytes = sizeof_dsa_key_der_1024;
  18648. #elif defined(USE_CERT_BUFFERS_2048)
  18649. byte tmp[TWOK_BUF];
  18650. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18651. bytes = sizeof_dsa_key_der_2048;
  18652. #else
  18653. byte tmp[TWOK_BUF];
  18654. XFILE fp = XBADFILE;
  18655. XMEMSET(tmp, 0, sizeof(tmp));
  18656. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18657. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18658. if (fp != XBADFILE)
  18659. XFCLOSE(fp);
  18660. #endif /* END USE_CERT_BUFFERS_1024 */
  18661. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18662. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18663. /* Test bad args. */
  18664. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18665. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18666. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18667. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18668. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18669. wc_FreeDsaKey(&key);
  18670. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18671. idx = 0; /* Reset */
  18672. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18673. /* Test bad args. */
  18674. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18675. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18676. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18677. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18678. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18679. wc_FreeDsaKey(&key);
  18680. #endif /* !NO_DSA */
  18681. return EXPECT_RESULT();
  18682. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18683. /*
  18684. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18685. */
  18686. static int test_wc_MakeDsaKey(void)
  18687. {
  18688. EXPECT_DECLS;
  18689. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18690. DsaKey genKey;
  18691. WC_RNG rng;
  18692. XMEMSET(&genKey, 0, sizeof(genKey));
  18693. XMEMSET(&rng, 0, sizeof(rng));
  18694. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  18695. ExpectIntEQ(wc_InitRng(&rng), 0);
  18696. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  18697. /* Test bad args. */
  18698. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  18699. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  18700. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  18701. BAD_FUNC_ARG);
  18702. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  18703. /* Test bad args. */
  18704. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  18705. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  18706. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18707. wc_FreeDsaKey(&genKey);
  18708. #endif
  18709. return EXPECT_RESULT();
  18710. } /* END test_wc_MakeDsaKey */
  18711. /*
  18712. * Testing wc_DsaKeyToDer()
  18713. */
  18714. static int test_wc_DsaKeyToDer(void)
  18715. {
  18716. EXPECT_DECLS;
  18717. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18718. DsaKey key;
  18719. word32 bytes;
  18720. word32 idx = 0;
  18721. #ifdef USE_CERT_BUFFERS_1024
  18722. byte tmp[ONEK_BUF];
  18723. byte der[ONEK_BUF];
  18724. XMEMSET(tmp, 0, sizeof(tmp));
  18725. XMEMSET(der, 0, sizeof(der));
  18726. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18727. bytes = sizeof_dsa_key_der_1024;
  18728. #elif defined(USE_CERT_BUFFERS_2048)
  18729. byte tmp[TWOK_BUF];
  18730. byte der[TWOK_BUF];
  18731. XMEMSET(tmp, 0, sizeof(tmp));
  18732. XMEMSET(der, 0, sizeof(der));
  18733. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18734. bytes = sizeof_dsa_key_der_2048;
  18735. #else
  18736. byte tmp[TWOK_BUF];
  18737. byte der[TWOK_BUF];
  18738. XFILE fp = XBADFILE;
  18739. XMEMSET(tmp, 0, sizeof(tmp));
  18740. XMEMSET(der, 0, sizeof(der));
  18741. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18742. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18743. if (fp != XBADFILE)
  18744. XFCLOSE(fp);
  18745. #endif /* END USE_CERT_BUFFERS_1024 */
  18746. XMEMSET(&key, 0, sizeof(DsaKey));
  18747. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18748. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18749. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  18750. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  18751. /* Test bad args. */
  18752. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18753. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18754. wc_FreeDsaKey(&key);
  18755. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18756. return EXPECT_RESULT();
  18757. } /* END test_wc_DsaKeyToDer */
  18758. /*
  18759. * Testing wc_DsaKeyToPublicDer()
  18760. * (indirectly testing setDsaPublicKey())
  18761. */
  18762. static int test_wc_DsaKeyToPublicDer(void)
  18763. {
  18764. EXPECT_DECLS;
  18765. #ifndef HAVE_SELFTEST
  18766. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18767. DsaKey key;
  18768. WC_RNG rng;
  18769. byte* der = NULL;
  18770. word32 sz = 0;
  18771. word32 idx = 0;
  18772. XMEMSET(&key, 0, sizeof(DsaKey));
  18773. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18774. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  18775. DYNAMIC_TYPE_TMP_BUFFER));
  18776. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18777. ExpectIntEQ(wc_InitRng(&rng), 0);
  18778. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  18779. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18780. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  18781. wc_FreeDsaKey(&key);
  18782. idx = 0;
  18783. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18784. /* Test without the SubjectPublicKeyInfo header */
  18785. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  18786. wc_FreeDsaKey(&key);
  18787. idx = 0;
  18788. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18789. /* Test bad args. */
  18790. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18791. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18792. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18793. wc_FreeDsaKey(&key);
  18794. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18795. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18796. #endif /* !HAVE_SELFTEST */
  18797. return EXPECT_RESULT();
  18798. } /* END test_wc_DsaKeyToPublicDer */
  18799. /*
  18800. * Testing wc_DsaImportParamsRaw()
  18801. */
  18802. static int test_wc_DsaImportParamsRaw(void)
  18803. {
  18804. EXPECT_DECLS;
  18805. #if !defined(NO_DSA)
  18806. DsaKey key;
  18807. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18808. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18809. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18810. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18811. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18812. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18813. "80a0093113a8bd73";
  18814. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18815. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18816. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18817. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18818. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18819. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18820. "76341a7e7d9";
  18821. /* invalid p and q parameters */
  18822. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18823. const char* invalidQ = "96c5390a";
  18824. XMEMSET(&key, 0, sizeof(DsaKey));
  18825. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18826. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18827. /* test bad args */
  18828. /* null key struct */
  18829. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  18830. /* null param pointers */
  18831. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  18832. /* illegal p length */
  18833. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  18834. /* illegal q length */
  18835. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  18836. wc_FreeDsaKey(&key);
  18837. #endif
  18838. return EXPECT_RESULT();
  18839. } /* END test_wc_DsaImportParamsRaw */
  18840. /*
  18841. * Testing wc_DsaImportParamsRawCheck()
  18842. */
  18843. static int test_wc_DsaImportParamsRawCheck(void)
  18844. {
  18845. EXPECT_DECLS;
  18846. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18847. DsaKey key;
  18848. int trusted = 0;
  18849. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18850. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18851. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18852. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18853. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18854. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18855. "80a0093113a8bd73";
  18856. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18857. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18858. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18859. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18860. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18861. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18862. "76341a7e7d9";
  18863. /* invalid p and q parameters */
  18864. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18865. const char* invalidQ = "96c5390a";
  18866. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18867. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  18868. /* test bad args */
  18869. /* null key struct */
  18870. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  18871. BAD_FUNC_ARG);
  18872. /* null param pointers */
  18873. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  18874. NULL), BAD_FUNC_ARG);
  18875. /* illegal p length */
  18876. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  18877. BAD_FUNC_ARG);
  18878. /* illegal q length */
  18879. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  18880. BAD_FUNC_ARG);
  18881. wc_FreeDsaKey(&key);
  18882. #endif
  18883. return EXPECT_RESULT();
  18884. } /* END test_wc_DsaImportParamsRawCheck */
  18885. /*
  18886. * Testing wc_DsaExportParamsRaw()
  18887. */
  18888. static int test_wc_DsaExportParamsRaw(void)
  18889. {
  18890. EXPECT_DECLS;
  18891. #if !defined(NO_DSA)
  18892. DsaKey key;
  18893. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18894. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18895. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18896. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18897. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18898. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18899. "80a0093113a8bd73";
  18900. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18901. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18902. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18903. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18904. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18905. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18906. "76341a7e7d9";
  18907. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18908. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18909. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18910. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18911. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18912. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18913. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18914. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18915. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18916. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18917. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18918. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18919. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18920. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18921. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18922. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18923. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18924. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18925. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18926. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18927. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18928. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18929. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18930. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18931. byte pOut[MAX_DSA_PARAM_SIZE];
  18932. byte qOut[MAX_DSA_PARAM_SIZE];
  18933. byte gOut[MAX_DSA_PARAM_SIZE];
  18934. word32 pOutSz;
  18935. word32 qOutSz;
  18936. word32 gOutSz;
  18937. XMEMSET(&key, 0, sizeof(DsaKey));
  18938. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18939. /* first test using imported raw parameters, for expected */
  18940. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18941. pOutSz = sizeof(pOut);
  18942. qOutSz = sizeof(qOut);
  18943. gOutSz = sizeof(gOut);
  18944. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18945. &gOutSz), 0);
  18946. /* validate exported parameters are correct */
  18947. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  18948. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  18949. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  18950. /* test bad args */
  18951. /* null key struct */
  18952. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18953. &gOutSz), BAD_FUNC_ARG);
  18954. /* null output pointers */
  18955. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  18956. &gOutSz), LENGTH_ONLY_E);
  18957. /* null output size pointers */
  18958. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  18959. NULL), BAD_FUNC_ARG);
  18960. /* p output buffer size too small */
  18961. pOutSz = 1;
  18962. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18963. &gOutSz), BUFFER_E);
  18964. pOutSz = sizeof(pOut);
  18965. /* q output buffer size too small */
  18966. qOutSz = 1;
  18967. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18968. &gOutSz), BUFFER_E);
  18969. qOutSz = sizeof(qOut);
  18970. /* g output buffer size too small */
  18971. gOutSz = 1;
  18972. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18973. &gOutSz), BUFFER_E);
  18974. wc_FreeDsaKey(&key);
  18975. #endif
  18976. return EXPECT_RESULT();
  18977. } /* END test_wc_DsaExportParamsRaw */
  18978. /*
  18979. * Testing wc_DsaExportKeyRaw()
  18980. */
  18981. static int test_wc_DsaExportKeyRaw(void)
  18982. {
  18983. EXPECT_DECLS;
  18984. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18985. DsaKey key;
  18986. WC_RNG rng;
  18987. byte xOut[MAX_DSA_PARAM_SIZE];
  18988. byte yOut[MAX_DSA_PARAM_SIZE];
  18989. word32 xOutSz, yOutSz;
  18990. XMEMSET(&key, 0, sizeof(key));
  18991. XMEMSET(&rng, 0, sizeof(rng));
  18992. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18993. ExpectIntEQ(wc_InitRng(&rng), 0);
  18994. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  18995. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18996. /* try successful export */
  18997. xOutSz = sizeof(xOut);
  18998. yOutSz = sizeof(yOut);
  18999. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19000. /* test bad args */
  19001. /* null key struct */
  19002. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19003. BAD_FUNC_ARG);
  19004. /* null output pointers */
  19005. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19006. LENGTH_ONLY_E);
  19007. /* null output size pointers */
  19008. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19009. BAD_FUNC_ARG);
  19010. /* x output buffer size too small */
  19011. xOutSz = 1;
  19012. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19013. BUFFER_E);
  19014. xOutSz = sizeof(xOut);
  19015. /* y output buffer size too small */
  19016. yOutSz = 1;
  19017. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19018. BUFFER_E);
  19019. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19020. wc_FreeDsaKey(&key);
  19021. #endif
  19022. return EXPECT_RESULT();
  19023. } /* END test_wc_DsaExportParamsRaw */
  19024. /*
  19025. * Testing wc_ed25519_make_key().
  19026. */
  19027. static int test_wc_ed25519_make_key(void)
  19028. {
  19029. EXPECT_DECLS;
  19030. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19031. ed25519_key key;
  19032. WC_RNG rng;
  19033. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19034. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19035. XMEMSET(&key, 0, sizeof(ed25519_key));
  19036. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19037. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19038. ExpectIntEQ(wc_InitRng(&rng), 0);
  19039. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19040. ECC_PRIV_KEY_E);
  19041. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19042. ECC_PRIV_KEY_E);
  19043. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19044. /* Test bad args. */
  19045. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19046. BAD_FUNC_ARG);
  19047. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19048. BAD_FUNC_ARG);
  19049. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19050. BAD_FUNC_ARG);
  19051. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19052. BAD_FUNC_ARG);
  19053. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19054. wc_ed25519_free(&key);
  19055. #endif
  19056. return EXPECT_RESULT();
  19057. } /* END test_wc_ed25519_make_key */
  19058. /*
  19059. * Testing wc_ed25519_init()
  19060. */
  19061. static int test_wc_ed25519_init(void)
  19062. {
  19063. EXPECT_DECLS;
  19064. #if defined(HAVE_ED25519)
  19065. ed25519_key key;
  19066. XMEMSET(&key, 0, sizeof(ed25519_key));
  19067. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19068. /* Test bad args. */
  19069. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19070. wc_ed25519_free(&key);
  19071. #endif
  19072. return EXPECT_RESULT();
  19073. } /* END test_wc_ed25519_init */
  19074. /*
  19075. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19076. */
  19077. static int test_wc_ed25519_sign_msg(void)
  19078. {
  19079. EXPECT_DECLS;
  19080. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19081. WC_RNG rng;
  19082. ed25519_key key;
  19083. byte msg[] = "Everybody gets Friday off.\n";
  19084. byte sig[ED25519_SIG_SIZE+1];
  19085. word32 msglen = sizeof(msg);
  19086. word32 siglen = ED25519_SIG_SIZE;
  19087. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19088. #ifdef HAVE_ED25519_VERIFY
  19089. int verify_ok = 0; /*1 = Verify success.*/
  19090. #endif
  19091. /* Initialize stack variables. */
  19092. XMEMSET(&key, 0, sizeof(ed25519_key));
  19093. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19094. XMEMSET(sig, 0, sizeof(sig));
  19095. /* Initialize key. */
  19096. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19097. ExpectIntEQ(wc_InitRng(&rng), 0);
  19098. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19099. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19100. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19101. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19102. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19103. /* Test bad args. */
  19104. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19105. BAD_FUNC_ARG);
  19106. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19107. BAD_FUNC_ARG);
  19108. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19109. BAD_FUNC_ARG);
  19110. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19111. BAD_FUNC_ARG);
  19112. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19113. BUFFER_E);
  19114. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19115. badSigLen -= 1;
  19116. #ifdef HAVE_ED25519_VERIFY
  19117. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19118. &key), 0);
  19119. ExpectIntEQ(verify_ok, 1);
  19120. /* Test bad args. */
  19121. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19122. &verify_ok, &key), BAD_FUNC_ARG);
  19123. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19124. &verify_ok, &key), BAD_FUNC_ARG);
  19125. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19126. &key), BAD_FUNC_ARG);
  19127. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19128. &key), BAD_FUNC_ARG);
  19129. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19130. BAD_FUNC_ARG);
  19131. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19132. NULL), BAD_FUNC_ARG);
  19133. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19134. &key), BAD_FUNC_ARG);
  19135. #endif /* Verify. */
  19136. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19137. wc_ed25519_free(&key);
  19138. #endif
  19139. return EXPECT_RESULT();
  19140. } /* END test_wc_ed25519_sign_msg */
  19141. /*
  19142. * Testing wc_ed25519_import_public()
  19143. */
  19144. static int test_wc_ed25519_import_public(void)
  19145. {
  19146. EXPECT_DECLS;
  19147. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19148. ed25519_key pubKey;
  19149. WC_RNG rng;
  19150. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19151. word32 inlen = sizeof(in);
  19152. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19153. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19154. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19155. ExpectIntEQ(wc_InitRng(&rng), 0);
  19156. #ifdef HAVE_ED25519_MAKE_KEY
  19157. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19158. #endif
  19159. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19160. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19161. /* Test bad args. */
  19162. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19163. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19164. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19165. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19166. wc_ed25519_free(&pubKey);
  19167. #endif
  19168. return EXPECT_RESULT();
  19169. } /* END wc_ed25519_import_public */
  19170. /*
  19171. * Testing wc_ed25519_import_private_key()
  19172. */
  19173. static int test_wc_ed25519_import_private_key(void)
  19174. {
  19175. EXPECT_DECLS;
  19176. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19177. ed25519_key key;
  19178. WC_RNG rng;
  19179. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19180. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19181. word32 privKeySz = sizeof(privKey);
  19182. word32 pubKeySz = sizeof(pubKey);
  19183. #ifdef HAVE_ED25519_KEY_EXPORT
  19184. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19185. word32 bothKeysSz = sizeof(bothKeys);
  19186. #endif
  19187. XMEMSET(&key, 0, sizeof(ed25519_key));
  19188. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19189. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19190. ExpectIntEQ(wc_InitRng(&rng), 0);
  19191. #ifdef HAVE_ED25519_MAKE_KEY
  19192. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19193. #endif
  19194. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19195. pubKeySz, &key, 1), 0);
  19196. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19197. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19198. #ifdef HAVE_ED25519_KEY_EXPORT
  19199. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19200. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19201. &key, 1), 0);
  19202. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19203. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19204. #endif
  19205. /* Test bad args. */
  19206. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19207. &key), BAD_FUNC_ARG);
  19208. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19209. pubKeySz, &key), BAD_FUNC_ARG);
  19210. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19211. pubKeySz, NULL), BAD_FUNC_ARG);
  19212. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19213. pubKeySz, &key), BAD_FUNC_ARG);
  19214. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19215. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19216. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19217. &key), BAD_FUNC_ARG);
  19218. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19219. wc_ed25519_free(&key);
  19220. #endif
  19221. return EXPECT_RESULT();
  19222. } /* END test_wc_ed25519_import_private_key */
  19223. /*
  19224. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19225. */
  19226. static int test_wc_ed25519_export(void)
  19227. {
  19228. EXPECT_DECLS;
  19229. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19230. ed25519_key key;
  19231. WC_RNG rng;
  19232. byte priv[ED25519_PRV_KEY_SIZE];
  19233. byte pub[ED25519_PUB_KEY_SIZE];
  19234. word32 privSz = sizeof(priv);
  19235. word32 pubSz = sizeof(pub);
  19236. #ifndef HAVE_ED25519_MAKE_KEY
  19237. const byte privKey[] = {
  19238. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19239. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19240. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19241. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19242. };
  19243. const byte pubKey[] = {
  19244. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19245. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19246. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19247. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19248. };
  19249. #endif
  19250. XMEMSET(&key, 0, sizeof(ed25519_key));
  19251. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19252. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19253. ExpectIntEQ(wc_InitRng(&rng), 0);
  19254. #ifdef HAVE_ED25519_MAKE_KEY
  19255. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19256. #else
  19257. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19258. pubKey, sizeof(pubKey), &key, 1), 0);
  19259. #endif
  19260. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19261. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19262. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19263. /* Test bad args. */
  19264. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19265. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19266. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19267. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19268. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19269. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19270. /* Test bad args. */
  19271. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19272. BAD_FUNC_ARG);
  19273. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19274. BAD_FUNC_ARG);
  19275. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19276. BAD_FUNC_ARG);
  19277. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19278. wc_ed25519_free(&key);
  19279. #endif
  19280. return EXPECT_RESULT();
  19281. } /* END test_wc_ed25519_export */
  19282. /*
  19283. * Testing wc_ed25519_size()
  19284. */
  19285. static int test_wc_ed25519_size(void)
  19286. {
  19287. EXPECT_DECLS;
  19288. #if defined(HAVE_ED25519)
  19289. ed25519_key key;
  19290. WC_RNG rng;
  19291. #ifndef HAVE_ED25519_MAKE_KEY
  19292. const byte privKey[] = {
  19293. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19294. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19295. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19296. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19297. };
  19298. const byte pubKey[] = {
  19299. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19300. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19301. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19302. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19303. };
  19304. #endif
  19305. XMEMSET(&key, 0, sizeof(ed25519_key));
  19306. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19307. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19308. ExpectIntEQ(wc_InitRng(&rng), 0);
  19309. #ifdef HAVE_ED25519_MAKE_KEY
  19310. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19311. #else
  19312. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19313. pubKey, sizeof(pubKey), &key, 1), 0);
  19314. #endif
  19315. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19316. /* Test bad args. */
  19317. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19318. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19319. /* Test bad args. */
  19320. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19321. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19322. /* Test bad args. */
  19323. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19324. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19325. /* Test bad args. */
  19326. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19327. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19328. wc_ed25519_free(&key);
  19329. #endif
  19330. return EXPECT_RESULT();
  19331. } /* END test_wc_ed25519_size */
  19332. /*
  19333. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19334. */
  19335. static int test_wc_ed25519_exportKey(void)
  19336. {
  19337. EXPECT_DECLS;
  19338. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19339. WC_RNG rng;
  19340. ed25519_key key;
  19341. byte priv[ED25519_PRV_KEY_SIZE];
  19342. byte pub[ED25519_PUB_KEY_SIZE];
  19343. byte privOnly[ED25519_PRV_KEY_SIZE];
  19344. word32 privSz = sizeof(priv);
  19345. word32 pubSz = sizeof(pub);
  19346. word32 privOnlySz = sizeof(privOnly);
  19347. #ifndef HAVE_ED25519_MAKE_KEY
  19348. const byte privKey[] = {
  19349. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19350. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19351. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19352. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19353. };
  19354. const byte pubKey[] = {
  19355. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19356. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19357. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19358. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19359. };
  19360. #endif
  19361. XMEMSET(&key, 0, sizeof(ed25519_key));
  19362. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19363. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19364. ExpectIntEQ(wc_InitRng(&rng), 0);
  19365. #ifdef HAVE_ED25519_MAKE_KEY
  19366. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19367. #else
  19368. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19369. pubKey, sizeof(pubKey), &key, 1), 0);
  19370. #endif
  19371. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19372. /* Test bad args. */
  19373. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19374. BAD_FUNC_ARG);
  19375. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19376. BAD_FUNC_ARG);
  19377. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19378. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19379. /* Test bad args. */
  19380. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19381. BAD_FUNC_ARG);
  19382. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19383. BAD_FUNC_ARG);
  19384. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19385. BAD_FUNC_ARG);
  19386. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19387. BAD_FUNC_ARG);
  19388. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19389. BAD_FUNC_ARG);
  19390. /* Cross check output. */
  19391. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19392. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19393. wc_ed25519_free(&key);
  19394. #endif
  19395. return EXPECT_RESULT();
  19396. } /* END test_wc_ed25519_exportKey */
  19397. /*
  19398. * Testing wc_Ed25519PublicKeyToDer
  19399. */
  19400. static int test_wc_Ed25519PublicKeyToDer(void)
  19401. {
  19402. EXPECT_DECLS;
  19403. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19404. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19405. ed25519_key key;
  19406. byte derBuf[1024];
  19407. XMEMSET(&key, 0, sizeof(ed25519_key));
  19408. /* Test bad args */
  19409. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19410. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19411. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19412. wc_ed25519_free(&key);
  19413. /* Test good args */
  19414. if (EXPECT_SUCCESS()) {
  19415. WC_RNG rng;
  19416. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19417. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19418. ExpectIntEQ(wc_InitRng(&rng), 0);
  19419. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19420. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19421. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19422. wc_ed25519_free(&key);
  19423. }
  19424. #endif
  19425. return EXPECT_RESULT();
  19426. } /* END testing wc_Ed25519PublicKeyToDer */
  19427. /*
  19428. * Testing wc_curve25519_init and wc_curve25519_free.
  19429. */
  19430. static int test_wc_curve25519_init(void)
  19431. {
  19432. EXPECT_DECLS;
  19433. #if defined(HAVE_CURVE25519)
  19434. curve25519_key key;
  19435. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19436. /* Test bad args for wc_curve25519_init */
  19437. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19438. /* Test good args for wc_curve_25519_free */
  19439. wc_curve25519_free(&key);
  19440. /* Test bad args for wc_curve25519 free. */
  19441. wc_curve25519_free(NULL);
  19442. #endif
  19443. return EXPECT_RESULT();
  19444. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19445. /*
  19446. * Testing test_wc_curve25519_size.
  19447. */
  19448. static int test_wc_curve25519_size(void)
  19449. {
  19450. EXPECT_DECLS;
  19451. #if defined(HAVE_CURVE25519)
  19452. curve25519_key key;
  19453. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19454. /* Test good args for wc_curve25519_size */
  19455. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19456. /* Test bad args for wc_curve25519_size */
  19457. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19458. wc_curve25519_free(&key);
  19459. #endif
  19460. return EXPECT_RESULT();
  19461. } /* END test_wc_curve25519_size*/
  19462. /*
  19463. * Testing test_wc_curve25519_export_key_raw().
  19464. */
  19465. static int test_wc_curve25519_export_key_raw(void)
  19466. {
  19467. EXPECT_DECLS;
  19468. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19469. curve25519_key key;
  19470. WC_RNG rng;
  19471. byte privateKey[CURVE25519_KEYSIZE];
  19472. byte publicKey[CURVE25519_KEYSIZE];
  19473. word32 prvkSz;
  19474. word32 pubkSz;
  19475. byte prik[CURVE25519_KEYSIZE];
  19476. byte pubk[CURVE25519_KEYSIZE];
  19477. word32 prksz;
  19478. word32 pbksz;
  19479. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19480. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19481. ExpectIntEQ(wc_InitRng(&rng), 0);
  19482. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19483. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19484. prvkSz = CURVE25519_KEYSIZE;
  19485. pubkSz = CURVE25519_KEYSIZE;
  19486. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19487. publicKey, &pubkSz), BAD_FUNC_ARG);
  19488. prvkSz = CURVE25519_KEYSIZE;
  19489. pubkSz = CURVE25519_KEYSIZE;
  19490. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19491. &pubkSz), BAD_FUNC_ARG);
  19492. prvkSz = CURVE25519_KEYSIZE;
  19493. pubkSz = CURVE25519_KEYSIZE;
  19494. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19495. publicKey, &pubkSz), BAD_FUNC_ARG);
  19496. /* prvkSz = CURVE25519_KEYSIZE; */
  19497. pubkSz = CURVE25519_KEYSIZE;
  19498. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19499. NULL, &pubkSz), BAD_FUNC_ARG);
  19500. prvkSz = CURVE25519_KEYSIZE;
  19501. pubkSz = CURVE25519_KEYSIZE;
  19502. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19503. publicKey, NULL), BAD_FUNC_ARG);
  19504. /* cross-testing */
  19505. prksz = CURVE25519_KEYSIZE;
  19506. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19507. pbksz = CURVE25519_KEYSIZE;
  19508. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19509. prvkSz = CURVE25519_KEYSIZE;
  19510. /* pubkSz = CURVE25519_KEYSIZE; */
  19511. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19512. publicKey, &pubkSz), 0);
  19513. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19514. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19515. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19516. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19517. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19518. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19519. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19520. wc_curve25519_free(&key);
  19521. #endif
  19522. return EXPECT_RESULT();
  19523. } /* end of test_wc_curve25519_export_key_raw */
  19524. /*
  19525. * Testing test_wc_curve25519_export_key_raw_ex().
  19526. */
  19527. static int test_wc_curve25519_export_key_raw_ex(void)
  19528. {
  19529. EXPECT_DECLS;
  19530. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19531. curve25519_key key;
  19532. WC_RNG rng;
  19533. byte privateKey[CURVE25519_KEYSIZE];
  19534. byte publicKey[CURVE25519_KEYSIZE];
  19535. word32 prvkSz;
  19536. word32 pubkSz;
  19537. byte prik[CURVE25519_KEYSIZE];
  19538. byte pubk[CURVE25519_KEYSIZE];
  19539. word32 prksz;
  19540. word32 pbksz;
  19541. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19542. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19543. ExpectIntEQ(wc_InitRng(&rng), 0);
  19544. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19545. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19546. prvkSz = CURVE25519_KEYSIZE;
  19547. pubkSz = CURVE25519_KEYSIZE;
  19548. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19549. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19550. prvkSz = CURVE25519_KEYSIZE;
  19551. pubkSz = CURVE25519_KEYSIZE;
  19552. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19553. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19554. prvkSz = CURVE25519_KEYSIZE;
  19555. pubkSz = CURVE25519_KEYSIZE;
  19556. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19557. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19558. /* prvkSz = CURVE25519_KEYSIZE; */
  19559. pubkSz = CURVE25519_KEYSIZE;
  19560. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19561. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19562. prvkSz = CURVE25519_KEYSIZE;
  19563. pubkSz = CURVE25519_KEYSIZE;
  19564. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19565. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19566. prvkSz = CURVE25519_KEYSIZE;
  19567. /* pubkSz = CURVE25519_KEYSIZE; */
  19568. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19569. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19570. prvkSz = CURVE25519_KEYSIZE;
  19571. pubkSz = CURVE25519_KEYSIZE;
  19572. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19573. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19574. prvkSz = CURVE25519_KEYSIZE;
  19575. pubkSz = CURVE25519_KEYSIZE;
  19576. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19577. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19578. /* prvkSz = CURVE25519_KEYSIZE; */
  19579. pubkSz = CURVE25519_KEYSIZE;
  19580. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19581. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19582. prvkSz = CURVE25519_KEYSIZE;
  19583. pubkSz = CURVE25519_KEYSIZE;
  19584. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19585. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19586. /* illegal value for endian */
  19587. prvkSz = CURVE25519_KEYSIZE;
  19588. /* pubkSz = CURVE25519_KEYSIZE; */
  19589. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19590. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  19591. /* cross-testing */
  19592. prksz = CURVE25519_KEYSIZE;
  19593. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  19594. pbksz = CURVE25519_KEYSIZE;
  19595. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  19596. prvkSz = CURVE25519_KEYSIZE;
  19597. /* pubkSz = CURVE25519_KEYSIZE; */
  19598. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19599. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19600. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19601. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19602. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19603. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19604. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19605. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19606. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19607. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  19608. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19609. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19610. /* try once with another endian */
  19611. prvkSz = CURVE25519_KEYSIZE;
  19612. pubkSz = CURVE25519_KEYSIZE;
  19613. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19614. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19615. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19616. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19617. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19618. wc_curve25519_free(&key);
  19619. #endif
  19620. return EXPECT_RESULT();
  19621. } /* end of test_wc_curve25519_export_key_raw_ex */
  19622. /*
  19623. * Testing wc_curve25519_make_key
  19624. */
  19625. static int test_wc_curve25519_make_key(void)
  19626. {
  19627. EXPECT_DECLS;
  19628. #if defined(HAVE_CURVE25519)
  19629. curve25519_key key;
  19630. WC_RNG rng;
  19631. int keysize;
  19632. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19633. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19634. ExpectIntEQ(wc_InitRng(&rng), 0);
  19635. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19636. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19637. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  19638. /* test bad cases*/
  19639. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  19640. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  19641. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  19642. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  19643. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19644. wc_curve25519_free(&key);
  19645. #endif
  19646. return EXPECT_RESULT();
  19647. } /* END test_wc_curve25519_make_key*/
  19648. /*
  19649. * Testing wc_curve25519_shared_secret_ex
  19650. */
  19651. static int test_wc_curve25519_shared_secret_ex(void)
  19652. {
  19653. EXPECT_DECLS;
  19654. #if defined(HAVE_CURVE25519)
  19655. curve25519_key private_key;
  19656. curve25519_key public_key;
  19657. WC_RNG rng;
  19658. byte out[CURVE25519_KEYSIZE];
  19659. word32 outLen = sizeof(out);
  19660. int endian = EC25519_BIG_ENDIAN;
  19661. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  19662. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  19663. ExpectIntEQ(wc_InitRng(&rng), 0);
  19664. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  19665. 0);
  19666. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  19667. 0);
  19668. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19669. &outLen, endian), 0);
  19670. /* test bad cases*/
  19671. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  19672. BAD_FUNC_ARG);
  19673. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  19674. endian), BAD_FUNC_ARG);
  19675. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  19676. endian), BAD_FUNC_ARG);
  19677. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19678. &outLen, endian), BAD_FUNC_ARG);
  19679. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19680. NULL, endian), BAD_FUNC_ARG);
  19681. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  19682. * increasing to 0x8f checks for error being returned*/
  19683. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19684. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19685. &outLen, endian), ECC_BAD_ARG_E);
  19686. outLen = outLen - 2;
  19687. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19688. &outLen, endian), BAD_FUNC_ARG);
  19689. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19690. wc_curve25519_free(&private_key);
  19691. wc_curve25519_free(&public_key);
  19692. #endif
  19693. return EXPECT_RESULT();
  19694. } /* END test_wc_curve25519_shared_secret_ex*/
  19695. /*
  19696. * Testing wc_curve25519_make_pub
  19697. */
  19698. static int test_wc_curve25519_make_pub(void)
  19699. {
  19700. EXPECT_DECLS;
  19701. #ifdef HAVE_CURVE25519
  19702. curve25519_key key;
  19703. WC_RNG rng;
  19704. byte out[CURVE25519_KEYSIZE];
  19705. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19706. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19707. ExpectIntEQ(wc_InitRng(&rng), 0);
  19708. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19709. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  19710. (int)sizeof(key.k), key.k), 0);
  19711. /* test bad cases*/
  19712. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  19713. (int)sizeof out, out), ECC_BAD_ARG_E);
  19714. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19715. NULL), ECC_BAD_ARG_E);
  19716. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  19717. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19718. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  19719. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19720. /* verify clamping test */
  19721. key.k[0] |= ~248;
  19722. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19723. key.k), ECC_BAD_ARG_E);
  19724. key.k[0] &= 248;
  19725. /* repeat the expected-to-succeed test. */
  19726. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19727. key.k), 0);
  19728. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19729. wc_curve25519_free(&key);
  19730. #endif
  19731. return EXPECT_RESULT();
  19732. } /* END test_wc_curve25519_make_pub */
  19733. /*
  19734. * Testing test_wc_curve25519_export_public_ex
  19735. */
  19736. static int test_wc_curve25519_export_public_ex(void)
  19737. {
  19738. EXPECT_DECLS;
  19739. #if defined(HAVE_CURVE25519)
  19740. curve25519_key key;
  19741. WC_RNG rng;
  19742. byte out[CURVE25519_KEYSIZE];
  19743. word32 outLen = sizeof(out);
  19744. int endian = EC25519_BIG_ENDIAN;
  19745. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19746. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19747. ExpectIntEQ(wc_InitRng(&rng), 0);
  19748. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19749. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  19750. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  19751. /* test bad cases*/
  19752. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  19753. BAD_FUNC_ARG);
  19754. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  19755. BAD_FUNC_ARG);
  19756. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  19757. BAD_FUNC_ARG);
  19758. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  19759. BAD_FUNC_ARG);
  19760. outLen = outLen - 2;
  19761. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  19762. ECC_BAD_ARG_E);
  19763. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19764. wc_curve25519_free(&key);
  19765. #endif
  19766. return EXPECT_RESULT();
  19767. } /* END test_wc_curve25519_export_public_ex*/
  19768. /*
  19769. * Testing test_wc_curve25519_import_private_raw_ex
  19770. */
  19771. static int test_wc_curve25519_import_private_raw_ex(void)
  19772. {
  19773. EXPECT_DECLS;
  19774. #if defined(HAVE_CURVE25519)
  19775. curve25519_key key;
  19776. WC_RNG rng;
  19777. byte priv[CURVE25519_KEYSIZE];
  19778. byte pub[CURVE25519_KEYSIZE];
  19779. word32 privSz = sizeof(priv);
  19780. word32 pubSz = sizeof(pub);
  19781. int endian = EC25519_BIG_ENDIAN;
  19782. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19783. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19784. ExpectIntEQ(wc_InitRng(&rng), 0);
  19785. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19786. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  19787. endian), 0);
  19788. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  19789. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19790. &key, endian), 0);
  19791. /* test bad cases*/
  19792. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19793. endian), BAD_FUNC_ARG);
  19794. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19795. &key, endian), BAD_FUNC_ARG);
  19796. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19797. &key, endian), BAD_FUNC_ARG);
  19798. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19799. NULL, endian), BAD_FUNC_ARG);
  19800. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19801. &key, endian), ECC_BAD_ARG_E);
  19802. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19803. &key, endian), ECC_BAD_ARG_E);
  19804. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19805. &key, EC25519_LITTLE_ENDIAN), 0);
  19806. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19807. wc_curve25519_free(&key);
  19808. #endif
  19809. return EXPECT_RESULT();
  19810. } /* END test_wc_curve25519_import_private_raw_ex*/
  19811. /*
  19812. * Testing test_wc_curve25519_import_private
  19813. */
  19814. static int test_wc_curve25519_import_private(void)
  19815. {
  19816. EXPECT_DECLS;
  19817. #if defined(HAVE_CURVE25519)
  19818. curve25519_key key;
  19819. WC_RNG rng;
  19820. byte priv[CURVE25519_KEYSIZE];
  19821. word32 privSz = sizeof(priv);
  19822. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19823. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19824. ExpectIntEQ(wc_InitRng(&rng), 0);
  19825. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19826. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  19827. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  19828. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19829. wc_curve25519_free(&key);
  19830. #endif
  19831. return EXPECT_RESULT();
  19832. } /* END test_wc_curve25519_import*/
  19833. /*
  19834. * Testing test_wc_curve25519_export_private_raw_ex
  19835. */
  19836. static int test_wc_curve25519_export_private_raw_ex(void)
  19837. {
  19838. EXPECT_DECLS;
  19839. #if defined(HAVE_CURVE25519)
  19840. curve25519_key key;
  19841. byte out[CURVE25519_KEYSIZE];
  19842. word32 outLen = sizeof(out);
  19843. int endian = EC25519_BIG_ENDIAN;
  19844. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19845. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19846. 0);
  19847. /* test bad cases*/
  19848. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  19849. BAD_FUNC_ARG);
  19850. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  19851. BAD_FUNC_ARG);
  19852. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  19853. endian), BAD_FUNC_ARG);
  19854. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  19855. BAD_FUNC_ARG);
  19856. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19857. EC25519_LITTLE_ENDIAN), 0);
  19858. outLen = outLen - 2;
  19859. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19860. ECC_BAD_ARG_E);
  19861. wc_curve25519_free(&key);
  19862. #endif
  19863. return EXPECT_RESULT();
  19864. } /* END test_wc_curve25519_export_private_raw_ex*/
  19865. /*
  19866. * Testing wc_ed448_make_key().
  19867. */
  19868. static int test_wc_ed448_make_key(void)
  19869. {
  19870. EXPECT_DECLS;
  19871. #if defined(HAVE_ED448)
  19872. ed448_key key;
  19873. WC_RNG rng;
  19874. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  19875. XMEMSET(&key, 0, sizeof(ed448_key));
  19876. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19877. ExpectIntEQ(wc_ed448_init(&key), 0);
  19878. ExpectIntEQ(wc_InitRng(&rng), 0);
  19879. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  19880. ECC_PRIV_KEY_E);
  19881. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19882. /* Test bad args. */
  19883. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  19884. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  19885. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  19886. BAD_FUNC_ARG);
  19887. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  19888. BAD_FUNC_ARG);
  19889. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19890. wc_ed448_free(&key);
  19891. #endif
  19892. return EXPECT_RESULT();
  19893. } /* END test_wc_ed448_make_key */
  19894. /*
  19895. * Testing wc_ed448_init()
  19896. */
  19897. static int test_wc_ed448_init(void)
  19898. {
  19899. EXPECT_DECLS;
  19900. #if defined(HAVE_ED448)
  19901. ed448_key key;
  19902. XMEMSET(&key, 0, sizeof(ed448_key));
  19903. ExpectIntEQ(wc_ed448_init(&key), 0);
  19904. /* Test bad args. */
  19905. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  19906. wc_ed448_free(&key);
  19907. #endif
  19908. return EXPECT_RESULT();
  19909. } /* END test_wc_ed448_init */
  19910. /*
  19911. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19912. */
  19913. static int test_wc_ed448_sign_msg(void)
  19914. {
  19915. EXPECT_DECLS;
  19916. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19917. ed448_key key;
  19918. WC_RNG rng;
  19919. byte msg[] = "Everybody gets Friday off.\n";
  19920. byte sig[ED448_SIG_SIZE];
  19921. word32 msglen = sizeof(msg);
  19922. word32 siglen = sizeof(sig);
  19923. word32 badSigLen = sizeof(sig) - 1;
  19924. #ifdef HAVE_ED448_VERIFY
  19925. int verify_ok = 0; /*1 = Verify success.*/
  19926. #endif
  19927. /* Initialize stack variables. */
  19928. XMEMSET(&key, 0, sizeof(ed448_key));
  19929. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19930. XMEMSET(sig, 0, siglen);
  19931. /* Initialize key. */
  19932. ExpectIntEQ(wc_ed448_init(&key), 0);
  19933. ExpectIntEQ(wc_InitRng(&rng), 0);
  19934. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19935. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  19936. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  19937. /* Test bad args. */
  19938. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  19939. BAD_FUNC_ARG);
  19940. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  19941. BAD_FUNC_ARG);
  19942. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  19943. BAD_FUNC_ARG);
  19944. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  19945. BAD_FUNC_ARG);
  19946. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  19947. BUFFER_E);
  19948. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  19949. badSigLen -= 1;
  19950. #ifdef HAVE_ED448_VERIFY
  19951. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  19952. NULL, 0), 0);
  19953. ExpectIntEQ(verify_ok, 1);
  19954. /* Test bad args. */
  19955. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  19956. &key, NULL, 0), BAD_FUNC_ARG);
  19957. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  19958. &key, NULL, 0), BAD_FUNC_ARG);
  19959. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19960. &key, NULL, 0), BAD_FUNC_ARG);
  19961. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  19962. &key, NULL, 0), BAD_FUNC_ARG);
  19963. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  19964. &key, NULL, 0), BAD_FUNC_ARG);
  19965. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19966. NULL, NULL, 0), BAD_FUNC_ARG);
  19967. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  19968. &key, NULL, 0), BAD_FUNC_ARG);
  19969. #endif /* Verify. */
  19970. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19971. wc_ed448_free(&key);
  19972. #endif
  19973. return EXPECT_RESULT();
  19974. } /* END test_wc_ed448_sign_msg */
  19975. /*
  19976. * Testing wc_ed448_import_public()
  19977. */
  19978. static int test_wc_ed448_import_public(void)
  19979. {
  19980. EXPECT_DECLS;
  19981. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19982. ed448_key pubKey;
  19983. WC_RNG rng;
  19984. const byte in[] =
  19985. "Ed448PublicKeyUnitTest.................................\n";
  19986. word32 inlen = sizeof(in);
  19987. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  19988. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19989. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  19990. ExpectIntEQ(wc_InitRng(&rng), 0);
  19991. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  19992. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  19993. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19994. /* Test bad args. */
  19995. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19996. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19997. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19998. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19999. wc_ed448_free(&pubKey);
  20000. #endif
  20001. return EXPECT_RESULT();
  20002. } /* END wc_ed448_import_public */
  20003. /*
  20004. * Testing wc_ed448_import_private_key()
  20005. */
  20006. static int test_wc_ed448_import_private_key(void)
  20007. {
  20008. EXPECT_DECLS;
  20009. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20010. ed448_key key;
  20011. WC_RNG rng;
  20012. const byte privKey[] =
  20013. "Ed448PrivateKeyUnitTest................................\n";
  20014. const byte pubKey[] =
  20015. "Ed448PublicKeyUnitTest.................................\n";
  20016. word32 privKeySz = sizeof(privKey);
  20017. word32 pubKeySz = sizeof(pubKey);
  20018. #ifdef HAVE_ED448_KEY_EXPORT
  20019. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20020. word32 bothKeysSz = sizeof(bothKeys);
  20021. #endif
  20022. XMEMSET(&key, 0, sizeof(ed448_key));
  20023. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20024. ExpectIntEQ(wc_ed448_init(&key), 0);
  20025. ExpectIntEQ(wc_InitRng(&rng), 0);
  20026. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20027. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20028. pubKeySz, &key, 1), 0);
  20029. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20030. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20031. #ifdef HAVE_ED448_KEY_EXPORT
  20032. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20033. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20034. &key, 1), 0);
  20035. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20036. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20037. #endif
  20038. /* Test bad args. */
  20039. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20040. &key), BAD_FUNC_ARG);
  20041. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20042. &key), BAD_FUNC_ARG);
  20043. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20044. pubKeySz, NULL), BAD_FUNC_ARG);
  20045. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20046. pubKeySz, &key), BAD_FUNC_ARG);
  20047. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20048. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20049. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20050. BAD_FUNC_ARG);
  20051. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20052. wc_ed448_free(&key);
  20053. #endif
  20054. return EXPECT_RESULT();
  20055. } /* END test_wc_ed448_import_private_key */
  20056. /*
  20057. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20058. */
  20059. static int test_wc_ed448_export(void)
  20060. {
  20061. EXPECT_DECLS;
  20062. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20063. ed448_key key;
  20064. WC_RNG rng;
  20065. byte priv[ED448_PRV_KEY_SIZE];
  20066. byte pub[ED448_PUB_KEY_SIZE];
  20067. word32 privSz = sizeof(priv);
  20068. word32 pubSz = sizeof(pub);
  20069. XMEMSET(&key, 0, sizeof(ed448_key));
  20070. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20071. ExpectIntEQ(wc_ed448_init(&key), 0);
  20072. ExpectIntEQ(wc_InitRng(&rng), 0);
  20073. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20074. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20075. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20076. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20077. /* Test bad args. */
  20078. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20079. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20080. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20081. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20082. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20083. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20084. /* Test bad args. */
  20085. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20086. BAD_FUNC_ARG);
  20087. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20088. BAD_FUNC_ARG);
  20089. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20090. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20091. wc_ed448_free(&key);
  20092. #endif
  20093. return EXPECT_RESULT();
  20094. } /* END test_wc_ed448_export */
  20095. /*
  20096. * Testing wc_ed448_size()
  20097. */
  20098. static int test_wc_ed448_size(void)
  20099. {
  20100. EXPECT_DECLS;
  20101. #if defined(HAVE_ED448)
  20102. ed448_key key;
  20103. WC_RNG rng;
  20104. XMEMSET(&key, 0, sizeof(ed448_key));
  20105. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20106. ExpectIntEQ(wc_ed448_init(&key), 0);
  20107. ExpectIntEQ(wc_InitRng(&rng), 0);
  20108. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20109. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20110. /* Test bad args. */
  20111. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20112. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20113. /* Test bad args. */
  20114. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20115. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20116. /* Test bad args. */
  20117. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20118. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20119. /* Test bad args. */
  20120. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20121. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20122. wc_ed448_free(&key);
  20123. #endif
  20124. return EXPECT_RESULT();
  20125. } /* END test_wc_ed448_size */
  20126. /*
  20127. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20128. */
  20129. static int test_wc_ed448_exportKey(void)
  20130. {
  20131. EXPECT_DECLS;
  20132. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20133. ed448_key key;
  20134. WC_RNG rng;
  20135. byte priv[ED448_PRV_KEY_SIZE];
  20136. byte pub[ED448_PUB_KEY_SIZE];
  20137. byte privOnly[ED448_PRV_KEY_SIZE];
  20138. word32 privSz = sizeof(priv);
  20139. word32 pubSz = sizeof(pub);
  20140. word32 privOnlySz = sizeof(privOnly);
  20141. XMEMSET(&key, 0, sizeof(ed448_key));
  20142. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20143. ExpectIntEQ(wc_ed448_init(&key), 0);
  20144. ExpectIntEQ(wc_InitRng(&rng), 0);
  20145. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20146. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20147. /* Test bad args. */
  20148. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20149. BAD_FUNC_ARG);
  20150. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20151. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20152. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20153. /* Test bad args. */
  20154. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20155. BAD_FUNC_ARG);
  20156. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20157. BAD_FUNC_ARG);
  20158. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20159. BAD_FUNC_ARG);
  20160. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20161. BAD_FUNC_ARG);
  20162. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20163. BAD_FUNC_ARG);
  20164. /* Cross check output. */
  20165. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20166. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20167. wc_ed448_free(&key);
  20168. #endif
  20169. return EXPECT_RESULT();
  20170. } /* END test_wc_ed448_exportKey */
  20171. /*
  20172. * Testing wc_Ed448PublicKeyToDer
  20173. */
  20174. static int test_wc_Ed448PublicKeyToDer(void)
  20175. {
  20176. EXPECT_DECLS;
  20177. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20178. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20179. ed448_key key;
  20180. byte derBuf[1024];
  20181. XMEMSET(&key, 0, sizeof(ed448_key));
  20182. /* Test bad args */
  20183. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20184. ExpectIntEQ(wc_ed448_init(&key), 0);
  20185. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20186. wc_ed448_free(&key);
  20187. /* Test good args */
  20188. if (EXPECT_SUCCESS()) {
  20189. WC_RNG rng;
  20190. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20191. ExpectIntEQ(wc_ed448_init(&key), 0);
  20192. ExpectIntEQ(wc_InitRng(&rng), 0);
  20193. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20194. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20195. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20196. wc_ed448_free(&key);
  20197. }
  20198. #endif
  20199. return EXPECT_RESULT();
  20200. } /* END testing wc_Ed448PublicKeyToDer */
  20201. /*
  20202. * Testing wc_curve448_init and wc_curve448_free.
  20203. */
  20204. static int test_wc_curve448_init(void)
  20205. {
  20206. EXPECT_DECLS;
  20207. #if defined(HAVE_CURVE448)
  20208. curve448_key key;
  20209. /* Test bad args for wc_curve448_init */
  20210. ExpectIntEQ(wc_curve448_init(&key), 0);
  20211. /* Test bad args for wc_curve448_init */
  20212. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20213. /* Test good args for wc_curve_448_free */
  20214. wc_curve448_free(&key);
  20215. /* Test bad args for wc_curve448_free */
  20216. wc_curve448_free(NULL);
  20217. #endif
  20218. return EXPECT_RESULT();
  20219. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20220. /*
  20221. * Testing wc_curve448_make_key
  20222. */
  20223. static int test_wc_curve448_make_key(void)
  20224. {
  20225. EXPECT_DECLS;
  20226. #if defined(HAVE_CURVE448)
  20227. curve448_key key;
  20228. WC_RNG rng;
  20229. int keysize;
  20230. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20231. ExpectIntEQ(wc_curve448_init(&key), 0);
  20232. ExpectIntEQ(wc_InitRng(&rng), 0);
  20233. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20234. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20235. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20236. /* test bad cases */
  20237. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20238. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20239. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20240. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20241. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20242. wc_curve448_free(&key);
  20243. #endif
  20244. return EXPECT_RESULT();
  20245. } /* END test_wc_curve448_make_key*/
  20246. /*
  20247. * Testing test_wc_curve448_shared_secret_ex
  20248. */
  20249. static int test_wc_curve448_shared_secret_ex(void)
  20250. {
  20251. EXPECT_DECLS;
  20252. #if defined(HAVE_CURVE448)
  20253. curve448_key private_key;
  20254. curve448_key public_key;
  20255. WC_RNG rng;
  20256. byte out[CURVE448_KEY_SIZE];
  20257. word32 outLen = sizeof(out);
  20258. int endian = EC448_BIG_ENDIAN;
  20259. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20260. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20261. ExpectIntEQ(wc_InitRng(&rng), 0);
  20262. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20263. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20264. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20265. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20266. &outLen, endian), 0);
  20267. /* test bad cases */
  20268. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20269. BAD_FUNC_ARG);
  20270. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20271. endian), BAD_FUNC_ARG);
  20272. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20273. endian), BAD_FUNC_ARG);
  20274. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20275. &outLen, endian), BAD_FUNC_ARG);
  20276. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20277. NULL, endian), BAD_FUNC_ARG);
  20278. outLen = outLen - 2;
  20279. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20280. &outLen, endian), BAD_FUNC_ARG);
  20281. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20282. wc_curve448_free(&private_key);
  20283. wc_curve448_free(&public_key);
  20284. #endif
  20285. return EXPECT_RESULT();
  20286. } /* END test_wc_curve448_shared_secret_ex*/
  20287. /*
  20288. * Testing test_wc_curve448_export_public_ex
  20289. */
  20290. static int test_wc_curve448_export_public_ex(void)
  20291. {
  20292. EXPECT_DECLS;
  20293. #if defined(HAVE_CURVE448)
  20294. WC_RNG rng;
  20295. curve448_key key;
  20296. byte out[CURVE448_KEY_SIZE];
  20297. word32 outLen = sizeof(out);
  20298. int endian = EC448_BIG_ENDIAN;
  20299. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20300. ExpectIntEQ(wc_curve448_init(&key), 0);
  20301. ExpectIntEQ(wc_InitRng(&rng), 0);
  20302. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20303. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20304. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20305. /* test bad cases*/
  20306. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20307. BAD_FUNC_ARG);
  20308. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20309. BAD_FUNC_ARG);
  20310. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20311. BAD_FUNC_ARG);
  20312. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20313. BAD_FUNC_ARG);
  20314. outLen = outLen - 2;
  20315. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20316. ECC_BAD_ARG_E);
  20317. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20318. wc_curve448_free(&key);
  20319. #endif
  20320. return EXPECT_RESULT();
  20321. } /* END test_wc_curve448_export_public_ex*/
  20322. /*
  20323. * Testing test_wc_curve448_export_private_raw_ex
  20324. */
  20325. static int test_wc_curve448_export_private_raw_ex(void)
  20326. {
  20327. EXPECT_DECLS;
  20328. #if defined(HAVE_CURVE448)
  20329. curve448_key key;
  20330. byte out[CURVE448_KEY_SIZE];
  20331. word32 outLen = sizeof(out);
  20332. int endian = EC448_BIG_ENDIAN;
  20333. ExpectIntEQ(wc_curve448_init(&key), 0);
  20334. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20335. 0);
  20336. /* test bad cases*/
  20337. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20338. BAD_FUNC_ARG);
  20339. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20340. BAD_FUNC_ARG);
  20341. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20342. BAD_FUNC_ARG);
  20343. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20344. BAD_FUNC_ARG);
  20345. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20346. EC448_LITTLE_ENDIAN), 0);
  20347. outLen = outLen - 2;
  20348. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20349. ECC_BAD_ARG_E);
  20350. wc_curve448_free(&key);
  20351. #endif
  20352. return EXPECT_RESULT();
  20353. } /* END test_wc_curve448_export_private_raw_ex*/
  20354. /*
  20355. * Testing test_wc_curve448_import_private_raw_ex
  20356. */
  20357. static int test_wc_curve448_import_private_raw_ex(void)
  20358. {
  20359. EXPECT_DECLS;
  20360. #if defined(HAVE_CURVE448)
  20361. curve448_key key;
  20362. WC_RNG rng;
  20363. byte priv[CURVE448_KEY_SIZE];
  20364. byte pub[CURVE448_KEY_SIZE];
  20365. word32 privSz = sizeof(priv);
  20366. word32 pubSz = sizeof(pub);
  20367. int endian = EC448_BIG_ENDIAN;
  20368. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20369. ExpectIntEQ(wc_curve448_init(&key), 0);
  20370. ExpectIntEQ(wc_InitRng(&rng), 0);
  20371. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20372. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20373. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20374. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20375. &key, endian), 0);
  20376. /* test bad cases */
  20377. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20378. BAD_FUNC_ARG);
  20379. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20380. &key, endian), BAD_FUNC_ARG);
  20381. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20382. &key, endian), BAD_FUNC_ARG);
  20383. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20384. NULL, endian), BAD_FUNC_ARG);
  20385. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20386. &key, endian), ECC_BAD_ARG_E);
  20387. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20388. &key, endian), ECC_BAD_ARG_E);
  20389. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20390. &key, EC448_LITTLE_ENDIAN), 0);
  20391. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20392. wc_curve448_free(&key);
  20393. #endif
  20394. return EXPECT_RESULT();
  20395. } /* END test_wc_curve448_import_private_raw_ex*/
  20396. /*
  20397. * Testing test_curve448_export_key_raw
  20398. */
  20399. static int test_wc_curve448_export_key_raw(void)
  20400. {
  20401. EXPECT_DECLS;
  20402. #if defined(HAVE_CURVE448)
  20403. curve448_key key;
  20404. WC_RNG rng;
  20405. byte priv[CURVE448_KEY_SIZE];
  20406. byte pub[CURVE448_KEY_SIZE];
  20407. word32 privSz = sizeof(priv);
  20408. word32 pubSz = sizeof(pub);
  20409. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20410. ExpectIntEQ(wc_curve448_init(&key), 0);
  20411. ExpectIntEQ(wc_InitRng(&rng), 0);
  20412. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20413. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20414. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20415. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20416. 0);
  20417. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20418. wc_curve448_free(&key);
  20419. #endif
  20420. return EXPECT_RESULT();
  20421. } /* END test_wc_curve448_import_private_raw_ex*/
  20422. /*
  20423. * Testing test_wc_curve448_import_private
  20424. */
  20425. static int test_wc_curve448_import_private(void)
  20426. {
  20427. EXPECT_DECLS;
  20428. #if defined(HAVE_CURVE448)
  20429. curve448_key key;
  20430. WC_RNG rng;
  20431. byte priv[CURVE448_KEY_SIZE];
  20432. word32 privSz = sizeof(priv);
  20433. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20434. ExpectIntEQ(wc_curve448_init(&key), 0);
  20435. ExpectIntEQ(wc_InitRng(&rng), 0);
  20436. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20437. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20438. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20439. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20440. wc_curve448_free(&key);
  20441. #endif
  20442. return EXPECT_RESULT();
  20443. } /* END test_wc_curve448_import*/
  20444. /*
  20445. * Testing test_wc_curve448_size.
  20446. */
  20447. static int test_wc_curve448_size(void)
  20448. {
  20449. EXPECT_DECLS;
  20450. #if defined(HAVE_CURVE448)
  20451. curve448_key key;
  20452. ExpectIntEQ(wc_curve448_init(&key), 0);
  20453. /* Test good args for wc_curve448_size */
  20454. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20455. /* Test bad args for wc_curve448_size */
  20456. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20457. wc_curve448_free(&key);
  20458. #endif
  20459. return EXPECT_RESULT();
  20460. } /* END test_wc_curve448_size*/
  20461. /*
  20462. * Testing wc_ecc_make_key.
  20463. */
  20464. static int test_wc_ecc_make_key(void)
  20465. {
  20466. EXPECT_DECLS;
  20467. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20468. ecc_key key;
  20469. WC_RNG rng;
  20470. int ret;
  20471. XMEMSET(&key, 0, sizeof(ecc_key));
  20472. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20473. ExpectIntEQ(wc_ecc_init(&key), 0);
  20474. ExpectIntEQ(wc_InitRng(&rng), 0);
  20475. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20476. #if defined(WOLFSSL_ASYNC_CRYPT)
  20477. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20478. #endif
  20479. ExpectIntEQ(ret, 0);
  20480. /* Pass in bad args. */
  20481. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20482. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20483. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20484. wc_ecc_free(&key);
  20485. #ifdef FP_ECC
  20486. wc_ecc_fp_free();
  20487. #endif
  20488. #endif
  20489. return EXPECT_RESULT();
  20490. } /* END test_wc_ecc_make_key */
  20491. /*
  20492. * Testing wc_ecc_init()
  20493. */
  20494. static int test_wc_ecc_init(void)
  20495. {
  20496. EXPECT_DECLS;
  20497. #ifdef HAVE_ECC
  20498. ecc_key key;
  20499. XMEMSET(&key, 0, sizeof(ecc_key));
  20500. ExpectIntEQ(wc_ecc_init(&key), 0);
  20501. /* Pass in bad args. */
  20502. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20503. wc_ecc_free(&key);
  20504. #endif
  20505. return EXPECT_RESULT();
  20506. } /* END test_wc_ecc_init */
  20507. /*
  20508. * Testing wc_ecc_check_key()
  20509. */
  20510. static int test_wc_ecc_check_key(void)
  20511. {
  20512. EXPECT_DECLS;
  20513. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20514. ecc_key key;
  20515. WC_RNG rng;
  20516. int ret;
  20517. XMEMSET(&rng, 0, sizeof(rng));
  20518. XMEMSET(&key, 0, sizeof(key));
  20519. ExpectIntEQ(wc_ecc_init(&key), 0);
  20520. ExpectIntEQ(wc_InitRng(&rng), 0);
  20521. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20522. #if defined(WOLFSSL_ASYNC_CRYPT)
  20523. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20524. #endif
  20525. ExpectIntEQ(ret, 0);
  20526. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20527. /* Pass in bad args. */
  20528. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20529. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20530. wc_ecc_free(&key);
  20531. #ifdef FP_ECC
  20532. wc_ecc_fp_free();
  20533. #endif
  20534. #endif
  20535. return EXPECT_RESULT();
  20536. } /* END test_wc_ecc_check_key */
  20537. /*
  20538. * Testing wc_ecc_get_generator()
  20539. */
  20540. static int test_wc_ecc_get_generator(void)
  20541. {
  20542. EXPECT_DECLS;
  20543. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20544. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20545. ecc_point* pt = NULL;
  20546. ExpectNotNull(pt = wc_ecc_new_point());
  20547. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20548. MP_OKAY);
  20549. /* Test bad args. */
  20550. /* Returns Zero for bad arg. */
  20551. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  20552. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20553. MP_OKAY);
  20554. /* If we ever get to 1000 curves increase this number */
  20555. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  20556. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  20557. wc_ecc_del_point(pt);
  20558. #endif
  20559. return EXPECT_RESULT();
  20560. } /* END test_wc_ecc_get_generator */
  20561. /*
  20562. * Testing wc_ecc_size()
  20563. */
  20564. static int test_wc_ecc_size(void)
  20565. {
  20566. EXPECT_DECLS;
  20567. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20568. WC_RNG rng;
  20569. ecc_key key;
  20570. int ret;
  20571. XMEMSET(&key, 0, sizeof(ecc_key));
  20572. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20573. ExpectIntEQ(wc_ecc_init(&key), 0);
  20574. ExpectIntEQ(wc_InitRng(&rng), 0);
  20575. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20576. #if defined(WOLFSSL_ASYNC_CRYPT)
  20577. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20578. #endif
  20579. ExpectIntEQ(ret, 0);
  20580. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  20581. /* Test bad args. */
  20582. /* Returns Zero for bad arg. */
  20583. ExpectIntEQ(wc_ecc_size(NULL), 0);
  20584. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20585. wc_ecc_free(&key);
  20586. #endif
  20587. return EXPECT_RESULT();
  20588. } /* END test_wc_ecc_size */
  20589. static int test_wc_ecc_params(void)
  20590. {
  20591. EXPECT_DECLS;
  20592. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20593. It was added after certifications */
  20594. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20595. const ecc_set_type* ecc_set;
  20596. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20597. /* Test for SECP256R1 curve */
  20598. int curve_id = ECC_SECP256R1;
  20599. int curve_idx;
  20600. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  20601. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  20602. ExpectIntEQ(ecc_set->id, curve_id);
  20603. #endif
  20604. /* Test case when SECP256R1 is not enabled */
  20605. /* Test that we get curve params for index 0 */
  20606. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  20607. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20608. return EXPECT_RESULT();
  20609. }
  20610. /*
  20611. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20612. */
  20613. static int test_wc_ecc_signVerify_hash(void)
  20614. {
  20615. EXPECT_DECLS;
  20616. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20617. ecc_key key;
  20618. WC_RNG rng;
  20619. int ret;
  20620. #ifdef HAVE_ECC_VERIFY
  20621. int verify = 0;
  20622. #endif
  20623. word32 siglen = ECC_BUFSIZE;
  20624. byte sig[ECC_BUFSIZE];
  20625. byte adjustedSig[ECC_BUFSIZE+1];
  20626. byte digest[] = TEST_STRING;
  20627. word32 digestlen = (word32)TEST_STRING_SZ;
  20628. /* Init stack var */
  20629. XMEMSET(&key, 0, sizeof(ecc_key));
  20630. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20631. XMEMSET(sig, 0, siglen);
  20632. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20633. /* Init structs. */
  20634. ExpectIntEQ(wc_ecc_init(&key), 0);
  20635. ExpectIntEQ(wc_InitRng(&rng), 0);
  20636. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20637. #if defined(WOLFSSL_ASYNC_CRYPT)
  20638. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20639. #endif
  20640. ExpectIntEQ(ret, 0);
  20641. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  20642. 0);
  20643. /* Check bad args. */
  20644. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  20645. ECC_BAD_ARG_E);
  20646. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  20647. ECC_BAD_ARG_E);
  20648. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  20649. ECC_BAD_ARG_E);
  20650. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  20651. ECC_BAD_ARG_E);
  20652. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  20653. ECC_BAD_ARG_E);
  20654. #ifdef HAVE_ECC_VERIFY
  20655. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20656. &key), 0);
  20657. ExpectIntEQ(verify, 1);
  20658. /* test check on length of signature passed in */
  20659. XMEMCPY(adjustedSig, sig, siglen);
  20660. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20661. #ifndef NO_STRICT_ECDSA_LEN
  20662. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20663. &verify, &key), 0);
  20664. #else
  20665. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20666. * is allowed */
  20667. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20668. &verify, &key), 0);
  20669. #endif
  20670. /* Test bad args. */
  20671. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  20672. &key), ECC_BAD_ARG_E);
  20673. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  20674. ECC_BAD_ARG_E);
  20675. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  20676. ECC_BAD_ARG_E);
  20677. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20678. NULL), ECC_BAD_ARG_E);
  20679. #endif /* HAVE_ECC_VERIFY */
  20680. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20681. wc_ecc_free(&key);
  20682. #ifdef FP_ECC
  20683. wc_ecc_fp_free();
  20684. #endif
  20685. #endif
  20686. return EXPECT_RESULT();
  20687. } /* END test_wc_ecc_sign_hash */
  20688. /*
  20689. * Testing wc_ecc_shared_secret()
  20690. */
  20691. static int test_wc_ecc_shared_secret(void)
  20692. {
  20693. EXPECT_DECLS;
  20694. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20695. ecc_key key;
  20696. ecc_key pubKey;
  20697. WC_RNG rng;
  20698. #if defined(NO_ECC256)
  20699. int ret;
  20700. #endif
  20701. byte out[KEY32];
  20702. int keySz = sizeof(out);
  20703. word32 outlen = (word32)sizeof(out);
  20704. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20705. const char* qx =
  20706. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20707. const char* qy =
  20708. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20709. const char* d =
  20710. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20711. const char* curveName = "SECP256R1";
  20712. const byte expected_shared_secret[] =
  20713. {
  20714. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20715. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20716. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20717. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20718. };
  20719. #endif
  20720. PRIVATE_KEY_UNLOCK();
  20721. /* Initialize variables. */
  20722. XMEMSET(&key, 0, sizeof(ecc_key));
  20723. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20724. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20725. XMEMSET(out, 0, keySz);
  20726. ExpectIntEQ(wc_ecc_init(&key), 0);
  20727. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20728. ExpectIntEQ(wc_InitRng(&rng), 0);
  20729. #if !defined(NO_ECC256)
  20730. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  20731. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  20732. #else
  20733. ret = wc_ecc_make_key(&rng, keySz, &key);
  20734. #if defined(WOLFSSL_ASYNC_CRYPT)
  20735. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20736. #endif
  20737. ExpectIntEQ(ret, 0);
  20738. ret = wc_ecc_make_key(&rng, keySz, &key);
  20739. #if defined(WOLFSSL_ASYNC_CRYPT)
  20740. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20741. #endif
  20742. ExpectIntEQ(ret, 0);
  20743. #endif
  20744. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20745. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20746. !defined(HAVE_SELFTEST)
  20747. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  20748. #endif
  20749. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  20750. #if !defined(NO_ECC256)
  20751. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  20752. #endif
  20753. /* Test bad args. */
  20754. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  20755. BAD_FUNC_ARG);
  20756. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  20757. BAD_FUNC_ARG);
  20758. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  20759. BAD_FUNC_ARG);
  20760. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  20761. BAD_FUNC_ARG);
  20762. /* Invalid length */
  20763. outlen = 1;
  20764. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  20765. BUFFER_E);
  20766. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20767. wc_ecc_free(&pubKey);
  20768. wc_ecc_free(&key);
  20769. #ifdef FP_ECC
  20770. wc_ecc_fp_free();
  20771. #endif
  20772. PRIVATE_KEY_LOCK();
  20773. #endif
  20774. return EXPECT_RESULT();
  20775. } /* END tests_wc_ecc_shared_secret */
  20776. /*
  20777. * testint wc_ecc_export_x963()
  20778. */
  20779. static int test_wc_ecc_export_x963(void)
  20780. {
  20781. EXPECT_DECLS;
  20782. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20783. ecc_key key;
  20784. WC_RNG rng;
  20785. byte out[ECC_ASN963_MAX_BUF_SZ];
  20786. word32 outlen = sizeof(out);
  20787. int ret;
  20788. PRIVATE_KEY_UNLOCK();
  20789. /* Initialize variables. */
  20790. XMEMSET(&key, 0, sizeof(ecc_key));
  20791. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20792. XMEMSET(out, 0, outlen);
  20793. ExpectIntEQ(wc_ecc_init(&key), 0);
  20794. ExpectIntEQ(wc_InitRng(&rng), 0);
  20795. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20796. #if defined(WOLFSSL_ASYNC_CRYPT)
  20797. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20798. #endif
  20799. ExpectIntEQ(ret, 0);
  20800. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  20801. /* Test bad args. */
  20802. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  20803. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  20804. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  20805. key.idx = -4;
  20806. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  20807. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20808. wc_ecc_free(&key);
  20809. #ifdef FP_ECC
  20810. wc_ecc_fp_free();
  20811. #endif
  20812. PRIVATE_KEY_LOCK();
  20813. #endif
  20814. return EXPECT_RESULT();
  20815. } /* END test_wc_ecc_export_x963 */
  20816. /*
  20817. * Testing wc_ecc_export_x963_ex()
  20818. * compile with --enable-compkey will use compression.
  20819. */
  20820. static int test_wc_ecc_export_x963_ex(void)
  20821. {
  20822. EXPECT_DECLS;
  20823. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20824. ecc_key key;
  20825. WC_RNG rng;
  20826. int ret;
  20827. byte out[ECC_ASN963_MAX_BUF_SZ];
  20828. word32 outlen = sizeof(out);
  20829. #ifdef HAVE_COMP_KEY
  20830. word32 badOutLen = 5;
  20831. #endif
  20832. /* Init stack variables. */
  20833. XMEMSET(&key, 0, sizeof(ecc_key));
  20834. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20835. XMEMSET(out, 0, outlen);
  20836. ExpectIntEQ(wc_ecc_init(&key), 0);
  20837. ExpectIntEQ(wc_InitRng(&rng), 0);
  20838. ret = wc_ecc_make_key(&rng, KEY64, &key);
  20839. #if defined(WOLFSSL_ASYNC_CRYPT)
  20840. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20841. #endif
  20842. ExpectIntEQ(ret, 0);
  20843. #ifdef HAVE_COMP_KEY
  20844. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  20845. #else
  20846. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  20847. #endif
  20848. /* Test bad args. */
  20849. #ifdef HAVE_COMP_KEY
  20850. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  20851. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  20852. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  20853. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  20854. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  20855. #else
  20856. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  20857. LENGTH_ONLY_E);
  20858. #endif
  20859. key.idx = -4;
  20860. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  20861. #else
  20862. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  20863. ECC_BAD_ARG_E);
  20864. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  20865. LENGTH_ONLY_E);
  20866. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  20867. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  20868. ECC_BAD_ARG_E);
  20869. key.idx = -4;
  20870. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  20871. ECC_BAD_ARG_E);
  20872. #endif
  20873. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20874. wc_ecc_free(&key);
  20875. #ifdef FP_ECC
  20876. wc_ecc_fp_free();
  20877. #endif
  20878. #endif
  20879. return EXPECT_RESULT();
  20880. } /* END test_wc_ecc_export_x963_ex */
  20881. /*
  20882. * testing wc_ecc_import_x963()
  20883. */
  20884. static int test_wc_ecc_import_x963(void)
  20885. {
  20886. EXPECT_DECLS;
  20887. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20888. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20889. ecc_key pubKey;
  20890. ecc_key key;
  20891. WC_RNG rng;
  20892. byte x963[ECC_ASN963_MAX_BUF_SZ];
  20893. word32 x963Len = (word32)sizeof(x963);
  20894. int ret;
  20895. /* Init stack variables. */
  20896. XMEMSET(&key, 0, sizeof(ecc_key));
  20897. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20898. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20899. XMEMSET(x963, 0, x963Len);
  20900. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20901. ExpectIntEQ(wc_ecc_init(&key), 0);
  20902. ExpectIntEQ(wc_InitRng(&rng), 0);
  20903. ret = wc_ecc_make_key(&rng, KEY24, &key);
  20904. #if defined(WOLFSSL_ASYNC_CRYPT)
  20905. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20906. #endif
  20907. ExpectIntEQ(ret, 0);
  20908. PRIVATE_KEY_UNLOCK();
  20909. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  20910. PRIVATE_KEY_LOCK();
  20911. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  20912. /* Test bad args. */
  20913. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  20914. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  20915. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  20916. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20917. wc_ecc_free(&key);
  20918. wc_ecc_free(&pubKey);
  20919. #ifdef FP_ECC
  20920. wc_ecc_fp_free();
  20921. #endif
  20922. #endif
  20923. return EXPECT_RESULT();
  20924. } /* END wc_ecc_import_x963 */
  20925. /*
  20926. * testing wc_ecc_import_private_key()
  20927. */
  20928. static int test_wc_ecc_import_private_key(void)
  20929. {
  20930. EXPECT_DECLS;
  20931. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20932. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20933. ecc_key key;
  20934. ecc_key keyImp;
  20935. WC_RNG rng;
  20936. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  20937. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  20938. word32 privKeySz = (word32)sizeof(privKey);
  20939. word32 x963KeySz = (word32)sizeof(x963Key);
  20940. int ret;
  20941. /* Init stack variables. */
  20942. XMEMSET(&key, 0, sizeof(ecc_key));
  20943. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  20944. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20945. XMEMSET(privKey, 0, privKeySz);
  20946. XMEMSET(x963Key, 0, x963KeySz);
  20947. ExpectIntEQ(wc_ecc_init(&key), 0);
  20948. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  20949. ExpectIntEQ(wc_InitRng(&rng), 0);
  20950. ret = wc_ecc_make_key(&rng, KEY48, &key);
  20951. #if defined(WOLFSSL_ASYNC_CRYPT)
  20952. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20953. #endif
  20954. ExpectIntEQ(ret, 0);
  20955. PRIVATE_KEY_UNLOCK();
  20956. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  20957. PRIVATE_KEY_LOCK();
  20958. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  20959. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20960. x963KeySz, &keyImp), 0);
  20961. /* Pass in bad args. */
  20962. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20963. x963KeySz, NULL), BAD_FUNC_ARG);
  20964. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  20965. &keyImp), BAD_FUNC_ARG);
  20966. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20967. wc_ecc_free(&keyImp);
  20968. wc_ecc_free(&key);
  20969. #ifdef FP_ECC
  20970. wc_ecc_fp_free();
  20971. #endif
  20972. #endif
  20973. return EXPECT_RESULT();
  20974. } /* END test_wc_ecc_import_private_key */
  20975. /*
  20976. * Testing wc_ecc_export_private_only()
  20977. */
  20978. static int test_wc_ecc_export_private_only(void)
  20979. {
  20980. EXPECT_DECLS;
  20981. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20982. ecc_key key;
  20983. WC_RNG rng;
  20984. byte out[ECC_PRIV_KEY_BUF];
  20985. word32 outlen = sizeof(out);
  20986. int ret;
  20987. /* Init stack variables. */
  20988. XMEMSET(&key, 0, sizeof(ecc_key));
  20989. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20990. XMEMSET(out, 0, outlen);
  20991. ExpectIntEQ(wc_ecc_init(&key), 0);
  20992. ExpectIntEQ(wc_InitRng(&rng), 0);
  20993. ret = wc_ecc_make_key(&rng, KEY32, &key);
  20994. #if defined(WOLFSSL_ASYNC_CRYPT)
  20995. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20996. #endif
  20997. ExpectIntEQ(ret, 0);
  20998. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  20999. /* Pass in bad args. */
  21000. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21001. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21002. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21003. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21004. wc_ecc_free(&key);
  21005. #ifdef FP_ECC
  21006. wc_ecc_fp_free();
  21007. #endif
  21008. #endif
  21009. return EXPECT_RESULT();
  21010. } /* END test_wc_ecc_export_private_only */
  21011. /*
  21012. * Testing wc_ecc_rs_to_sig()
  21013. */
  21014. static int test_wc_ecc_rs_to_sig(void)
  21015. {
  21016. EXPECT_DECLS;
  21017. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21018. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21019. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21020. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21021. const char* zeroStr = "0";
  21022. byte sig[ECC_MAX_SIG_SIZE];
  21023. word32 siglen = (word32)sizeof(sig);
  21024. /* R and S max size is the order of curve. 2^192.*/
  21025. int keySz = KEY24;
  21026. byte r[KEY24];
  21027. byte s[KEY24];
  21028. word32 rlen = (word32)sizeof(r);
  21029. word32 slen = (word32)sizeof(s);
  21030. /* Init stack variables. */
  21031. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21032. XMEMSET(r, 0, keySz);
  21033. XMEMSET(s, 0, keySz);
  21034. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21035. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21036. /* Test bad args. */
  21037. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21038. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21039. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21040. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21041. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21042. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21043. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21044. ECC_BAD_ARG_E);
  21045. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21046. ECC_BAD_ARG_E);
  21047. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21048. ECC_BAD_ARG_E);
  21049. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21050. ECC_BAD_ARG_E);
  21051. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21052. ECC_BAD_ARG_E);
  21053. #endif
  21054. return EXPECT_RESULT();
  21055. } /* END test_wc_ecc_rs_to_sig */
  21056. static int test_wc_ecc_import_raw(void)
  21057. {
  21058. EXPECT_DECLS;
  21059. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21060. ecc_key key;
  21061. const char* qx =
  21062. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21063. const char* qy =
  21064. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21065. const char* d =
  21066. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21067. const char* curveName = "SECP256R1";
  21068. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21069. const char* kNullStr = "";
  21070. int ret;
  21071. #endif
  21072. XMEMSET(&key, 0, sizeof(ecc_key));
  21073. ExpectIntEQ(wc_ecc_init(&key), 0);
  21074. /* Test good import */
  21075. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21076. /* Test bad args. */
  21077. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21078. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21079. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21080. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21081. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21082. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21083. wc_ecc_free(&key);
  21084. #endif
  21085. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21086. curveName), 0);
  21087. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21088. #endif
  21089. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21090. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21091. wc_ecc_free(&key);
  21092. #endif
  21093. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21094. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21095. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21096. #else
  21097. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21098. #endif
  21099. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21100. wc_ecc_free(&key);
  21101. #endif
  21102. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21103. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21104. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21105. #else
  21106. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21107. #endif
  21108. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21109. wc_ecc_free(&key);
  21110. #endif
  21111. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21112. #endif
  21113. wc_ecc_free(&key);
  21114. #endif
  21115. return EXPECT_RESULT();
  21116. } /* END test_wc_ecc_import_raw */
  21117. static int test_wc_ecc_import_unsigned(void)
  21118. {
  21119. EXPECT_DECLS;
  21120. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21121. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21122. HAVE_FIPS_VERSION >= 2))
  21123. ecc_key key;
  21124. const byte qx[] = {
  21125. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21126. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21127. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21128. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21129. };
  21130. const byte qy[] = {
  21131. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21132. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21133. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21134. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21135. };
  21136. const byte d[] = {
  21137. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21138. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21139. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21140. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21141. };
  21142. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21143. const byte nullBytes[32] = {0};
  21144. int ret;
  21145. #endif
  21146. int curveId = ECC_SECP256R1;
  21147. XMEMSET(&key, 0, sizeof(ecc_key));
  21148. ExpectIntEQ(wc_ecc_init(&key), 0);
  21149. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21150. curveId), 0);
  21151. /* Test bad args. */
  21152. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21153. curveId), BAD_FUNC_ARG);
  21154. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21155. curveId), BAD_FUNC_ARG);
  21156. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21157. curveId), BAD_FUNC_ARG);
  21158. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21159. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21160. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21161. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21162. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21163. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21164. #endif
  21165. wc_ecc_free(&key);
  21166. #endif
  21167. return EXPECT_RESULT();
  21168. } /* END test_wc_ecc_import_unsigned */
  21169. /*
  21170. * Testing wc_ecc_sig_size()
  21171. */
  21172. static int test_wc_ecc_sig_size(void)
  21173. {
  21174. EXPECT_DECLS;
  21175. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21176. ecc_key key;
  21177. WC_RNG rng;
  21178. int keySz = KEY16;
  21179. int ret;
  21180. XMEMSET(&rng, 0, sizeof(rng));
  21181. XMEMSET(&key, 0, sizeof(key));
  21182. ExpectIntEQ(wc_ecc_init(&key), 0);
  21183. ExpectIntEQ(wc_InitRng(&rng), 0);
  21184. ret = wc_ecc_make_key(&rng, keySz, &key);
  21185. #if defined(WOLFSSL_ASYNC_CRYPT)
  21186. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21187. #endif
  21188. ExpectIntEQ(ret, 0);
  21189. ExpectIntLE(wc_ecc_sig_size(&key),
  21190. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21192. wc_ecc_free(&key);
  21193. #endif
  21194. return EXPECT_RESULT();
  21195. } /* END test_wc_ecc_sig_size */
  21196. /*
  21197. * Testing wc_ecc_ctx_new()
  21198. */
  21199. static int test_wc_ecc_ctx_new(void)
  21200. {
  21201. EXPECT_DECLS;
  21202. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21203. WC_RNG rng;
  21204. ecEncCtx* cli = NULL;
  21205. ecEncCtx* srv = NULL;
  21206. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21207. ExpectIntEQ(wc_InitRng(&rng), 0);
  21208. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21209. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21210. wc_ecc_ctx_free(cli);
  21211. cli = NULL;
  21212. wc_ecc_ctx_free(srv);
  21213. /* Test bad args. */
  21214. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21215. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21216. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21217. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21218. wc_ecc_ctx_free(cli);
  21219. #endif
  21220. return EXPECT_RESULT();
  21221. } /* END test_wc_ecc_ctx_new */
  21222. /*
  21223. * Tesing wc_ecc_reset()
  21224. */
  21225. static int test_wc_ecc_ctx_reset(void)
  21226. {
  21227. EXPECT_DECLS;
  21228. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21229. ecEncCtx* ctx = NULL;
  21230. WC_RNG rng;
  21231. XMEMSET(&rng, 0, sizeof(rng));
  21232. ExpectIntEQ(wc_InitRng(&rng), 0);
  21233. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21234. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21235. /* Pass in bad args. */
  21236. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21237. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21238. wc_ecc_ctx_free(ctx);
  21239. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21240. #endif
  21241. return EXPECT_RESULT();
  21242. } /* END test_wc_ecc_ctx_reset */
  21243. /*
  21244. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21245. */
  21246. static int test_wc_ecc_ctx_set_peer_salt(void)
  21247. {
  21248. EXPECT_DECLS;
  21249. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21250. WC_RNG rng;
  21251. ecEncCtx* cliCtx = NULL;
  21252. ecEncCtx* servCtx = NULL;
  21253. const byte* cliSalt = NULL;
  21254. const byte* servSalt = NULL;
  21255. XMEMSET(&rng, 0, sizeof(rng));
  21256. ExpectIntEQ(wc_InitRng(&rng), 0);
  21257. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21258. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21259. /* Test bad args. */
  21260. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21261. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21262. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21263. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21264. /* Test bad args. */
  21265. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21266. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21267. wc_ecc_ctx_free(cliCtx);
  21268. wc_ecc_ctx_free(servCtx);
  21269. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21270. #endif
  21271. return EXPECT_RESULT();
  21272. } /* END test_wc_ecc_ctx_set_peer_salt */
  21273. /*
  21274. * Testing wc_ecc_ctx_set_info()
  21275. */
  21276. static int test_wc_ecc_ctx_set_info(void)
  21277. {
  21278. EXPECT_DECLS;
  21279. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21280. ecEncCtx* ctx = NULL;
  21281. WC_RNG rng;
  21282. const char* optInfo = "Optional Test Info.";
  21283. int optInfoSz = (int)XSTRLEN(optInfo);
  21284. const char* badOptInfo = NULL;
  21285. XMEMSET(&rng, 0, sizeof(rng));
  21286. ExpectIntEQ(wc_InitRng(&rng), 0);
  21287. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21288. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21289. /* Test bad args. */
  21290. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21291. BAD_FUNC_ARG);
  21292. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21293. BAD_FUNC_ARG);
  21294. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21295. wc_ecc_ctx_free(ctx);
  21296. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21297. #endif
  21298. return EXPECT_RESULT();
  21299. } /* END test_wc_ecc_ctx_set_info */
  21300. /*
  21301. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21302. */
  21303. static int test_wc_ecc_encryptDecrypt(void)
  21304. {
  21305. EXPECT_DECLS;
  21306. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21307. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21308. ecc_key srvKey;
  21309. ecc_key cliKey;
  21310. ecc_key tmpKey;
  21311. WC_RNG rng;
  21312. int ret;
  21313. const char* msg = "EccBlock Size 16";
  21314. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21315. #ifdef WOLFSSL_ECIES_OLD
  21316. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21317. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21318. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21319. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21320. #else
  21321. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21322. WC_SHA256_DIGEST_SIZE];
  21323. #endif
  21324. word32 outSz = (word32)sizeof(out);
  21325. byte plain[sizeof("EccBlock Size 16")];
  21326. word32 plainSz = (word32)sizeof(plain);
  21327. int keySz = KEY20;
  21328. /* Init stack variables. */
  21329. XMEMSET(out, 0, outSz);
  21330. XMEMSET(plain, 0, plainSz);
  21331. XMEMSET(&rng, 0, sizeof(rng));
  21332. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21333. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21334. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21335. ExpectIntEQ(wc_InitRng(&rng), 0);
  21336. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21337. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21338. #if defined(WOLFSSL_ASYNC_CRYPT)
  21339. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21340. #endif
  21341. ExpectIntEQ(ret, 0);
  21342. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21343. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21344. #if defined(WOLFSSL_ASYNC_CRYPT)
  21345. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21346. #endif
  21347. ExpectIntEQ(ret, 0);
  21348. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21349. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21350. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21351. !defined(HAVE_SELFTEST)
  21352. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21353. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21354. #endif
  21355. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21356. &outSz, NULL), 0);
  21357. /* Test bad args. */
  21358. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21359. NULL), BAD_FUNC_ARG);
  21360. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21361. NULL), BAD_FUNC_ARG);
  21362. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21363. NULL), BAD_FUNC_ARG);
  21364. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21365. &outSz, NULL), BAD_FUNC_ARG);
  21366. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21367. NULL), BAD_FUNC_ARG);
  21368. #ifdef WOLFSSL_ECIES_OLD
  21369. tmpKey.dp = cliKey.dp;
  21370. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21371. #endif
  21372. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21373. NULL), 0);
  21374. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21375. NULL), BAD_FUNC_ARG);
  21376. #ifdef WOLFSSL_ECIES_OLD
  21377. /* NULL parameter allowed in new implementations - public key comes from
  21378. * the message. */
  21379. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21380. NULL), BAD_FUNC_ARG);
  21381. #endif
  21382. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21383. NULL), BAD_FUNC_ARG);
  21384. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21385. NULL), BAD_FUNC_ARG);
  21386. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21387. BAD_FUNC_ARG);
  21388. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21389. wc_ecc_free(&tmpKey);
  21390. wc_ecc_free(&srvKey);
  21391. wc_ecc_free(&cliKey);
  21392. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21393. #endif
  21394. return EXPECT_RESULT();
  21395. } /* END test_wc_ecc_encryptDecrypt */
  21396. /*
  21397. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21398. */
  21399. static int test_wc_ecc_del_point(void)
  21400. {
  21401. EXPECT_DECLS;
  21402. #if defined(HAVE_ECC)
  21403. ecc_point* pt = NULL;
  21404. ExpectNotNull(pt = wc_ecc_new_point());
  21405. wc_ecc_del_point(pt);
  21406. #endif
  21407. return EXPECT_RESULT();
  21408. } /* END test_wc_ecc_del_point */
  21409. /*
  21410. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21411. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21412. * and wc_ecc_cmp_point()
  21413. */
  21414. static int test_wc_ecc_pointFns(void)
  21415. {
  21416. EXPECT_DECLS;
  21417. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21418. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21419. !defined(WOLFSSL_ATECC608A)
  21420. ecc_key key;
  21421. WC_RNG rng;
  21422. int ret;
  21423. ecc_point* point = NULL;
  21424. ecc_point* cpypt = NULL;
  21425. int idx = 0;
  21426. int keySz = KEY32;
  21427. byte der[DER_SZ(KEY32)];
  21428. word32 derlenChk = 0;
  21429. word32 derSz = DER_SZ(KEY32);
  21430. /* Init stack variables. */
  21431. XMEMSET(der, 0, derSz);
  21432. XMEMSET(&key, 0, sizeof(ecc_key));
  21433. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21434. ExpectIntEQ(wc_InitRng(&rng), 0);
  21435. ExpectIntEQ(wc_ecc_init(&key), 0);
  21436. ret = wc_ecc_make_key(&rng, keySz, &key);
  21437. #if defined(WOLFSSL_ASYNC_CRYPT)
  21438. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21439. #endif
  21440. ExpectIntEQ(ret, 0);
  21441. ExpectNotNull(point = wc_ecc_new_point());
  21442. ExpectNotNull(cpypt = wc_ecc_new_point());
  21443. /* Export */
  21444. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21445. &derlenChk), LENGTH_ONLY_E);
  21446. /* Check length value. */
  21447. ExpectIntEQ(derSz, derlenChk);
  21448. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21449. &derSz), 0);
  21450. /* Test bad args. */
  21451. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21452. ECC_BAD_ARG_E);
  21453. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21454. ECC_BAD_ARG_E);
  21455. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21456. NULL), ECC_BAD_ARG_E);
  21457. /* Import */
  21458. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21459. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21460. /* Test bad args. */
  21461. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21462. ECC_BAD_ARG_E);
  21463. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21464. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21465. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21466. ECC_BAD_ARG_E);
  21467. /* Copy */
  21468. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21469. /* Test bad args. */
  21470. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21471. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21472. /* Compare point */
  21473. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21474. /* Test bad args. */
  21475. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21476. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21477. /* At infinity if return == 1, otherwise return == 0. */
  21478. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21479. /* Test bad args. */
  21480. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21481. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21482. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21483. #ifdef USE_ECC_B_PARAM
  21484. /* On curve if ret == 0 */
  21485. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21486. /* Test bad args. */
  21487. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21488. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21489. #endif /* USE_ECC_B_PARAM */
  21490. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21491. /* Free */
  21492. wc_ecc_del_point(point);
  21493. wc_ecc_del_point(cpypt);
  21494. wc_ecc_free(&key);
  21495. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21496. #endif
  21497. return EXPECT_RESULT();
  21498. } /* END test_wc_ecc_pointFns */
  21499. /*
  21500. * Testing wc_ecc_sahred_secret_ssh()
  21501. */
  21502. static int test_wc_ecc_shared_secret_ssh(void)
  21503. {
  21504. EXPECT_DECLS;
  21505. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21506. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21507. !defined(WOLFSSL_ATECC608A)
  21508. ecc_key key;
  21509. ecc_key key2;
  21510. WC_RNG rng;
  21511. int ret;
  21512. int keySz = KEY32;
  21513. int key2Sz = KEY24;
  21514. byte secret[KEY32];
  21515. word32 secretLen = keySz;
  21516. /* Init stack variables. */
  21517. XMEMSET(&key, 0, sizeof(ecc_key));
  21518. XMEMSET(&key2, 0, sizeof(ecc_key));
  21519. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21520. XMEMSET(secret, 0, secretLen);
  21521. /* Make keys */
  21522. ExpectIntEQ(wc_ecc_init(&key), 0);
  21523. ExpectIntEQ(wc_InitRng(&rng), 0);
  21524. ret = wc_ecc_make_key(&rng, keySz, &key);
  21525. #if defined(WOLFSSL_ASYNC_CRYPT)
  21526. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21527. #endif
  21528. ExpectIntEQ(ret, 0);
  21529. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21530. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21531. ExpectIntEQ(wc_InitRng(&rng), 0);
  21532. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21533. #if defined(WOLFSSL_ASYNC_CRYPT)
  21534. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21535. #endif
  21536. ExpectIntEQ(ret, 0);
  21537. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21538. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21539. !defined(HAVE_SELFTEST)
  21540. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21541. #endif
  21542. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21543. &secretLen), 0);
  21544. /* Pass in bad args. */
  21545. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  21546. &secretLen), BAD_FUNC_ARG);
  21547. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  21548. BAD_FUNC_ARG);
  21549. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  21550. BAD_FUNC_ARG);
  21551. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  21552. BAD_FUNC_ARG);
  21553. key.type = ECC_PUBLICKEY;
  21554. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21555. &secretLen), ECC_BAD_ARG_E);
  21556. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21557. wc_ecc_free(&key);
  21558. wc_ecc_free(&key2);
  21559. #ifdef FP_ECC
  21560. wc_ecc_fp_free();
  21561. #endif
  21562. #endif
  21563. return EXPECT_RESULT();
  21564. } /* END test_wc_ecc_shared_secret_ssh */
  21565. /*
  21566. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  21567. */
  21568. static int test_wc_ecc_verify_hash_ex(void)
  21569. {
  21570. EXPECT_DECLS;
  21571. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  21572. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21573. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  21574. ecc_key key;
  21575. WC_RNG rng;
  21576. int ret;
  21577. mp_int r;
  21578. mp_int s;
  21579. mp_int z;
  21580. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  21581. unsigned char iHash[] = "Everyone gets Friday off.......";
  21582. unsigned char shortHash[] = TEST_STRING;
  21583. word32 hashlen = sizeof(hash);
  21584. word32 iHashLen = sizeof(iHash);
  21585. word32 shortHashLen = sizeof(shortHash);
  21586. int keySz = KEY32;
  21587. int verify_ok = 0;
  21588. XMEMSET(&key, 0, sizeof(ecc_key));
  21589. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21590. XMEMSET(&r, 0, sizeof(mp_int));
  21591. XMEMSET(&s, 0, sizeof(mp_int));
  21592. XMEMSET(&z, 0, sizeof(mp_int));
  21593. /* Initialize r, s and z. */
  21594. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  21595. ExpectIntEQ(wc_ecc_init(&key), 0);
  21596. ExpectIntEQ(wc_InitRng(&rng), 0);
  21597. ret = wc_ecc_make_key(&rng, keySz, &key);
  21598. #if defined(WOLFSSL_ASYNC_CRYPT)
  21599. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21600. #endif
  21601. ExpectIntEQ(ret, 0);
  21602. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  21603. /* verify_ok should be 1. */
  21604. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  21605. 0);
  21606. ExpectIntEQ(verify_ok, 1);
  21607. /* verify_ok should be 0 */
  21608. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  21609. &key), 0);
  21610. ExpectIntEQ(verify_ok, 0);
  21611. /* verify_ok should be 0. */
  21612. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21613. &verify_ok, &key), 0);
  21614. ExpectIntEQ(verify_ok, 0);
  21615. /* Test bad args. */
  21616. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  21617. ECC_BAD_ARG_E);
  21618. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  21619. ECC_BAD_ARG_E);
  21620. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  21621. ECC_BAD_ARG_E);
  21622. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  21623. ECC_BAD_ARG_E);
  21624. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  21625. ECC_BAD_ARG_E);
  21626. /* Test bad args. */
  21627. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  21628. &verify_ok, &key), ECC_BAD_ARG_E);
  21629. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  21630. &verify_ok, &key), ECC_BAD_ARG_E);
  21631. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  21632. &verify_ok, &key), MP_ZERO_E);
  21633. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  21634. &verify_ok, &key), MP_ZERO_E);
  21635. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  21636. &verify_ok, &key), MP_ZERO_E);
  21637. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  21638. &key), ECC_BAD_ARG_E);
  21639. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  21640. &key), ECC_BAD_ARG_E);
  21641. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21642. &verify_ok, NULL), ECC_BAD_ARG_E);
  21643. wc_ecc_free(&key);
  21644. mp_free(&r);
  21645. mp_free(&s);
  21646. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21647. #endif
  21648. return EXPECT_RESULT();
  21649. } /* END test_wc_ecc_verify_hash_ex */
  21650. /*
  21651. * Testing wc_ecc_mulmod()
  21652. */
  21653. static int test_wc_ecc_mulmod(void)
  21654. {
  21655. EXPECT_DECLS;
  21656. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  21657. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  21658. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21659. ecc_key key1;
  21660. ecc_key key2;
  21661. ecc_key key3;
  21662. WC_RNG rng;
  21663. int ret;
  21664. XMEMSET(&key1, 0, sizeof(ecc_key));
  21665. XMEMSET(&key2, 0, sizeof(ecc_key));
  21666. XMEMSET(&key3, 0, sizeof(ecc_key));
  21667. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21668. ExpectIntEQ(wc_ecc_init(&key1), 0);
  21669. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21670. ExpectIntEQ(wc_ecc_init(&key3), 0);
  21671. ExpectIntEQ(wc_InitRng(&rng), 0);
  21672. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  21673. #if defined(WOLFSSL_ASYNC_CRYPT)
  21674. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  21675. #endif
  21676. ExpectIntEQ(ret, 0);
  21677. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21678. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  21679. key1.dp->Af, ECC_SECP256R1), 0);
  21680. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  21681. key1.dp->prime, ECC_SECP256R1), 0);
  21682. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21683. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  21684. 1), 0);
  21685. /* Test bad args. */
  21686. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  21687. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21688. ECC_BAD_ARG_E);
  21689. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  21690. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21691. ECC_BAD_ARG_E);
  21692. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  21693. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21694. ECC_BAD_ARG_E);
  21695. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21696. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  21697. wc_ecc_free(&key1);
  21698. wc_ecc_free(&key2);
  21699. wc_ecc_free(&key3);
  21700. #ifdef FP_ECC
  21701. wc_ecc_fp_free();
  21702. #endif
  21703. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  21704. return EXPECT_RESULT();
  21705. } /* END test_wc_ecc_mulmod */
  21706. /*
  21707. * Testing wc_ecc_is_valid_idx()
  21708. */
  21709. static int test_wc_ecc_is_valid_idx(void)
  21710. {
  21711. EXPECT_DECLS;
  21712. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21713. ecc_key key;
  21714. WC_RNG rng;
  21715. int ret;
  21716. int iVal = -2;
  21717. int iVal2 = 3000;
  21718. XMEMSET(&key, 0, sizeof(ecc_key));
  21719. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21720. ExpectIntEQ(wc_ecc_init(&key), 0);
  21721. ExpectIntEQ(wc_InitRng(&rng), 0);
  21722. ret = wc_ecc_make_key(&rng, 32, &key);
  21723. #if defined(WOLFSSL_ASYNC_CRYPT)
  21724. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21725. #endif
  21726. ExpectIntEQ(ret, 0);
  21727. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  21728. /* Test bad args. */
  21729. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  21730. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  21731. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21732. wc_ecc_free(&key);
  21733. #ifdef FP_ECC
  21734. wc_ecc_fp_free();
  21735. #endif
  21736. #endif
  21737. return EXPECT_RESULT();
  21738. } /* END test_wc_ecc_is_valid_idx */
  21739. /*
  21740. * Testing wc_ecc_get_curve_id_from_oid()
  21741. */
  21742. static int test_wc_ecc_get_curve_id_from_oid(void)
  21743. {
  21744. EXPECT_DECLS;
  21745. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21746. !defined(HAVE_FIPS)
  21747. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  21748. word32 len = sizeof(oid);
  21749. /* Bad Cases */
  21750. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  21751. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  21752. /* Good Case */
  21753. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  21754. #endif
  21755. return EXPECT_RESULT();
  21756. } /* END test_wc_ecc_get_curve_id_from_oid */
  21757. /*
  21758. * Testing wc_ecc_sig_size_calc()
  21759. */
  21760. static int test_wc_ecc_sig_size_calc(void)
  21761. {
  21762. EXPECT_DECLS;
  21763. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  21764. ecc_key key;
  21765. WC_RNG rng;
  21766. int sz = 0;
  21767. int ret;
  21768. XMEMSET(&key, 0, sizeof(ecc_key));
  21769. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21770. ExpectIntEQ(wc_ecc_init(&key), 0);
  21771. ExpectIntEQ(wc_InitRng(&rng), 0);
  21772. ret = wc_ecc_make_key(&rng, 16, &key);
  21773. #if defined(WOLFSSL_ASYNC_CRYPT)
  21774. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21775. #endif
  21776. ExpectIntEQ(ret, 0);
  21777. sz = key.dp->size;
  21778. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  21779. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21780. wc_ecc_free(&key);
  21781. #endif
  21782. return EXPECT_RESULT();
  21783. } /* END test_wc_ecc_sig_size_calc */
  21784. /*
  21785. * Testing wc_ecc_sm2_make_key()
  21786. */
  21787. static int test_wc_ecc_sm2_make_key(void)
  21788. {
  21789. int res = TEST_SKIPPED;
  21790. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21791. EXPECT_DECLS;
  21792. WC_RNG rng[1];
  21793. ecc_key key[1];
  21794. XMEMSET(rng, 0, sizeof(*rng));
  21795. XMEMSET(key, 0, sizeof(*key));
  21796. ExpectIntEQ(wc_InitRng(rng), 0);
  21797. ExpectIntEQ(wc_ecc_init(key), 0);
  21798. /* Test invalid parameters. */
  21799. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  21800. BAD_FUNC_ARG);
  21801. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  21802. BAD_FUNC_ARG);
  21803. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  21804. BAD_FUNC_ARG);
  21805. /* Test valid parameters. */
  21806. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  21807. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  21808. wc_ecc_free(key);
  21809. wc_FreeRng(rng);
  21810. #ifdef FP_ECC
  21811. wc_ecc_fp_free();
  21812. #endif
  21813. res = EXPECT_RESULT();
  21814. #endif
  21815. return res;
  21816. }
  21817. /*
  21818. * Testing wc_ecc_sm2_shared_secret()
  21819. */
  21820. static int test_wc_ecc_sm2_shared_secret(void)
  21821. {
  21822. int res = TEST_SKIPPED;
  21823. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21824. EXPECT_DECLS;
  21825. WC_RNG rng[1];
  21826. ecc_key keyA[1];
  21827. ecc_key keyB[1];
  21828. byte outA[32];
  21829. byte outB[32];
  21830. word32 outALen = 32;
  21831. word32 outBLen = 32;
  21832. XMEMSET(rng, 0, sizeof(*rng));
  21833. XMEMSET(keyA, 0, sizeof(*keyA));
  21834. XMEMSET(keyB, 0, sizeof(*keyB));
  21835. ExpectIntEQ(wc_InitRng(rng), 0);
  21836. ExpectIntEQ(wc_ecc_init(keyA), 0);
  21837. ExpectIntEQ(wc_ecc_init(keyB), 0);
  21838. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  21839. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  21840. #ifdef ECC_TIMING_RESISTANT
  21841. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  21842. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  21843. #endif
  21844. /* Test invalid parameters. */
  21845. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  21846. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  21847. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  21848. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  21849. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  21850. BAD_FUNC_ARG);
  21851. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  21852. BAD_FUNC_ARG);
  21853. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  21854. BAD_FUNC_ARG);
  21855. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  21856. BAD_FUNC_ARG);
  21857. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  21858. /* Test valid parameters. */
  21859. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  21860. ExpectIntLE(outALen, 32);
  21861. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  21862. ExpectIntLE(outBLen, 32);
  21863. ExpectIntEQ(outALen, outBLen);
  21864. ExpectBufEQ(outA, outB, outALen);
  21865. wc_ecc_free(keyB);
  21866. wc_ecc_free(keyA);
  21867. wc_FreeRng(rng);
  21868. #ifdef FP_ECC
  21869. wc_ecc_fp_free();
  21870. #endif
  21871. res = EXPECT_RESULT();
  21872. #endif
  21873. return res;
  21874. }
  21875. /*
  21876. * Testing wc_ecc_sm2_create_digest()
  21877. */
  21878. static int test_wc_ecc_sm2_create_digest(void)
  21879. {
  21880. int res = TEST_SKIPPED;
  21881. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  21882. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  21883. EXPECT_DECLS;
  21884. ecc_key key[1];
  21885. enum wc_HashType hashType;
  21886. unsigned char pub[] = {
  21887. 0x04,
  21888. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21889. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21890. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21891. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21892. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21893. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21894. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21895. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21896. };
  21897. unsigned char id[] = {
  21898. 0x01, 0x02, 0x03,
  21899. };
  21900. unsigned char msg[] = {
  21901. 0x01, 0x02, 0x03,
  21902. };
  21903. unsigned char hash[32];
  21904. #ifdef WOLFSSL_SM3
  21905. unsigned char expHash[32] = {
  21906. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  21907. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  21908. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  21909. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  21910. };
  21911. #else
  21912. unsigned char expHash[32] = {
  21913. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  21914. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  21915. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  21916. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  21917. };
  21918. #endif
  21919. #ifdef WOLFSSL_SM3
  21920. hashType = WC_HASH_TYPE_SM3;
  21921. #else
  21922. hashType = WC_HASH_TYPE_SHA256;
  21923. #endif
  21924. XMEMSET(key, 0, sizeof(*key));
  21925. ExpectIntEQ(wc_ecc_init(key), 0);
  21926. /* Test with no curve set. */
  21927. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21928. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21929. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  21930. /* Test invalid parameters. */
  21931. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21932. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21933. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21934. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21935. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21936. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21937. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21938. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21939. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21940. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21941. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21942. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21943. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21944. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21945. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21946. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21947. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21948. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21949. /* Bad hash type. */
  21950. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21951. -1, hash, 0, key), BAD_FUNC_ARG);
  21952. /* Bad hash size. */
  21953. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21954. hashType, hash, 0, key), BUFFER_E);
  21955. /* Test valid parameters. */
  21956. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21957. hashType, hash, sizeof(hash), key), 0);
  21958. ExpectBufEQ(hash, expHash, sizeof(expHash));
  21959. wc_ecc_free(key);
  21960. res = EXPECT_RESULT();
  21961. #endif
  21962. return res;
  21963. }
  21964. /*
  21965. * Testing wc_ecc_sm2_verify_hash_ex()
  21966. */
  21967. static int test_wc_ecc_sm2_verify_hash_ex(void)
  21968. {
  21969. int res = TEST_SKIPPED;
  21970. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  21971. defined(WOLFSSL_PUBLIC_MP)
  21972. EXPECT_DECLS;
  21973. ecc_key key[1];
  21974. mp_int r[1];
  21975. mp_int s[1];
  21976. int verified;
  21977. unsigned char pub[] = {
  21978. 0x04,
  21979. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21980. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21981. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21982. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21983. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21984. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21985. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21986. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21987. };
  21988. unsigned char hash[] = {
  21989. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  21990. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  21991. 0x50, 0x69, 0x5B, 0x20
  21992. };
  21993. unsigned char rData[] = {
  21994. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  21995. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  21996. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  21997. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  21998. };
  21999. unsigned char sData[] = {
  22000. 0x1D,
  22001. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22002. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22003. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22004. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22005. };
  22006. unsigned char rBadData[] = {
  22007. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22008. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22009. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22010. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22011. };
  22012. XMEMSET(key, 0, sizeof(*key));
  22013. XMEMSET(r, 0, sizeof(*r));
  22014. XMEMSET(s, 0, sizeof(*s));
  22015. ExpectIntEQ(mp_init(r), 0);
  22016. ExpectIntEQ(mp_init(s), 0);
  22017. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22018. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22019. ExpectIntEQ(wc_ecc_init(key), 0);
  22020. /* Test with no curve set. */
  22021. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22022. &verified, key), BAD_FUNC_ARG);
  22023. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22024. /* Test invalid parameters. */
  22025. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22026. NULL, NULL), BAD_FUNC_ARG);
  22027. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22028. NULL, NULL), BAD_FUNC_ARG);
  22029. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22030. NULL, NULL), BAD_FUNC_ARG);
  22031. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22032. NULL, NULL), BAD_FUNC_ARG);
  22033. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22034. &verified, NULL), BAD_FUNC_ARG);
  22035. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22036. NULL, key), BAD_FUNC_ARG);
  22037. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22038. &verified, key), BAD_FUNC_ARG);
  22039. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22040. &verified, key), BAD_FUNC_ARG);
  22041. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22042. &verified, key), BAD_FUNC_ARG);
  22043. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22044. NULL, key), BAD_FUNC_ARG);
  22045. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22046. &verified, NULL), BAD_FUNC_ARG);
  22047. /* Make key not on the SM2 curve. */
  22048. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22049. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22050. &verified, key), BAD_FUNC_ARG);
  22051. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22052. /* Test valid parameters. */
  22053. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22054. &verified, key), 0);
  22055. ExpectIntEQ(verified, 1);
  22056. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22057. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22058. &verified, key), 0);
  22059. ExpectIntEQ(verified, 0);
  22060. mp_free(s);
  22061. mp_free(r);
  22062. wc_ecc_free(key);
  22063. #ifdef FP_ECC
  22064. wc_ecc_fp_free();
  22065. #endif
  22066. res = EXPECT_RESULT();
  22067. #endif
  22068. return res;
  22069. }
  22070. /*
  22071. * Testing wc_ecc_sm2_verify_hash()
  22072. */
  22073. static int test_wc_ecc_sm2_verify_hash(void)
  22074. {
  22075. int res = TEST_SKIPPED;
  22076. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22077. EXPECT_DECLS;
  22078. ecc_key key[1];
  22079. int verified;
  22080. unsigned char pub[] = {
  22081. 0x04,
  22082. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22083. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22084. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22085. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22086. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22087. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22088. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22089. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22090. };
  22091. unsigned char hash[] = {
  22092. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22093. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22094. 0x50, 0x69, 0x5B, 0x20
  22095. };
  22096. unsigned char sig[] = {
  22097. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22098. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22099. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22100. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22101. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22102. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22103. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22104. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22105. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22106. };
  22107. unsigned char sigBad[] = {
  22108. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22109. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22110. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22111. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22112. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22113. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22114. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22115. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22116. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22117. };
  22118. XMEMSET(key, 0, sizeof(*key));
  22119. ExpectIntEQ(wc_ecc_init(key), 0);
  22120. /* Test with no curve set. */
  22121. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22122. &verified, key), BAD_FUNC_ARG);
  22123. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22124. /* Test invalid parameters. */
  22125. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22126. NULL, NULL), BAD_FUNC_ARG);
  22127. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22128. NULL, NULL), BAD_FUNC_ARG);
  22129. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22130. NULL, NULL), BAD_FUNC_ARG);
  22131. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22132. &verified, NULL), BAD_FUNC_ARG);
  22133. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22134. NULL, key), BAD_FUNC_ARG);
  22135. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22136. &verified, key), BAD_FUNC_ARG);
  22137. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22138. &verified, key), BAD_FUNC_ARG);
  22139. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22140. NULL, key), BAD_FUNC_ARG);
  22141. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22142. &verified, NULL), BAD_FUNC_ARG);
  22143. /* Make key not on the SM2 curve. */
  22144. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22145. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22146. &verified, key), BAD_FUNC_ARG);
  22147. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22148. /* Test valid parameters. */
  22149. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22150. &verified, key), 0);
  22151. ExpectIntEQ(verified, 1);
  22152. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22153. sizeof(hash), &verified, key), 0);
  22154. ExpectIntEQ(verified, 0);
  22155. wc_ecc_free(key);
  22156. #ifdef FP_ECC
  22157. wc_ecc_fp_free();
  22158. #endif
  22159. res = EXPECT_RESULT();
  22160. #endif
  22161. return res;
  22162. }
  22163. /*
  22164. * Testing wc_ecc_sm2_verify_hash_ex()
  22165. */
  22166. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22167. {
  22168. int res = TEST_SKIPPED;
  22169. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22170. defined(WOLFSSL_PUBLIC_MP)
  22171. EXPECT_DECLS;
  22172. WC_RNG rng[1];
  22173. ecc_key key[1];
  22174. mp_int r[1];
  22175. mp_int s[1];
  22176. unsigned char hash[32];
  22177. #ifdef HAVE_ECC_VERIFY
  22178. int verified;
  22179. #endif
  22180. XMEMSET(rng, 0, sizeof(*rng));
  22181. XMEMSET(key, 0, sizeof(*key));
  22182. XMEMSET(r, 0, sizeof(*r));
  22183. XMEMSET(s, 0, sizeof(*s));
  22184. ExpectIntEQ(wc_InitRng(rng), 0);
  22185. ExpectIntEQ(mp_init(r), 0);
  22186. ExpectIntEQ(mp_init(s), 0);
  22187. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22188. ExpectIntEQ(wc_ecc_init(key), 0);
  22189. /* Test with no curve set. */
  22190. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22191. BAD_FUNC_ARG);
  22192. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22193. /* Test invalid parameters. */
  22194. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22195. NULL), BAD_FUNC_ARG);
  22196. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22197. NULL), BAD_FUNC_ARG);
  22198. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22199. NULL), BAD_FUNC_ARG);
  22200. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22201. NULL), BAD_FUNC_ARG);
  22202. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22203. NULL), BAD_FUNC_ARG);
  22204. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22205. s), BAD_FUNC_ARG);
  22206. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22207. BAD_FUNC_ARG);
  22208. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22209. BAD_FUNC_ARG);
  22210. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22211. BAD_FUNC_ARG);
  22212. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22213. BAD_FUNC_ARG);
  22214. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22215. BAD_FUNC_ARG);
  22216. /* Make key not on the SM2 curve. */
  22217. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22218. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22219. BAD_FUNC_ARG);
  22220. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22221. #ifdef WOLFSSL_SP_MATH_ALL
  22222. {
  22223. mp_int smallR[1];
  22224. sp_init_size(smallR, 1);
  22225. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22226. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22227. smallR, s), 0);
  22228. }
  22229. #endif
  22230. /* Test valid parameters. */
  22231. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22232. 0);
  22233. #ifdef HAVE_ECC_VERIFY
  22234. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22235. key), 0);
  22236. ExpectIntEQ(verified, 1);
  22237. #endif
  22238. mp_free(s);
  22239. mp_free(r);
  22240. wc_ecc_free(key);
  22241. wc_FreeRng(rng);
  22242. #ifdef FP_ECC
  22243. wc_ecc_fp_free();
  22244. #endif
  22245. res = EXPECT_RESULT();
  22246. #endif
  22247. return res;
  22248. }
  22249. /*
  22250. * Testing wc_ecc_sm2_verify_hash()
  22251. */
  22252. static int test_wc_ecc_sm2_sign_hash(void)
  22253. {
  22254. int res = TEST_SKIPPED;
  22255. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22256. EXPECT_DECLS;
  22257. WC_RNG rng[1];
  22258. ecc_key key[1];
  22259. unsigned char hash[32];
  22260. unsigned char sig[72];
  22261. word32 sigSz = sizeof(sig);
  22262. #ifdef HAVE_ECC_VERIFY
  22263. int verified;
  22264. #endif
  22265. XMEMSET(rng, 0, sizeof(*rng));
  22266. XMEMSET(key, 0, sizeof(*key));
  22267. ExpectIntEQ(wc_InitRng(rng), 0);
  22268. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22269. ExpectIntEQ(wc_ecc_init(key), 0);
  22270. /* Test with no curve set. */
  22271. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22272. BAD_FUNC_ARG);
  22273. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22274. /* Test invalid parameters. */
  22275. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22276. NULL), BAD_FUNC_ARG);
  22277. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22278. NULL), BAD_FUNC_ARG);
  22279. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22280. NULL), BAD_FUNC_ARG);
  22281. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22282. NULL), BAD_FUNC_ARG);
  22283. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22284. NULL), BAD_FUNC_ARG);
  22285. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22286. key), BAD_FUNC_ARG);
  22287. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22288. key), BAD_FUNC_ARG);
  22289. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22290. key), BAD_FUNC_ARG);
  22291. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22292. key), BAD_FUNC_ARG);
  22293. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22294. key), BAD_FUNC_ARG);
  22295. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22296. NULL), BAD_FUNC_ARG);
  22297. /* Make key not on the SM2 curve. */
  22298. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22299. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22300. BAD_FUNC_ARG);
  22301. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22302. /* Test valid parameters. */
  22303. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22304. 0);
  22305. #ifdef HAVE_ECC_VERIFY
  22306. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22307. &verified, key), 0);
  22308. ExpectIntEQ(verified, 1);
  22309. #endif
  22310. wc_ecc_free(key);
  22311. wc_FreeRng(rng);
  22312. #ifdef FP_ECC
  22313. wc_ecc_fp_free();
  22314. #endif
  22315. res = EXPECT_RESULT();
  22316. #endif
  22317. return res;
  22318. }
  22319. /*
  22320. * Testing ToTraditional
  22321. */
  22322. static int test_ToTraditional(void)
  22323. {
  22324. EXPECT_DECLS;
  22325. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22326. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22327. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22328. XFILE f = XBADFILE;
  22329. byte input[TWOK_BUF];
  22330. word32 sz;
  22331. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22332. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22333. if (f != XBADFILE)
  22334. XFCLOSE(f);
  22335. /* Good case */
  22336. ExpectIntGT(ToTraditional(input, sz), 0);
  22337. /* Bad cases */
  22338. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22339. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22340. #ifdef WOLFSSL_ASN_TEMPLATE
  22341. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22342. #else
  22343. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22344. #endif
  22345. #endif
  22346. return EXPECT_RESULT();
  22347. } /* End test_ToTraditional*/
  22348. /*
  22349. * Testing wc_EccPrivateKeyToDer
  22350. */
  22351. static int test_wc_EccPrivateKeyToDer(void)
  22352. {
  22353. EXPECT_DECLS;
  22354. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22355. byte output[ONEK_BUF];
  22356. ecc_key eccKey;
  22357. WC_RNG rng;
  22358. word32 inLen;
  22359. word32 outLen = 0;
  22360. int ret;
  22361. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22362. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22363. ExpectIntEQ(wc_InitRng(&rng), 0);
  22364. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22365. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22366. #if defined(WOLFSSL_ASYNC_CRYPT)
  22367. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22368. #endif
  22369. ExpectIntEQ(ret, 0);
  22370. inLen = (word32)sizeof(output);
  22371. /* Bad Cases */
  22372. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22373. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22374. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22375. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22376. /* Good Case */
  22377. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22378. wc_ecc_free(&eccKey);
  22379. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22380. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22381. {
  22382. /* test importing private only into a PKEY struct */
  22383. EC_KEY* ec = NULL;
  22384. EVP_PKEY* pkey = NULL;
  22385. const unsigned char* der;
  22386. der = output;
  22387. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22388. der = output;
  22389. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22390. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22391. if (EXPECT_FAIL()) {
  22392. EC_KEY_free(ec);
  22393. }
  22394. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22395. }
  22396. #endif
  22397. #endif
  22398. return EXPECT_RESULT();
  22399. } /* End test_wc_EccPrivateKeyToDer*/
  22400. /*
  22401. * Testing wc_DhPublicKeyDecode
  22402. */
  22403. static int test_wc_DhPublicKeyDecode(void)
  22404. {
  22405. EXPECT_DECLS;
  22406. #ifndef NO_DH
  22407. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22408. DhKey key;
  22409. word32 inOutIdx;
  22410. XMEMSET(&key, 0, sizeof(DhKey));
  22411. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22412. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22413. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22414. BAD_FUNC_ARG);
  22415. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22416. BAD_FUNC_ARG);
  22417. inOutIdx = 0;
  22418. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22419. BAD_FUNC_ARG);
  22420. inOutIdx = 0;
  22421. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22422. BAD_FUNC_ARG);
  22423. inOutIdx = 0;
  22424. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22425. sizeof_dh_pub_key_der_2048), 0);
  22426. ExpectIntNE(key.p.used, 0);
  22427. ExpectIntNE(key.g.used, 0);
  22428. ExpectIntEQ(key.q.used, 0);
  22429. ExpectIntNE(key.pub.used, 0);
  22430. ExpectIntEQ(key.priv.used, 0);
  22431. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22432. #endif
  22433. #endif /* !NO_DH */
  22434. return EXPECT_RESULT();
  22435. }
  22436. /*
  22437. * Testing wc_Ed25519KeyToDer
  22438. */
  22439. static int test_wc_Ed25519KeyToDer(void)
  22440. {
  22441. EXPECT_DECLS;
  22442. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22443. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22444. byte output[ONEK_BUF];
  22445. ed25519_key ed25519Key;
  22446. WC_RNG rng;
  22447. word32 inLen;
  22448. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22449. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22450. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22451. ExpectIntEQ(wc_InitRng(&rng), 0);
  22452. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22453. inLen = (word32)sizeof(output);
  22454. /* Bad Cases */
  22455. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22456. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22457. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22458. /* Good Cases */
  22459. /* length only */
  22460. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22461. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22462. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22463. wc_ed25519_free(&ed25519Key);
  22464. #endif
  22465. return EXPECT_RESULT();
  22466. } /* End test_wc_Ed25519KeyToDer*/
  22467. /*
  22468. * Testing wc_Ed25519PrivateKeyToDer
  22469. */
  22470. static int test_wc_Ed25519PrivateKeyToDer(void)
  22471. {
  22472. EXPECT_DECLS;
  22473. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22474. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22475. byte output[ONEK_BUF];
  22476. ed25519_key ed25519PrivKey;
  22477. WC_RNG rng;
  22478. word32 inLen;
  22479. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22480. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22481. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22482. ExpectIntEQ(wc_InitRng(&rng), 0);
  22483. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22484. 0);
  22485. inLen = (word32)sizeof(output);
  22486. /* Bad Cases */
  22487. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22488. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22489. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22490. BAD_FUNC_ARG);
  22491. /* Good Cases */
  22492. /* length only */
  22493. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22494. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22495. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22496. wc_ed25519_free(&ed25519PrivKey);
  22497. #endif
  22498. return EXPECT_RESULT();
  22499. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22500. /*
  22501. * Testing wc_Ed448KeyToDer
  22502. */
  22503. static int test_wc_Ed448KeyToDer(void)
  22504. {
  22505. EXPECT_DECLS;
  22506. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22507. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22508. byte output[ONEK_BUF];
  22509. ed448_key ed448Key;
  22510. WC_RNG rng;
  22511. word32 inLen;
  22512. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22513. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22514. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22515. ExpectIntEQ(wc_InitRng(&rng), 0);
  22516. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22517. inLen = (word32)sizeof(output);
  22518. /* Bad Cases */
  22519. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22520. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22521. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22522. /* Good Cases */
  22523. /* length only */
  22524. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22525. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22526. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22527. wc_ed448_free(&ed448Key);
  22528. #endif
  22529. return EXPECT_RESULT();
  22530. } /* End test_wc_Ed448KeyToDer*/
  22531. /*
  22532. * Testing wc_Ed448PrivateKeyToDer
  22533. */
  22534. static int test_wc_Ed448PrivateKeyToDer(void)
  22535. {
  22536. EXPECT_DECLS;
  22537. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22538. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22539. byte output[ONEK_BUF];
  22540. ed448_key ed448PrivKey;
  22541. WC_RNG rng;
  22542. word32 inLen;
  22543. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  22544. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22545. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  22546. ExpectIntEQ(wc_InitRng(&rng), 0);
  22547. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  22548. 0);
  22549. inLen = (word32)sizeof(output);
  22550. /* Bad Cases */
  22551. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22552. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22553. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  22554. BAD_FUNC_ARG);
  22555. /* Good cases */
  22556. /* length only */
  22557. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  22558. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  22559. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22560. wc_ed448_free(&ed448PrivKey);
  22561. #endif
  22562. return EXPECT_RESULT();
  22563. } /* End test_wc_Ed448PrivateKeyToDer*/
  22564. /*
  22565. * Testing wc_SetSubjectBuffer
  22566. */
  22567. static int test_wc_SetSubjectBuffer(void)
  22568. {
  22569. EXPECT_DECLS;
  22570. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  22571. Cert cert;
  22572. XFILE file = XBADFILE;
  22573. byte* der = NULL;
  22574. word32 derSz;
  22575. derSz = FOURK_BUF;
  22576. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22577. DYNAMIC_TYPE_TMP_BUFFER));
  22578. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  22579. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  22580. if (file != XBADFILE)
  22581. XFCLOSE(file);
  22582. ExpectIntEQ(wc_InitCert(&cert), 0);
  22583. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  22584. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  22585. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22586. #endif
  22587. return EXPECT_RESULT();
  22588. } /* End test_wc_SetSubjectBuffer*/
  22589. /*
  22590. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22591. */
  22592. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  22593. {
  22594. EXPECT_DECLS;
  22595. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22596. WC_RNG rng;
  22597. Cert cert;
  22598. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22599. RsaKey rsaKey;
  22600. int bits = 2048;
  22601. #endif
  22602. #if defined(HAVE_ECC)
  22603. ecc_key eccKey;
  22604. int ret;
  22605. #endif
  22606. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22607. ed25519_key ed25519Key;
  22608. #endif
  22609. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22610. ed448_key ed448Key;
  22611. #endif
  22612. #ifndef HAVE_FIPS
  22613. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22614. #else
  22615. ExpectIntEQ(wc_InitRng(&rng), 0);
  22616. #endif
  22617. ExpectIntEQ(wc_InitCert(&cert), 0);
  22618. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22619. /* RSA */
  22620. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22621. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22622. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22623. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  22624. 0);
  22625. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22626. #endif
  22627. #if defined(HAVE_ECC)
  22628. /* ECC */
  22629. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22630. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22631. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22632. #if defined(WOLFSSL_ASYNC_CRYPT)
  22633. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22634. #endif
  22635. ExpectIntEQ(ret, 0);
  22636. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  22637. 0);
  22638. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22639. #endif
  22640. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22641. /* ED25519 */
  22642. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22643. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22644. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22645. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22646. &ed25519Key), 0);
  22647. wc_ed25519_free(&ed25519Key);
  22648. #endif
  22649. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22650. /* ED448 */
  22651. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22652. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22653. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22654. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22655. &ed448Key), 0);
  22656. wc_ed448_free(&ed448Key);
  22657. #endif
  22658. wc_FreeRng(&rng);
  22659. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22660. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  22661. return EXPECT_RESULT();
  22662. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22663. /*
  22664. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22665. */
  22666. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  22667. {
  22668. EXPECT_DECLS;
  22669. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22670. WC_RNG rng;
  22671. Cert cert;
  22672. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22673. RsaKey rsaKey;
  22674. int bits = 2048;
  22675. #endif
  22676. #if defined(HAVE_ECC)
  22677. ecc_key eccKey;
  22678. int ret;
  22679. #endif
  22680. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22681. ed25519_key ed25519Key;
  22682. #endif
  22683. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22684. ed448_key ed448Key;
  22685. #endif
  22686. #ifndef HAVE_FIPS
  22687. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22688. #else
  22689. ExpectIntEQ(wc_InitRng(&rng), 0);
  22690. #endif
  22691. ExpectIntEQ(wc_InitCert(&cert), 0);
  22692. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22693. /* RSA */
  22694. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22695. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22696. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22697. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  22698. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22699. #endif
  22700. #if defined(HAVE_ECC)
  22701. /* ECC */
  22702. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22703. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22704. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22705. #if defined(WOLFSSL_ASYNC_CRYPT)
  22706. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22707. #endif
  22708. ExpectIntEQ(ret, 0);
  22709. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  22710. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22711. #endif
  22712. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22713. /* ED25519 */
  22714. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22715. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22716. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22717. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22718. &ed25519Key), 0);
  22719. wc_ed25519_free(&ed25519Key);
  22720. #endif
  22721. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22722. /* ED448 */
  22723. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22724. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22725. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22726. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  22727. 0);
  22728. wc_ed448_free(&ed448Key);
  22729. #endif
  22730. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22731. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  22732. return EXPECT_RESULT();
  22733. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  22734. /*
  22735. * Testing wc_PKCS7_New()
  22736. */
  22737. static int test_wc_PKCS7_New(void)
  22738. {
  22739. EXPECT_DECLS;
  22740. #if defined(HAVE_PKCS7)
  22741. PKCS7* pkcs7 = NULL;
  22742. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  22743. wc_PKCS7_Free(pkcs7);
  22744. #endif
  22745. return EXPECT_RESULT();
  22746. } /* END test-wc_PKCS7_New */
  22747. /*
  22748. * Testing wc_PKCS7_Init()
  22749. */
  22750. static int test_wc_PKCS7_Init(void)
  22751. {
  22752. EXPECT_DECLS;
  22753. #if defined(HAVE_PKCS7)
  22754. PKCS7* pkcs7 = NULL;
  22755. void* heap = NULL;
  22756. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  22757. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  22758. /* Pass in bad args. */
  22759. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  22760. wc_PKCS7_Free(pkcs7);
  22761. #endif
  22762. return EXPECT_RESULT();
  22763. } /* END test-wc_PKCS7_Init */
  22764. /*
  22765. * Testing wc_PKCS7_InitWithCert()
  22766. */
  22767. static int test_wc_PKCS7_InitWithCert(void)
  22768. {
  22769. EXPECT_DECLS;
  22770. #if defined(HAVE_PKCS7)
  22771. PKCS7* pkcs7 = NULL;
  22772. #ifndef NO_RSA
  22773. #if defined(USE_CERT_BUFFERS_2048)
  22774. unsigned char cert[sizeof(client_cert_der_2048)];
  22775. int certSz = (int)sizeof(cert);
  22776. XMEMSET(cert, 0, certSz);
  22777. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  22778. #elif defined(USE_CERT_BUFFERS_1024)
  22779. unsigned char cert[sizeof(client_cert_der_1024)];
  22780. int certSz = (int)sizeof(cert);
  22781. XMEMSET(cert, 0, certSz);
  22782. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  22783. #else
  22784. unsigned char cert[ONEK_BUF];
  22785. XFILE fp = XBADFILE;
  22786. int certSz;
  22787. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22788. XBADFILE);
  22789. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22790. fp), 0);
  22791. if (fp != XBADFILE)
  22792. XFCLOSE(fp);
  22793. #endif
  22794. #elif defined(HAVE_ECC)
  22795. #if defined(USE_CERT_BUFFERS_256)
  22796. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22797. int certSz = (int)sizeof(cert);
  22798. XMEMSET(cert, 0, certSz);
  22799. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  22800. #else
  22801. unsigned char cert[ONEK_BUF];
  22802. XFILE fp = XBADFILE;
  22803. int certSz;
  22804. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22805. XBADFILE);
  22806. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  22807. fp), 0);
  22808. if (fp != XBADFILE)
  22809. XFCLOSE(fp);
  22810. #endif
  22811. #else
  22812. #error PKCS7 requires ECC or RSA
  22813. #endif
  22814. #ifdef HAVE_ECC
  22815. {
  22816. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  22817. static unsigned char certWithInvalidEccKey[] = {
  22818. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  22819. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  22820. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  22821. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  22822. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22823. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  22824. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22825. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22826. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22827. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22828. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22829. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22830. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22831. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22832. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22833. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  22834. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  22835. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  22836. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  22837. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  22838. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  22839. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  22840. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  22841. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  22842. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  22843. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  22844. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  22845. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  22846. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  22847. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  22848. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  22849. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  22850. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  22851. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  22852. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  22853. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  22854. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  22855. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  22856. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  22857. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  22858. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22859. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  22860. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  22861. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22862. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  22863. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22864. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  22865. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22866. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22867. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22868. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22869. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22870. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22871. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22872. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22873. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22874. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  22875. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  22876. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  22877. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  22878. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  22879. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  22880. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  22881. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  22882. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  22883. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  22884. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  22885. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  22886. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  22887. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  22888. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  22889. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  22890. 0x08, 0xA8, 0xB4
  22891. };
  22892. #endif
  22893. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22894. /* If initialization is not successful, it's free'd in init func. */
  22895. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  22896. 0);
  22897. wc_PKCS7_Free(pkcs7);
  22898. pkcs7 = NULL;
  22899. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22900. /* Valid initialization usage. */
  22901. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22902. /* Pass in bad args. No need free for null checks, free at end.*/
  22903. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  22904. BAD_FUNC_ARG);
  22905. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  22906. BAD_FUNC_ARG);
  22907. #ifdef HAVE_ECC
  22908. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  22909. sizeof(certWithInvalidEccKey)), 0);
  22910. }
  22911. #endif
  22912. wc_PKCS7_Free(pkcs7);
  22913. #endif
  22914. return EXPECT_RESULT();
  22915. } /* END test_wc_PKCS7_InitWithCert */
  22916. /*
  22917. * Testing wc_PKCS7_EncodeData()
  22918. */
  22919. static int test_wc_PKCS7_EncodeData(void)
  22920. {
  22921. EXPECT_DECLS;
  22922. #if defined(HAVE_PKCS7)
  22923. PKCS7* pkcs7 = NULL;
  22924. byte output[FOURK_BUF];
  22925. byte data[] = "My encoded DER cert.";
  22926. #ifndef NO_RSA
  22927. #if defined(USE_CERT_BUFFERS_2048)
  22928. unsigned char cert[sizeof(client_cert_der_2048)];
  22929. unsigned char key[sizeof(client_key_der_2048)];
  22930. int certSz = (int)sizeof(cert);
  22931. int keySz = (int)sizeof(key);
  22932. XMEMSET(cert, 0, certSz);
  22933. XMEMSET(key, 0, keySz);
  22934. XMEMCPY(cert, client_cert_der_2048, certSz);
  22935. XMEMCPY(key, client_key_der_2048, keySz);
  22936. #elif defined(USE_CERT_BUFFERS_1024)
  22937. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  22938. unsigned char key[sizeof_client_key_der_1024];
  22939. int certSz = (int)sizeof(cert);
  22940. int keySz = (int)sizeof(key);
  22941. XMEMSET(cert, 0, certSz);
  22942. XMEMSET(key, 0, keySz);
  22943. XMEMCPY(cert, client_cert_der_1024, certSz);
  22944. XMEMCPY(key, client_key_der_1024, keySz);
  22945. #else
  22946. unsigned char cert[ONEK_BUF];
  22947. unsigned char key[ONEK_BUF];
  22948. XFILE fp = XBADFILE;
  22949. int certSz;
  22950. int keySz;
  22951. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22952. XBADFILE);
  22953. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22954. fp), 0);
  22955. if (fp != XBADFILE) {
  22956. XFCLOSE(fp);
  22957. fp = XBADFILE;
  22958. }
  22959. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  22960. XBADFILE);
  22961. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  22962. 0);
  22963. if (fp != XBADFILE)
  22964. XFCLOSE(fp);
  22965. #endif
  22966. #elif defined(HAVE_ECC)
  22967. #if defined(USE_CERT_BUFFERS_256)
  22968. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22969. unsigned char key[sizeof(ecc_clikey_der_256)];
  22970. int certSz = (int)sizeof(cert);
  22971. int keySz = (int)sizeof(key);
  22972. XMEMSET(cert, 0, certSz);
  22973. XMEMSET(key, 0, keySz);
  22974. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  22975. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  22976. #else
  22977. unsigned char cert[ONEK_BUF];
  22978. unsigned char key[ONEK_BUF];
  22979. XFILE fp = XBADFILE;
  22980. int certSz, keySz;
  22981. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22982. XBADFILE);
  22983. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  22984. fp), 0);
  22985. if (fp != XBADFILE) {
  22986. XFCLOSE(fp);
  22987. fp = XBADFILE;
  22988. }
  22989. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  22990. XBADFILE);
  22991. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  22992. 0);
  22993. if (fp != XBADFILE)
  22994. XFCLOSE(fp);
  22995. #endif
  22996. #endif
  22997. XMEMSET(output, 0, sizeof(output));
  22998. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22999. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23000. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23001. if (pkcs7 != NULL) {
  23002. pkcs7->content = data;
  23003. pkcs7->contentSz = sizeof(data);
  23004. pkcs7->privateKey = key;
  23005. pkcs7->privateKeySz = keySz;
  23006. }
  23007. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23008. /* Test bad args. */
  23009. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23010. BAD_FUNC_ARG);
  23011. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23012. BAD_FUNC_ARG);
  23013. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23014. wc_PKCS7_Free(pkcs7);
  23015. #endif
  23016. return EXPECT_RESULT();
  23017. } /* END test_wc_PKCS7_EncodeData */
  23018. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23019. !defined(NO_RSA) && !defined(NO_SHA256)
  23020. /* RSA sign raw digest callback */
  23021. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23022. byte* out, word32 outSz, byte* privateKey,
  23023. word32 privateKeySz, int devid, int hashOID)
  23024. {
  23025. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23026. byte digInfoEncoding[] = {
  23027. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23028. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23029. 0x00, 0x04, 0x20
  23030. };
  23031. int ret;
  23032. byte digestInfo[ONEK_BUF];
  23033. byte sig[FOURK_BUF];
  23034. word32 digestInfoSz = 0;
  23035. word32 idx = 0;
  23036. RsaKey rsa;
  23037. /* SHA-256 required only for this example callback due to above
  23038. * digInfoEncoding[] */
  23039. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23040. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23041. (hashOID != SHA256h)) {
  23042. return -1;
  23043. }
  23044. /* build DigestInfo */
  23045. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23046. digestInfoSz += sizeof(digInfoEncoding);
  23047. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23048. digestInfoSz += digestSz;
  23049. /* set up RSA key */
  23050. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23051. if (ret != 0) {
  23052. return ret;
  23053. }
  23054. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23055. /* sign DigestInfo */
  23056. if (ret == 0) {
  23057. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23058. &rsa, pkcs7->rng);
  23059. if (ret > 0) {
  23060. if (ret > (int)outSz) {
  23061. /* output buffer too small */
  23062. ret = -1;
  23063. }
  23064. else {
  23065. /* success, ret holds sig size */
  23066. XMEMCPY(out, sig, ret);
  23067. }
  23068. }
  23069. }
  23070. wc_FreeRsaKey(&rsa);
  23071. return ret;
  23072. }
  23073. #endif
  23074. /*
  23075. * Testing wc_PKCS7_EncodeSignedData()
  23076. */
  23077. static int test_wc_PKCS7_EncodeSignedData(void)
  23078. {
  23079. EXPECT_DECLS;
  23080. #if defined(HAVE_PKCS7)
  23081. PKCS7* pkcs7 = NULL;
  23082. WC_RNG rng;
  23083. byte output[FOURK_BUF];
  23084. byte badOut[1];
  23085. word32 outputSz = (word32)sizeof(output);
  23086. word32 badOutSz = 0;
  23087. byte data[] = "Test data to encode.";
  23088. #ifndef NO_RSA
  23089. #if defined(USE_CERT_BUFFERS_2048)
  23090. byte key[sizeof(client_key_der_2048)];
  23091. byte cert[sizeof(client_cert_der_2048)];
  23092. word32 keySz = (word32)sizeof(key);
  23093. word32 certSz = (word32)sizeof(cert);
  23094. XMEMSET(key, 0, keySz);
  23095. XMEMSET(cert, 0, certSz);
  23096. XMEMCPY(key, client_key_der_2048, keySz);
  23097. XMEMCPY(cert, client_cert_der_2048, certSz);
  23098. #elif defined(USE_CERT_BUFFERS_1024)
  23099. byte key[sizeof_client_key_der_1024];
  23100. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23101. word32 keySz = (word32)sizeof(key);
  23102. word32 certSz = (word32)sizeof(cert);
  23103. XMEMSET(key, 0, keySz);
  23104. XMEMSET(cert, 0, certSz);
  23105. XMEMCPY(key, client_key_der_1024, keySz);
  23106. XMEMCPY(cert, client_cert_der_1024, certSz);
  23107. #else
  23108. unsigned char cert[ONEK_BUF];
  23109. unsigned char key[ONEK_BUF];
  23110. XFILE fp = XBADFILE;
  23111. int certSz;
  23112. int keySz;
  23113. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23114. XBADFILE);
  23115. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23116. fp), 0);
  23117. if (fp != XBADFILE) {
  23118. XFCLOSE(fp);
  23119. fp = XBADFILE;
  23120. }
  23121. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23122. XBADFILE);
  23123. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23124. 0);
  23125. if (fp != XBADFILE)
  23126. XFCLOSE(fp);
  23127. #endif
  23128. #elif defined(HAVE_ECC)
  23129. #if defined(USE_CERT_BUFFERS_256)
  23130. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23131. unsigned char key[sizeof(ecc_clikey_der_256)];
  23132. int certSz = (int)sizeof(cert);
  23133. int keySz = (int)sizeof(key);
  23134. XMEMSET(cert, 0, certSz);
  23135. XMEMSET(key, 0, keySz);
  23136. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23137. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23138. #else
  23139. unsigned char cert[ONEK_BUF];
  23140. unsigned char key[ONEK_BUF];
  23141. XFILE fp = XBADFILE;
  23142. int certSz;
  23143. int keySz;
  23144. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23145. XBADFILE);
  23146. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23147. if (fp != XBADFILE) {
  23148. XFCLOSE(fp);
  23149. fp = XBADFILE;
  23150. }
  23151. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23152. XBADFILE);
  23153. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23154. if (fp != XBADFILE)
  23155. XFCLOSE(fp);
  23156. #endif
  23157. #endif
  23158. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23159. XMEMSET(output, 0, outputSz);
  23160. ExpectIntEQ(wc_InitRng(&rng), 0);
  23161. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23162. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23163. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23164. if (pkcs7 != NULL) {
  23165. pkcs7->content = data;
  23166. pkcs7->contentSz = (word32)sizeof(data);
  23167. pkcs7->privateKey = key;
  23168. pkcs7->privateKeySz = (word32)sizeof(key);
  23169. pkcs7->encryptOID = RSAk;
  23170. #ifdef NO_SHA
  23171. pkcs7->hashOID = SHA256h;
  23172. #else
  23173. pkcs7->hashOID = SHAh;
  23174. #endif
  23175. pkcs7->rng = &rng;
  23176. }
  23177. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23178. wc_PKCS7_Free(pkcs7);
  23179. pkcs7 = NULL;
  23180. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23181. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23182. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23183. /* Pass in bad args. */
  23184. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23185. BAD_FUNC_ARG);
  23186. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23187. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23188. badOutSz), BAD_FUNC_ARG);
  23189. if (pkcs7 != NULL) {
  23190. pkcs7->hashOID = 0; /* bad hashOID */
  23191. }
  23192. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23193. BAD_FUNC_ARG);
  23194. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23195. !defined(NO_RSA) && !defined(NO_SHA256)
  23196. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23197. * Example callback assumes SHA-256, so only run test if compiled in. */
  23198. wc_PKCS7_Free(pkcs7);
  23199. pkcs7 = NULL;
  23200. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23201. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23202. if (pkcs7 != NULL) {
  23203. pkcs7->content = data;
  23204. pkcs7->contentSz = (word32)sizeof(data);
  23205. pkcs7->privateKey = key;
  23206. pkcs7->privateKeySz = (word32)sizeof(key);
  23207. pkcs7->encryptOID = RSAk;
  23208. pkcs7->hashOID = SHA256h;
  23209. pkcs7->rng = &rng;
  23210. }
  23211. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23212. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23213. #endif
  23214. wc_PKCS7_Free(pkcs7);
  23215. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23216. #endif
  23217. return EXPECT_RESULT();
  23218. } /* END test_wc_PKCS7_EncodeSignedData */
  23219. /*
  23220. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23221. */
  23222. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23223. {
  23224. EXPECT_DECLS;
  23225. #if defined(HAVE_PKCS7)
  23226. int i;
  23227. PKCS7* pkcs7 = NULL;
  23228. WC_RNG rng;
  23229. byte outputHead[FOURK_BUF/2];
  23230. byte outputFoot[FOURK_BUF/2];
  23231. word32 outputHeadSz = (word32)sizeof(outputHead);
  23232. word32 outputFootSz = (word32)sizeof(outputFoot);
  23233. byte data[FOURK_BUF];
  23234. wc_HashAlg hash;
  23235. #ifdef NO_SHA
  23236. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23237. #else
  23238. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23239. #endif
  23240. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23241. word32 hashSz = wc_HashGetDigestSize(hashType);
  23242. #ifndef NO_RSA
  23243. #if defined(USE_CERT_BUFFERS_2048)
  23244. byte key[sizeof(client_key_der_2048)];
  23245. byte cert[sizeof(client_cert_der_2048)];
  23246. word32 keySz = (word32)sizeof(key);
  23247. word32 certSz = (word32)sizeof(cert);
  23248. XMEMSET(key, 0, keySz);
  23249. XMEMSET(cert, 0, certSz);
  23250. XMEMCPY(key, client_key_der_2048, keySz);
  23251. XMEMCPY(cert, client_cert_der_2048, certSz);
  23252. #elif defined(USE_CERT_BUFFERS_1024)
  23253. byte key[sizeof_client_key_der_1024];
  23254. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23255. word32 keySz = (word32)sizeof(key);
  23256. word32 certSz = (word32)sizeof(cert);
  23257. XMEMSET(key, 0, keySz);
  23258. XMEMSET(cert, 0, certSz);
  23259. XMEMCPY(key, client_key_der_1024, keySz);
  23260. XMEMCPY(cert, client_cert_der_1024, certSz);
  23261. #else
  23262. unsigned char cert[ONEK_BUF];
  23263. unsigned char key[ONEK_BUF];
  23264. XFILE fp = XBADFILE;
  23265. int certSz;
  23266. int keySz;
  23267. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23268. XBADFILE);
  23269. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23270. fp), 0);
  23271. if (fp != XBADFILE) {
  23272. XFCLOSE(fp);
  23273. fp = XBADFILE;
  23274. }
  23275. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23276. XBADFILE);
  23277. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23278. 0);
  23279. if (fp != XBADFILE)
  23280. XFCLOSE(fp);
  23281. #endif
  23282. #elif defined(HAVE_ECC)
  23283. #if defined(USE_CERT_BUFFERS_256)
  23284. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23285. unsigned char key[sizeof(ecc_clikey_der_256)];
  23286. int certSz = (int)sizeof(cert);
  23287. int keySz = (int)sizeof(key);
  23288. XMEMSET(cert, 0, certSz);
  23289. XMEMSET(key, 0, keySz);
  23290. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23291. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23292. #else
  23293. unsigned char cert[ONEK_BUF];
  23294. unsigned char key[ONEK_BUF];
  23295. XFILE fp = XBADFILE;
  23296. int certSz;
  23297. int keySz;
  23298. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23299. XBADFILE);
  23300. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23301. fp), 0);
  23302. if (fp != XBADFILE) {
  23303. XFCLOSE(fp);
  23304. fp = XBADFILE;
  23305. }
  23306. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23307. XBADFILE);
  23308. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23309. 0);
  23310. if (fp != XBADFILE)
  23311. XFCLOSE(fp);
  23312. #endif
  23313. #endif
  23314. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23315. /* initialize large data with sequence */
  23316. for (i=0; i<(int)sizeof(data); i++)
  23317. data[i] = i & 0xff;
  23318. XMEMSET(outputHead, 0, outputHeadSz);
  23319. XMEMSET(outputFoot, 0, outputFootSz);
  23320. ExpectIntEQ(wc_InitRng(&rng), 0);
  23321. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23322. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23323. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23324. if (pkcs7 != NULL) {
  23325. pkcs7->content = NULL; /* not used for ex */
  23326. pkcs7->contentSz = (word32)sizeof(data);
  23327. pkcs7->privateKey = key;
  23328. pkcs7->privateKeySz = (word32)sizeof(key);
  23329. pkcs7->encryptOID = RSAk;
  23330. #ifdef NO_SHA
  23331. pkcs7->hashOID = SHA256h;
  23332. #else
  23333. pkcs7->hashOID = SHAh;
  23334. #endif
  23335. pkcs7->rng = &rng;
  23336. }
  23337. /* calculate hash for content */
  23338. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23339. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23340. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23341. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23342. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23343. /* Perform PKCS7 sign using hash directly */
  23344. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23345. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23346. ExpectIntGT(outputHeadSz, 0);
  23347. ExpectIntGT(outputFootSz, 0);
  23348. wc_PKCS7_Free(pkcs7);
  23349. pkcs7 = NULL;
  23350. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23351. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23352. /* required parameter even on verify when using _ex, if using outputHead
  23353. * and outputFoot */
  23354. if (pkcs7 != NULL) {
  23355. pkcs7->contentSz = (word32)sizeof(data);
  23356. }
  23357. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23358. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23359. wc_PKCS7_Free(pkcs7);
  23360. pkcs7 = NULL;
  23361. /* assembly complete PKCS7 sign and use normal verify */
  23362. {
  23363. byte* output = NULL;
  23364. word32 outputSz = 0;
  23365. ExpectNotNull(output = (byte*)XMALLOC(
  23366. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23367. DYNAMIC_TYPE_TMP_BUFFER));
  23368. if (output != NULL) {
  23369. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23370. outputSz += outputHeadSz;
  23371. XMEMCPY(&output[outputSz], data, sizeof(data));
  23372. outputSz += sizeof(data);
  23373. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23374. outputSz += outputFootSz;
  23375. }
  23376. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23377. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23378. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23379. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23380. }
  23381. /* Pass in bad args. */
  23382. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23383. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23384. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23385. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23386. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23387. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23388. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23389. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23390. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23391. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23392. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23393. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23394. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23395. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23396. if (pkcs7 != NULL) {
  23397. pkcs7->hashOID = 0; /* bad hashOID */
  23398. }
  23399. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23400. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23401. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23402. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23403. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23404. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23405. #ifndef NO_PKCS7_STREAM
  23406. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23407. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23408. #else
  23409. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23410. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23411. #endif
  23412. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23413. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23414. #ifndef NO_PKCS7_STREAM
  23415. /* can pass in 0 buffer length with streaming API */
  23416. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23417. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23418. #else
  23419. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23420. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23421. #endif
  23422. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23423. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23424. #ifndef NO_PKCS7_STREAM
  23425. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23426. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23427. #else
  23428. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23429. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  23430. #endif
  23431. wc_PKCS7_Free(pkcs7);
  23432. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23433. #endif
  23434. return EXPECT_RESULT();
  23435. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23436. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23437. /**
  23438. * Loads certs/keys from files or buffers into the argument buffers,
  23439. * helper function called by CreatePKCS7SignedData().
  23440. *
  23441. * Returns 0 on success, negative on error.
  23442. */
  23443. static int LoadPKCS7SignedDataCerts(
  23444. int useIntermediateCertChain, int pkAlgoType,
  23445. byte* intCARoot, word32* intCARootSz,
  23446. byte* intCA1, word32* intCA1Sz,
  23447. byte* intCA2, word32* intCA2Sz,
  23448. byte* cert, word32* certSz,
  23449. byte* key, word32* keySz)
  23450. {
  23451. EXPECT_DECLS;
  23452. int ret = 0;
  23453. XFILE fp = XBADFILE;
  23454. #ifndef NO_RSA
  23455. const char* intCARootRSA = "./certs/ca-cert.der";
  23456. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  23457. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  23458. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  23459. const char* intServKeyRSA = "./certs/server-key.der";
  23460. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  23461. const char* cli1024Cert = "./certs/1024/client-cert.der";
  23462. const char* cli1024Key = "./certs/1024/client-key.der";
  23463. #endif
  23464. #endif
  23465. #ifdef HAVE_ECC
  23466. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  23467. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  23468. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  23469. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  23470. const char* intServKeyECC = "./certs/ecc-key.der";
  23471. #ifndef USE_CERT_BUFFERS_256
  23472. const char* cliEccCert = "./certs/client-ecc-cert.der";
  23473. const char* cliEccKey = "./certs/client-ecc-key.der";
  23474. #endif
  23475. #endif
  23476. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  23477. ((useIntermediateCertChain == 1) &&
  23478. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  23479. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  23480. return BAD_FUNC_ARG;
  23481. }
  23482. /* Read/load certs and keys to use for signing based on PK type and chain */
  23483. switch (pkAlgoType) {
  23484. #ifndef NO_RSA
  23485. case RSA_TYPE:
  23486. if (useIntermediateCertChain == 1) {
  23487. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  23488. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  23489. if (fp != XBADFILE) {
  23490. XFCLOSE(fp);
  23491. fp = XBADFILE;
  23492. }
  23493. ExpectIntGT(*intCARootSz, 0);
  23494. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  23495. if (fp != XBADFILE) {
  23496. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23497. XFCLOSE(fp);
  23498. fp = XBADFILE;
  23499. }
  23500. ExpectIntGT(*intCA1Sz, 0);
  23501. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  23502. if (fp != XBADFILE) {
  23503. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23504. XFCLOSE(fp);
  23505. fp = XBADFILE;
  23506. }
  23507. ExpectIntGT(*intCA2Sz, 0);
  23508. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  23509. if (fp != XBADFILE) {
  23510. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23511. XFCLOSE(fp);
  23512. fp = XBADFILE;
  23513. }
  23514. ExpectIntGT(*certSz, 0);
  23515. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  23516. if (fp != XBADFILE) {
  23517. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23518. XFCLOSE(fp);
  23519. fp = XBADFILE;
  23520. }
  23521. ExpectIntGT(*keySz, 0);
  23522. }
  23523. else {
  23524. #if defined(USE_CERT_BUFFERS_2048)
  23525. *keySz = sizeof_client_key_der_2048;
  23526. *certSz = sizeof_client_cert_der_2048;
  23527. XMEMCPY(key, client_key_der_2048, *keySz);
  23528. XMEMCPY(cert, client_cert_der_2048, *certSz);
  23529. #elif defined(USE_CERT_BUFFERS_1024)
  23530. *keySz = sizeof_client_key_der_1024;
  23531. *certSz = sizeof_client_cert_der_1024;
  23532. XMEMCPY(key, client_key_der_1024, *keySz);
  23533. XMEMCPY(cert, client_cert_der_1024, *certSz);
  23534. #else
  23535. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  23536. if (fp != XBADFILE) {
  23537. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23538. XFCLOSE(fp);
  23539. fp = XBADFILE;
  23540. }
  23541. ExpectIntGT(*keySz, 0);
  23542. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  23543. if (fp != XBADFILE) {
  23544. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23545. XFCLOSE(fp);
  23546. fp = XBADFILE;
  23547. }
  23548. ExpectIntGT(*certSz, 0);
  23549. #endif /* USE_CERT_BUFFERS_2048 */
  23550. }
  23551. break;
  23552. #endif /* !NO_RSA */
  23553. #ifdef HAVE_ECC
  23554. case ECC_TYPE:
  23555. if (useIntermediateCertChain == 1) {
  23556. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  23557. if (fp != XBADFILE) {
  23558. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  23559. fp);
  23560. XFCLOSE(fp);
  23561. fp = XBADFILE;
  23562. }
  23563. ExpectIntGT(*intCARootSz, 0);
  23564. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  23565. if (fp != XBADFILE) {
  23566. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23567. XFCLOSE(fp);
  23568. fp = XBADFILE;
  23569. }
  23570. ExpectIntGT(*intCA1Sz, 0);
  23571. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  23572. if (fp != XBADFILE) {
  23573. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23574. XFCLOSE(fp);
  23575. fp = XBADFILE;
  23576. }
  23577. ExpectIntGT(*intCA2Sz, 0);
  23578. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  23579. if (fp != XBADFILE) {
  23580. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23581. XFCLOSE(fp);
  23582. fp = XBADFILE;
  23583. }
  23584. ExpectIntGT(*certSz, 0);
  23585. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  23586. if (fp != XBADFILE) {
  23587. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23588. XFCLOSE(fp);
  23589. fp = XBADFILE;
  23590. }
  23591. ExpectIntGT(*keySz, 0);
  23592. }
  23593. else {
  23594. #if defined(USE_CERT_BUFFERS_256)
  23595. *keySz = sizeof_ecc_clikey_der_256;
  23596. *certSz = sizeof_cliecc_cert_der_256;
  23597. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  23598. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  23599. #else
  23600. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  23601. if (fp != XBADFILE) {
  23602. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23603. XFCLOSE(fp);
  23604. fp = XBADFILE;
  23605. }
  23606. ExpectIntGT(*keySz, 0);
  23607. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  23608. if (fp != XBADFILE) {
  23609. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23610. XFCLOSE(fp);
  23611. fp = XBADFILE;
  23612. }
  23613. ExpectIntGT(*certSz, 0);
  23614. #endif /* USE_CERT_BUFFERS_256 */
  23615. }
  23616. break;
  23617. #endif /* HAVE_ECC */
  23618. default:
  23619. WOLFSSL_MSG("Unsupported SignedData PK type");
  23620. ret = BAD_FUNC_ARG;
  23621. break;
  23622. }
  23623. if (EXPECT_FAIL() && (ret == 0)) {
  23624. ret = BAD_FUNC_ARG;
  23625. }
  23626. return ret;
  23627. }
  23628. /**
  23629. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  23630. *
  23631. * output output buffer to place SignedData
  23632. * outputSz size of output buffer
  23633. * data data buffer to be signed
  23634. * dataSz size of data buffer
  23635. * withAttribs [1/0] include attributes in SignedData message
  23636. * detachedSig [1/0] create detached signature, no content
  23637. * useIntCertChain [1/0] use certificate chain and include intermediate and
  23638. * root CAs in bundle
  23639. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  23640. *
  23641. * Return size of bundle created on success, negative on error */
  23642. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23643. byte* data, word32 dataSz,
  23644. int withAttribs, int detachedSig,
  23645. int useIntermediateCertChain,
  23646. int pkAlgoType)
  23647. {
  23648. EXPECT_DECLS;
  23649. int ret = 0;
  23650. WC_RNG rng;
  23651. PKCS7* pkcs7 = NULL;
  23652. static byte messageTypeOid[] =
  23653. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23654. 0x09, 0x02 };
  23655. static byte messageType[] = { 0x13, 2, '1', '9' };
  23656. PKCS7Attrib attribs[] =
  23657. {
  23658. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23659. sizeof(messageType) }
  23660. };
  23661. byte intCARoot[TWOK_BUF];
  23662. byte intCA1[TWOK_BUF];
  23663. byte intCA2[TWOK_BUF];
  23664. byte cert[TWOK_BUF];
  23665. byte key[TWOK_BUF];
  23666. word32 intCARootSz = sizeof(intCARoot);
  23667. word32 intCA1Sz = sizeof(intCA1);
  23668. word32 intCA2Sz = sizeof(intCA2);
  23669. word32 certSz = sizeof(cert);
  23670. word32 keySz = sizeof(key);
  23671. XMEMSET(intCARoot, 0, intCARootSz);
  23672. XMEMSET(intCA1, 0, intCA1Sz);
  23673. XMEMSET(intCA2, 0, intCA2Sz);
  23674. XMEMSET(cert, 0, certSz);
  23675. XMEMSET(key, 0, keySz);
  23676. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  23677. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  23678. cert, &certSz, key, &keySz);
  23679. ExpectIntEQ(ret, 0);
  23680. XMEMSET(output, 0, outputSz);
  23681. ExpectIntEQ(wc_InitRng(&rng), 0);
  23682. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23683. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23684. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23685. if (useIntermediateCertChain == 1) {
  23686. /* Add intermediate and root CA certs into SignedData Certs SET */
  23687. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  23688. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  23689. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  23690. }
  23691. if (pkcs7 != NULL) {
  23692. pkcs7->content = data;
  23693. pkcs7->contentSz = dataSz;
  23694. pkcs7->privateKey = key;
  23695. pkcs7->privateKeySz = (word32)sizeof(key);
  23696. if (pkAlgoType == RSA_TYPE) {
  23697. pkcs7->encryptOID = RSAk;
  23698. }
  23699. else {
  23700. pkcs7->encryptOID = ECDSAk;
  23701. }
  23702. #ifdef NO_SHA
  23703. pkcs7->hashOID = SHA256h;
  23704. #else
  23705. pkcs7->hashOID = SHAh;
  23706. #endif
  23707. pkcs7->rng = &rng;
  23708. if (withAttribs) {
  23709. /* include a signed attribute */
  23710. pkcs7->signedAttribs = attribs;
  23711. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23712. }
  23713. }
  23714. if (detachedSig) {
  23715. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23716. }
  23717. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  23718. ExpectIntGT(outputSz, 0);
  23719. wc_PKCS7_Free(pkcs7);
  23720. pkcs7 = NULL;
  23721. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23722. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23723. if (detachedSig && (pkcs7 != NULL)) {
  23724. pkcs7->content = data;
  23725. pkcs7->contentSz = dataSz;
  23726. }
  23727. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23728. wc_PKCS7_Free(pkcs7);
  23729. wc_FreeRng(&rng);
  23730. if (EXPECT_FAIL()) {
  23731. outputSz = 0;
  23732. }
  23733. return outputSz;
  23734. }
  23735. #endif
  23736. /*
  23737. * Testing wc_PKCS_VerifySignedData()
  23738. */
  23739. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  23740. {
  23741. EXPECT_DECLS;
  23742. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  23743. PKCS7* pkcs7 = NULL;
  23744. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23745. word32 outputSz = sizeof(output);
  23746. byte data[] = "Test data to encode.";
  23747. byte badOut[1];
  23748. word32 badOutSz = 0;
  23749. byte badContent[] = "This is different content than was signed";
  23750. wc_HashAlg hash;
  23751. #ifdef NO_SHA
  23752. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23753. #else
  23754. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23755. #endif
  23756. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23757. word32 hashSz = wc_HashGetDigestSize(hashType);
  23758. #ifndef NO_RSA
  23759. PKCS7DecodedAttrib* decodedAttrib = NULL;
  23760. /* contentType OID (1.2.840.113549.1.9.3) */
  23761. static const byte contentTypeOid[] =
  23762. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  23763. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  23764. static const byte dataType[] =
  23765. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  23766. /* messageDigest OID (1.2.840.113549.1.9.4) */
  23767. static const byte messageDigestOid[] =
  23768. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  23769. #ifndef NO_ASN_TIME
  23770. /* signingTime OID () */
  23771. static const byte signingTimeOid[] =
  23772. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  23773. #endif
  23774. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23775. int dateLength = 0;
  23776. byte dateFormat;
  23777. const byte* datePart = NULL;
  23778. struct tm timearg;
  23779. time_t now;
  23780. struct tm* nowTm = NULL;
  23781. #ifdef NEED_TMP_TIME
  23782. struct tm tmpTimeStorage;
  23783. struct tm* tmpTime = &tmpTimeStorage;
  23784. #endif
  23785. #endif /* !NO_ASN && !NO_ASN_TIME */
  23786. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23787. /* Success test with RSA certs/key */
  23788. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23789. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  23790. /* calculate hash for content, used later */
  23791. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23792. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23793. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23794. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23795. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23796. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23797. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23798. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23799. /* Check that decoded signed attributes are correct */
  23800. /* messageDigest should be first */
  23801. if (pkcs7 != NULL) {
  23802. decodedAttrib = pkcs7->decodedAttrib;
  23803. }
  23804. ExpectNotNull(decodedAttrib);
  23805. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  23806. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  23807. decodedAttrib->oidSz), 0);
  23808. /* + 2 for OCTET STRING and length bytes */
  23809. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  23810. ExpectNotNull(decodedAttrib->value);
  23811. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  23812. #ifndef NO_ASN_TIME
  23813. /* signingTime should be second */
  23814. if (decodedAttrib != NULL) {
  23815. decodedAttrib = decodedAttrib->next;
  23816. }
  23817. ExpectNotNull(decodedAttrib);
  23818. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  23819. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  23820. decodedAttrib->oidSz), 0);
  23821. ExpectIntGT(decodedAttrib->valueSz, 0);
  23822. ExpectNotNull(decodedAttrib->value);
  23823. #endif
  23824. /* Verify signingTime if ASN and time are available */
  23825. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23826. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  23827. &datePart, &dateFormat, &dateLength), 0);
  23828. ExpectNotNull(datePart);
  23829. ExpectIntGT(dateLength, 0);
  23830. XMEMSET(&timearg, 0, sizeof(timearg));
  23831. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  23832. &timearg), 0);
  23833. /* Get current time and compare year/month/day against attribute value */
  23834. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  23835. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  23836. ExpectNotNull(nowTm);
  23837. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  23838. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  23839. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  23840. #endif /* !NO_ASN && !NO_ASN_TIME */
  23841. /* contentType should be third */
  23842. if (decodedAttrib != NULL) {
  23843. decodedAttrib = decodedAttrib->next;
  23844. }
  23845. ExpectNotNull(decodedAttrib);
  23846. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  23847. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  23848. decodedAttrib->oidSz), 0);
  23849. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  23850. ExpectNotNull(decodedAttrib->value);
  23851. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  23852. 0);
  23853. #endif /* !NO_RSA */
  23854. /* Test bad args. */
  23855. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  23856. BAD_FUNC_ARG);
  23857. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  23858. BAD_FUNC_ARG);
  23859. #ifndef NO_PKCS7_STREAM
  23860. /* can pass in 0 buffer length with streaming API */
  23861. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23862. badOutSz), WC_PKCS7_WANT_READ_E);
  23863. #else
  23864. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23865. badOutSz), BAD_FUNC_ARG);
  23866. #endif
  23867. wc_PKCS7_Free(pkcs7);
  23868. pkcs7 = NULL;
  23869. #ifndef NO_RSA
  23870. /* Try RSA certs/key/sig first */
  23871. outputSz = sizeof(output);
  23872. XMEMSET(output, 0, outputSz);
  23873. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23874. (word32)sizeof(data),
  23875. 1, 1, 0, RSA_TYPE)), 0);
  23876. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23877. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23878. if (pkcs7 != NULL) {
  23879. pkcs7->content = badContent;
  23880. pkcs7->contentSz = sizeof(badContent);
  23881. }
  23882. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23883. SIG_VERIFY_E);
  23884. wc_PKCS7_Free(pkcs7);
  23885. pkcs7 = NULL;
  23886. /* Test success case with detached signature and valid content */
  23887. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23888. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23889. if (pkcs7 != NULL) {
  23890. pkcs7->content = data;
  23891. pkcs7->contentSz = sizeof(data);
  23892. }
  23893. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23894. wc_PKCS7_Free(pkcs7);
  23895. pkcs7 = NULL;
  23896. /* verify using pre-computed content digest only (no content) */
  23897. {
  23898. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23899. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23900. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23901. output, outputSz, NULL, 0), 0);
  23902. wc_PKCS7_Free(pkcs7);
  23903. pkcs7 = NULL;
  23904. }
  23905. #endif /* !NO_RSA */
  23906. /* Test verify on signedData containing intermediate/root CA certs */
  23907. #ifndef NO_RSA
  23908. outputSz = sizeof(output);
  23909. XMEMSET(output, 0, outputSz);
  23910. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23911. (word32)sizeof(data),
  23912. 0, 0, 1, RSA_TYPE)), 0);
  23913. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23914. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23915. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23916. wc_PKCS7_Free(pkcs7);
  23917. pkcs7 = NULL;
  23918. #endif /* !NO_RSA */
  23919. #endif
  23920. return EXPECT_RESULT();
  23921. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  23922. /*
  23923. * Testing wc_PKCS_VerifySignedData()
  23924. */
  23925. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  23926. {
  23927. EXPECT_DECLS;
  23928. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  23929. PKCS7* pkcs7 = NULL;
  23930. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23931. word32 outputSz = sizeof(output);
  23932. byte data[] = "Test data to encode.";
  23933. byte badContent[] = "This is different content than was signed";
  23934. wc_HashAlg hash;
  23935. #ifdef NO_SHA
  23936. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23937. #else
  23938. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23939. #endif
  23940. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23941. word32 hashSz = wc_HashGetDigestSize(hashType);
  23942. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23943. /* Success test with ECC certs/key */
  23944. outputSz = sizeof(output);
  23945. XMEMSET(output, 0, outputSz);
  23946. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23947. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  23948. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23949. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23950. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23951. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23952. wc_PKCS7_Free(pkcs7);
  23953. pkcs7 = NULL;
  23954. /* Invalid content should error, use detached signature so we can
  23955. * easily change content */
  23956. outputSz = sizeof(output);
  23957. XMEMSET(output, 0, outputSz);
  23958. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23959. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  23960. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23961. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23962. if (pkcs7 != NULL) {
  23963. pkcs7->content = badContent;
  23964. pkcs7->contentSz = sizeof(badContent);
  23965. }
  23966. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23967. SIG_VERIFY_E);
  23968. wc_PKCS7_Free(pkcs7);
  23969. pkcs7 = NULL;
  23970. /* Test success case with detached signature and valid content */
  23971. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23973. if (pkcs7 != NULL) {
  23974. pkcs7->content = data;
  23975. pkcs7->contentSz = sizeof(data);
  23976. }
  23977. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23978. wc_PKCS7_Free(pkcs7);
  23979. pkcs7 = NULL;
  23980. /* verify using pre-computed content digest only (no content) */
  23981. {
  23982. /* calculate hash for content */
  23983. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23984. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23985. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23986. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23987. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23988. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23989. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23990. output, outputSz, NULL, 0), 0);
  23991. wc_PKCS7_Free(pkcs7);
  23992. pkcs7 = NULL;
  23993. }
  23994. /* Test verify on signedData containing intermediate/root CA certs */
  23995. outputSz = sizeof(output);
  23996. XMEMSET(output, 0, outputSz);
  23997. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23998. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  23999. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24000. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24001. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24002. wc_PKCS7_Free(pkcs7);
  24003. pkcs7 = NULL;
  24004. #endif
  24005. return EXPECT_RESULT();
  24006. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  24007. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24008. !defined(NO_AES_256)
  24009. static const byte defKey[] = {
  24010. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24011. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24012. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24013. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24014. };
  24015. static byte aesHandle[32]; /* simulated hardware key handle */
  24016. /* return 0 on success */
  24017. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24018. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24019. byte* in, int inSz, byte* out, void* usrCtx)
  24020. {
  24021. int ret;
  24022. Aes aes;
  24023. if (usrCtx == NULL) {
  24024. /* no simulated handle passed in */
  24025. return -1;
  24026. }
  24027. switch (encryptOID) {
  24028. case AES256CBCb:
  24029. if (ivSz != AES_BLOCK_SIZE)
  24030. return BAD_FUNC_ARG;
  24031. break;
  24032. default:
  24033. WOLFSSL_MSG("Unsupported content cipher type for test");
  24034. return ALGO_ID_E;
  24035. };
  24036. /* simulate using handle to get key */
  24037. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24038. if (ret == 0) {
  24039. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24040. if (ret == 0)
  24041. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24042. wc_AesFree(&aes);
  24043. }
  24044. (void)aad;
  24045. (void)aadSz;
  24046. (void)authTag;
  24047. (void)authTagSz;
  24048. (void)pkcs7;
  24049. return ret;
  24050. }
  24051. /* returns key size on success */
  24052. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24053. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24054. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24055. {
  24056. int ret = -1;
  24057. if (out == NULL)
  24058. return BAD_FUNC_ARG;
  24059. if (keyId[0] != 0x00) {
  24060. return -1;
  24061. }
  24062. if (type != (int)PKCS7_KEKRI) {
  24063. return -1;
  24064. }
  24065. switch (keyWrapAlgo) {
  24066. case AES256_WRAP:
  24067. /* simulate setting a handle for later decryption but use key
  24068. * as handle in the test case here */
  24069. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24070. aesHandle, sizeof(aesHandle), NULL);
  24071. if (ret < 0)
  24072. return ret;
  24073. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24074. if (ret < 0)
  24075. return ret;
  24076. /* return key size on success */
  24077. return sizeof(defKey);
  24078. default:
  24079. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24080. return BAD_KEYWRAP_ALG_E;
  24081. };
  24082. (void)cekSz;
  24083. (void)cek;
  24084. (void)outSz;
  24085. (void)keyIdSz;
  24086. (void)direction;
  24087. (void)orginKey; /* used with KAKRI */
  24088. (void)orginKeySz;
  24089. return ret;
  24090. }
  24091. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24092. /*
  24093. * Testing wc_PKCS7_EncodeEnvelopedData()
  24094. */
  24095. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24096. {
  24097. EXPECT_DECLS;
  24098. #if defined(HAVE_PKCS7)
  24099. PKCS7* pkcs7 = NULL;
  24100. #ifdef ECC_TIMING_RESISTANT
  24101. WC_RNG rng;
  24102. #endif
  24103. word32 tempWrd32 = 0;
  24104. byte* tmpBytePtr = NULL;
  24105. const char input[] = "Test data to encode.";
  24106. int i;
  24107. int testSz = 0;
  24108. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24109. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24110. byte* rsaCert = NULL;
  24111. byte* rsaPrivKey = NULL;
  24112. word32 rsaCertSz;
  24113. word32 rsaPrivKeySz;
  24114. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24115. !defined(USE_CERT_BUFFERS_2048) )
  24116. static const char* rsaClientCert = "./certs/client-cert.der";
  24117. static const char* rsaClientKey = "./certs/client-key.der";
  24118. rsaCertSz = (word32)sizeof(rsaClientCert);
  24119. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24120. #endif
  24121. #endif
  24122. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24123. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24124. byte* eccCert = NULL;
  24125. byte* eccPrivKey = NULL;
  24126. word32 eccCertSz;
  24127. word32 eccPrivKeySz;
  24128. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24129. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24130. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24131. #endif
  24132. #endif
  24133. /* Generic buffer size. */
  24134. byte output[ONEK_BUF];
  24135. byte decoded[sizeof(input)/sizeof(char)];
  24136. int decodedSz = 0;
  24137. #ifndef NO_FILESYSTEM
  24138. XFILE certFile = XBADFILE;
  24139. XFILE keyFile = XBADFILE;
  24140. #endif
  24141. #ifdef ECC_TIMING_RESISTANT
  24142. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24143. #endif
  24144. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24145. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24146. /* RSA certs and keys. */
  24147. #if defined(USE_CERT_BUFFERS_1024)
  24148. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24149. /* Allocate buffer space. */
  24150. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24151. DYNAMIC_TYPE_TMP_BUFFER));
  24152. /* Init buffer. */
  24153. if (rsaCert != NULL) {
  24154. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24155. }
  24156. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24157. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24158. DYNAMIC_TYPE_TMP_BUFFER));
  24159. if (rsaPrivKey != NULL) {
  24160. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24161. }
  24162. #elif defined(USE_CERT_BUFFERS_2048)
  24163. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24164. /* Allocate buffer */
  24165. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24166. DYNAMIC_TYPE_TMP_BUFFER));
  24167. /* Init buffer. */
  24168. if (rsaCert != NULL) {
  24169. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24170. }
  24171. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24172. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24173. DYNAMIC_TYPE_TMP_BUFFER));
  24174. if (rsaPrivKey != NULL) {
  24175. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24176. }
  24177. #else
  24178. /* File system. */
  24179. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24180. rsaCertSz = (word32)FOURK_BUF;
  24181. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24182. DYNAMIC_TYPE_TMP_BUFFER));
  24183. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24184. certFile)) > 0);
  24185. if (certFile != XBADFILE)
  24186. XFCLOSE(certFile);
  24187. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  24188. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24189. DYNAMIC_TYPE_TMP_BUFFER));
  24190. rsaPrivKeySz = (word32)FOURK_BUF;
  24191. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  24192. keyFile)) > 0);
  24193. if (keyFile != XBADFILE)
  24194. XFCLOSE(keyFile);
  24195. #endif /* USE_CERT_BUFFERS */
  24196. #endif /* NO_RSA */
  24197. /* ECC */
  24198. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24199. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24200. #ifdef USE_CERT_BUFFERS_256
  24201. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24202. DYNAMIC_TYPE_TMP_BUFFER));
  24203. /* Init buffer. */
  24204. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24205. if (eccCert != NULL) {
  24206. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24207. }
  24208. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24209. DYNAMIC_TYPE_TMP_BUFFER));
  24210. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24211. if (eccPrivKey != NULL) {
  24212. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24213. }
  24214. #else /* File system. */
  24215. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  24216. eccCertSz = (word32)FOURK_BUF;
  24217. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24218. DYNAMIC_TYPE_TMP_BUFFER));
  24219. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  24220. certFile)) > 0);
  24221. if (certFile != XBADFILE) {
  24222. XFCLOSE(certFile);
  24223. }
  24224. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  24225. eccPrivKeySz = (word32)FOURK_BUF;
  24226. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24227. DYNAMIC_TYPE_TMP_BUFFER));
  24228. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  24229. keyFile)) > 0);
  24230. if (keyFile != XBADFILE) {
  24231. XFCLOSE(keyFile);
  24232. }
  24233. #endif /* USE_CERT_BUFFERS_256 */
  24234. #endif /* END HAVE_ECC */
  24235. #ifndef NO_FILESYSTEM
  24236. /* Silence. */
  24237. (void)keyFile;
  24238. (void)certFile;
  24239. #endif
  24240. {
  24241. const pkcs7EnvelopedVector testVectors[] = {
  24242. /* DATA is a global variable defined in the makefile. */
  24243. #if !defined(NO_RSA)
  24244. #ifndef NO_DES3
  24245. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24246. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24247. #endif /* NO_DES3 */
  24248. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24249. #ifndef NO_AES_128
  24250. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24251. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24252. #endif
  24253. #ifndef NO_AES_192
  24254. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24255. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24256. #endif
  24257. #ifndef NO_AES_256
  24258. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24259. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24260. #endif
  24261. #endif /* NO_AES && HAVE_AES_CBC */
  24262. #endif /* NO_RSA */
  24263. #if defined(HAVE_ECC)
  24264. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24265. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24266. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24267. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  24268. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24269. #endif
  24270. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24271. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24272. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  24273. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24274. #endif
  24275. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24276. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24277. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  24278. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24279. #endif
  24280. #endif /* NO_AES && HAVE_AES_CBC*/
  24281. #endif /* END HAVE_ECC */
  24282. }; /* END pkcs7EnvelopedVector */
  24283. #ifdef ECC_TIMING_RESISTANT
  24284. ExpectIntEQ(wc_InitRng(&rng), 0);
  24285. #endif
  24286. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24287. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24288. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24289. for (i = 0; i < testSz; i++) {
  24290. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24291. (word32)(testVectors + i)->certSz), 0);
  24292. if (pkcs7 != NULL) {
  24293. #ifdef ECC_TIMING_RESISTANT
  24294. pkcs7->rng = &rng;
  24295. #endif
  24296. pkcs7->content = (byte*)(testVectors + i)->content;
  24297. pkcs7->contentSz = (testVectors + i)->contentSz;
  24298. pkcs7->contentOID = (testVectors + i)->contentOID;
  24299. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24300. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24301. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24302. pkcs7->privateKey = (testVectors + i)->privateKey;
  24303. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24304. }
  24305. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24306. (word32)sizeof(output)), 0);
  24307. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24308. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24309. ExpectIntGE(decodedSz, 0);
  24310. /* Verify the size of each buffer. */
  24311. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24312. /* Don't free the last time through the loop. */
  24313. if (i < testSz - 1) {
  24314. wc_PKCS7_Free(pkcs7);
  24315. pkcs7 = NULL;
  24316. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24317. }
  24318. } /* END test loop. */
  24319. }
  24320. /* Test bad args. */
  24321. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24322. (word32)sizeof(output)), BAD_FUNC_ARG);
  24323. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24324. (word32)sizeof(output)), BAD_FUNC_ARG);
  24325. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24326. /* Decode. */
  24327. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24328. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24329. BAD_FUNC_ARG);
  24330. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24331. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24332. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24333. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24334. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24335. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24336. BAD_FUNC_ARG);
  24337. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24338. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24339. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24340. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24341. /* only a failure for KARI test cases */
  24342. if (pkcs7 != NULL) {
  24343. tempWrd32 = pkcs7->singleCertSz;
  24344. pkcs7->singleCertSz = 0;
  24345. }
  24346. #if defined(WOLFSSL_ASN_TEMPLATE)
  24347. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24348. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24349. BUFFER_E);
  24350. #else
  24351. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24352. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24353. ASN_PARSE_E);
  24354. #endif
  24355. if (pkcs7 != NULL) {
  24356. pkcs7->singleCertSz = tempWrd32;
  24357. tmpBytePtr = pkcs7->singleCert;
  24358. pkcs7->singleCert = NULL;
  24359. }
  24360. #ifndef NO_RSA
  24361. #if defined(NO_PKCS7_STREAM)
  24362. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  24363. * is returned from kari parse which gets set to bad func arg */
  24364. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24365. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24366. BAD_FUNC_ARG);
  24367. #else
  24368. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24369. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24370. ASN_PARSE_E);
  24371. #endif
  24372. #endif /* !NO_RSA */
  24373. if (pkcs7 != NULL) {
  24374. pkcs7->singleCert = tmpBytePtr;
  24375. }
  24376. #endif
  24377. if (pkcs7 != NULL) {
  24378. tempWrd32 = pkcs7->privateKeySz;
  24379. pkcs7->privateKeySz = 0;
  24380. }
  24381. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24382. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24383. BAD_FUNC_ARG);
  24384. if (pkcs7 != NULL) {
  24385. pkcs7->privateKeySz = tempWrd32;
  24386. tmpBytePtr = pkcs7->privateKey;
  24387. pkcs7->privateKey = NULL;
  24388. }
  24389. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24390. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24391. BAD_FUNC_ARG);
  24392. if (pkcs7 != NULL) {
  24393. pkcs7->privateKey = tmpBytePtr;
  24394. }
  24395. wc_PKCS7_Free(pkcs7);
  24396. pkcs7 = NULL;
  24397. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24398. /* test of decrypt callback with KEKRI enveloped data */
  24399. {
  24400. int envelopedSz = 0;
  24401. const byte keyId[] = { 0x00 };
  24402. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24403. if (pkcs7 != NULL) {
  24404. pkcs7->content = (byte*)input;
  24405. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24406. pkcs7->contentOID = DATA;
  24407. pkcs7->encryptOID = AES256CBCb;
  24408. }
  24409. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24410. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24411. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24412. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24413. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24414. (word32)sizeof(output))), 0);
  24415. wc_PKCS7_Free(pkcs7);
  24416. pkcs7 = NULL;
  24417. /* decode envelopedData */
  24418. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24419. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24420. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24421. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24422. envelopedSz, decoded, sizeof(decoded))), 0);
  24423. wc_PKCS7_Free(pkcs7);
  24424. pkcs7 = NULL;
  24425. }
  24426. #endif /* !NO_AES && !NO_AES_256 */
  24427. #ifndef NO_RSA
  24428. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24429. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24430. #endif /* NO_RSA */
  24431. #ifdef HAVE_ECC
  24432. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24433. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24434. #endif /* HAVE_ECC */
  24435. #ifdef ECC_TIMING_RESISTANT
  24436. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24437. #endif
  24438. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  24439. !defined(NO_RSA) && !defined(NO_SHA)
  24440. {
  24441. byte out[7];
  24442. byte *cms = NULL;
  24443. word32 cmsSz;
  24444. XFILE cmsFile = XBADFILE;
  24445. XMEMSET(out, 0, sizeof(out));
  24446. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24447. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  24448. != XBADFILE);
  24449. cmsSz = (word32)FOURK_BUF;
  24450. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24451. DYNAMIC_TYPE_TMP_BUFFER));
  24452. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  24453. if (cmsFile != XBADFILE)
  24454. XFCLOSE(cmsFile);
  24455. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24456. sizeof_client_cert_der_2048), 0);
  24457. if (pkcs7 != NULL) {
  24458. pkcs7->privateKey = (byte*)client_key_der_2048;
  24459. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24460. }
  24461. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24462. 2), 0);
  24463. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24464. sizeof(out)), 0);
  24465. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24466. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  24467. wc_PKCS7_Free(pkcs7);
  24468. pkcs7 = NULL;
  24469. }
  24470. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  24471. #endif /* HAVE_PKCS7 */
  24472. return EXPECT_RESULT();
  24473. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  24474. /*
  24475. * Testing wc_PKCS7_EncodeEncryptedData()
  24476. */
  24477. static int test_wc_PKCS7_EncodeEncryptedData(void)
  24478. {
  24479. EXPECT_DECLS;
  24480. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24481. PKCS7* pkcs7 = NULL;
  24482. byte* tmpBytePtr = NULL;
  24483. byte encrypted[TWOK_BUF];
  24484. byte decoded[TWOK_BUF];
  24485. word32 tmpWrd32 = 0;
  24486. int tmpInt = 0;
  24487. int decodedSz;
  24488. int encryptedSz = 0;
  24489. int testSz;
  24490. int i;
  24491. const byte data[] = { /* Hello World */
  24492. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24493. 0x72,0x6c,0x64
  24494. };
  24495. #ifndef NO_DES3
  24496. byte desKey[] = {
  24497. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24498. };
  24499. byte des3Key[] = {
  24500. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24501. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24502. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24503. };
  24504. #endif
  24505. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24506. #ifndef NO_AES_128
  24507. byte aes128Key[] = {
  24508. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24509. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24510. };
  24511. #endif
  24512. #ifndef NO_AES_192
  24513. byte aes192Key[] = {
  24514. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24515. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24516. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24517. };
  24518. #endif
  24519. #ifndef NO_AES_256
  24520. byte aes256Key[] = {
  24521. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24522. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24523. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24524. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24525. };
  24526. #endif
  24527. #endif /* !NO_AES && HAVE_AES_CBC */
  24528. const pkcs7EncryptedVector testVectors[] =
  24529. {
  24530. #ifndef NO_DES3
  24531. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24532. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24533. #endif /* !NO_DES3 */
  24534. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24535. #ifndef NO_AES_128
  24536. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24537. sizeof(aes128Key)},
  24538. #endif
  24539. #ifndef NO_AES_192
  24540. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24541. sizeof(aes192Key)},
  24542. #endif
  24543. #ifndef NO_AES_256
  24544. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24545. sizeof(aes256Key)},
  24546. #endif
  24547. #endif /* !NO_AES && HAVE_AES_CBC */
  24548. };
  24549. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24550. for (i = 0; i < testSz; i++) {
  24551. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24552. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24553. if (pkcs7 != NULL) {
  24554. pkcs7->content = (byte*)testVectors[i].content;
  24555. pkcs7->contentSz = testVectors[i].contentSz;
  24556. pkcs7->contentOID = testVectors[i].contentOID;
  24557. pkcs7->encryptOID = testVectors[i].encryptOID;
  24558. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24559. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24560. pkcs7->heap = HEAP_HINT;
  24561. }
  24562. /* encode encryptedData */
  24563. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24564. sizeof(encrypted)), 0);
  24565. /* Decode encryptedData */
  24566. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  24567. encryptedSz, decoded, sizeof(decoded)), 0);
  24568. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24569. /* Keep values for last itr. */
  24570. if (i < testSz - 1) {
  24571. wc_PKCS7_Free(pkcs7);
  24572. pkcs7 = NULL;
  24573. }
  24574. }
  24575. if (pkcs7 == NULL || testSz == 0) {
  24576. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24577. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24578. }
  24579. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24580. sizeof(encrypted)),BAD_FUNC_ARG);
  24581. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24582. sizeof(encrypted)), BAD_FUNC_ARG);
  24583. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24584. 0), BAD_FUNC_ARG);
  24585. /* Testing the struct. */
  24586. if (pkcs7 != NULL) {
  24587. tmpBytePtr = pkcs7->content;
  24588. pkcs7->content = NULL;
  24589. }
  24590. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24591. sizeof(encrypted)), BAD_FUNC_ARG);
  24592. if (pkcs7 != NULL) {
  24593. pkcs7->content = tmpBytePtr;
  24594. tmpWrd32 = pkcs7->contentSz;
  24595. pkcs7->contentSz = 0;
  24596. }
  24597. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24598. sizeof(encrypted)), BAD_FUNC_ARG);
  24599. if (pkcs7 != NULL) {
  24600. pkcs7->contentSz = tmpWrd32;
  24601. tmpInt = pkcs7->encryptOID;
  24602. pkcs7->encryptOID = 0;
  24603. }
  24604. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24605. sizeof(encrypted)), BAD_FUNC_ARG);
  24606. if (pkcs7 != NULL) {
  24607. pkcs7->encryptOID = tmpInt;
  24608. tmpBytePtr = pkcs7->encryptionKey;
  24609. pkcs7->encryptionKey = NULL;
  24610. }
  24611. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24612. sizeof(encrypted)), BAD_FUNC_ARG);
  24613. if (pkcs7 != NULL) {
  24614. pkcs7->encryptionKey = tmpBytePtr;
  24615. tmpWrd32 = pkcs7->encryptionKeySz;
  24616. pkcs7->encryptionKeySz = 0;
  24617. }
  24618. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24619. sizeof(encrypted)), BAD_FUNC_ARG);
  24620. if (pkcs7 != NULL) {
  24621. pkcs7->encryptionKeySz = tmpWrd32;
  24622. }
  24623. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24624. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24625. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24626. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24627. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24628. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24629. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24630. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24631. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24632. decoded, 0), BAD_FUNC_ARG);
  24633. /* Test struct fields */
  24634. if (pkcs7 != NULL) {
  24635. tmpBytePtr = pkcs7->encryptionKey;
  24636. pkcs7->encryptionKey = NULL;
  24637. }
  24638. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24639. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24640. if (pkcs7 != NULL) {
  24641. pkcs7->encryptionKey = tmpBytePtr;
  24642. pkcs7->encryptionKeySz = 0;
  24643. }
  24644. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24645. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24646. wc_PKCS7_Free(pkcs7);
  24647. #endif
  24648. return EXPECT_RESULT();
  24649. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24650. /*
  24651. * Testing wc_PKCS7_Degenerate()
  24652. */
  24653. static int test_wc_PKCS7_Degenerate(void)
  24654. {
  24655. EXPECT_DECLS;
  24656. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24657. PKCS7* pkcs7 = NULL;
  24658. char fName[] = "./certs/test-degenerate.p7b";
  24659. XFILE f = XBADFILE;
  24660. byte der[4096];
  24661. word32 derSz = 0;
  24662. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24663. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24664. if (f != XBADFILE)
  24665. XFCLOSE(f);
  24666. /* test degenerate success */
  24667. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24668. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24669. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24670. #ifndef NO_RSA
  24671. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24672. #else
  24673. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24674. #endif
  24675. wc_PKCS7_Free(pkcs7);
  24676. pkcs7 = NULL;
  24677. /* test with turning off degenerate cases */
  24678. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24679. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24680. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24681. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24682. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  24683. PKCS7_NO_SIGNER_E);
  24684. wc_PKCS7_Free(pkcs7);
  24685. #endif
  24686. return EXPECT_RESULT();
  24687. } /* END test_wc_PKCS7_Degenerate() */
  24688. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24689. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  24690. static byte berContent[] = {
  24691. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24692. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24693. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24694. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24695. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24696. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24697. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24698. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24699. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24700. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24701. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24702. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24703. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24704. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24705. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24706. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24707. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24708. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24709. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24710. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24711. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24712. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24713. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24714. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24715. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24716. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24717. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24718. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24719. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24720. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24721. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24722. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24723. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24724. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24725. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24726. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24727. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24728. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24729. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24730. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24731. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24732. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24733. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24734. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24735. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24736. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24737. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24738. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24739. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24740. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24741. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24742. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24743. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24744. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24745. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24746. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24747. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24748. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24749. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24750. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24751. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24752. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24753. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24754. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24755. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24756. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24757. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24758. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24759. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24760. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24761. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24762. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24763. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24764. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24765. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24766. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24767. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24768. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24769. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24770. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24771. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24772. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24773. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24774. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24775. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24776. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24777. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24778. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24779. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24780. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24781. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24782. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24783. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24784. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24785. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24786. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24787. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24788. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24789. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24790. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24791. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24792. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24793. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24794. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24795. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24796. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24797. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24798. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24799. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24800. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24801. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24802. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24803. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24804. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24805. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24806. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24807. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24808. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24809. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24810. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24811. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24812. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24813. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24814. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24815. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24816. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24817. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24818. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24819. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24820. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24821. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24822. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  24823. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  24824. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  24825. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  24826. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  24827. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  24828. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  24829. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  24830. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  24831. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  24832. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  24833. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  24834. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  24835. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  24836. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  24837. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  24838. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  24839. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  24840. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  24841. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  24842. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  24843. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  24844. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  24845. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  24846. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  24847. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  24848. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  24849. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  24850. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  24851. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  24852. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  24853. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  24854. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  24855. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  24856. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  24857. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  24858. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  24859. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  24860. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  24861. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  24862. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  24863. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  24864. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  24865. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  24866. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  24867. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  24868. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  24869. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  24870. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  24871. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  24872. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  24873. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  24874. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  24875. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  24876. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  24877. 0x00, 0x00, 0x00, 0x00, 0x00
  24878. };
  24879. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  24880. * !NO_DES3 && !NO_SHA
  24881. */
  24882. /*
  24883. * Testing wc_PKCS7_BER()
  24884. */
  24885. static int test_wc_PKCS7_BER(void)
  24886. {
  24887. EXPECT_DECLS;
  24888. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24889. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  24890. PKCS7* pkcs7 = NULL;
  24891. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  24892. XFILE f = XBADFILE;
  24893. byte der[4096];
  24894. #ifndef NO_DES3
  24895. byte decoded[2048];
  24896. #endif
  24897. word32 derSz = 0;
  24898. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24899. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24900. if (f != XBADFILE) {
  24901. XFCLOSE(f);
  24902. f = XBADFILE;
  24903. }
  24904. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24905. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24906. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24907. #ifndef NO_RSA
  24908. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24909. #else
  24910. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24911. #endif
  24912. wc_PKCS7_Free(pkcs7);
  24913. pkcs7 = NULL;
  24914. #ifndef NO_DES3
  24915. /* decode BER content */
  24916. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  24917. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24918. if (f != XBADFILE) {
  24919. XFCLOSE(f);
  24920. f = XBADFILE;
  24921. }
  24922. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24923. #ifndef NO_RSA
  24924. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24925. #else
  24926. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24927. #endif
  24928. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  24929. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24930. if (f != XBADFILE) {
  24931. XFCLOSE(f);
  24932. f = XBADFILE;
  24933. }
  24934. if (pkcs7 != NULL) {
  24935. pkcs7->privateKey = der;
  24936. pkcs7->privateKeySz = derSz;
  24937. }
  24938. #ifndef NO_RSA
  24939. #ifdef WOLFSSL_SP_MATH
  24940. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24941. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  24942. #else
  24943. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24944. sizeof(berContent), decoded, sizeof(decoded)), 0);
  24945. #endif
  24946. #else
  24947. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24948. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  24949. #endif
  24950. wc_PKCS7_Free(pkcs7);
  24951. #endif /* !NO_DES3 */
  24952. #endif
  24953. return EXPECT_RESULT();
  24954. } /* END test_wc_PKCS7_BER() */
  24955. static int test_wc_PKCS7_signed_enveloped(void)
  24956. {
  24957. EXPECT_DECLS;
  24958. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  24959. !defined(NO_FILESYSTEM)
  24960. XFILE f = XBADFILE;
  24961. PKCS7* pkcs7 = NULL;
  24962. #ifdef HAVE_AES_CBC
  24963. PKCS7* inner = NULL;
  24964. #endif
  24965. void* pt = NULL;
  24966. WC_RNG rng;
  24967. unsigned char key[FOURK_BUF/2];
  24968. unsigned char cert[FOURK_BUF/2];
  24969. unsigned char env[FOURK_BUF];
  24970. int envSz = FOURK_BUF;
  24971. int keySz = 0;
  24972. int certSz = 0;
  24973. unsigned char sig[FOURK_BUF * 2];
  24974. int sigSz = FOURK_BUF * 2;
  24975. #ifdef HAVE_AES_CBC
  24976. unsigned char decoded[FOURK_BUF];
  24977. int decodedSz = FOURK_BUF;
  24978. #endif
  24979. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24980. /* load cert */
  24981. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  24982. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  24983. if (f != XBADFILE) {
  24984. XFCLOSE(f);
  24985. f = XBADFILE;
  24986. }
  24987. /* load key */
  24988. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  24989. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  24990. if (f != XBADFILE) {
  24991. XFCLOSE(f);
  24992. f = XBADFILE;
  24993. }
  24994. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  24995. /* sign cert for envelope */
  24996. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24997. ExpectIntEQ(wc_InitRng(&rng), 0);
  24998. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24999. if (pkcs7 != NULL) {
  25000. pkcs7->content = cert;
  25001. pkcs7->contentSz = certSz;
  25002. pkcs7->contentOID = DATA;
  25003. pkcs7->privateKey = key;
  25004. pkcs7->privateKeySz = keySz;
  25005. pkcs7->encryptOID = RSAk;
  25006. pkcs7->hashOID = SHA256h;
  25007. pkcs7->rng = &rng;
  25008. }
  25009. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25010. wc_PKCS7_Free(pkcs7);
  25011. pkcs7 = NULL;
  25012. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25013. #ifdef HAVE_AES_CBC
  25014. /* create envelope */
  25015. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25016. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25017. if (pkcs7 != NULL) {
  25018. pkcs7->content = sig;
  25019. pkcs7->contentSz = sigSz;
  25020. pkcs7->contentOID = DATA;
  25021. pkcs7->encryptOID = AES256CBCb;
  25022. pkcs7->privateKey = key;
  25023. pkcs7->privateKeySz = keySz;
  25024. }
  25025. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25026. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25027. wc_PKCS7_Free(pkcs7);
  25028. pkcs7 = NULL;
  25029. #endif
  25030. /* create bad signed enveloped data */
  25031. sigSz = FOURK_BUF * 2;
  25032. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25033. ExpectIntEQ(wc_InitRng(&rng), 0);
  25034. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25035. if (pkcs7 != NULL) {
  25036. pkcs7->content = env;
  25037. pkcs7->contentSz = envSz;
  25038. pkcs7->contentOID = DATA;
  25039. pkcs7->privateKey = key;
  25040. pkcs7->privateKeySz = keySz;
  25041. pkcs7->encryptOID = RSAk;
  25042. pkcs7->hashOID = SHA256h;
  25043. pkcs7->rng = &rng;
  25044. }
  25045. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25046. * free it later. */
  25047. if (pkcs7 != NULL) {
  25048. pt = (void*)pkcs7->certList;
  25049. pkcs7->certList = NULL; /* no certs in bundle */
  25050. }
  25051. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25052. if (pkcs7 != NULL) {
  25053. /* restore pointer for PKCS7 free call */
  25054. pkcs7->certList = (Pkcs7Cert*)pt;
  25055. }
  25056. wc_PKCS7_Free(pkcs7);
  25057. pkcs7 = NULL;
  25058. /* check verify fails */
  25059. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25060. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25061. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25062. PKCS7_SIGNEEDS_CHECK);
  25063. /* try verifying the signature manually */
  25064. {
  25065. RsaKey rKey;
  25066. word32 idx = 0;
  25067. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25068. WC_MAX_DIGEST_SIZE];
  25069. int digestSz;
  25070. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25071. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25072. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  25073. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  25074. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25075. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25076. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  25077. /* verify was success */
  25078. }
  25079. wc_PKCS7_Free(pkcs7);
  25080. pkcs7 = NULL;
  25081. /* initializing the PKCS7 struct with the signing certificate should pass */
  25082. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25083. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25084. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25085. wc_PKCS7_Free(pkcs7);
  25086. pkcs7 = NULL;
  25087. /* create valid degenerate bundle */
  25088. sigSz = FOURK_BUF * 2;
  25089. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25090. if (pkcs7 != NULL) {
  25091. pkcs7->content = env;
  25092. pkcs7->contentSz = envSz;
  25093. pkcs7->contentOID = DATA;
  25094. pkcs7->privateKey = key;
  25095. pkcs7->privateKeySz = keySz;
  25096. pkcs7->encryptOID = RSAk;
  25097. pkcs7->hashOID = SHA256h;
  25098. pkcs7->rng = &rng;
  25099. }
  25100. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25101. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25102. wc_PKCS7_Free(pkcs7);
  25103. pkcs7 = NULL;
  25104. wc_FreeRng(&rng);
  25105. /* check verify */
  25106. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25107. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25108. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25109. ExpectNotNull(pkcs7->content);
  25110. #ifdef HAVE_AES_CBC
  25111. /* check decode */
  25112. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  25113. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25114. if (inner != NULL) {
  25115. inner->privateKey = key;
  25116. inner->privateKeySz = keySz;
  25117. }
  25118. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25119. pkcs7->contentSz, decoded, decodedSz)), 0);
  25120. wc_PKCS7_Free(inner);
  25121. inner = NULL;
  25122. #endif
  25123. wc_PKCS7_Free(pkcs7);
  25124. pkcs7 = NULL;
  25125. #ifdef HAVE_AES_CBC
  25126. /* check cert set */
  25127. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25128. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25129. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25130. ExpectNotNull(pkcs7->singleCert);
  25131. ExpectIntNE(pkcs7->singleCertSz, 0);
  25132. wc_PKCS7_Free(pkcs7);
  25133. pkcs7 = NULL;
  25134. #endif
  25135. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25136. return EXPECT_RESULT();
  25137. }
  25138. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  25139. {
  25140. EXPECT_DECLS;
  25141. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25142. && !defined(NO_AES)
  25143. PKCS7* pkcs7 = NULL;
  25144. void* heap = NULL;
  25145. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25146. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25147. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25148. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25149. wc_PKCS7_Free(pkcs7);
  25150. #endif
  25151. return EXPECT_RESULT();
  25152. }
  25153. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  25154. {
  25155. EXPECT_DECLS;
  25156. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25157. && !defined(NO_AES)
  25158. PKCS7* pkcs7 = NULL;
  25159. void* heap = NULL;
  25160. WOLFSSL_CTX* ctx = NULL;
  25161. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25162. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25163. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25164. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25165. wc_PKCS7_Free(pkcs7);
  25166. #endif
  25167. return EXPECT_RESULT();
  25168. }
  25169. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  25170. {
  25171. EXPECT_DECLS;
  25172. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25173. && !defined(NO_AES)
  25174. PKCS7* pkcs7 = NULL;
  25175. void* heap = NULL;
  25176. WOLFSSL_CTX* ctx = NULL;
  25177. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25178. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25179. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25180. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25181. wc_PKCS7_Free(pkcs7);
  25182. #endif
  25183. return EXPECT_RESULT();
  25184. }
  25185. static int test_wc_PKCS7_DecodeCompressedData(void)
  25186. {
  25187. EXPECT_DECLS;
  25188. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25189. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25190. PKCS7* pkcs7 = NULL;
  25191. void* heap = NULL;
  25192. byte out[4096];
  25193. byte* decompressed = NULL;
  25194. int outSz;
  25195. int decompressedSz;
  25196. const char* cert = "./certs/client-cert.pem";
  25197. byte* cert_buf = NULL;
  25198. size_t cert_sz = 0;
  25199. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25200. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  25201. DYNAMIC_TYPE_TMP_BUFFER)));
  25202. decompressedSz = (int)cert_sz;
  25203. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25204. if (pkcs7 != NULL) {
  25205. pkcs7->content = (byte*)cert_buf;
  25206. pkcs7->contentSz = (word32)cert_sz;
  25207. pkcs7->contentOID = DATA;
  25208. }
  25209. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25210. sizeof(out))), 0);
  25211. wc_PKCS7_Free(pkcs7);
  25212. pkcs7 = NULL;
  25213. /* compressed key should be smaller than when started */
  25214. ExpectIntLT(outSz, cert_sz);
  25215. /* test decompression */
  25216. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25217. ExpectIntEQ(pkcs7->contentOID, 0);
  25218. /* fail case with out buffer too small */
  25219. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25220. decompressed, outSz), 0);
  25221. /* success case */
  25222. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25223. decompressed, decompressedSz), cert_sz);
  25224. ExpectIntEQ(pkcs7->contentOID, DATA);
  25225. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25226. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25227. decompressed = NULL;
  25228. /* test decompression function with different 'max' inputs */
  25229. outSz = sizeof(out);
  25230. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25231. 0);
  25232. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25233. out, outSz, 0, heap), 0);
  25234. ExpectNull(decompressed);
  25235. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25236. out, outSz, 0, heap), 0);
  25237. ExpectNotNull(decompressed);
  25238. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25239. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25240. decompressed = NULL;
  25241. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25242. out, outSz, 0, heap), 0);
  25243. ExpectNotNull(decompressed);
  25244. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25245. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25246. if (cert_buf != NULL)
  25247. free(cert_buf);
  25248. wc_PKCS7_Free(pkcs7);
  25249. #endif
  25250. return EXPECT_RESULT();
  25251. }
  25252. static int test_wc_i2d_PKCS12(void)
  25253. {
  25254. EXPECT_DECLS;
  25255. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25256. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25257. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25258. WC_PKCS12* pkcs12 = NULL;
  25259. unsigned char der[FOURK_BUF * 2];
  25260. unsigned char* pt;
  25261. int derSz = 0;
  25262. unsigned char out[FOURK_BUF * 2];
  25263. int outSz = FOURK_BUF * 2;
  25264. const char p12_f[] = "./certs/test-servercert.p12";
  25265. XFILE f = XBADFILE;
  25266. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  25267. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  25268. if (f != XBADFILE)
  25269. XFCLOSE(f);
  25270. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25271. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25272. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25273. ExpectIntEQ(outSz, derSz);
  25274. outSz = derSz - 1;
  25275. pt = out;
  25276. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25277. outSz = derSz;
  25278. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25279. ExpectIntEQ((pt == out), 0);
  25280. pt = NULL;
  25281. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25282. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25283. wc_PKCS12_free(pkcs12);
  25284. pkcs12 = NULL;
  25285. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25286. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25287. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25288. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25289. ExpectIntEQ(outSz, derSz);
  25290. wc_PKCS12_free(pkcs12);
  25291. pkcs12 = NULL;
  25292. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25293. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25294. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25295. ExpectIntEQ(outSz, derSz);
  25296. wc_PKCS12_free(pkcs12);
  25297. pkcs12 = NULL;
  25298. #endif
  25299. return EXPECT_RESULT();
  25300. }
  25301. /* Testing wc_SignatureGetSize() for signature type ECC */
  25302. static int test_wc_SignatureGetSize_ecc(void)
  25303. {
  25304. EXPECT_DECLS;
  25305. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  25306. enum wc_SignatureType sig_type;
  25307. word32 key_len;
  25308. ecc_key ecc;
  25309. const char* qx =
  25310. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25311. const char* qy =
  25312. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25313. const char* d =
  25314. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25315. XMEMSET(&ecc, 0, sizeof(ecc_key));
  25316. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  25317. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  25318. /* Input for signature type ECC */
  25319. sig_type = WC_SIGNATURE_TYPE_ECC;
  25320. key_len = sizeof(ecc_key);
  25321. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  25322. /* Test bad args */
  25323. sig_type = (enum wc_SignatureType) 100;
  25324. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25325. sig_type = WC_SIGNATURE_TYPE_ECC;
  25326. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25327. key_len = (word32)0;
  25328. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25329. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  25330. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  25331. return EXPECT_RESULT();
  25332. } /* END test_wc_SignatureGetSize_ecc() */
  25333. /* Testing wc_SignatureGetSize() for signature type rsa */
  25334. static int test_wc_SignatureGetSize_rsa(void)
  25335. {
  25336. EXPECT_DECLS;
  25337. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  25338. enum wc_SignatureType sig_type;
  25339. word32 key_len;
  25340. word32 idx = 0;
  25341. RsaKey rsa_key;
  25342. byte* tmp = NULL;
  25343. size_t bytes;
  25344. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  25345. #ifdef USE_CERT_BUFFERS_1024
  25346. bytes = (size_t)sizeof_client_key_der_1024;
  25347. if (bytes < (size_t)sizeof_client_key_der_1024)
  25348. bytes = (size_t)sizeof_client_cert_der_1024;
  25349. #elif defined(USE_CERT_BUFFERS_2048)
  25350. bytes = (size_t)sizeof_client_key_der_2048;
  25351. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25352. bytes = (size_t)sizeof_client_cert_der_2048;
  25353. #else
  25354. bytes = FOURK_BUF;
  25355. #endif
  25356. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  25357. DYNAMIC_TYPE_TMP_BUFFER));
  25358. if (tmp != NULL) {
  25359. #ifdef USE_CERT_BUFFERS_1024
  25360. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  25361. #elif defined(USE_CERT_BUFFERS_2048)
  25362. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  25363. #elif !defined(NO_FILESYSTEM)
  25364. XFILE file = XBADFILE;
  25365. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  25366. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  25367. if (file != XBADFILE)
  25368. XFCLOSE(file);
  25369. }
  25370. #else
  25371. ExpectFail();
  25372. #endif
  25373. }
  25374. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  25375. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  25376. /* Input for signature type RSA */
  25377. sig_type = WC_SIGNATURE_TYPE_RSA;
  25378. key_len = sizeof(RsaKey);
  25379. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  25380. /* Test bad args */
  25381. sig_type = (enum wc_SignatureType)100;
  25382. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25383. sig_type = WC_SIGNATURE_TYPE_RSA;
  25384. #ifndef HAVE_USER_RSA
  25385. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  25386. #else
  25387. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25388. #endif
  25389. key_len = (word32)0;
  25390. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25391. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  25392. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25393. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  25394. return EXPECT_RESULT();
  25395. } /* END test_wc_SignatureGetSize_rsa(void) */
  25396. /*----------------------------------------------------------------------------*
  25397. | hash.h Tests
  25398. *----------------------------------------------------------------------------*/
  25399. static int test_wc_HashInit(void)
  25400. {
  25401. EXPECT_DECLS;
  25402. int i; /* 0 indicates tests passed, 1 indicates failure */
  25403. wc_HashAlg hash;
  25404. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25405. enum wc_HashType enumArray[] = {
  25406. #ifndef NO_MD5
  25407. WC_HASH_TYPE_MD5,
  25408. #endif
  25409. #ifndef NO_SHA
  25410. WC_HASH_TYPE_SHA,
  25411. #endif
  25412. #ifdef WOLFSSL_SHA224
  25413. WC_HASH_TYPE_SHA224,
  25414. #endif
  25415. #ifndef NO_SHA256
  25416. WC_HASH_TYPE_SHA256,
  25417. #endif
  25418. #ifdef WOLFSSL_SHA384
  25419. WC_HASH_TYPE_SHA384,
  25420. #endif
  25421. #ifdef WOLFSSL_SHA512
  25422. WC_HASH_TYPE_SHA512,
  25423. #endif
  25424. };
  25425. /* dynamically finds the length */
  25426. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25427. /* For loop to test various arguments... */
  25428. for (i = 0; i < enumlen; i++) {
  25429. /* check for bad args */
  25430. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25431. wc_HashFree(&hash, enumArray[i]);
  25432. /* check for null ptr */
  25433. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  25434. } /* end of for loop */
  25435. return EXPECT_RESULT();
  25436. } /* end of test_wc_HashInit */
  25437. /*
  25438. * Unit test function for wc_HashSetFlags()
  25439. */
  25440. static int test_wc_HashSetFlags(void)
  25441. {
  25442. EXPECT_DECLS;
  25443. #ifdef WOLFSSL_HASH_FLAGS
  25444. wc_HashAlg hash;
  25445. word32 flags = 0;
  25446. int i, j;
  25447. int notSupportedLen;
  25448. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25449. enum wc_HashType enumArray[] = {
  25450. #ifndef NO_MD5
  25451. WC_HASH_TYPE_MD5,
  25452. #endif
  25453. #ifndef NO_SHA
  25454. WC_HASH_TYPE_SHA,
  25455. #endif
  25456. #ifdef WOLFSSL_SHA224
  25457. WC_HASH_TYPE_SHA224,
  25458. #endif
  25459. #ifndef NO_SHA256
  25460. WC_HASH_TYPE_SHA256,
  25461. #endif
  25462. #ifdef WOLFSSL_SHA384
  25463. WC_HASH_TYPE_SHA384,
  25464. #endif
  25465. #ifdef WOLFSSL_SHA512
  25466. WC_HASH_TYPE_SHA512,
  25467. #endif
  25468. #ifdef WOLFSSL_SHA3
  25469. WC_HASH_TYPE_SHA3_224,
  25470. #endif
  25471. };
  25472. enum wc_HashType notSupported[] = {
  25473. WC_HASH_TYPE_MD5_SHA,
  25474. WC_HASH_TYPE_MD2,
  25475. WC_HASH_TYPE_MD4,
  25476. WC_HASH_TYPE_BLAKE2B,
  25477. WC_HASH_TYPE_BLAKE2S,
  25478. WC_HASH_TYPE_NONE,
  25479. };
  25480. /* dynamically finds the length */
  25481. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25482. /* For loop to test various arguments... */
  25483. for (i = 0; i < enumlen; i++) {
  25484. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25485. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  25486. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25487. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  25488. wc_HashFree(&hash, enumArray[i]);
  25489. }
  25490. /* For loop to test not supported cases */
  25491. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25492. for (j = 0; j < notSupportedLen; j++) {
  25493. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25494. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  25495. BAD_FUNC_ARG);
  25496. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25497. }
  25498. #endif
  25499. return EXPECT_RESULT();
  25500. } /* END test_wc_HashSetFlags */
  25501. /*
  25502. * Unit test function for wc_HashGetFlags()
  25503. */
  25504. static int test_wc_HashGetFlags(void)
  25505. {
  25506. EXPECT_DECLS;
  25507. #ifdef WOLFSSL_HASH_FLAGS
  25508. wc_HashAlg hash;
  25509. word32 flags = 0;
  25510. int i, j;
  25511. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25512. enum wc_HashType enumArray[] = {
  25513. #ifndef NO_MD5
  25514. WC_HASH_TYPE_MD5,
  25515. #endif
  25516. #ifndef NO_SHA
  25517. WC_HASH_TYPE_SHA,
  25518. #endif
  25519. #ifdef WOLFSSL_SHA224
  25520. WC_HASH_TYPE_SHA224,
  25521. #endif
  25522. #ifndef NO_SHA256
  25523. WC_HASH_TYPE_SHA256,
  25524. #endif
  25525. #ifdef WOLFSSL_SHA384
  25526. WC_HASH_TYPE_SHA384,
  25527. #endif
  25528. #ifdef WOLFSSL_SHA512
  25529. WC_HASH_TYPE_SHA512,
  25530. #endif
  25531. #ifdef WOLFSSL_SHA3
  25532. WC_HASH_TYPE_SHA3_224,
  25533. #endif
  25534. };
  25535. enum wc_HashType notSupported[] = {
  25536. WC_HASH_TYPE_MD5_SHA,
  25537. WC_HASH_TYPE_MD2,
  25538. WC_HASH_TYPE_MD4,
  25539. WC_HASH_TYPE_BLAKE2B,
  25540. WC_HASH_TYPE_BLAKE2S,
  25541. WC_HASH_TYPE_NONE,
  25542. };
  25543. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25544. int notSupportedLen;
  25545. /* For loop to test various arguments... */
  25546. for (i = 0; i < enumlen; i++) {
  25547. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25548. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  25549. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25550. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  25551. wc_HashFree(&hash, enumArray[i]);
  25552. }
  25553. /* For loop to test not supported cases */
  25554. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25555. for (j = 0; j < notSupportedLen; j++) {
  25556. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25557. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  25558. BAD_FUNC_ARG);
  25559. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25560. }
  25561. #endif
  25562. return EXPECT_RESULT();
  25563. } /* END test_wc_HashGetFlags */
  25564. /*----------------------------------------------------------------------------*
  25565. | Compatibility Tests
  25566. *----------------------------------------------------------------------------*/
  25567. /*----------------------------------------------------------------------------*
  25568. | ASN.1 Tests
  25569. *----------------------------------------------------------------------------*/
  25570. static int test_wolfSSL_ASN1_BIT_STRING(void)
  25571. {
  25572. EXPECT_DECLS;
  25573. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  25574. ASN1_BIT_STRING* str = NULL;
  25575. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25576. /* Empty data testing. */
  25577. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  25578. ASN1_BIT_STRING_free(str);
  25579. str = NULL;
  25580. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25581. /* Invalid parameter testing. */
  25582. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  25583. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  25584. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  25585. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  25586. /* No bit string - bit is always 0. */
  25587. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  25588. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  25589. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25590. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  25591. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  25592. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  25593. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  25594. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25595. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  25596. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  25597. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  25598. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  25599. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  25600. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  25601. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  25602. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  25603. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  25604. ASN1_BIT_STRING_free(str);
  25605. ASN1_BIT_STRING_free(NULL);
  25606. #endif
  25607. return EXPECT_RESULT();
  25608. }
  25609. static int test_wolfSSL_ASN1_INTEGER(void)
  25610. {
  25611. EXPECT_DECLS;
  25612. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25613. ASN1_INTEGER* a = NULL;
  25614. ASN1_INTEGER* dup = NULL;
  25615. const unsigned char invalidLenDer[] = {
  25616. 0x02, 0x20, 0x00
  25617. };
  25618. const unsigned char longDer[] = {
  25619. 0x02, 0x20,
  25620. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25621. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25622. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25623. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  25624. };
  25625. const unsigned char* p;
  25626. /* Invalid parameter testing. */
  25627. ASN1_INTEGER_free(NULL);
  25628. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  25629. ExpectNotNull(a = ASN1_INTEGER_new());
  25630. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25631. ASN1_INTEGER_free(dup);
  25632. dup = NULL;
  25633. ASN1_INTEGER_free(a);
  25634. a = NULL;
  25635. p = longDer;
  25636. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  25637. p = longDer;
  25638. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  25639. ExpectPtrNE(p, longDer);
  25640. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25641. ASN1_INTEGER_free(dup);
  25642. ASN1_INTEGER_free(a);
  25643. #endif
  25644. return EXPECT_RESULT();
  25645. }
  25646. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  25647. {
  25648. EXPECT_DECLS;
  25649. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25650. ASN1_INTEGER* a = NULL;
  25651. ASN1_INTEGER* b = NULL;
  25652. ExpectNotNull(a = ASN1_INTEGER_new());
  25653. ExpectNotNull(b = ASN1_INTEGER_new());
  25654. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  25655. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25656. /* Invalid parameter testing. */
  25657. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  25658. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  25659. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  25660. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25661. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  25662. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25663. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  25664. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25665. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25666. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25667. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  25668. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  25669. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25670. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  25671. ASN1_INTEGER_free(b);
  25672. ASN1_INTEGER_free(a);
  25673. #endif
  25674. return EXPECT_RESULT();
  25675. }
  25676. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  25677. {
  25678. EXPECT_DECLS;
  25679. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25680. ASN1_INTEGER* ai = NULL;
  25681. ASN1_INTEGER* ai2 = NULL;
  25682. BIGNUM* bn = NULL;
  25683. BIGNUM* bn2 = NULL;
  25684. ExpectNotNull(ai = ASN1_INTEGER_new());
  25685. ExpectNotNull(bn2 = BN_new());
  25686. /* Invalid parameter testing. */
  25687. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  25688. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  25689. /* at the moment hard setting since no set function */
  25690. if (ai != NULL) {
  25691. ai->data[0] = 0xff; /* No DER encoding. */
  25692. ai->length = 1;
  25693. }
  25694. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25695. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25696. BN_free(bn);
  25697. bn = NULL;
  25698. #else
  25699. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25700. #endif
  25701. if (ai != NULL) {
  25702. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25703. ai->data[1] = 0x04; /* bad length of integer */
  25704. ai->data[2] = 0x03;
  25705. ai->length = 3;
  25706. }
  25707. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25708. /* Interpreted as a number 0x020403. */
  25709. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25710. BN_free(bn);
  25711. bn = NULL;
  25712. #else
  25713. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25714. #endif
  25715. if (ai != NULL) {
  25716. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25717. ai->data[1] = 0x01; /* length of integer */
  25718. ai->data[2] = 0x03;
  25719. ai->length = 3;
  25720. }
  25721. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25722. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  25723. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25724. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25725. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25726. if (ai != NULL) {
  25727. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25728. ai->data[1] = 0x02; /* length of integer */
  25729. ai->data[2] = 0x00; /* padding byte to ensure positive */
  25730. ai->data[3] = 0xff;
  25731. ai->length = 4;
  25732. }
  25733. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25734. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25735. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25736. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25737. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25738. if (ai != NULL) {
  25739. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25740. ai->data[1] = 0x01; /* length of integer */
  25741. ai->data[2] = 0x00;
  25742. ai->length = 3;
  25743. }
  25744. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25745. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25746. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25747. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25748. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25749. if (ai != NULL) {
  25750. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25751. ai->data[1] = 0x01; /* length of integer */
  25752. ai->data[2] = 0x01;
  25753. ai->length = 3;
  25754. ai->negative = 1;
  25755. }
  25756. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25757. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25758. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25759. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25760. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25761. BN_free(bn2);
  25762. BN_free(bn);
  25763. ASN1_INTEGER_free(ai2);
  25764. ASN1_INTEGER_free(ai);
  25765. #endif
  25766. return EXPECT_RESULT();
  25767. }
  25768. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  25769. {
  25770. EXPECT_DECLS;
  25771. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25772. ASN1_INTEGER *a = NULL;
  25773. long val;
  25774. ExpectNotNull(a = ASN1_INTEGER_new());
  25775. /* Invalid parameter testing. */
  25776. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  25777. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25778. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  25779. #else
  25780. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  25781. #endif
  25782. ASN1_INTEGER_free(a);
  25783. a = NULL;
  25784. ExpectNotNull(a = ASN1_INTEGER_new());
  25785. val = 0;
  25786. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  25787. ASN1_INTEGER_free(a);
  25788. a = NULL;
  25789. /* 0 */
  25790. ExpectNotNull(a = ASN1_INTEGER_new());
  25791. val = 0;
  25792. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25793. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25794. ASN1_INTEGER_free(a);
  25795. a = NULL;
  25796. /* 40 */
  25797. ExpectNotNull(a = ASN1_INTEGER_new());
  25798. val = 40;
  25799. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25800. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25801. ASN1_INTEGER_free(a);
  25802. a = NULL;
  25803. /* -40 */
  25804. ExpectNotNull(a = ASN1_INTEGER_new());
  25805. val = -40;
  25806. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25807. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25808. ASN1_INTEGER_free(a);
  25809. a = NULL;
  25810. /* 128 */
  25811. ExpectNotNull(a = ASN1_INTEGER_new());
  25812. val = 128;
  25813. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25814. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25815. ASN1_INTEGER_free(a);
  25816. a = NULL;
  25817. /* -128 */
  25818. ExpectNotNull(a = ASN1_INTEGER_new());
  25819. val = -128;
  25820. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25821. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25822. ASN1_INTEGER_free(a);
  25823. a = NULL;
  25824. /* 200 */
  25825. ExpectNotNull(a = ASN1_INTEGER_new());
  25826. val = 200;
  25827. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25828. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25829. ASN1_INTEGER_free(a);
  25830. a = NULL;
  25831. /* int max (2147483647) */
  25832. ExpectNotNull(a = ASN1_INTEGER_new());
  25833. val = 2147483647;
  25834. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25835. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25836. ASN1_INTEGER_free(a);
  25837. a = NULL;
  25838. /* int min (-2147483648) */
  25839. ExpectNotNull(a = ASN1_INTEGER_new());
  25840. val = -2147483647 - 1;
  25841. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25842. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25843. ASN1_INTEGER_free(a);
  25844. a = NULL;
  25845. /* long max positive */
  25846. ExpectNotNull(a = ASN1_INTEGER_new());
  25847. val = (long)(((unsigned long)-1) >> 1);
  25848. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25849. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25850. ASN1_INTEGER_free(a);
  25851. #endif
  25852. return EXPECT_RESULT();
  25853. }
  25854. #if defined(OPENSSL_EXTRA)
  25855. typedef struct ASN1IntTestVector {
  25856. const byte* der;
  25857. const size_t derSz;
  25858. const long value;
  25859. } ASN1IntTestVector;
  25860. #endif
  25861. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  25862. {
  25863. EXPECT_DECLS;
  25864. #if defined(OPENSSL_EXTRA)
  25865. size_t i;
  25866. WOLFSSL_ASN1_INTEGER* a = NULL;
  25867. WOLFSSL_ASN1_INTEGER* b = NULL;
  25868. WOLFSSL_ASN1_INTEGER* c = NULL;
  25869. const byte* p = NULL;
  25870. byte* p2 = NULL;
  25871. byte* reEncoded = NULL;
  25872. int reEncodedSz;
  25873. static const byte zeroDer[] = {
  25874. 0x02, 0x01, 0x00
  25875. };
  25876. static const byte oneDer[] = {
  25877. 0x02, 0x01, 0x01
  25878. };
  25879. static const byte negativeDer[] = {
  25880. 0x02, 0x03, 0xC1, 0x16, 0x0D
  25881. };
  25882. static const byte positiveDer[] = {
  25883. 0x02, 0x03, 0x01, 0x00, 0x01
  25884. };
  25885. static const byte primeDer[] = {
  25886. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  25887. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  25888. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  25889. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  25890. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  25891. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  25892. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  25893. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  25894. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  25895. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  25896. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  25897. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  25898. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  25899. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  25900. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  25901. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  25902. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  25903. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  25904. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  25905. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  25906. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  25907. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  25908. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  25909. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  25910. };
  25911. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  25912. static const ASN1IntTestVector testVectors[] = {
  25913. {zeroDer, sizeof(zeroDer), 0},
  25914. {oneDer, sizeof(oneDer), 1},
  25915. {negativeDer, sizeof(negativeDer), -4123123},
  25916. {positiveDer, sizeof(positiveDer), 65537},
  25917. {primeDer, sizeof(primeDer), 0}
  25918. };
  25919. static const size_t NUM_TEST_VECTORS =
  25920. sizeof(testVectors)/sizeof(testVectors[0]);
  25921. /* Check d2i error conditions */
  25922. /* NULL pointer to input. */
  25923. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  25924. ExpectNull(b);
  25925. /* NULL input. */
  25926. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  25927. ExpectNull(b);
  25928. /* 0 length. */
  25929. p = testVectors[0].der;
  25930. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  25931. ExpectNull(b);
  25932. /* Negative length. */
  25933. p = testVectors[0].der;
  25934. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  25935. ExpectNull(b);
  25936. /* Garbage DER input. */
  25937. p = garbageDer;
  25938. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  25939. ExpectNull(b);
  25940. /* Check i2d error conditions */
  25941. /* NULL input. */
  25942. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  25943. /* 0 length input data buffer (a->length == 0). */
  25944. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  25945. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25946. if (a != NULL)
  25947. a->data = NULL;
  25948. /* NULL input data buffer. */
  25949. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25950. if (a != NULL) {
  25951. /* Reset a->data. */
  25952. a->data = a->intData;
  25953. }
  25954. /* Set a to valid value. */
  25955. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  25956. /* NULL output buffer. */
  25957. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  25958. wolfSSL_ASN1_INTEGER_free(a);
  25959. a = NULL;
  25960. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  25961. p = testVectors[i].der;
  25962. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  25963. testVectors[i].derSz));
  25964. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25965. if (testVectors[i].derSz <= sizeof(long)) {
  25966. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  25967. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  25968. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  25969. wolfSSL_ASN1_INTEGER_free(c);
  25970. c = NULL;
  25971. }
  25972. /* Convert to DER without a pre-allocated output buffer. */
  25973. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  25974. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25975. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25976. /* Convert to DER with a pre-allocated output buffer. In this case, the
  25977. * output buffer pointer should be incremented just past the end of the
  25978. * encoded data. */
  25979. p2 = reEncoded;
  25980. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  25981. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25982. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  25983. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25984. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  25985. reEncoded = NULL;
  25986. wolfSSL_ASN1_INTEGER_free(a);
  25987. a = NULL;
  25988. }
  25989. #endif /* OPENSSL_EXTRA */
  25990. return EXPECT_RESULT();
  25991. }
  25992. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  25993. {
  25994. EXPECT_DECLS;
  25995. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  25996. BIO* bio = NULL;
  25997. BIO* out = NULL;
  25998. BIO* fixed = NULL;
  25999. ASN1_INTEGER* ai = NULL;
  26000. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  26001. char tmp[1024];
  26002. int tmpSz;
  26003. const char expected1[] = "123456";
  26004. const char expected2[] = "112345678912345678901234567890";
  26005. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  26006. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  26007. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  26008. ExpectNotNull(ai = ASN1_INTEGER_new());
  26009. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  26010. /* Invalid parameter testing. */
  26011. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  26012. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  26013. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  26014. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  26015. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  26016. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  26017. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  26018. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  26019. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  26020. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  26021. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  26022. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  26023. /* No data to read from BIO. */
  26024. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  26025. /* read first line */
  26026. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26027. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  26028. XMEMSET(tmp, 0, 1024);
  26029. tmpSz = BIO_read(out, tmp, 1024);
  26030. ExpectIntEQ(tmpSz, 6);
  26031. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  26032. /* fail on second line (not % 2) */
  26033. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26034. /* read 3rd long line */
  26035. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26036. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  26037. XMEMSET(tmp, 0, 1024);
  26038. tmpSz = BIO_read(out, tmp, 1024);
  26039. ExpectIntEQ(tmpSz, 30);
  26040. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  26041. /* fail on empty line */
  26042. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26043. BIO_free(bio);
  26044. bio = NULL;
  26045. /* Make long integer, requiring dynamic memory, even longer. */
  26046. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  26047. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26048. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  26049. XMEMSET(tmp, 0, 1024);
  26050. tmpSz = BIO_read(out, tmp, 1024);
  26051. ExpectIntEQ(tmpSz, 48);
  26052. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26053. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  26054. XMEMSET(tmp, 0, 1024);
  26055. tmpSz = BIO_read(out, tmp, 1024);
  26056. ExpectIntEQ(tmpSz, 56);
  26057. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  26058. BIO_free(bio);
  26059. BIO_free(out);
  26060. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26061. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26062. /* Ensure there is 0 bytes available to write into. */
  26063. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  26064. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26065. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26066. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26067. BIO_free(fixed);
  26068. ASN1_INTEGER_free(ai);
  26069. #endif
  26070. return EXPECT_RESULT();
  26071. }
  26072. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  26073. {
  26074. EXPECT_DECLS;
  26075. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26076. ASN1_INTEGER *a = NULL;
  26077. unsigned char *pp,*tpp;
  26078. int ret;
  26079. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  26080. /* Invalid parameter testing. */
  26081. /* Set pp to an invalid value. */
  26082. pp = NULL;
  26083. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  26084. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  26085. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  26086. /* 40 */
  26087. if (a != NULL) {
  26088. a->intData[0] = ASN_INTEGER;
  26089. a->intData[1] = 1;
  26090. a->intData[2] = 40;
  26091. }
  26092. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26093. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26094. DYNAMIC_TYPE_TMP_BUFFER));
  26095. tpp = pp;
  26096. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26097. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26098. tpp--;
  26099. ExpectIntEQ(*tpp, 40);
  26100. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26101. pp = NULL;
  26102. /* 128 */
  26103. if (a != NULL) {
  26104. a->intData[0] = ASN_INTEGER;
  26105. a->intData[1] = 1;
  26106. a->intData[2] = 128;
  26107. }
  26108. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26109. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26110. DYNAMIC_TYPE_TMP_BUFFER));
  26111. tpp = pp;
  26112. if (tpp != NULL) {
  26113. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26114. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26115. tpp--;
  26116. ExpectIntEQ(*(tpp--), 128);
  26117. ExpectIntEQ(*tpp, 0);
  26118. }
  26119. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26120. pp = NULL;
  26121. /* -40 */
  26122. if (a != NULL) {
  26123. a->intData[0] = ASN_INTEGER;
  26124. a->intData[1] = 1;
  26125. a->intData[2] = 40;
  26126. a->negative = 1;
  26127. }
  26128. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26129. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26130. DYNAMIC_TYPE_TMP_BUFFER));
  26131. tpp = pp;
  26132. if (tpp != NULL) {
  26133. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26134. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26135. tpp--;
  26136. ExpectIntEQ(*tpp, 216);
  26137. }
  26138. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26139. pp = NULL;
  26140. /* -128 */
  26141. if (a != NULL) {
  26142. a->intData[0] = ASN_INTEGER;
  26143. a->intData[1] = 1;
  26144. a->intData[2] = 128;
  26145. a->negative = 1;
  26146. }
  26147. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26148. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26149. DYNAMIC_TYPE_TMP_BUFFER));
  26150. tpp = pp;
  26151. if (tpp != NULL) {
  26152. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26153. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26154. tpp--;
  26155. ExpectIntEQ(*tpp, 128);
  26156. }
  26157. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26158. pp = NULL;
  26159. /* -200 */
  26160. if (a != NULL) {
  26161. a->intData[0] = ASN_INTEGER;
  26162. a->intData[1] = 1;
  26163. a->intData[2] = 200;
  26164. a->negative = 1;
  26165. }
  26166. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26167. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26168. DYNAMIC_TYPE_TMP_BUFFER));
  26169. tpp = pp;
  26170. if (tpp != NULL) {
  26171. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26172. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26173. tpp--;
  26174. ExpectIntEQ(*(tpp--), 56);
  26175. ExpectIntEQ(*tpp, 255);
  26176. }
  26177. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26178. pp = NULL;
  26179. /* Empty */
  26180. if (a != NULL) {
  26181. a->intData[0] = ASN_INTEGER;
  26182. a->intData[1] = 0;
  26183. a->negative = 0;
  26184. }
  26185. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26186. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26187. DYNAMIC_TYPE_TMP_BUFFER));
  26188. tpp = pp;
  26189. if (tpp != NULL) {
  26190. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26191. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26192. tpp--;
  26193. ExpectIntEQ(*tpp, 0);
  26194. }
  26195. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26196. pp = NULL;
  26197. /* 0 */
  26198. if (a != NULL) {
  26199. a->intData[0] = ASN_INTEGER;
  26200. a->intData[1] = 1;
  26201. a->intData[2] = 0;
  26202. a->negative = 1;
  26203. }
  26204. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26205. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26206. DYNAMIC_TYPE_TMP_BUFFER));
  26207. if (tpp != NULL) {
  26208. tpp = pp;
  26209. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26210. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26211. tpp--;
  26212. ExpectIntEQ(*tpp, 0);
  26213. }
  26214. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26215. pp = NULL;
  26216. /* 0x100 */
  26217. if (a != NULL) {
  26218. a->intData[0] = ASN_INTEGER;
  26219. a->intData[1] = 2;
  26220. a->intData[2] = 0x01;
  26221. a->intData[3] = 0x00;
  26222. a->negative = 0;
  26223. }
  26224. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26225. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26226. DYNAMIC_TYPE_TMP_BUFFER));
  26227. if (tpp != NULL) {
  26228. tpp = pp;
  26229. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26230. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26231. tpp -= 2;
  26232. ExpectIntEQ(tpp[0], 0x01);
  26233. ExpectIntEQ(tpp[1], 0x00);
  26234. }
  26235. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26236. pp = NULL;
  26237. /* -0x8000 => 0x8000 */
  26238. if (a != NULL) {
  26239. a->intData[0] = ASN_INTEGER;
  26240. a->intData[1] = 2;
  26241. a->intData[2] = 0x80;
  26242. a->intData[3] = 0x00;
  26243. a->negative = 1;
  26244. }
  26245. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26246. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26247. DYNAMIC_TYPE_TMP_BUFFER));
  26248. tpp = pp;
  26249. if (tpp != NULL) {
  26250. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26251. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26252. tpp -= 2;
  26253. ExpectIntEQ(tpp[0], 0x80);
  26254. ExpectIntEQ(tpp[1], 0x00);
  26255. }
  26256. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26257. pp = NULL;
  26258. /* -0x8001 => 0xFF7FFF */
  26259. if (a != NULL) {
  26260. a->intData[0] = ASN_INTEGER;
  26261. a->intData[1] = 2;
  26262. a->intData[2] = 0x80;
  26263. a->intData[3] = 0x01;
  26264. a->negative = 1;
  26265. }
  26266. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  26267. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26268. DYNAMIC_TYPE_TMP_BUFFER));
  26269. tpp = pp;
  26270. if (tpp != NULL) {
  26271. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26272. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  26273. tpp -= 3;
  26274. ExpectIntEQ(tpp[0], 0xFF);
  26275. ExpectIntEQ(tpp[1], 0x7F);
  26276. ExpectIntEQ(tpp[2], 0xFF);
  26277. }
  26278. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26279. wolfSSL_ASN1_INTEGER_free(a);
  26280. #endif /* OPENSSL_EXTRA && !NO_ASN */
  26281. return EXPECT_RESULT();
  26282. }
  26283. static int test_wolfSSL_ASN1_OBJECT(void)
  26284. {
  26285. EXPECT_DECLS;
  26286. #if defined(OPENSSL_EXTRA)
  26287. ASN1_OBJECT* a = NULL;
  26288. ASN1_OBJECT s;
  26289. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  26290. /* Invalid parameter testing. */
  26291. ASN1_OBJECT_free(NULL);
  26292. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  26293. /* Test that a static ASN1_OBJECT can be freed. */
  26294. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26295. ASN1_OBJECT_free(&s);
  26296. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26297. ASN1_OBJECT_free(a);
  26298. a = NULL;
  26299. s.obj = der;
  26300. s.objSz = sizeof(der);
  26301. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26302. ASN1_OBJECT_free(a);
  26303. ASN1_OBJECT_free(&s);
  26304. #endif /* OPENSSL_EXTRA */
  26305. return EXPECT_RESULT();
  26306. }
  26307. static int test_wolfSSL_ASN1_get_object(void)
  26308. {
  26309. EXPECT_DECLS;
  26310. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26311. const unsigned char* derBuf = cliecc_cert_der_256;
  26312. const unsigned char* nullPtr = NULL;
  26313. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  26314. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  26315. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  26316. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  26317. const unsigned char* p;
  26318. unsigned char objDer[8];
  26319. unsigned char* der;
  26320. unsigned char* derPtr;
  26321. int len = sizeof_cliecc_cert_der_256;
  26322. long asnLen = 0;
  26323. int tag = 0;
  26324. int cls = 0;
  26325. ASN1_OBJECT* a = NULL;
  26326. ASN1_OBJECT s;
  26327. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26328. /* Invalid encoding at length. */
  26329. p = objDerInvalidLen;
  26330. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26331. 0x80);
  26332. p = objDerBadLen;
  26333. /* Error = 0x80, Constructed = 0x20 */
  26334. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26335. 0x80 | 0x20);
  26336. /* Read a couple TLV triplets and make sure they match the expected values
  26337. */
  26338. /* SEQUENCE */
  26339. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  26340. ExpectIntEQ(asnLen, 862);
  26341. ExpectIntEQ(tag, 0x10);
  26342. ExpectIntEQ(cls, 0);
  26343. /* SEQUENCE */
  26344. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26345. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26346. ExpectIntEQ(asnLen, 772);
  26347. ExpectIntEQ(tag, 0x10);
  26348. ExpectIntEQ(cls, 0);
  26349. /* [0] */
  26350. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26351. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26352. ExpectIntEQ(asnLen, 3);
  26353. ExpectIntEQ(tag, 0);
  26354. ExpectIntEQ(cls, 0x80);
  26355. /* INTEGER */
  26356. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26357. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26358. ExpectIntEQ(asnLen, 1);
  26359. ExpectIntEQ(tag, 0x2);
  26360. ExpectIntEQ(cls, 0);
  26361. derBuf += asnLen;
  26362. /* INTEGER */
  26363. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26364. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26365. ExpectIntEQ(asnLen, 20);
  26366. ExpectIntEQ(tag, 0x2);
  26367. ExpectIntEQ(cls, 0);
  26368. derBuf += asnLen;
  26369. /* SEQUENCE */
  26370. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26371. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26372. ExpectIntEQ(asnLen, 10);
  26373. ExpectIntEQ(tag, 0x10);
  26374. ExpectIntEQ(cls, 0);
  26375. /* Found OBJECT_ID. */
  26376. /* Invalid parameter testing. */
  26377. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  26378. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  26379. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  26380. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  26381. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  26382. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  26383. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  26384. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  26385. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  26386. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  26387. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26388. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26389. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  26390. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  26391. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  26392. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  26393. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  26394. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26395. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26396. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  26397. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  26398. /* Invalid encoding at length. */
  26399. p = objDerInvalidLen;
  26400. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  26401. p = objDerBadLen;
  26402. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  26403. p = objDerNotObj;
  26404. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  26405. p = objDerNoData;
  26406. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  26407. /* Create an ASN OBJECT from content */
  26408. p = derBuf + 2;
  26409. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  26410. ASN1_OBJECT_free(a);
  26411. a = NULL;
  26412. /* Create an ASN OBJECT from DER */
  26413. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  26414. /* Invalid parameter testing. */
  26415. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  26416. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  26417. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  26418. der = NULL;
  26419. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  26420. derPtr = objDer;
  26421. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  26422. ExpectPtrNE(derPtr, objDer);
  26423. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  26424. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  26425. ASN1_OBJECT_free(a);
  26426. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26427. return EXPECT_RESULT();
  26428. }
  26429. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  26430. {
  26431. EXPECT_DECLS;
  26432. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26433. ASN1_OBJECT* obj = NULL;
  26434. ASN1_OBJECT* a = NULL;
  26435. BIO *bio = NULL;
  26436. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  26437. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  26438. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  26439. const unsigned char* p;
  26440. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  26441. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  26442. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  26443. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  26444. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  26445. /* No DER encoding in ASN1_OBJECT. */
  26446. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  26447. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26448. ASN1_OBJECT_free(a);
  26449. a = NULL;
  26450. /* DER encoding - not OBJECT_ID */
  26451. p = notObjDer;
  26452. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26453. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26454. ASN1_OBJECT_free(a);
  26455. a = NULL;
  26456. /* Bad length encoding. */
  26457. p = badLenDer;
  26458. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26459. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26460. ASN1_OBJECT_free(a);
  26461. a = NULL;
  26462. /* Good encoding - but unknown. */
  26463. p = goodDer;
  26464. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26465. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26466. ASN1_OBJECT_free(a);
  26467. BIO_free(bio);
  26468. ASN1_OBJECT_free(obj);
  26469. #endif
  26470. return EXPECT_RESULT();
  26471. }
  26472. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  26473. {
  26474. EXPECT_DECLS;
  26475. #if defined(OPENSSL_EXTRA) && \
  26476. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  26477. char buf[50] = {0};
  26478. ASN1_OBJECT* obj;
  26479. const char* oid = "2.5.29.19";
  26480. const char* ln = "X509v3 Basic Constraints";
  26481. obj = NULL;
  26482. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  26483. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  26484. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  26485. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  26486. XMEMSET(buf, 0, sizeof(buf));
  26487. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  26488. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  26489. ASN1_OBJECT_free(obj);
  26490. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  26491. return EXPECT_RESULT();
  26492. }
  26493. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  26494. {
  26495. EXPECT_DECLS;
  26496. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  26497. WOLFSSL_STACK* sk = NULL;
  26498. WOLFSSL_ASN1_OBJECT* obj;
  26499. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26500. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26501. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26502. sk = NULL;
  26503. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26504. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  26505. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  26506. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  26507. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26508. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  26509. sk = NULL;
  26510. /* obj freed in pop_free call. */
  26511. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26512. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26513. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26514. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  26515. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26516. wolfSSL_ASN1_OBJECT_free(obj);
  26517. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  26518. return EXPECT_RESULT();
  26519. }
  26520. static int test_wolfSSL_ASN1_STRING(void)
  26521. {
  26522. EXPECT_DECLS;
  26523. #if defined(OPENSSL_EXTRA)
  26524. ASN1_STRING* str = NULL;
  26525. ASN1_STRING* c = NULL;
  26526. const char data[] = "hello wolfSSL";
  26527. const char data2[] = "Same len data";
  26528. const char longData[] =
  26529. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26530. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26531. ASN1_STRING_free(str);
  26532. str = NULL;
  26533. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26534. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  26535. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  26536. /* Check setting to NULL works. */
  26537. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  26538. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  26539. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26540. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  26541. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  26542. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  26543. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  26544. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  26545. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  26546. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  26547. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  26548. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  26549. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  26550. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26551. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  26552. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  26553. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  26554. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  26555. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26556. /* Check setting back to smaller size frees dynamic data. */
  26557. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26558. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  26559. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  26560. ExpectNull(ASN1_STRING_get0_data(NULL));
  26561. ExpectNotNull(ASN1_STRING_get0_data(str));
  26562. ExpectNull(ASN1_STRING_data(NULL));
  26563. ExpectNotNull(ASN1_STRING_data(str));
  26564. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  26565. ExpectIntGT(ASN1_STRING_length(str), 0);
  26566. ASN1_STRING_free(c);
  26567. ASN1_STRING_free(str);
  26568. ASN1_STRING_free(NULL);
  26569. #ifndef NO_WOLFSSL_STUB
  26570. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  26571. #endif
  26572. #endif
  26573. return EXPECT_RESULT();
  26574. }
  26575. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26576. {
  26577. EXPECT_DECLS;
  26578. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  26579. !defined(NO_FILESYSTEM)
  26580. WOLFSSL_X509* x509 = NULL;
  26581. WOLFSSL_X509_NAME* subject = NULL;
  26582. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  26583. WOLFSSL_ASN1_STRING* a = NULL;
  26584. FILE* file = XBADFILE;
  26585. int idx = 0;
  26586. char targetOutput[16] = "www.wolfssl.com";
  26587. unsigned char* actual_output = NULL;
  26588. int len = 0;
  26589. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26590. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26591. if (file != NULL)
  26592. fclose(file);
  26593. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  26594. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26595. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26596. NID_commonName, -1)), 5);
  26597. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26598. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26599. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26600. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  26601. a = NULL;
  26602. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  26603. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  26604. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  26605. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  26606. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  26607. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  26608. wolfSSL_X509_free(x509);
  26609. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26610. ExpectNotNull(a = ASN1_STRING_new());
  26611. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  26612. ASN1_STRING_free(a);
  26613. #endif
  26614. return EXPECT_RESULT();
  26615. }
  26616. static int test_wolfSSL_i2s_ASN1_STRING(void)
  26617. {
  26618. EXPECT_DECLS;
  26619. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26620. WOLFSSL_ASN1_STRING* str = NULL;
  26621. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  26622. char* ret = NULL;
  26623. ExpectNotNull(str = ASN1_STRING_new());
  26624. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  26625. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26626. ret = NULL;
  26627. /* No data. */
  26628. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26629. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26630. ret = NULL;
  26631. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  26632. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26633. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26634. ret = NULL;
  26635. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  26636. /* No type. */
  26637. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26638. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26639. ASN1_STRING_free(str);
  26640. #endif
  26641. return EXPECT_RESULT();
  26642. }
  26643. static int test_wolfSSL_ASN1_STRING_canon(void)
  26644. {
  26645. EXPECT_DECLS;
  26646. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  26647. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  26648. defined(OPENSSL_EXTRA_X509_SMALL))
  26649. WOLFSSL_ASN1_STRING* orig = NULL;
  26650. WOLFSSL_ASN1_STRING* canon = NULL;
  26651. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  26652. const char* whitespaceOnly = "\t\r\n";
  26653. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  26654. const char* canonData = "\x01 \x02 \xff tt";
  26655. const char longData[] =
  26656. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26657. ExpectNotNull(orig = ASN1_STRING_new());
  26658. ExpectNotNull(canon = ASN1_STRING_new());
  26659. /* Invalid parameter testing. */
  26660. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  26661. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  26662. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  26663. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26664. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26665. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  26666. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26667. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26668. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  26669. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26670. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26671. ASN1_STRING_free(orig);
  26672. orig = NULL;
  26673. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26674. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  26675. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26676. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  26677. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26678. ASN1_STRING_free(orig);
  26679. orig = NULL;
  26680. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26681. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  26682. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26683. ASN1_STRING_free(orig);
  26684. orig = NULL;
  26685. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26686. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26687. ASN1_STRING_free(orig);
  26688. ASN1_STRING_free(canon);
  26689. #endif
  26690. #endif
  26691. return EXPECT_RESULT();
  26692. }
  26693. static int test_wolfSSL_ASN1_STRING_print(void)
  26694. {
  26695. EXPECT_DECLS;
  26696. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  26697. !defined(NO_BIO)
  26698. ASN1_STRING* asnStr = NULL;
  26699. const char HELLO_DATA[]= \
  26700. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  26701. #define MAX_UNPRINTABLE_CHAR 32
  26702. #define MAX_BUF 255
  26703. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  26704. unsigned char expected[sizeof(unprintableData)+1];
  26705. unsigned char rbuf[MAX_BUF];
  26706. BIO *bio = NULL;
  26707. int p_len;
  26708. int i;
  26709. /* setup */
  26710. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  26711. unprintableData[i] = HELLO_DATA[i];
  26712. expected[i] = HELLO_DATA[i];
  26713. }
  26714. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  26715. unprintableData[sizeof(HELLO_DATA)+i] = i;
  26716. if (i == (int)'\n' || i == (int)'\r')
  26717. expected[sizeof(HELLO_DATA)+i] = i;
  26718. else
  26719. expected[sizeof(HELLO_DATA)+i] = '.';
  26720. }
  26721. unprintableData[sizeof(unprintableData)-1] = '\0';
  26722. expected[sizeof(expected)-1] = '\0';
  26723. XMEMSET(rbuf, 0, MAX_BUF);
  26724. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26725. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  26726. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26727. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  26728. (int)sizeof(unprintableData)), 1);
  26729. /* test */
  26730. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  26731. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  26732. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  26733. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  26734. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  26735. ExpectStrEQ((char*)rbuf, (const char*)expected);
  26736. BIO_free(bio);
  26737. bio = NULL;
  26738. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26739. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26740. /* Ensure there is 0 bytes available to write into. */
  26741. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  26742. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26743. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26744. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26745. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  26746. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26747. BIO_free(bio);
  26748. ASN1_STRING_free(asnStr);
  26749. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  26750. return EXPECT_RESULT();
  26751. }
  26752. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  26753. {
  26754. EXPECT_DECLS;
  26755. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26756. ASN1_STRING* asn_str = NULL;
  26757. const char data[] = "Hello wolfSSL!";
  26758. ASN1_STRING* esc_str = NULL;
  26759. const char esc_data[] = "a+;<>";
  26760. ASN1_STRING* neg_int = NULL;
  26761. const char neg_int_data[] = "\xff";
  26762. ASN1_STRING* neg_enum = NULL;
  26763. const char neg_enum_data[] = "\xff";
  26764. BIO *bio = NULL;
  26765. BIO *fixed = NULL;
  26766. unsigned long flags;
  26767. int p_len;
  26768. unsigned char rbuf[255];
  26769. /* setup */
  26770. XMEMSET(rbuf, 0, 255);
  26771. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26772. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  26773. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26774. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26775. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  26776. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26777. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  26778. sizeof(esc_data)), 1);
  26779. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  26780. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  26781. sizeof(neg_int_data) - 1), 1);
  26782. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  26783. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  26784. sizeof(neg_enum_data) - 1), 1);
  26785. /* Invalid parameter testing. */
  26786. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  26787. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  26788. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  26789. /* no flags */
  26790. XMEMSET(rbuf, 0, 255);
  26791. flags = 0;
  26792. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  26793. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  26794. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  26795. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26796. /* Ensure there is 0 bytes available to write into. */
  26797. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26798. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26799. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26800. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26801. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  26802. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26803. /* RFC2253 Escape */
  26804. XMEMSET(rbuf, 0, 255);
  26805. flags = ASN1_STRFLGS_ESC_2253;
  26806. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  26807. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  26808. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  26809. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26810. /* Ensure there is 0 bytes available to write into. */
  26811. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26812. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26813. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26814. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26815. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  26816. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26817. /* Show type */
  26818. XMEMSET(rbuf, 0, 255);
  26819. flags = ASN1_STRFLGS_SHOW_TYPE;
  26820. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  26821. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  26822. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  26823. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26824. /* Ensure there is 0 bytes available to write into. */
  26825. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26826. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26827. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26828. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26829. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  26830. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26831. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  26832. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26833. /* Dump All */
  26834. XMEMSET(rbuf, 0, 255);
  26835. flags = ASN1_STRFLGS_DUMP_ALL;
  26836. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  26837. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  26838. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  26839. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26840. /* Ensure there is 0 bytes available to write into. */
  26841. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26842. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26843. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26844. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26845. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26846. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26847. /* Dump Der */
  26848. XMEMSET(rbuf, 0, 255);
  26849. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  26850. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  26851. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  26852. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  26853. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26854. /* Ensure there is 0 bytes available to write into. */
  26855. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26856. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26857. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26858. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26859. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  26860. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26861. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26862. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26863. /* Dump All + Show type */
  26864. XMEMSET(rbuf, 0, 255);
  26865. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26866. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  26867. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  26868. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  26869. /* Dump All + Show type - Negative Integer. */
  26870. XMEMSET(rbuf, 0, 255);
  26871. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26872. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  26873. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  26874. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  26875. /* Dump All + Show type - Negative Enumerated. */
  26876. XMEMSET(rbuf, 0, 255);
  26877. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26878. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  26879. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  26880. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  26881. BIO_free(fixed);
  26882. BIO_free(bio);
  26883. ASN1_STRING_free(asn_str);
  26884. ASN1_STRING_free(esc_str);
  26885. ASN1_STRING_free(neg_int);
  26886. ASN1_STRING_free(neg_enum);
  26887. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  26888. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  26889. #endif
  26890. return EXPECT_RESULT();
  26891. }
  26892. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  26893. {
  26894. EXPECT_DECLS;
  26895. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  26896. ASN1_STRING* asn1str_test = NULL;
  26897. ASN1_STRING* asn1str_answer = NULL;
  26898. /* Each character is encoded using 4 bytes */
  26899. char input[] = {
  26900. 0, 0, 0, 'T',
  26901. 0, 0, 0, 'e',
  26902. 0, 0, 0, 's',
  26903. 0, 0, 0, 't',
  26904. };
  26905. char output[] = "Test";
  26906. char badInput[] = {
  26907. 1, 0, 0, 'T',
  26908. 0, 1, 0, 'e',
  26909. 0, 0, 1, 's',
  26910. };
  26911. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  26912. /* Test wrong type. */
  26913. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26914. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26915. ASN1_STRING_free(asn1str_test);
  26916. asn1str_test = NULL;
  26917. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  26918. /* Test bad length. */
  26919. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  26920. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26921. /* Test bad input. */
  26922. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  26923. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26924. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  26925. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26926. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  26927. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26928. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  26929. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  26930. ExpectNotNull(
  26931. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26932. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  26933. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  26934. ASN1_STRING_free(asn1str_test);
  26935. ASN1_STRING_free(asn1str_answer);
  26936. #endif /* OPENSSL_ALL && !NO_ASN */
  26937. return EXPECT_RESULT();
  26938. }
  26939. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  26940. {
  26941. EXPECT_DECLS;
  26942. #if defined(OPENSSL_EXTRA)
  26943. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  26944. unsigned char nullstr[32];
  26945. XMEMSET(nullstr, 0, 32);
  26946. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  26947. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  26948. if (asn1_gtime != NULL) {
  26949. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  26950. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  26951. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  26952. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26953. }
  26954. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  26955. #endif /* OPENSSL_EXTRA */
  26956. return EXPECT_RESULT();
  26957. }
  26958. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  26959. {
  26960. EXPECT_DECLS;
  26961. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  26962. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  26963. BIO* bio = NULL;
  26964. unsigned char buf[24];
  26965. int i;
  26966. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26967. BIO_set_write_buf_size(bio, 24);
  26968. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  26969. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  26970. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  26971. /* Type not set. */
  26972. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26973. gtime.type = V_ASN1_GENERALIZEDTIME;
  26974. /* Invalid parameters testing. */
  26975. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  26976. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  26977. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  26978. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  26979. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  26980. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  26981. BIO_free(bio);
  26982. bio = NULL;
  26983. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26984. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26985. /* Ensure there is 0 bytes available to write into. */
  26986. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  26987. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26988. for (i = 1; i < 20; i++) {
  26989. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  26990. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26991. }
  26992. BIO_free(bio);
  26993. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  26994. #endif /* OPENSSL_EXTRA */
  26995. return EXPECT_RESULT();
  26996. }
  26997. static int test_wolfSSL_ASN1_TIME(void)
  26998. {
  26999. EXPECT_DECLS;
  27000. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27001. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27002. unsigned char *data;
  27003. ExpectNotNull(asn_time = ASN1_TIME_new());
  27004. #ifndef NO_WOLFSSL_STUB
  27005. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  27006. #endif
  27007. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  27008. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  27009. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  27010. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  27011. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  27012. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  27013. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  27014. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  27015. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  27016. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  27017. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  27018. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  27019. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  27020. ASN1_TIME_free(asn_time);
  27021. ASN1_TIME_free(NULL);
  27022. #endif
  27023. return EXPECT_RESULT();
  27024. }
  27025. static int test_wolfSSL_ASN1_TIME_to_string(void)
  27026. {
  27027. EXPECT_DECLS;
  27028. #ifndef NO_ASN_TIME
  27029. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27030. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  27031. WOLFSSL_ASN1_TIME* t = NULL;
  27032. char buf[ASN_GENERALIZED_TIME_SIZE];
  27033. ExpectNotNull((t = ASN1_TIME_new()));
  27034. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  27035. /* Invalid parameter testing. */
  27036. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  27037. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  27038. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  27039. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  27040. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  27041. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  27042. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  27043. /* Buffer needs to be longer than minimum of 5 characters. */
  27044. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  27045. ASN1_TIME_free(t);
  27046. #endif
  27047. #endif /* NO_ASN_TIME */
  27048. return EXPECT_RESULT();
  27049. }
  27050. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27051. {
  27052. EXPECT_DECLS;
  27053. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27054. ASN1_TIME* fromTime = NULL;
  27055. ASN1_TIME* closeToTime = NULL;
  27056. ASN1_TIME* toTime = NULL;
  27057. ASN1_TIME* invalidTime = NULL;
  27058. int daysDiff;
  27059. int secsDiff;
  27060. ExpectNotNull((fromTime = ASN1_TIME_new()));
  27061. /* Feb 22, 2003, 21:15:15 */
  27062. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  27063. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  27064. /* Feb 22, 2003, 21:16:15 */
  27065. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  27066. ExpectNotNull((toTime = ASN1_TIME_new()));
  27067. /* Dec 19, 2010, 18:10:11 */
  27068. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  27069. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  27070. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  27071. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  27072. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  27073. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  27074. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27075. /* Error conditions. */
  27076. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  27077. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  27078. /* If both times are NULL, difference is 0. */
  27079. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  27080. ExpectIntEQ(daysDiff, 0);
  27081. ExpectIntEQ(secsDiff, 0);
  27082. /* If one time is NULL, it defaults to the current time. */
  27083. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  27084. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  27085. /* Normal operation. Both times non-NULL. */
  27086. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27087. ExpectIntEQ(daysDiff, 2856);
  27088. ExpectIntEQ(secsDiff, 75296);
  27089. /* Swapping the times should return negative values. */
  27090. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  27091. ExpectIntEQ(daysDiff, -2856);
  27092. ExpectIntEQ(secsDiff, -75296);
  27093. /* Compare with invalid time string. */
  27094. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  27095. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  27096. /* Compare with days difference of 0. */
  27097. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  27098. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  27099. /* Days and seconds differences not 0. */
  27100. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27101. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27102. /* Same time. */
  27103. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27104. /* Compare regression test: No seconds difference, just difference in days.
  27105. */
  27106. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27107. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27108. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27109. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27110. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27111. /* Edge case with Unix epoch. */
  27112. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27113. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27114. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27115. ExpectIntEQ(daysDiff, 3652);
  27116. ExpectIntEQ(secsDiff, 0);
  27117. /* Edge case with year > 2038 (year 2038 problem). */
  27118. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27119. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27120. ExpectIntEQ(daysDiff, 2932896);
  27121. ExpectIntEQ(secsDiff, 86399);
  27122. ASN1_TIME_free(fromTime);
  27123. ASN1_TIME_free(closeToTime);
  27124. ASN1_TIME_free(toTime);
  27125. ASN1_TIME_free(invalidTime);
  27126. #endif
  27127. return EXPECT_RESULT();
  27128. }
  27129. static int test_wolfSSL_ASN1_TIME_adj(void)
  27130. {
  27131. EXPECT_DECLS;
  27132. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  27133. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  27134. const int year = 365*24*60*60;
  27135. const int day = 24*60*60;
  27136. const int hour = 60*60;
  27137. const int mini = 60;
  27138. const byte asn_utc_time = ASN_UTC_TIME;
  27139. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27140. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  27141. #endif
  27142. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27143. WOLFSSL_ASN1_TIME* s = NULL;
  27144. int offset_day;
  27145. long offset_sec;
  27146. char date_str[CTC_DATE_SIZE + 1];
  27147. time_t t;
  27148. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  27149. /* UTC notation test */
  27150. /* 2000/2/15 20:30:00 */
  27151. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  27152. offset_day = 7;
  27153. offset_sec = 45 * mini;
  27154. /* offset_sec = -45 * min;*/
  27155. ExpectNotNull(asn_time =
  27156. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  27157. ExpectTrue(asn_time->type == asn_utc_time);
  27158. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27159. CTC_DATE_SIZE));
  27160. date_str[CTC_DATE_SIZE] = '\0';
  27161. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  27162. /* negative offset */
  27163. offset_sec = -45 * mini;
  27164. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  27165. ExpectNotNull(asn_time);
  27166. ExpectTrue(asn_time->type == asn_utc_time);
  27167. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27168. CTC_DATE_SIZE));
  27169. date_str[CTC_DATE_SIZE] = '\0';
  27170. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  27171. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27172. s = NULL;
  27173. XMEMSET(date_str, 0, sizeof(date_str));
  27174. /* Generalized time will overflow time_t if not long */
  27175. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27176. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  27177. DYNAMIC_TYPE_OPENSSL);
  27178. /* GeneralizedTime notation test */
  27179. /* 2055/03/01 09:00:00 */
  27180. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  27181. offset_day = 12;
  27182. offset_sec = 10 * mini;
  27183. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27184. offset_sec));
  27185. ExpectTrue(asn_time->type == asn_gen_time);
  27186. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27187. CTC_DATE_SIZE));
  27188. date_str[CTC_DATE_SIZE] = '\0';
  27189. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  27190. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27191. s = NULL;
  27192. XMEMSET(date_str, 0, sizeof(date_str));
  27193. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  27194. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  27195. s = NULL;
  27196. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  27197. offset_day = 7;
  27198. offset_sec = 45 * mini;
  27199. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27200. offset_sec));
  27201. ExpectTrue(asn_time->type == asn_utc_time);
  27202. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27203. CTC_DATE_SIZE));
  27204. date_str[CTC_DATE_SIZE] = '\0';
  27205. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27206. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27207. asn_time = NULL;
  27208. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  27209. offset_sec));
  27210. ExpectTrue(asn_time->type == asn_utc_time);
  27211. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27212. CTC_DATE_SIZE));
  27213. date_str[CTC_DATE_SIZE] = '\0';
  27214. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27215. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27216. #endif
  27217. return EXPECT_RESULT();
  27218. }
  27219. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  27220. {
  27221. EXPECT_DECLS;
  27222. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27223. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27224. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  27225. ASN1_TIME asnTime;
  27226. struct tm tm;
  27227. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  27228. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27229. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  27230. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  27231. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27232. ExpectIntEQ(tm.tm_sec, 15);
  27233. ExpectIntEQ(tm.tm_min, 15);
  27234. ExpectIntEQ(tm.tm_hour, 21);
  27235. ExpectIntEQ(tm.tm_mday, 22);
  27236. ExpectIntEQ(tm.tm_mon, 1);
  27237. ExpectIntEQ(tm.tm_year, 100);
  27238. ExpectIntEQ(tm.tm_isdst, 0);
  27239. #ifdef XMKTIME
  27240. ExpectIntEQ(tm.tm_wday, 2);
  27241. ExpectIntEQ(tm.tm_yday, 52);
  27242. #endif
  27243. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  27244. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27245. ExpectIntEQ(tm.tm_year, 50);
  27246. /* Get current time. */
  27247. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  27248. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  27249. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27250. /* 0 length. */
  27251. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27252. /* No type. */
  27253. asnTime.length = 1;
  27254. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27255. /* Not UTCTIME length. */
  27256. asnTime.type = V_ASN1_UTCTIME;
  27257. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27258. /* Not GENERALIZEDTIME length. */
  27259. asnTime.type = V_ASN1_GENERALIZEDTIME;
  27260. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27261. /* Not Zulu timezone. */
  27262. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  27263. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27264. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  27265. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27266. #ifdef XMKTIME
  27267. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  27268. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27269. ExpectIntEQ(tm.tm_sec, 47);
  27270. ExpectIntEQ(tm.tm_min, 22);
  27271. ExpectIntEQ(tm.tm_hour, 21);
  27272. ExpectIntEQ(tm.tm_mday, 12);
  27273. ExpectIntEQ(tm.tm_mon, 4);
  27274. ExpectIntEQ(tm.tm_year, 123);
  27275. ExpectIntEQ(tm.tm_wday, 5);
  27276. ExpectIntEQ(tm.tm_yday, 131);
  27277. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  27278. fields are zeroed out as expected. */
  27279. ExpectIntEQ(tm.tm_isdst, 0);
  27280. #endif
  27281. #endif
  27282. return EXPECT_RESULT();
  27283. }
  27284. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  27285. {
  27286. EXPECT_DECLS;
  27287. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27288. WOLFSSL_ASN1_TIME *t = NULL;
  27289. WOLFSSL_ASN1_TIME *out = NULL;
  27290. WOLFSSL_ASN1_TIME *gtime = NULL;
  27291. int tlen = 0;
  27292. unsigned char *data = NULL;
  27293. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27294. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  27295. /* type not set. */
  27296. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27297. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27298. t = NULL;
  27299. /* UTC Time test */
  27300. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27301. if (t != NULL) {
  27302. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  27303. t->type = ASN_UTC_TIME;
  27304. t->length = ASN_UTC_TIME_SIZE;
  27305. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27306. }
  27307. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  27308. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  27309. out = NULL;
  27310. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27311. wolfSSL_ASN1_TIME_free(gtime);
  27312. gtime = NULL;
  27313. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  27314. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27315. ExpectPtrEq(gtime, out);
  27316. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27317. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27318. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27319. /* Generalized Time test */
  27320. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27321. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27322. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27323. if (t != NULL) {
  27324. t->type = ASN_GENERALIZED_TIME;
  27325. t->length = ASN_GENERALIZED_TIME_SIZE;
  27326. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  27327. }
  27328. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  27329. ASN_GENERALIZED_TIME_SIZE);
  27330. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  27331. "20050727123456Z");
  27332. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27333. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27334. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27335. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27336. /* UTC Time to Generalized Time 1900's test */
  27337. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27338. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27339. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27340. if (t != NULL) {
  27341. t->type = ASN_UTC_TIME;
  27342. t->length = ASN_UTC_TIME_SIZE;
  27343. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  27344. }
  27345. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27346. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27347. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27348. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  27349. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27350. /* Null parameter test */
  27351. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27352. gtime = NULL;
  27353. out = NULL;
  27354. if (t != NULL) {
  27355. t->type = ASN_UTC_TIME;
  27356. t->length = ASN_UTC_TIME_SIZE;
  27357. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27358. }
  27359. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  27360. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27361. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27362. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27363. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27364. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27365. #endif
  27366. return EXPECT_RESULT();
  27367. }
  27368. static int test_wolfSSL_ASN1_TIME_print(void)
  27369. {
  27370. EXPECT_DECLS;
  27371. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  27372. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27373. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27374. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  27375. !defined(NO_ASN_TIME)
  27376. BIO* bio = NULL;
  27377. BIO* fixed = NULL;
  27378. X509* x509 = NULL;
  27379. const unsigned char* der = client_cert_der_2048;
  27380. ASN1_TIME* notAfter;
  27381. ASN1_TIME* notBefore;
  27382. unsigned char buf[25];
  27383. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27384. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27385. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27386. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27387. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  27388. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  27389. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  27390. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  27391. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  27392. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27393. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27394. /* Test BIO_write fails. */
  27395. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27396. /* Ensure there is 0 bytes available to write into. */
  27397. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  27398. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27399. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27400. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27401. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  27402. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27403. /* create a bad time and test results */
  27404. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  27405. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  27406. if (EXPECT_SUCCESS()) {
  27407. notAfter->data[8] = 0;
  27408. notAfter->data[3] = 0;
  27409. }
  27410. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  27411. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27412. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27413. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  27414. BIO_free(bio);
  27415. BIO_free(fixed);
  27416. X509_free(x509);
  27417. #endif
  27418. return EXPECT_RESULT();
  27419. }
  27420. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27421. {
  27422. EXPECT_DECLS;
  27423. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27424. BIO* bio = NULL;
  27425. ASN1_UTCTIME* utc = NULL;
  27426. unsigned char buf[25];
  27427. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27428. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27429. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  27430. /* Valid date */
  27431. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27432. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27433. DYNAMIC_TYPE_ASN1));
  27434. if (utc != NULL) {
  27435. utc->type = ASN_UTC_TIME;
  27436. utc->length = ASN_UTC_TIME_SIZE;
  27437. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27438. }
  27439. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  27440. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  27441. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  27442. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27443. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27444. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27445. XMEMSET(buf, 0, sizeof(buf));
  27446. BIO_free(bio);
  27447. bio = NULL;
  27448. /* Invalid format */
  27449. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27450. if (utc != NULL) {
  27451. utc->type = ASN_UTC_TIME;
  27452. utc->length = ASN_UTC_TIME_SIZE;
  27453. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27454. }
  27455. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27456. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27457. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27458. /* Invalid type */
  27459. if (utc != NULL) {
  27460. utc->type = ASN_GENERALIZED_TIME;
  27461. utc->length = ASN_GENERALIZED_TIME_SIZE;
  27462. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  27463. }
  27464. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27465. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27466. BIO_free(bio);
  27467. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27468. return EXPECT_RESULT();
  27469. }
  27470. static int test_wolfSSL_ASN1_TYPE(void)
  27471. {
  27472. EXPECT_DECLS;
  27473. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  27474. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  27475. WOLFSSL_ASN1_TYPE* t = NULL;
  27476. WOLFSSL_ASN1_OBJECT* obj = NULL;
  27477. #ifndef NO_ASN_TIME
  27478. WOLFSSL_ASN1_TIME* time = NULL;
  27479. #endif
  27480. WOLFSSL_ASN1_STRING* str = NULL;
  27481. unsigned char data[] = { 0x00 };
  27482. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  27483. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27484. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  27485. wolfSSL_ASN1_TYPE_free(t);
  27486. t = NULL;
  27487. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27488. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  27489. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  27490. wolfSSL_ASN1_TYPE_free(t);
  27491. t = NULL;
  27492. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27493. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27494. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  27495. wolfSSL_ASN1_TYPE_free(t);
  27496. t = NULL;
  27497. #ifndef NO_ASN_TIME
  27498. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27499. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27500. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  27501. wolfSSL_ASN1_TYPE_free(t);
  27502. t = NULL;
  27503. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27504. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27505. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  27506. wolfSSL_ASN1_TYPE_free(t);
  27507. t = NULL;
  27508. #endif
  27509. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27510. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27511. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  27512. wolfSSL_ASN1_TYPE_free(t);
  27513. t = NULL;
  27514. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27515. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27516. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  27517. wolfSSL_ASN1_TYPE_free(t);
  27518. t = NULL;
  27519. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27520. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27521. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  27522. wolfSSL_ASN1_TYPE_free(t);
  27523. t = NULL;
  27524. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27525. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27526. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  27527. wolfSSL_ASN1_TYPE_free(t);
  27528. t = NULL;
  27529. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27530. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27531. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  27532. wolfSSL_ASN1_TYPE_free(t);
  27533. t = NULL;
  27534. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27535. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27536. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  27537. wolfSSL_ASN1_TYPE_free(t);
  27538. t = NULL;
  27539. #endif
  27540. return EXPECT_RESULT();
  27541. }
  27542. /* Testing code used in dpp.c in hostap */
  27543. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27544. typedef struct {
  27545. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  27546. * as an OID identifying the curve */
  27547. X509_ALGOR *alg;
  27548. /* Compressed format public key per ANSI X9.63 */
  27549. ASN1_BIT_STRING *pub_key;
  27550. } DPP_BOOTSTRAPPING_KEY;
  27551. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  27552. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  27553. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  27554. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  27555. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  27556. typedef struct {
  27557. ASN1_INTEGER *integer;
  27558. } TEST_ASN1;
  27559. ASN1_SEQUENCE(TEST_ASN1) = {
  27560. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  27561. } ASN1_SEQUENCE_END(TEST_ASN1)
  27562. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  27563. typedef struct {
  27564. ASN1_OCTET_STRING *octet_string;
  27565. } TEST_FAIL_ASN1;
  27566. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  27567. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  27568. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  27569. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  27570. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  27571. #endif
  27572. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  27573. {
  27574. EXPECT_DECLS;
  27575. /* Testing code used in dpp.c in hostap */
  27576. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27577. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27578. EC_KEY *eckey = NULL;
  27579. EVP_PKEY *key = NULL;
  27580. size_t len;
  27581. unsigned char *der = NULL;
  27582. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  27583. const unsigned char *in = ecc_clikey_der_256;
  27584. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  27585. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  27586. const EC_GROUP *group = NULL;
  27587. const EC_POINT *point = NULL;
  27588. int nid;
  27589. TEST_ASN1 *test_asn1 = NULL;
  27590. TEST_FAIL_ASN1 test_fail_asn1;
  27591. const unsigned char badObjDer[] = { 0x06, 0x00 };
  27592. const unsigned char goodObjDer[] = {
  27593. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  27594. };
  27595. WOLFSSL_ASN1_ITEM emptyTemplate;
  27596. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  27597. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27598. der = NULL;
  27599. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  27600. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  27601. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27602. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  27603. (long)sizeof_ecc_clikey_der_256));
  27604. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  27605. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  27606. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  27607. nid = EC_GROUP_get_curve_name(group);
  27608. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  27609. group_obj = OBJ_nid2obj(nid);
  27610. if ((ec_obj != NULL) && (group_obj != NULL)) {
  27611. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  27612. group_obj), 1);
  27613. if (EXPECT_SUCCESS()) {
  27614. ec_obj = NULL;
  27615. group_obj = NULL;
  27616. }
  27617. }
  27618. wolfSSL_ASN1_OBJECT_free(group_obj);
  27619. wolfSSL_ASN1_OBJECT_free(ec_obj);
  27620. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  27621. #ifdef HAVE_COMP_KEY
  27622. ExpectIntGT((len = EC_POINT_point2oct(
  27623. group, point, POINT_CONVERSION_COMPRESSED,
  27624. NULL, 0, NULL)), 0);
  27625. #else
  27626. ExpectIntGT((len = EC_POINT_point2oct(
  27627. group, point, POINT_CONVERSION_UNCOMPRESSED,
  27628. NULL, 0, NULL)), 0);
  27629. #endif
  27630. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  27631. #ifdef HAVE_COMP_KEY
  27632. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27633. der, len-1, NULL), 0);
  27634. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27635. der, len, NULL), len);
  27636. #else
  27637. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27638. der, len-1, NULL), 0);
  27639. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27640. der, len, NULL), len);
  27641. #endif
  27642. if (EXPECT_SUCCESS()) {
  27643. bootstrap->pub_key->data = der;
  27644. bootstrap->pub_key->length = (int)len;
  27645. /* Not actually used */
  27646. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27647. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27648. }
  27649. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  27650. der = NULL;
  27651. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27652. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27653. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27654. EVP_PKEY_free(key);
  27655. EC_KEY_free(eckey);
  27656. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27657. bootstrap = NULL;
  27658. DPP_BOOTSTRAPPING_KEY_free(NULL);
  27659. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  27660. * a NULL and an empty BIT_STRING. */
  27661. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27662. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  27663. if (EXPECT_SUCCESS()) {
  27664. bootstrap->alg->algorithm->obj = badObjDer;
  27665. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  27666. }
  27667. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  27668. if (EXPECT_SUCCESS()) {
  27669. bootstrap->alg->parameter->type = V_ASN1_NULL;
  27670. bootstrap->alg->parameter->value.ptr = NULL;
  27671. bootstrap->pub_key->data = NULL;
  27672. bootstrap->pub_key->length = 0;
  27673. /* Not actually used */
  27674. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27675. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27676. }
  27677. /* Encode with bad OBJECT_ID. */
  27678. der = NULL;
  27679. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27680. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  27681. if (EXPECT_SUCCESS()) {
  27682. bootstrap->alg->algorithm->obj = goodObjDer;
  27683. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  27684. bootstrap->alg->algorithm->grp = 2;
  27685. }
  27686. der = NULL;
  27687. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  27688. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  27689. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27690. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27691. /* Test integer */
  27692. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  27693. der = NULL;
  27694. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  27695. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  27696. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27697. TEST_ASN1_free(test_asn1);
  27698. /* Test integer cases. */
  27699. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27700. TEST_ASN1_free(NULL);
  27701. /* Test error cases. */
  27702. ExpectNull(TEST_FAIL_ASN1_new());
  27703. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27704. TEST_FAIL_ASN1_free(NULL);
  27705. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  27706. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  27707. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27708. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27709. return EXPECT_RESULT();
  27710. }
  27711. static int test_wolfSSL_lhash(void)
  27712. {
  27713. EXPECT_DECLS;
  27714. #ifdef OPENSSL_ALL
  27715. const char testStr[] = "Like a true nature's child\n"
  27716. "We were born\n"
  27717. "Born to be wild";
  27718. #ifdef NO_SHA
  27719. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  27720. #else
  27721. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  27722. #endif
  27723. #endif
  27724. return EXPECT_RESULT();
  27725. }
  27726. static int test_wolfSSL_X509_NAME(void)
  27727. {
  27728. EXPECT_DECLS;
  27729. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  27730. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27731. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  27732. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  27733. defined(OPENSSL_EXTRA))
  27734. X509* x509 = NULL;
  27735. const unsigned char* c;
  27736. unsigned char buf[4096];
  27737. int bytes;
  27738. XFILE f = XBADFILE;
  27739. const X509_NAME* a = NULL;
  27740. const X509_NAME* b = NULL;
  27741. X509_NAME* d2i_name = NULL;
  27742. int sz = 0;
  27743. unsigned char* tmp;
  27744. char file[] = "./certs/ca-cert.der";
  27745. #ifndef OPENSSL_EXTRA_X509_SMALL
  27746. byte empty[] = { /* CN=empty emailAddress= */
  27747. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  27748. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  27749. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  27750. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27751. 0x01, 0x16, 0x00
  27752. };
  27753. #endif
  27754. #ifndef OPENSSL_EXTRA_X509_SMALL
  27755. /* test compile of deprecated function, returns 0 */
  27756. ExpectIntEQ(CRYPTO_thread_id(), 0);
  27757. #endif
  27758. ExpectNotNull(a = X509_NAME_new());
  27759. X509_NAME_free((X509_NAME*)a);
  27760. a = NULL;
  27761. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  27762. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  27763. if (f != XBADFILE)
  27764. XFCLOSE(f);
  27765. c = buf;
  27766. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  27767. /* test cmp function */
  27768. ExpectNotNull(a = X509_get_issuer_name(x509));
  27769. ExpectNotNull(b = X509_get_subject_name(x509));
  27770. #ifndef OPENSSL_EXTRA_X509_SMALL
  27771. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  27772. #endif
  27773. tmp = buf;
  27774. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  27775. if (sz > 0 && tmp == buf) {
  27776. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  27777. __LINE__);
  27778. fprintf(stderr, " Expected pointer to be incremented\n");
  27779. abort();
  27780. }
  27781. #ifndef OPENSSL_EXTRA_X509_SMALL
  27782. tmp = buf;
  27783. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27784. #endif
  27785. /* if output parameter is NULL, should still return required size. */
  27786. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  27787. /* retry but with the function creating a buffer */
  27788. tmp = NULL;
  27789. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27790. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27791. tmp = NULL;
  27792. #ifdef WOLFSSL_CERT_NAME_ALL
  27793. /* test for givenName and name */
  27794. {
  27795. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  27796. const byte gName[] = "test-given-name";
  27797. const byte name[] = "test-name";
  27798. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27799. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  27800. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27801. 1);
  27802. wolfSSL_X509_NAME_ENTRY_free(entry);
  27803. entry = NULL;
  27804. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27805. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  27806. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27807. 1);
  27808. wolfSSL_X509_NAME_ENTRY_free(entry);
  27809. tmp = NULL;
  27810. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27811. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27812. }
  27813. #endif
  27814. b = NULL;
  27815. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  27816. #ifndef OPENSSL_EXTRA_X509_SMALL
  27817. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  27818. #endif
  27819. X509_NAME_free((X509_NAME*)b);
  27820. X509_NAME_free(d2i_name);
  27821. d2i_name = NULL;
  27822. X509_free(x509);
  27823. #ifndef OPENSSL_EXTRA_X509_SMALL
  27824. /* test with an empty domain component */
  27825. tmp = empty;
  27826. sz = sizeof(empty);
  27827. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27828. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  27829. /* size of empty emailAddress will be 0 */
  27830. tmp = buf;
  27831. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  27832. (char*)tmp, sizeof(buf)), 0);
  27833. /* should contain no organization name */
  27834. tmp = buf;
  27835. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  27836. (char*)tmp, sizeof(buf)), -1);
  27837. X509_NAME_free(d2i_name);
  27838. #endif
  27839. #endif
  27840. return EXPECT_RESULT();
  27841. }
  27842. static int test_wolfSSL_X509_NAME_hash(void)
  27843. {
  27844. EXPECT_DECLS;
  27845. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27846. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  27847. BIO* bio = NULL;
  27848. X509* x509 = NULL;
  27849. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27850. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27851. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27852. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  27853. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  27854. X509_free(x509);
  27855. BIO_free(bio);
  27856. #endif
  27857. return EXPECT_RESULT();
  27858. }
  27859. static int test_wolfSSL_X509_NAME_print_ex(void)
  27860. {
  27861. EXPECT_DECLS;
  27862. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  27863. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  27864. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  27865. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  27866. !defined(NO_BIO) && !defined(NO_RSA)
  27867. int memSz;
  27868. byte* mem = NULL;
  27869. BIO* bio = NULL;
  27870. BIO* membio = NULL;
  27871. X509* x509 = NULL;
  27872. X509_NAME* name = NULL;
  27873. const char* expNormal = "C=US, CN=wolfssl.com";
  27874. const char* expReverse = "CN=wolfssl.com, C=US";
  27875. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  27876. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  27877. const char* expRFC5523 =
  27878. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  27879. /* Test with real cert (svrCertFile) first */
  27880. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27881. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27882. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27883. ExpectNotNull(name = X509_get_subject_name(x509));
  27884. /* Test without flags */
  27885. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27886. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27887. BIO_free(membio);
  27888. membio = NULL;
  27889. /* Test flag: XN_FLAG_RFC2253 */
  27890. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27891. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27892. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27893. BIO_free(membio);
  27894. membio = NULL;
  27895. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  27896. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27897. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27898. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27899. BIO_free(membio);
  27900. membio = NULL;
  27901. X509_free(x509);
  27902. BIO_free(bio);
  27903. name = NULL;
  27904. /* Test normal case without escaped characters */
  27905. {
  27906. /* Create name: "/C=US/CN=wolfssl.com" */
  27907. ExpectNotNull(name = X509_NAME_new());
  27908. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27909. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  27910. WOLFSSL_SUCCESS);
  27911. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27912. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  27913. WOLFSSL_SUCCESS);
  27914. /* Test without flags */
  27915. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27916. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27917. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27918. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  27919. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  27920. BIO_free(membio);
  27921. membio = NULL;
  27922. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  27923. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27924. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27925. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27926. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27927. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27928. BIO_free(membio);
  27929. membio = NULL;
  27930. /* Test flags: XN_FLAG_DN_REV - reversed */
  27931. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27932. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27933. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27934. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27935. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27936. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  27937. BIO_free(membio);
  27938. membio = NULL;
  27939. X509_NAME_free(name);
  27940. name = NULL;
  27941. }
  27942. /* Test RFC2253 characters are escaped with backslashes */
  27943. {
  27944. ExpectNotNull(name = X509_NAME_new());
  27945. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27946. /* space at beginning and end, and: ,+"\ */
  27947. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  27948. WOLFSSL_SUCCESS);
  27949. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27950. /* # at beginning, and: <>;*/
  27951. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  27952. WOLFSSL_SUCCESS);
  27953. /* Test without flags */
  27954. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27955. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27956. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27957. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  27958. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  27959. XSTRLEN(expNotEscaped)), 0);
  27960. BIO_free(membio);
  27961. membio = NULL;
  27962. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  27963. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27964. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27965. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27966. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27967. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  27968. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  27969. BIO_free(membio);
  27970. membio = NULL;
  27971. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  27972. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27973. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27974. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27975. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27976. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  27977. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  27978. XSTRLEN(expNotEscapedRev)), 0);
  27979. BIO_free(membio);
  27980. X509_NAME_free(name);
  27981. }
  27982. #endif
  27983. return EXPECT_RESULT();
  27984. }
  27985. #ifndef NO_BIO
  27986. static int test_wolfSSL_X509_INFO_multiple_info(void)
  27987. {
  27988. EXPECT_DECLS;
  27989. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  27990. STACK_OF(X509_INFO) *info_stack = NULL;
  27991. X509_INFO *info = NULL;
  27992. int len;
  27993. int i;
  27994. const char* files[] = {
  27995. cliCertFile,
  27996. cliKeyFile,
  27997. /* This needs to be the order as svrCertFile contains the
  27998. * intermediate cert as well. */
  27999. svrKeyFile,
  28000. svrCertFile,
  28001. NULL,
  28002. };
  28003. const char** curFile;
  28004. BIO *fileBIO = NULL;
  28005. BIO *concatBIO = NULL;
  28006. byte tmp[FOURK_BUF];
  28007. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  28008. * to group objects together. */
  28009. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  28010. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  28011. int fileLen;
  28012. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  28013. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  28014. if (EXPECT_SUCCESS()) {
  28015. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  28016. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  28017. fileLen -= len;
  28018. if (EXPECT_FAIL())
  28019. break;
  28020. }
  28021. /* Make sure we read the entire file */
  28022. ExpectIntEQ(fileLen, 0);
  28023. }
  28024. BIO_free(fileBIO);
  28025. fileBIO = NULL;
  28026. }
  28027. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  28028. NULL));
  28029. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  28030. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28031. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28032. ExpectNotNull(info->x509);
  28033. ExpectNull(info->crl);
  28034. if (i != 0) {
  28035. ExpectNotNull(info->x_pkey);
  28036. ExpectIntEQ(X509_check_private_key(info->x509,
  28037. info->x_pkey->dec_pkey), 1);
  28038. }
  28039. else {
  28040. ExpectNull(info->x_pkey);
  28041. }
  28042. }
  28043. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28044. BIO_free(concatBIO);
  28045. #endif
  28046. return EXPECT_RESULT();
  28047. }
  28048. #endif
  28049. #ifndef NO_BIO
  28050. static int test_wolfSSL_X509_INFO(void)
  28051. {
  28052. EXPECT_DECLS;
  28053. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28054. STACK_OF(X509_INFO) *info_stack = NULL;
  28055. X509_INFO *info = NULL;
  28056. BIO *cert = NULL;
  28057. int i;
  28058. /* PEM in hex format to avoid null terminator */
  28059. byte data[] = {
  28060. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  28061. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  28062. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  28063. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  28064. 0x2d, 0x2d, 0x2d
  28065. };
  28066. /* PEM in hex format to avoid null terminator */
  28067. byte data2[] = {
  28068. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  28069. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  28070. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  28071. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  28072. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  28073. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  28074. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  28075. };
  28076. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28077. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28078. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28079. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28080. ExpectNotNull(info->x509);
  28081. ExpectNull(info->crl);
  28082. ExpectNull(info->x_pkey);
  28083. }
  28084. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28085. info_stack = NULL;
  28086. BIO_free(cert);
  28087. cert = NULL;
  28088. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28089. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28090. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28091. info_stack = NULL;
  28092. BIO_free(cert);
  28093. cert = NULL;
  28094. /* This case should fail due to invalid input. */
  28095. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28096. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  28097. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28098. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28099. info_stack = NULL;
  28100. BIO_free(cert);
  28101. cert = NULL;
  28102. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28103. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  28104. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28105. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28106. BIO_free(cert);
  28107. #endif
  28108. return EXPECT_RESULT();
  28109. }
  28110. #endif
  28111. static int test_wolfSSL_X509_subject_name_hash(void)
  28112. {
  28113. EXPECT_DECLS;
  28114. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28115. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28116. X509* x509 = NULL;
  28117. X509_NAME* subjectName = NULL;
  28118. unsigned long ret1 = 0;
  28119. unsigned long ret2 = 0;
  28120. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28121. SSL_FILETYPE_PEM));
  28122. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  28123. /* These two
  28124. * - X509_subject_name_hash(x509)
  28125. * - X509_NAME_hash(X509_get_subject_name(x509))
  28126. * should give the same hash, if !defined(NO_SHA) is true. */
  28127. ret1 = X509_subject_name_hash(x509);
  28128. ExpectIntNE(ret1, 0);
  28129. #if !defined(NO_SHA)
  28130. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  28131. ExpectIntNE(ret2, 0);
  28132. ExpectIntEQ(ret1, ret2);
  28133. #else
  28134. (void) ret2;
  28135. #endif
  28136. X509_free(x509);
  28137. #endif
  28138. return EXPECT_RESULT();
  28139. }
  28140. static int test_wolfSSL_X509_issuer_name_hash(void)
  28141. {
  28142. EXPECT_DECLS;
  28143. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28144. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28145. X509* x509 = NULL;
  28146. X509_NAME* issuertName = NULL;
  28147. unsigned long ret1 = 0;
  28148. unsigned long ret2 = 0;
  28149. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28150. SSL_FILETYPE_PEM));
  28151. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  28152. /* These two
  28153. * - X509_issuer_name_hash(x509)
  28154. * - X509_NAME_hash(X509_get_issuer_name(x509))
  28155. * should give the same hash, if !defined(NO_SHA) is true. */
  28156. ret1 = X509_issuer_name_hash(x509);
  28157. ExpectIntNE(ret1, 0);
  28158. #if !defined(NO_SHA)
  28159. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  28160. ExpectIntNE(ret2, 0);
  28161. ExpectIntEQ(ret1, ret2);
  28162. #else
  28163. (void) ret2;
  28164. #endif
  28165. X509_free(x509);
  28166. #endif
  28167. return EXPECT_RESULT();
  28168. }
  28169. static int test_wolfSSL_X509_check_host(void)
  28170. {
  28171. EXPECT_DECLS;
  28172. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28173. && !defined(NO_SHA) && !defined(NO_RSA)
  28174. X509* x509 = NULL;
  28175. const char altName[] = "example.com";
  28176. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28177. SSL_FILETYPE_PEM));
  28178. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  28179. WOLFSSL_SUCCESS);
  28180. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  28181. WOLFSSL_FAILURE);
  28182. X509_free(x509);
  28183. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  28184. WOLFSSL_FAILURE);
  28185. #endif
  28186. return EXPECT_RESULT();
  28187. }
  28188. static int test_wolfSSL_X509_check_email(void)
  28189. {
  28190. EXPECT_DECLS;
  28191. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  28192. X509* x509 = NULL;
  28193. const char goodEmail[] = "info@wolfssl.com";
  28194. const char badEmail[] = "disinfo@wolfssl.com";
  28195. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28196. SSL_FILETYPE_PEM));
  28197. /* Should fail on non-matching email address */
  28198. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  28199. WOLFSSL_FAILURE);
  28200. /* Should succeed on matching email address */
  28201. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  28202. WOLFSSL_SUCCESS);
  28203. /* Should compute length internally when not provided */
  28204. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  28205. WOLFSSL_SUCCESS);
  28206. /* Should fail when email address is NULL */
  28207. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  28208. WOLFSSL_FAILURE);
  28209. X509_free(x509);
  28210. /* Should fail when x509 is NULL */
  28211. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  28212. WOLFSSL_FAILURE);
  28213. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  28214. return EXPECT_RESULT();
  28215. }
  28216. static int test_wc_PemToDer(void)
  28217. {
  28218. EXPECT_DECLS;
  28219. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28220. int ret;
  28221. DerBuffer* pDer = NULL;
  28222. const char* ca_cert = "./certs/server-cert.pem";
  28223. byte* cert_buf = NULL;
  28224. size_t cert_sz = 0;
  28225. int eccKey = 0;
  28226. EncryptedInfo info;
  28227. XMEMSET(&info, 0, sizeof(info));
  28228. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28229. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  28230. &info, &eccKey), 0);
  28231. wc_FreeDer(&pDer);
  28232. pDer = NULL;
  28233. if (cert_buf != NULL) {
  28234. free(cert_buf);
  28235. cert_buf = NULL;
  28236. }
  28237. #ifdef HAVE_ECC
  28238. {
  28239. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  28240. byte key_buf[256] = {0};
  28241. /* Test fail of loading a key with cert type */
  28242. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  28243. key_buf[0] = '\n';
  28244. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  28245. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  28246. &pDer, NULL, &info, &eccKey)), 0);
  28247. #ifdef OPENSSL_EXTRA
  28248. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  28249. &pDer, NULL, &info, &eccKey)), 0);
  28250. #endif
  28251. wc_FreeDer(&pDer);
  28252. if (cert_buf != NULL)
  28253. free(cert_buf);
  28254. }
  28255. #endif
  28256. #endif
  28257. return EXPECT_RESULT();
  28258. }
  28259. static int test_wc_AllocDer(void)
  28260. {
  28261. EXPECT_DECLS;
  28262. #if !defined(NO_CERTS)
  28263. DerBuffer* pDer = NULL;
  28264. word32 testSize = 1024;
  28265. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  28266. BAD_FUNC_ARG);
  28267. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  28268. ExpectNotNull(pDer);
  28269. wc_FreeDer(&pDer);
  28270. #endif
  28271. return EXPECT_RESULT();
  28272. }
  28273. static int test_wc_CertPemToDer(void)
  28274. {
  28275. EXPECT_DECLS;
  28276. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28277. const char* ca_cert = "./certs/ca-cert.pem";
  28278. byte* cert_buf = NULL;
  28279. size_t cert_sz = 0;
  28280. size_t cert_dersz = 0;
  28281. byte* cert_der = NULL;
  28282. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28283. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28284. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28285. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28286. (int)cert_dersz, CERT_TYPE), 0);
  28287. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  28288. BAD_FUNC_ARG);
  28289. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  28290. BAD_FUNC_ARG);
  28291. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  28292. BAD_FUNC_ARG);
  28293. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  28294. CERT_TYPE), BAD_FUNC_ARG);
  28295. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  28296. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28297. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  28298. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28299. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  28300. CERT_TYPE), BAD_FUNC_ARG);
  28301. if (cert_der != NULL)
  28302. free(cert_der);
  28303. if (cert_buf != NULL)
  28304. free(cert_buf);
  28305. #endif
  28306. return EXPECT_RESULT();
  28307. }
  28308. static int test_wc_KeyPemToDer(void)
  28309. {
  28310. EXPECT_DECLS;
  28311. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28312. int ret;
  28313. const byte cert_buf[] = \
  28314. "-----BEGIN PRIVATE KEY-----\n"
  28315. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  28316. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  28317. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  28318. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  28319. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  28320. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  28321. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  28322. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  28323. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  28324. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  28325. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  28326. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  28327. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  28328. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  28329. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  28330. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  28331. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  28332. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  28333. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  28334. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  28335. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  28336. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  28337. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  28338. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  28339. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  28340. "AQue5o6oVfhgLiJzMdo/77gw\n"
  28341. "-----END PRIVATE KEY-----\n";
  28342. const int cert_sz = sizeof(cert_buf);
  28343. const char cert_pw[] = "password";
  28344. int cert_dersz = 0;
  28345. byte* cert_der = NULL;
  28346. /* Bad arg: Cert buffer is NULL */
  28347. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  28348. BAD_FUNC_ARG);
  28349. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  28350. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  28351. BAD_FUNC_ARG);
  28352. /* Test normal operation */
  28353. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28354. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28355. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28356. cert_pw), 0);
  28357. ExpectIntLE(ret, cert_sz);
  28358. if (cert_der != NULL) {
  28359. free(cert_der);
  28360. cert_der = NULL;
  28361. }
  28362. /* Test NULL for DER buffer to return needed DER buffer size */
  28363. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  28364. ExpectIntLE(ret, cert_sz);
  28365. if (EXPECT_SUCCESS())
  28366. cert_dersz = ret;
  28367. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28368. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28369. cert_pw), 0);
  28370. ExpectIntLE(ret, cert_sz);
  28371. if (cert_der != NULL)
  28372. free(cert_der);
  28373. #endif
  28374. return EXPECT_RESULT();
  28375. }
  28376. static int test_wc_PubKeyPemToDer(void)
  28377. {
  28378. EXPECT_DECLS;
  28379. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  28380. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28381. int ret = 0;
  28382. const char* key = "./certs/ecc-client-keyPub.pem";
  28383. byte* cert_buf = NULL;
  28384. size_t cert_sz = 0, cert_dersz = 0;
  28385. byte* cert_der = NULL;
  28386. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  28387. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  28388. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  28389. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28390. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28391. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28392. (int)cert_dersz), 0);
  28393. if (cert_der != NULL) {
  28394. free(cert_der);
  28395. cert_der = NULL;
  28396. }
  28397. /* Test NULL for DER buffer to return needed DER buffer size */
  28398. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  28399. ExpectIntLE(ret, cert_sz);
  28400. cert_dersz = ret;
  28401. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28402. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28403. (int)cert_dersz), 0);
  28404. if (cert_der != NULL) {
  28405. free(cert_der);
  28406. }
  28407. if (cert_buf != NULL) {
  28408. free(cert_buf);
  28409. }
  28410. #endif
  28411. return EXPECT_RESULT();
  28412. }
  28413. static int test_wc_PemPubKeyToDer(void)
  28414. {
  28415. EXPECT_DECLS;
  28416. #if !defined(NO_FILESYSTEM) && \
  28417. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28418. const char* key = "./certs/ecc-client-keyPub.pem";
  28419. size_t cert_dersz = 1024;
  28420. byte* cert_der = NULL;
  28421. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  28422. BAD_FUNC_ARG);
  28423. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28424. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  28425. if (cert_der != NULL) {
  28426. free(cert_der);
  28427. }
  28428. #endif
  28429. return EXPECT_RESULT();
  28430. }
  28431. static int test_wc_GetPubKeyDerFromCert(void)
  28432. {
  28433. EXPECT_DECLS;
  28434. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28435. int ret;
  28436. word32 idx = 0;
  28437. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28438. word32 keyDerSz = (word32)sizeof(keyDer);
  28439. DecodedCert decoded;
  28440. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28441. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  28442. word32 certBufSz = sizeof(certBuf);
  28443. #endif
  28444. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  28445. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28446. XFILE fp = XBADFILE;
  28447. #endif
  28448. #ifndef NO_RSA
  28449. RsaKey rsaKey;
  28450. #if defined(USE_CERT_BUFFERS_2048)
  28451. byte* rsaCertDer = (byte*)client_cert_der_2048;
  28452. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  28453. #elif defined(USE_CERT_BUFFERS_1024)
  28454. byte* rsaCertDer = (byte*)client_cert_der_1024;
  28455. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  28456. #else
  28457. unsigned char rsaCertDer[TWOK_BUF];
  28458. word32 rsaCertDerSz;
  28459. #endif
  28460. #endif
  28461. #ifdef HAVE_ECC
  28462. ecc_key eccKey;
  28463. #if defined(USE_CERT_BUFFERS_256)
  28464. byte* eccCert = (byte*)cliecc_cert_der_256;
  28465. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  28466. #else
  28467. unsigned char eccCert[ONEK_BUF];
  28468. word32 eccCertSz;
  28469. XFILE fp2 = XBADFILE;
  28470. #endif
  28471. #endif
  28472. #ifndef NO_RSA
  28473. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  28474. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  28475. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  28476. fp), 0);
  28477. if (fp != XBADFILE) {
  28478. XFCLOSE(fp);
  28479. fp = XBADFILE;
  28480. }
  28481. #endif
  28482. /* good test case - RSA DER cert */
  28483. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  28484. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28485. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28486. ExpectIntGT(keyDerSz, 0);
  28487. /* sanity check, verify we can import DER public key */
  28488. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28489. ExpectIntEQ(ret, 0);
  28490. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28491. if (ret == 0) {
  28492. wc_FreeRsaKey(&rsaKey);
  28493. }
  28494. /* test LENGTH_ONLY_E case */
  28495. keyDerSz = 0;
  28496. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28497. LENGTH_ONLY_E);
  28498. ExpectIntGT(keyDerSz, 0);
  28499. /* bad args: DecodedCert NULL */
  28500. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  28501. /* bad args: output key buff size */
  28502. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  28503. /* bad args: zero size output key buffer */
  28504. keyDerSz = 0;
  28505. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28506. BAD_FUNC_ARG);
  28507. wc_FreeDecodedCert(&decoded);
  28508. /* Certificate Request Tests */
  28509. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28510. {
  28511. XMEMSET(certBuf, 0, sizeof(certBuf));
  28512. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  28513. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  28514. if (fp != XBADFILE) {
  28515. XFCLOSE(fp);
  28516. }
  28517. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  28518. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  28519. /* good test case - RSA DER certificate request */
  28520. keyDerSz = sizeof(keyDer);
  28521. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28522. 0);
  28523. ExpectIntGT(keyDerSz, 0);
  28524. /* sanity check, verify we can import DER public key */
  28525. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28526. ExpectIntEQ(ret, 0);
  28527. idx = 0;
  28528. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28529. if (ret == 0) {
  28530. wc_FreeRsaKey(&rsaKey);
  28531. }
  28532. wc_FreeDecodedCert(&decoded);
  28533. }
  28534. #endif /* WOLFSSL_CERT_REQ */
  28535. #endif /* NO_RSA */
  28536. #ifdef HAVE_ECC
  28537. #ifndef USE_CERT_BUFFERS_256
  28538. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  28539. XBADFILE);
  28540. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  28541. if (fp2 != XBADFILE) {
  28542. XFCLOSE(fp2);
  28543. }
  28544. #endif
  28545. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  28546. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28547. /* good test case - ECC */
  28548. XMEMSET(keyDer, 0, sizeof(keyDer));
  28549. keyDerSz = sizeof(keyDer);
  28550. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28551. ExpectIntGT(keyDerSz, 0);
  28552. /* sanity check, verify we can import DER public key */
  28553. ret = wc_ecc_init(&eccKey);
  28554. ExpectIntEQ(ret, 0);
  28555. idx = 0; /* reset idx to 0, used above in RSA case */
  28556. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  28557. if (ret == 0) {
  28558. wc_ecc_free(&eccKey);
  28559. }
  28560. /* test LENGTH_ONLY_E case */
  28561. keyDerSz = 0;
  28562. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28563. LENGTH_ONLY_E);
  28564. ExpectIntGT(keyDerSz, 0);
  28565. wc_FreeDecodedCert(&decoded);
  28566. #endif
  28567. #endif /* !NO_RSA || HAVE_ECC */
  28568. return EXPECT_RESULT();
  28569. }
  28570. static int test_wc_CheckCertSigPubKey(void)
  28571. {
  28572. EXPECT_DECLS;
  28573. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28574. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  28575. int ret;
  28576. const char* ca_cert = "./certs/ca-cert.pem";
  28577. byte* cert_buf = NULL;
  28578. size_t cert_sz = 0;
  28579. byte* cert_der = NULL;
  28580. word32 cert_dersz = 0;
  28581. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28582. word32 keyDerSz = (word32)sizeof(keyDer);
  28583. DecodedCert decoded;
  28584. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28585. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  28586. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28587. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28588. (int)cert_dersz, CERT_TYPE), 0);
  28589. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  28590. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28591. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28592. ExpectIntGT(keyDerSz, 0);
  28593. /* Good test case. */
  28594. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28595. keyDerSz, RSAk), 0);
  28596. /* No certificate. */
  28597. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  28598. ECDSAk), BAD_FUNC_ARG);
  28599. /* Bad cert size. */
  28600. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  28601. RSAk), 0);
  28602. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  28603. /* No public key. */
  28604. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  28605. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  28606. /* Bad public key size. */
  28607. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  28608. RSAk), BAD_FUNC_ARG);
  28609. /* Wrong aglo. */
  28610. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28611. keyDerSz, ECDSAk), ASN_PARSE_E);
  28612. wc_FreeDecodedCert(&decoded);
  28613. if (cert_der != NULL)
  28614. free(cert_der);
  28615. if (cert_buf != NULL)
  28616. free(cert_buf);
  28617. #endif
  28618. return EXPECT_RESULT();
  28619. }
  28620. static int test_wolfSSL_certs(void)
  28621. {
  28622. EXPECT_DECLS;
  28623. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28624. !defined(NO_RSA)
  28625. X509* x509ext = NULL;
  28626. #ifdef OPENSSL_ALL
  28627. X509* x509 = NULL;
  28628. WOLFSSL_X509_EXTENSION* ext = NULL;
  28629. ASN1_OBJECT* obj = NULL;
  28630. #endif
  28631. WOLFSSL* ssl = NULL;
  28632. WOLFSSL_CTX* ctx = NULL;
  28633. STACK_OF(ASN1_OBJECT)* sk = NULL;
  28634. ASN1_STRING* asn1_str = NULL;
  28635. AUTHORITY_KEYID* akey = NULL;
  28636. BASIC_CONSTRAINTS* bc = NULL;
  28637. int crit;
  28638. #ifndef NO_WOLFSSL_SERVER
  28639. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  28640. #else
  28641. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  28642. #endif
  28643. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28644. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28645. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28646. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28647. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  28648. #endif
  28649. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28650. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28651. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  28652. #endif
  28653. ExpectNotNull(ssl = SSL_new(ctx));
  28654. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28655. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28656. #endif
  28657. #ifdef HAVE_PK_CALLBACKS
  28658. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  28659. #endif /* HAVE_PK_CALLBACKS */
  28660. /* create and use x509 */
  28661. #ifdef OPENSSL_ALL
  28662. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28663. WOLFSSL_FILETYPE_PEM));
  28664. #endif
  28665. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  28666. WOLFSSL_FILETYPE_PEM));
  28667. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  28668. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28669. /* with loading in a new cert the check on private key should now fail */
  28670. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28671. #endif
  28672. #if defined(USE_CERT_BUFFERS_2048)
  28673. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  28674. (unsigned char*)server_cert_der_2048,
  28675. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  28676. #endif
  28677. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28678. /************* Get Digest of Certificate ******************/
  28679. {
  28680. byte digest[64]; /* max digest size */
  28681. word32 digestSz;
  28682. XMEMSET(digest, 0, sizeof(digest));
  28683. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  28684. WOLFSSL_SUCCESS);
  28685. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  28686. WOLFSSL_SUCCESS);
  28687. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  28688. WOLFSSL_FAILURE);
  28689. }
  28690. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  28691. /* test and checkout X509 extensions */
  28692. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  28693. NID_basic_constraints, &crit, NULL));
  28694. ExpectIntEQ(crit, 0);
  28695. #ifdef OPENSSL_ALL
  28696. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  28697. X509_EXTENSION_free(ext);
  28698. ext = NULL;
  28699. ExpectNotNull(ext = X509_EXTENSION_new());
  28700. X509_EXTENSION_set_critical(ext, 1);
  28701. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  28702. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  28703. ASN1_OBJECT_free(obj);
  28704. obj = NULL;
  28705. X509_EXTENSION_free(ext);
  28706. ext = NULL;
  28707. ExpectNotNull(ext = X509_EXTENSION_new());
  28708. X509_EXTENSION_set_critical(ext, 0);
  28709. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  28710. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  28711. NULL);
  28712. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  28713. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  28714. * and X509_get_ext_d2i has created new */
  28715. asn1_str = NULL;
  28716. X509_EXTENSION_free(ext);
  28717. ext = NULL;
  28718. #endif
  28719. BASIC_CONSTRAINTS_free(bc);
  28720. bc = NULL;
  28721. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  28722. NID_key_usage, &crit, NULL));
  28723. ExpectIntEQ(crit, 1);
  28724. ExpectIntEQ(asn1_str->type, NID_key_usage);
  28725. #ifdef OPENSSL_ALL
  28726. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  28727. X509_EXTENSION_free(ext);
  28728. ext = NULL;
  28729. #endif
  28730. ASN1_STRING_free(asn1_str);
  28731. asn1_str = NULL;
  28732. #ifdef OPENSSL_ALL
  28733. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  28734. NID_ext_key_usage, &crit, NULL));
  28735. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  28736. X509_EXTENSION_free(ext);
  28737. ext = NULL;
  28738. EXTENDED_KEY_USAGE_free(sk);
  28739. sk = NULL;
  28740. #else
  28741. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  28742. &crit, NULL);
  28743. ExpectNull(sk);
  28744. #endif
  28745. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  28746. NID_authority_key_identifier, &crit, NULL));
  28747. #ifdef OPENSSL_ALL
  28748. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  28749. akey));
  28750. X509_EXTENSION_free(ext);
  28751. ext = NULL;
  28752. #endif
  28753. wolfSSL_AUTHORITY_KEYID_free(akey);
  28754. akey = NULL;
  28755. /* NID not yet supported */
  28756. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28757. NID_private_key_usage_period, &crit, NULL));
  28758. ExpectIntEQ(crit, -1);
  28759. sk_ASN1_OBJECT_free(sk);
  28760. sk = NULL;
  28761. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  28762. NID_subject_alt_name, &crit, NULL));
  28763. {
  28764. int i;
  28765. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  28766. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  28767. ExpectIntEQ(gen->type, GEN_DNS);
  28768. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  28769. }
  28770. }
  28771. sk_GENERAL_NAME_free(sk);
  28772. sk = NULL;
  28773. /* NID not yet supported */
  28774. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28775. NID_issuer_alt_name, &crit, NULL));
  28776. ExpectIntEQ(crit, -1);
  28777. sk_ASN1_OBJECT_free(sk);
  28778. sk = NULL;
  28779. /* NID not yet supported */
  28780. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28781. NID_info_access, &crit, NULL));
  28782. sk_ASN1_OBJECT_free(sk);
  28783. sk = NULL;
  28784. /* NID not yet supported */
  28785. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28786. NID_sinfo_access, &crit, NULL));
  28787. ExpectIntEQ(crit, -1);
  28788. sk_ASN1_OBJECT_free(sk);
  28789. sk = NULL;
  28790. /* NID not yet supported */
  28791. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28792. NID_name_constraints, &crit, NULL));
  28793. ExpectIntEQ(crit, -1);
  28794. sk_ASN1_OBJECT_free(sk);
  28795. sk = NULL;
  28796. /* no cert policy set */
  28797. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28798. NID_certificate_policies, &crit, NULL));
  28799. sk_ASN1_OBJECT_free(sk);
  28800. sk = NULL;
  28801. /* NID not yet supported */
  28802. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28803. NID_policy_mappings, &crit, NULL));
  28804. ExpectIntEQ(crit, -1);
  28805. sk_ASN1_OBJECT_free(sk);
  28806. sk = NULL;
  28807. /* NID not yet supported */
  28808. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28809. NID_policy_constraints, &crit, NULL));
  28810. ExpectIntEQ(crit, -1);
  28811. sk_ASN1_OBJECT_free(sk);
  28812. sk = NULL;
  28813. /* NID not yet supported */
  28814. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28815. NID_inhibit_any_policy, &crit, NULL));
  28816. ExpectIntEQ(crit, -1);
  28817. sk_ASN1_OBJECT_free(sk);
  28818. sk = NULL;
  28819. /* NID not yet supported */
  28820. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28821. NID_tlsfeature, &crit, NULL));
  28822. ExpectIntEQ(crit, -1);
  28823. sk_ASN1_OBJECT_free(sk);
  28824. sk = NULL;
  28825. /* test invalid cases */
  28826. crit = 0;
  28827. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  28828. NULL));
  28829. ExpectIntEQ(crit, -1);
  28830. /* NULL passed for criticality. */
  28831. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  28832. NID_tlsfeature, NULL, NULL));
  28833. ExpectIntEQ(SSL_get_hit(ssl), 0);
  28834. #ifdef OPENSSL_ALL
  28835. X509_free(x509);
  28836. #endif
  28837. X509_free(x509ext);
  28838. SSL_free(ssl);
  28839. SSL_CTX_free(ctx);
  28840. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  28841. return EXPECT_RESULT();
  28842. }
  28843. static int test_wolfSSL_X509_check_private_key(void)
  28844. {
  28845. EXPECT_DECLS;
  28846. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  28847. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  28848. X509* x509 = NULL;
  28849. EVP_PKEY* pkey = NULL;
  28850. const byte* key;
  28851. /* Check with correct key */
  28852. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  28853. SSL_FILETYPE_PEM)));
  28854. key = client_key_der_2048;
  28855. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28856. (long)sizeof_client_key_der_2048));
  28857. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  28858. EVP_PKEY_free(pkey);
  28859. pkey = NULL;
  28860. /* Check with wrong key */
  28861. key = server_key_der_2048;
  28862. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28863. (long)sizeof_server_key_der_2048));
  28864. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  28865. /* test for incorrect parameter */
  28866. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  28867. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  28868. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  28869. EVP_PKEY_free(pkey);
  28870. X509_free(x509);
  28871. #endif
  28872. return EXPECT_RESULT();
  28873. }
  28874. static int test_wolfSSL_private_keys(void)
  28875. {
  28876. EXPECT_DECLS;
  28877. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28878. !defined(NO_FILESYSTEM)
  28879. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28880. WOLFSSL* ssl = NULL;
  28881. WOLFSSL_CTX* ctx = NULL;
  28882. EVP_PKEY* pkey = NULL;
  28883. OpenSSL_add_all_digests();
  28884. OpenSSL_add_all_algorithms();
  28885. #ifndef NO_RSA
  28886. #ifndef NO_WOLFSSL_SERVER
  28887. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28888. #else
  28889. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28890. #endif
  28891. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28892. /* Have to load a cert before you can check the private key against that
  28893. * certificates public key! */
  28894. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28895. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  28896. #endif
  28897. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28898. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28899. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28900. #endif
  28901. ExpectNotNull(ssl = SSL_new(ctx));
  28902. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28903. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28904. #endif
  28905. #ifdef USE_CERT_BUFFERS_2048
  28906. {
  28907. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28908. unsigned char buf[FOURK_BUF];
  28909. word32 bufSz;
  28910. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  28911. (unsigned char*)client_key_der_2048,
  28912. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28913. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28914. /* Should mismatch now that a different private key loaded */
  28915. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28916. #endif
  28917. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28918. (unsigned char*)server_key,
  28919. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28920. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28921. /* After loading back in DER format of original key, should match */
  28922. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28923. #endif
  28924. /* test loading private key to the WOLFSSL_CTX */
  28925. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28926. (unsigned char*)client_key_der_2048,
  28927. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28928. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28929. /* Should mismatch now that a different private key loaded */
  28930. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28931. #endif
  28932. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28933. (unsigned char*)server_key,
  28934. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28935. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28936. /* After loading back in DER format of original key, should match */
  28937. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28938. #endif
  28939. /* pkey not set yet, expecting to fail */
  28940. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28941. /* set PKEY and test again */
  28942. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28943. &server_key, (long)sizeof_server_key_der_2048));
  28944. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28945. /* reuse PKEY structure and test
  28946. * this should be checked with a memory management sanity checker */
  28947. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  28948. server_key = (const unsigned char*)server_key_der_2048;
  28949. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28950. &server_key, (long)sizeof_server_key_der_2048));
  28951. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28952. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28953. bufSz = FOURK_BUF;
  28954. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28955. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28956. RSAk, NULL, 0)), 0);
  28957. server_key = (const unsigned char*)buf;
  28958. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28959. (long)bufSz));
  28960. }
  28961. #endif
  28962. EVP_PKEY_free(pkey);
  28963. pkey = NULL;
  28964. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28965. ssl = NULL;
  28966. SSL_CTX_free(ctx);
  28967. ctx = NULL;
  28968. #endif /* end of RSA private key match tests */
  28969. #ifdef HAVE_ECC
  28970. #ifndef NO_WOLFSSL_SERVER
  28971. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28972. #else
  28973. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28974. #endif
  28975. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28976. WOLFSSL_FILETYPE_PEM));
  28977. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28978. WOLFSSL_FILETYPE_PEM));
  28979. ExpectNotNull(ssl = SSL_new(ctx));
  28980. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28981. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28982. #endif
  28983. SSL_free(ssl);
  28984. ssl = NULL;
  28985. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28986. WOLFSSL_FILETYPE_PEM));
  28987. ExpectNotNull(ssl = SSL_new(ctx));
  28988. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28989. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28990. #endif
  28991. SSL_free(ssl);
  28992. ssl = NULL;
  28993. SSL_CTX_free(ctx);
  28994. ctx = NULL;
  28995. #endif /* end of ECC private key match tests */
  28996. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28997. #ifndef NO_WOLFSSL_SERVER
  28998. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28999. #else
  29000. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29001. #endif
  29002. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  29003. WOLFSSL_FILETYPE_PEM));
  29004. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  29005. WOLFSSL_FILETYPE_PEM));
  29006. ExpectNotNull(ssl = SSL_new(ctx));
  29007. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29008. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29009. #endif
  29010. SSL_free(ssl);
  29011. ssl = NULL;
  29012. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  29013. WOLFSSL_FILETYPE_PEM));
  29014. ExpectNotNull(ssl = SSL_new(ctx));
  29015. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29016. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29017. #endif
  29018. SSL_free(ssl);
  29019. ssl = NULL;
  29020. SSL_CTX_free(ctx);
  29021. ctx = NULL;
  29022. #endif /* end of Ed25519 private key match tests */
  29023. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  29024. #ifndef NO_WOLFSSL_SERVER
  29025. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29026. #else
  29027. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29028. #endif
  29029. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  29030. WOLFSSL_FILETYPE_PEM));
  29031. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  29032. WOLFSSL_FILETYPE_PEM));
  29033. ExpectNotNull(ssl = SSL_new(ctx));
  29034. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29035. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29036. #endif
  29037. SSL_free(ssl);
  29038. ssl = NULL;
  29039. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  29040. WOLFSSL_FILETYPE_PEM));
  29041. ExpectNotNull(ssl = SSL_new(ctx));
  29042. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29043. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29044. #endif
  29045. SSL_free(ssl);
  29046. ssl = NULL;
  29047. SSL_CTX_free(ctx);
  29048. ctx = NULL;
  29049. #endif /* end of Ed448 private key match tests */
  29050. EVP_cleanup();
  29051. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  29052. CONF_modules_free();
  29053. ENGINE_cleanup();
  29054. CONF_modules_unload();
  29055. (void)ssl;
  29056. (void)ctx;
  29057. (void)pkey;
  29058. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29059. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29060. return EXPECT_RESULT();
  29061. }
  29062. static int test_wolfSSL_PEM_read_PrivateKey(void)
  29063. {
  29064. EXPECT_DECLS;
  29065. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29066. && !defined(NO_FILESYSTEM)
  29067. XFILE file = XBADFILE;
  29068. const char* fname = "./certs/server-key.pem";
  29069. EVP_PKEY* pkey = NULL;
  29070. RSA* rsa = NULL;
  29071. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  29072. unsigned char* sig = NULL;
  29073. size_t sigLen;
  29074. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  29075. size_t tbsLen = sizeof(tbs);
  29076. /* Check error case. */
  29077. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  29078. /* Read in an RSA key. */
  29079. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29080. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  29081. if (file != XBADFILE)
  29082. XFCLOSE(file);
  29083. /* Make sure the key is usable by signing some data with it. */
  29084. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  29085. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  29086. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  29087. DYNAMIC_TYPE_TMP_BUFFER));
  29088. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  29089. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  29090. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  29091. WOLFSSL_SUCCESS);
  29092. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29093. EVP_PKEY_CTX_free(ctx);
  29094. EVP_PKEY_free(pkey);
  29095. #endif
  29096. return EXPECT_RESULT();
  29097. }
  29098. static int test_wolfSSL_PEM_read_PUBKEY(void)
  29099. {
  29100. EXPECT_DECLS;
  29101. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29102. && !defined(NO_FILESYSTEM)
  29103. XFILE file = XBADFILE;
  29104. const char* fname = "./certs/client-keyPub.pem";
  29105. EVP_PKEY* pkey = NULL;
  29106. /* Check error case. */
  29107. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  29108. /* Read in an RSA key. */
  29109. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29110. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  29111. EVP_PKEY_free(pkey);
  29112. if (file != XBADFILE)
  29113. XFCLOSE(file);
  29114. #endif
  29115. return EXPECT_RESULT();
  29116. }
  29117. static int test_wolfSSL_PEM_PrivateKey(void)
  29118. {
  29119. EXPECT_DECLS;
  29120. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29121. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  29122. #ifndef NO_BIO
  29123. BIO* bio = NULL;
  29124. #endif
  29125. EVP_PKEY* pkey = NULL;
  29126. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29127. #ifndef NO_BIO
  29128. /* test creating new EVP_PKEY with bad arg */
  29129. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  29130. /* test loading RSA key using BIO */
  29131. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29132. {
  29133. XFILE file = XBADFILE;
  29134. const char* fname = "./certs/server-key.pem";
  29135. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  29136. size_t sz;
  29137. byte* buf = NULL;
  29138. EVP_PKEY* pkey2 = NULL;
  29139. EVP_PKEY* pkey3 = NULL;
  29140. RSA* rsa_key = NULL;
  29141. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29142. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29143. ExpectIntGT(sz = XFTELL(file), 0);
  29144. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29145. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29146. if (buf != NULL) {
  29147. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29148. }
  29149. if (file != XBADFILE) {
  29150. XFCLOSE(file);
  29151. file = XBADFILE;
  29152. }
  29153. /* Test using BIO new mem and loading PEM private key */
  29154. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29155. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29156. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29157. buf = NULL;
  29158. BIO_free(bio);
  29159. bio = NULL;
  29160. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29161. if (pkey2 != NULL) {
  29162. pkey2->type = EVP_PKEY_RSA;
  29163. }
  29164. /* Test parameter copy */
  29165. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  29166. EVP_PKEY_free(pkey2);
  29167. EVP_PKEY_free(pkey);
  29168. pkey = NULL;
  29169. /* Qt unit test case : rsa pkcs8 key */
  29170. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  29171. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29172. ExpectIntGT(sz = XFTELL(file), 0);
  29173. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29174. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29175. if (buf) {
  29176. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29177. }
  29178. if (file != XBADFILE) {
  29179. XFCLOSE(file);
  29180. file = XBADFILE;
  29181. }
  29182. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29183. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29184. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29185. buf = NULL;
  29186. BIO_free(bio);
  29187. bio = NULL;
  29188. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29189. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  29190. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  29191. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29192. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29193. #else
  29194. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29195. #endif
  29196. RSA_free(rsa_key);
  29197. EVP_PKEY_free(pkey3);
  29198. EVP_PKEY_free(pkey);
  29199. pkey = NULL;
  29200. }
  29201. #endif
  29202. /* test loading ECC key using BIO */
  29203. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29204. {
  29205. XFILE file = XBADFILE;
  29206. const char* fname = "./certs/ecc-key.pem";
  29207. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  29208. size_t sz = 0;
  29209. byte* buf = NULL;
  29210. EVP_PKEY* pkey2 = NULL;
  29211. EVP_PKEY* pkey3 = NULL;
  29212. EC_KEY* ec_key = NULL;
  29213. int nid = 0;
  29214. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29215. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29216. ExpectIntGT(sz = XFTELL(file), 0);
  29217. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29218. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29219. if (buf) {
  29220. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29221. }
  29222. if (file != XBADFILE) {
  29223. XFCLOSE(file);
  29224. file = XBADFILE;
  29225. }
  29226. /* Test using BIO new mem and loading PEM private key */
  29227. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29228. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29229. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29230. buf = NULL;
  29231. BIO_free(bio);
  29232. bio = NULL;
  29233. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29234. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29235. if (pkey2 != NULL) {
  29236. pkey2->type = EVP_PKEY_EC;
  29237. }
  29238. /* Test parameter copy */
  29239. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  29240. /* Qt unit test case 1*/
  29241. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29242. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29243. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29244. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29245. #else
  29246. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29247. #endif
  29248. /* Test default digest */
  29249. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  29250. ExpectIntEQ(nid, NID_sha256);
  29251. EC_KEY_free(ec_key);
  29252. ec_key = NULL;
  29253. EVP_PKEY_free(pkey3);
  29254. pkey3 = NULL;
  29255. EVP_PKEY_free(pkey2);
  29256. pkey2 = NULL;
  29257. EVP_PKEY_free(pkey);
  29258. pkey = NULL;
  29259. /* Qt unit test case ec pkcs8 key */
  29260. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  29261. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29262. ExpectIntGT(sz = XFTELL(file), 0);
  29263. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29264. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29265. if (buf) {
  29266. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29267. }
  29268. if (file != XBADFILE) {
  29269. XFCLOSE(file);
  29270. file = XBADFILE;
  29271. }
  29272. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29273. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29274. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29275. buf = NULL;
  29276. BIO_free(bio);
  29277. bio = NULL;
  29278. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29279. /* Qt unit test case */
  29280. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29281. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29282. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29283. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29284. #else
  29285. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29286. #endif
  29287. EC_KEY_free(ec_key);
  29288. EVP_PKEY_free(pkey3);
  29289. EVP_PKEY_free(pkey);
  29290. pkey = NULL;
  29291. }
  29292. #endif
  29293. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  29294. defined(WOLFSSL_CERT_GEN))
  29295. {
  29296. #define BIO_PEM_TEST_CHAR 'a'
  29297. EVP_PKEY* pkey2 = NULL;
  29298. unsigned char extra[10];
  29299. int i;
  29300. BIO* pub_bio = NULL;
  29301. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  29302. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29303. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  29304. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29305. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  29306. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  29307. &server_key, (long)sizeof_server_key_der_2048));
  29308. ExpectNull(pkey);
  29309. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29310. &server_key, (long)sizeof_server_key_der_2048));
  29311. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  29312. NULL), WOLFSSL_FAILURE);
  29313. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29314. NULL), WOLFSSL_FAILURE);
  29315. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29316. NULL), WOLFSSL_SUCCESS);
  29317. ExpectIntGT(BIO_pending(bio), 0);
  29318. ExpectIntEQ(BIO_pending(bio), 1679);
  29319. /* Check if the pubkey API writes only the public key */
  29320. #ifdef WOLFSSL_KEY_GEN
  29321. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  29322. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  29323. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  29324. ExpectIntGT(BIO_pending(pub_bio), 0);
  29325. /* Previously both the private key and the pubkey calls would write
  29326. * out the private key and the PEM header was the only difference.
  29327. * The public PEM should be significantly shorter than the
  29328. * private key versison. */
  29329. ExpectIntEQ(BIO_pending(pub_bio), 451);
  29330. #endif
  29331. /* test creating new EVP_PKEY with good args */
  29332. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29333. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  29334. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  29335. /* test of reuse of EVP_PKEY */
  29336. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29337. ExpectIntEQ(BIO_pending(bio), 0);
  29338. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  29339. SSL_SUCCESS);
  29340. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  29341. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29342. ExpectNotNull(pkey);
  29343. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  29344. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  29345. }
  29346. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  29347. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  29348. for (i = 0; i < 10; i++) {
  29349. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  29350. }
  29351. BIO_free(pub_bio);
  29352. BIO_free(bio);
  29353. bio = NULL;
  29354. EVP_PKEY_free(pkey);
  29355. pkey = NULL;
  29356. EVP_PKEY_free(pkey2);
  29357. }
  29358. #endif
  29359. /* key is DES encrypted */
  29360. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  29361. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  29362. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  29363. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  29364. {
  29365. XFILE f = XBADFILE;
  29366. wc_pem_password_cb* passwd_cb = NULL;
  29367. void* passwd_cb_userdata;
  29368. SSL_CTX* ctx = NULL;
  29369. char passwd[] = "bad password";
  29370. #ifndef WOLFSSL_NO_TLS12
  29371. #ifndef NO_WOLFSSL_SERVER
  29372. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29373. #else
  29374. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29375. #endif
  29376. #else
  29377. #ifndef NO_WOLFSSL_SERVER
  29378. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29379. #else
  29380. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29381. #endif
  29382. #endif
  29383. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29384. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  29385. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  29386. ExpectNull(passwd_cb_userdata =
  29387. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29388. /* fail case with password call back */
  29389. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  29390. (void*)passwd));
  29391. BIO_free(bio);
  29392. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29393. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29394. (void*)passwd));
  29395. BIO_free(bio);
  29396. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  29397. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  29398. if ((bio == NULL) && (f != XBADFILE)) {
  29399. XFCLOSE(f);
  29400. }
  29401. /* use callback that works */
  29402. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29403. (void*)"yassl123"));
  29404. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29405. EVP_PKEY_free(pkey);
  29406. pkey = NULL;
  29407. BIO_free(bio);
  29408. bio = NULL;
  29409. SSL_CTX_free(ctx);
  29410. }
  29411. #endif /* !defined(NO_DES3) */
  29412. #endif /* !NO_BIO */
  29413. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29414. {
  29415. unsigned char buf[2048];
  29416. size_t bytes = 0;
  29417. XFILE f = XBADFILE;
  29418. SSL_CTX* ctx = NULL;
  29419. #ifndef WOLFSSL_NO_TLS12
  29420. #ifndef NO_WOLFSSL_SERVER
  29421. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29422. #else
  29423. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29424. #endif
  29425. #else
  29426. #ifndef NO_WOLFSSL_SERVER
  29427. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29428. #else
  29429. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29430. #endif
  29431. #endif
  29432. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  29433. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  29434. if (f != XBADFILE)
  29435. XFCLOSE(f);
  29436. server_key = buf;
  29437. pkey = NULL;
  29438. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  29439. ExpectNull(pkey);
  29440. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  29441. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29442. EVP_PKEY_free(pkey);
  29443. pkey = NULL;
  29444. SSL_CTX_free(ctx);
  29445. server_key = NULL;
  29446. }
  29447. #endif
  29448. #ifndef NO_BIO
  29449. (void)bio;
  29450. #endif
  29451. (void)pkey;
  29452. (void)server_key;
  29453. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  29454. return EXPECT_RESULT();
  29455. }
  29456. static int test_wolfSSL_PEM_file_RSAKey(void)
  29457. {
  29458. EXPECT_DECLS;
  29459. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29460. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29461. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29462. RSA* rsa = NULL;
  29463. XFILE fp = XBADFILE;
  29464. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  29465. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  29466. if (fp != XBADFILE)
  29467. XFCLOSE(fp);
  29468. ExpectIntEQ(RSA_size(rsa), 256);
  29469. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  29470. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  29471. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  29472. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  29473. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29474. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  29475. RSA_free(rsa);
  29476. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29477. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29478. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29479. return EXPECT_RESULT();
  29480. }
  29481. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  29482. {
  29483. EXPECT_DECLS;
  29484. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  29485. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  29486. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  29487. RSA* rsa = NULL;
  29488. XFILE f = NULL;
  29489. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  29490. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29491. ExpectIntEQ(RSA_size(rsa), 256);
  29492. if (f != XBADFILE) {
  29493. XFCLOSE(f);
  29494. f = XBADFILE;
  29495. }
  29496. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  29497. NULL), WOLFSSL_FAILURE);
  29498. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  29499. NULL), WOLFSSL_FAILURE);
  29500. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  29501. WOLFSSL_SUCCESS);
  29502. RSA_free(rsa);
  29503. #ifdef HAVE_ECC
  29504. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  29505. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29506. if (f != XBADFILE)
  29507. XFCLOSE(f);
  29508. #endif /* HAVE_ECC */
  29509. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29510. return EXPECT_RESULT();
  29511. }
  29512. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  29513. {
  29514. EXPECT_DECLS;
  29515. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29516. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29517. XFILE file = XBADFILE;
  29518. const char* fname = "./certs/client-keyPub.pem";
  29519. RSA *rsa = NULL;
  29520. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  29521. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29522. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  29523. ExpectIntEQ(RSA_size(rsa), 256);
  29524. RSA_free(rsa);
  29525. if (file != XBADFILE)
  29526. XFCLOSE(file);
  29527. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29528. return EXPECT_RESULT();
  29529. }
  29530. #ifndef NO_BIO
  29531. static int test_wolfSSL_PEM_bio_RSAKey(void)
  29532. {
  29533. EXPECT_DECLS;
  29534. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29535. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29536. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29537. RSA* rsa = NULL;
  29538. BIO* bio = NULL;
  29539. /* PrivateKey */
  29540. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29541. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  29542. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  29543. ExpectNotNull(rsa);
  29544. ExpectIntEQ(RSA_size(rsa), 256);
  29545. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  29546. NULL), WOLFSSL_FAILURE);
  29547. BIO_free(bio);
  29548. bio = NULL;
  29549. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29550. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  29551. NULL), WOLFSSL_SUCCESS);
  29552. BIO_free(bio);
  29553. bio = NULL;
  29554. RSA_free(rsa);
  29555. rsa = NULL;
  29556. /* PUBKEY */
  29557. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  29558. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29559. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29560. ExpectIntEQ(RSA_size(rsa), 256);
  29561. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29562. BIO_free(bio);
  29563. bio = NULL;
  29564. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29565. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  29566. BIO_free(bio);
  29567. bio = NULL;
  29568. RSA_free(rsa);
  29569. rsa = NULL;
  29570. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  29571. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  29572. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29573. BIO_free(bio);
  29574. bio = NULL;
  29575. RSA_free(rsa);
  29576. rsa = NULL;
  29577. #ifdef HAVE_ECC
  29578. /* ensure that non-rsa keys do not work */
  29579. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29580. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29581. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29582. BIO_free(bio);
  29583. bio = NULL;
  29584. RSA_free(rsa);
  29585. rsa = NULL;
  29586. #endif /* HAVE_ECC */
  29587. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29588. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29589. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29590. return EXPECT_RESULT();
  29591. }
  29592. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  29593. {
  29594. EXPECT_DECLS;
  29595. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29596. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29597. RSA* rsa = NULL;
  29598. RSA* rsa_dup = NULL;
  29599. BIO* bio = NULL;
  29600. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29601. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29602. ExpectIntEQ(RSA_size(rsa), 256);
  29603. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  29604. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  29605. /* Test duplicating empty key. */
  29606. ExpectNotNull(rsa_dup = RSA_new());
  29607. ExpectNull(RSAPublicKey_dup(rsa_dup));
  29608. RSA_free(rsa_dup);
  29609. rsa_dup = NULL;
  29610. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  29611. ExpectPtrNE(rsa_dup, rsa);
  29612. #endif
  29613. /* test if valgrind complains about unreleased memory */
  29614. RSA_up_ref(rsa);
  29615. RSA_free(rsa);
  29616. BIO_free(bio);
  29617. bio = NULL;
  29618. RSA_free(rsa);
  29619. rsa = NULL;
  29620. RSA_free(rsa_dup);
  29621. rsa_dup = NULL;
  29622. #ifdef HAVE_ECC
  29623. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  29624. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29625. BIO_free(bio);
  29626. #endif /* HAVE_ECC */
  29627. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29628. return EXPECT_RESULT();
  29629. }
  29630. static int test_wolfSSL_PEM_bio_DSAKey(void)
  29631. {
  29632. EXPECT_DECLS;
  29633. #ifndef HAVE_SELFTEST
  29634. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  29635. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  29636. DSA* dsa = NULL;
  29637. BIO* bio = NULL;
  29638. /* PrivateKey */
  29639. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  29640. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  29641. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29642. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29643. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29644. NULL), WOLFSSL_FAILURE);
  29645. BIO_free(bio);
  29646. bio = NULL;
  29647. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29648. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  29649. NULL), WOLFSSL_SUCCESS);
  29650. BIO_free(bio);
  29651. bio = NULL;
  29652. DSA_free(dsa);
  29653. dsa = NULL;
  29654. /* PUBKEY */
  29655. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  29656. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29657. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29658. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29659. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29660. BIO_free(bio);
  29661. bio = NULL;
  29662. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29663. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  29664. BIO_free(bio);
  29665. bio = NULL;
  29666. DSA_free(dsa);
  29667. dsa = NULL;
  29668. #ifdef HAVE_ECC
  29669. /* ensure that non-dsa keys do not work */
  29670. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29671. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29672. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29673. BIO_free(bio);
  29674. bio = NULL;
  29675. DSA_free(dsa);
  29676. dsa = NULL;
  29677. #endif /* HAVE_ECC */
  29678. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  29679. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  29680. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  29681. #endif /* HAVE_SELFTEST */
  29682. return EXPECT_RESULT();
  29683. }
  29684. static int test_wolfSSL_PEM_bio_ECKey(void)
  29685. {
  29686. EXPECT_DECLS;
  29687. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29688. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  29689. EC_KEY* ec = NULL;
  29690. EC_KEY* ec2;
  29691. BIO* bio = NULL;
  29692. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29693. unsigned char* pem = NULL;
  29694. int pLen;
  29695. #endif
  29696. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  29697. "MAA=\n"
  29698. "-----END PUBLIC KEY-----";
  29699. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  29700. "MAA=\n"
  29701. "-----END EC PRIVATE KEY-----";
  29702. /* PrivateKey */
  29703. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29704. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  29705. ec2 = NULL;
  29706. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  29707. ExpectIntEQ(ec == ec2, 1);
  29708. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29709. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29710. NULL), WOLFSSL_FAILURE);
  29711. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29712. NULL), WOLFSSL_FAILURE);
  29713. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  29714. WOLFSSL_FAILURE);
  29715. BIO_free(bio);
  29716. bio = NULL;
  29717. /* Public key data - fail. */
  29718. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29719. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29720. BIO_free(bio);
  29721. bio = NULL;
  29722. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29723. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  29724. NULL), WOLFSSL_SUCCESS);
  29725. BIO_free(bio);
  29726. bio = NULL;
  29727. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  29728. NULL),WOLFSSL_FAILURE);
  29729. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  29730. WOLFSSL_FAILURE);
  29731. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  29732. WOLFSSL_FAILURE);
  29733. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  29734. WOLFSSL_SUCCESS);
  29735. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29736. NULL), 0);
  29737. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29738. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29739. NULL), 0);
  29740. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29741. NULL), 0);
  29742. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29743. &pLen), 0);
  29744. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29745. &pLen), 0);
  29746. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29747. &pLen), 0);
  29748. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29749. NULL), 0);
  29750. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29751. &pLen), 1);
  29752. ExpectIntGT(pLen, 0);
  29753. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29754. #endif
  29755. EC_KEY_free(ec);
  29756. ec = NULL;
  29757. /* PUBKEY */
  29758. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29759. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  29760. ec2 = NULL;
  29761. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  29762. ExpectIntEQ(ec == ec2, 1);
  29763. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29764. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29765. BIO_free(bio);
  29766. bio = NULL;
  29767. /* Test 0x30, 0x00 fails. */
  29768. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  29769. sizeof(ec_key_bad_1)));
  29770. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29771. BIO_free(bio);
  29772. bio = NULL;
  29773. /* Private key data - fail. */
  29774. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29775. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29776. BIO_free(bio);
  29777. bio = NULL;
  29778. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29779. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  29780. BIO_free(bio);
  29781. bio = NULL;
  29782. /* Same test as above, but with a file pointer rather than a BIO. */
  29783. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29784. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  29785. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29786. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  29787. EC_KEY_free(ec);
  29788. ec = NULL;
  29789. #ifndef NO_RSA
  29790. /* ensure that non-ec keys do not work */
  29791. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  29792. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  29793. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  29794. BIO_free(bio);
  29795. bio = NULL;
  29796. EC_KEY_free(ec);
  29797. ec = NULL;
  29798. #endif /* !NO_RSA */
  29799. /* Test 0x30, 0x00 fails. */
  29800. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  29801. sizeof(ec_priv_key_bad_1)));
  29802. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29803. BIO_free(bio);
  29804. bio = NULL;
  29805. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29806. return EXPECT_RESULT();
  29807. }
  29808. static int test_wolfSSL_PEM_PUBKEY(void)
  29809. {
  29810. EXPECT_DECLS;
  29811. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  29812. BIO* bio = NULL;
  29813. EVP_PKEY* pkey = NULL;
  29814. /* test creating new EVP_PKEY with bad arg */
  29815. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  29816. /* test loading ECC key using BIO */
  29817. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29818. {
  29819. XFILE file = XBADFILE;
  29820. const char* fname = "./certs/ecc-client-keyPub.pem";
  29821. size_t sz;
  29822. byte* buf = NULL;
  29823. EVP_PKEY* pkey2 = NULL;
  29824. EC_KEY* ec_key = NULL;
  29825. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29826. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  29827. ExpectIntGT(sz = XFTELL(file), 0);
  29828. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  29829. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29830. if (buf != NULL) {
  29831. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29832. }
  29833. if (file != XBADFILE) {
  29834. XFCLOSE(file);
  29835. }
  29836. /* Test using BIO new mem and loading PEM private key */
  29837. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29838. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  29839. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29840. BIO_free(bio);
  29841. bio = NULL;
  29842. /* Qt unit test case*/
  29843. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29844. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29845. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  29846. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29847. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  29848. #else
  29849. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  29850. #endif
  29851. EC_KEY_free(ec_key);
  29852. EVP_PKEY_free(pkey2);
  29853. EVP_PKEY_free(pkey);
  29854. pkey = NULL;
  29855. }
  29856. #endif
  29857. (void)bio;
  29858. (void)pkey;
  29859. #endif
  29860. return EXPECT_RESULT();
  29861. }
  29862. #endif /* !NO_BIO */
  29863. static int test_DSA_do_sign_verify(void)
  29864. {
  29865. EXPECT_DECLS;
  29866. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  29867. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29868. !defined(NO_DSA)
  29869. unsigned char digest[WC_SHA_DIGEST_SIZE];
  29870. DSA_SIG* sig = NULL;
  29871. DSA* dsa = NULL;
  29872. word32 bytes;
  29873. byte sigBin[DSA_SIG_SIZE];
  29874. int dsacheck;
  29875. #ifdef USE_CERT_BUFFERS_1024
  29876. byte tmp[ONEK_BUF];
  29877. XMEMSET(tmp, 0, sizeof(tmp));
  29878. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  29879. bytes = sizeof_dsa_key_der_1024;
  29880. #elif defined(USE_CERT_BUFFERS_2048)
  29881. byte tmp[TWOK_BUF];
  29882. XMEMSET(tmp, 0, sizeof(tmp));
  29883. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  29884. bytes = sizeof_dsa_key_der_2048;
  29885. #else
  29886. byte tmp[TWOK_BUF];
  29887. XFILE fp = XBADFILE;
  29888. XMEMSET(tmp, 0, sizeof(tmp));
  29889. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  29890. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  29891. if (fp != XBADFILE)
  29892. XFCLOSE(fp);
  29893. #endif /* END USE_CERT_BUFFERS_1024 */
  29894. XMEMSET(digest, 202, sizeof(digest));
  29895. ExpectNotNull(dsa = DSA_new());
  29896. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  29897. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  29898. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  29899. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  29900. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  29901. DSA_SIG_free(sig);
  29902. DSA_free(dsa);
  29903. #endif
  29904. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  29905. return EXPECT_RESULT();
  29906. }
  29907. static int test_wolfSSL_tmp_dh(void)
  29908. {
  29909. EXPECT_DECLS;
  29910. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29911. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  29912. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29913. byte buff[6000];
  29914. char file[] = "./certs/dsaparams.pem";
  29915. XFILE f = XBADFILE;
  29916. int bytes = 0;
  29917. DSA* dsa = NULL;
  29918. DH* dh = NULL;
  29919. #if defined(WOLFSSL_DH_EXTRA) && \
  29920. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29921. DH* dh2 = NULL;
  29922. #endif
  29923. BIO* bio = NULL;
  29924. SSL* ssl = NULL;
  29925. SSL_CTX* ctx = NULL;
  29926. #ifndef NO_WOLFSSL_SERVER
  29927. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29928. #else
  29929. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29930. #endif
  29931. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29932. WOLFSSL_FILETYPE_PEM));
  29933. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29934. WOLFSSL_FILETYPE_PEM));
  29935. ExpectNotNull(ssl = SSL_new(ctx));
  29936. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29937. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  29938. if (f != XBADFILE)
  29939. XFCLOSE(f);
  29940. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29941. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  29942. ExpectNotNull(dsa);
  29943. dh = wolfSSL_DSA_dup_DH(dsa);
  29944. ExpectNotNull(dh);
  29945. #if defined(WOLFSSL_DH_EXTRA) && \
  29946. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29947. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  29948. #endif
  29949. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  29950. #ifndef NO_WOLFSSL_SERVER
  29951. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  29952. #else
  29953. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  29954. #endif
  29955. BIO_free(bio);
  29956. DSA_free(dsa);
  29957. DH_free(dh);
  29958. #if defined(WOLFSSL_DH_EXTRA) && \
  29959. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29960. DH_free(dh2);
  29961. #endif
  29962. SSL_free(ssl);
  29963. SSL_CTX_free(ctx);
  29964. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29965. #endif
  29966. return EXPECT_RESULT();
  29967. }
  29968. static int test_wolfSSL_ctrl(void)
  29969. {
  29970. EXPECT_DECLS;
  29971. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  29972. byte buff[6000];
  29973. BIO* bio = NULL;
  29974. int bytes;
  29975. BUF_MEM* ptr = NULL;
  29976. XMEMSET(buff, 0, sizeof(buff));
  29977. bytes = sizeof(buff);
  29978. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29979. ExpectNotNull(BIO_s_socket());
  29980. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  29981. /* needs tested after stubs filled out @TODO
  29982. SSL_ctrl
  29983. SSL_CTX_ctrl
  29984. */
  29985. BIO_free(bio);
  29986. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  29987. return EXPECT_RESULT();
  29988. }
  29989. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  29990. {
  29991. EXPECT_DECLS;
  29992. #ifdef OPENSSL_EXTRA
  29993. static const unsigned char pw[] = "password";
  29994. static const int pwSz = sizeof(pw) - 1;
  29995. size_t checkPwSz = 0;
  29996. const unsigned char* checkPw = NULL;
  29997. WOLFSSL_EVP_PKEY* key = NULL;
  29998. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  29999. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  30000. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30001. pwSz));
  30002. if (key != NULL) {
  30003. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  30004. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  30005. ExpectIntEQ(key->pkey_sz, pwSz);
  30006. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  30007. }
  30008. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  30009. ExpectIntEQ((int)checkPwSz, pwSz);
  30010. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  30011. wolfSSL_EVP_PKEY_free(key);
  30012. key = NULL;
  30013. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30014. 0));
  30015. ExpectIntEQ(key->pkey_sz, 0);
  30016. if (EXPECT_SUCCESS()) {
  30017. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30018. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30019. }
  30020. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30021. ExpectIntEQ((int)checkPwSz, 0);
  30022. wolfSSL_EVP_PKEY_free(key);
  30023. key = NULL;
  30024. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  30025. 0));
  30026. ExpectIntEQ(key->pkey_sz, 0);
  30027. if (EXPECT_SUCCESS()) {
  30028. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30029. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30030. }
  30031. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30032. ExpectIntEQ((int)checkPwSz, 0);
  30033. wolfSSL_EVP_PKEY_free(key);
  30034. key = NULL;
  30035. #endif /* OPENSSL_EXTRA */
  30036. return EXPECT_RESULT();
  30037. }
  30038. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  30039. {
  30040. EXPECT_DECLS;
  30041. #ifdef OPENSSL_EXTRA
  30042. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  30043. const char *priv = "ABCDEFGHIJKLMNOP";
  30044. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  30045. WOLFSSL_EVP_PKEY* key = NULL;
  30046. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30047. NULL, NULL, AES_128_KEY_SIZE, cipher));
  30048. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30049. NULL, (const unsigned char *)priv, 0, cipher));
  30050. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30051. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  30052. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30053. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  30054. wolfSSL_EVP_PKEY_free(key);
  30055. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  30056. #endif /* OPENSSL_EXTRA */
  30057. return EXPECT_RESULT();
  30058. }
  30059. static int test_wolfSSL_EVP_Digest(void)
  30060. {
  30061. EXPECT_DECLS;
  30062. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30063. const char* in = "abc";
  30064. int inLen = (int)XSTRLEN(in);
  30065. byte out[WC_SHA256_DIGEST_SIZE];
  30066. unsigned int outLen;
  30067. const char* expOut =
  30068. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  30069. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  30070. "\x15\xAD";
  30071. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  30072. "SHA256", NULL), 1);
  30073. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  30074. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  30075. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  30076. return EXPECT_RESULT();
  30077. }
  30078. static int test_wolfSSL_EVP_Digest_all(void)
  30079. {
  30080. EXPECT_DECLS;
  30081. #ifdef OPENSSL_EXTRA
  30082. const char* digests[] = {
  30083. #ifndef NO_MD5
  30084. "MD5",
  30085. #endif
  30086. #ifndef NO_SHA
  30087. "SHA",
  30088. #endif
  30089. #ifdef WOLFSSL_SHA224
  30090. "SHA224",
  30091. #endif
  30092. #ifndef NO_SHA256
  30093. "SHA256",
  30094. #endif
  30095. #ifdef WOLFSSL_SHA384
  30096. "SHA384",
  30097. #endif
  30098. #ifdef WOLFSSL_SHA512
  30099. "SHA512",
  30100. #endif
  30101. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  30102. "SHA512_224",
  30103. #endif
  30104. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  30105. "SHA512_256",
  30106. #endif
  30107. #ifdef WOLFSSL_SHA3
  30108. #ifndef WOLFSSL_NOSHA3_224
  30109. "SHA3_224",
  30110. #endif
  30111. #ifndef WOLFSSL_NOSHA3_256
  30112. "SHA3_256",
  30113. #endif
  30114. "SHA3_384",
  30115. #ifndef WOLFSSL_NOSHA3_512
  30116. "SHA3_512",
  30117. #endif
  30118. #endif /* WOLFSSL_SHA3 */
  30119. NULL
  30120. };
  30121. const char** d;
  30122. const unsigned char in[] = "abc";
  30123. int inLen = XSTR_SIZEOF(in);
  30124. byte out[WC_MAX_DIGEST_SIZE];
  30125. unsigned int outLen;
  30126. for (d = digests; *d != NULL; d++) {
  30127. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  30128. ExpectIntGT(outLen, 0);
  30129. ExpectIntEQ(EVP_MD_size(*d), outLen);
  30130. }
  30131. #endif
  30132. return EXPECT_RESULT();
  30133. }
  30134. static int test_wolfSSL_EVP_MD_size(void)
  30135. {
  30136. EXPECT_DECLS;
  30137. #ifdef OPENSSL_EXTRA
  30138. WOLFSSL_EVP_MD_CTX mdCtx;
  30139. #ifdef WOLFSSL_SHA3
  30140. #ifndef WOLFSSL_NOSHA3_224
  30141. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30142. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  30143. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  30144. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  30145. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30146. #endif
  30147. #ifndef WOLFSSL_NOSHA3_256
  30148. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30149. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  30150. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  30151. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  30152. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30153. #endif
  30154. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30155. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  30156. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  30157. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  30158. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30159. #ifndef WOLFSSL_NOSHA3_512
  30160. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30161. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  30162. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  30163. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  30164. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30165. #endif
  30166. #endif /* WOLFSSL_SHA3 */
  30167. #ifndef NO_SHA256
  30168. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30169. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  30170. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30171. WC_SHA256_DIGEST_SIZE);
  30172. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30173. WC_SHA256_BLOCK_SIZE);
  30174. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  30175. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  30176. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30177. #endif
  30178. #ifndef NO_MD5
  30179. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30180. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  30181. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30182. WC_MD5_DIGEST_SIZE);
  30183. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30184. WC_MD5_BLOCK_SIZE);
  30185. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  30186. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  30187. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30188. #endif
  30189. #ifdef WOLFSSL_SHA224
  30190. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30191. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  30192. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30193. WC_SHA224_DIGEST_SIZE);
  30194. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30195. WC_SHA224_BLOCK_SIZE);
  30196. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  30197. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  30198. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30199. #endif
  30200. #ifdef WOLFSSL_SHA384
  30201. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30202. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  30203. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30204. WC_SHA384_DIGEST_SIZE);
  30205. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30206. WC_SHA384_BLOCK_SIZE);
  30207. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  30208. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  30209. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30210. #endif
  30211. #ifdef WOLFSSL_SHA512
  30212. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30213. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  30214. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30215. WC_SHA512_DIGEST_SIZE);
  30216. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30217. WC_SHA512_BLOCK_SIZE);
  30218. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  30219. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  30220. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30221. #endif
  30222. #ifndef NO_SHA
  30223. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30224. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  30225. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30226. WC_SHA_DIGEST_SIZE);
  30227. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30228. WC_SHA_BLOCK_SIZE);
  30229. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30230. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30231. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30232. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30233. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  30234. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30235. WC_SHA_DIGEST_SIZE);
  30236. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30237. WC_SHA_BLOCK_SIZE);
  30238. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30239. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30240. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30241. #endif
  30242. /* error case */
  30243. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30244. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  30245. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30246. BAD_FUNC_ARG);
  30247. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  30248. /* Cleanup is valid on uninit'ed struct */
  30249. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30250. #endif /* OPENSSL_EXTRA */
  30251. return EXPECT_RESULT();
  30252. }
  30253. static int test_wolfSSL_EVP_MD_pkey_type(void)
  30254. {
  30255. EXPECT_DECLS;
  30256. #ifdef OPENSSL_EXTRA
  30257. const WOLFSSL_EVP_MD* md;
  30258. #ifndef NO_MD5
  30259. ExpectNotNull(md = EVP_md5());
  30260. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  30261. #endif
  30262. #ifndef NO_SHA
  30263. ExpectNotNull(md = EVP_sha1());
  30264. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  30265. #endif
  30266. #ifdef WOLFSSL_SHA224
  30267. ExpectNotNull(md = EVP_sha224());
  30268. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  30269. #endif
  30270. ExpectNotNull(md = EVP_sha256());
  30271. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  30272. #ifdef WOLFSSL_SHA384
  30273. ExpectNotNull(md = EVP_sha384());
  30274. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  30275. #endif
  30276. #ifdef WOLFSSL_SHA512
  30277. ExpectNotNull(md = EVP_sha512());
  30278. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  30279. #endif
  30280. #endif
  30281. return EXPECT_RESULT();
  30282. }
  30283. #ifdef OPENSSL_EXTRA
  30284. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  30285. size_t testKeySz, const char* testData, size_t testDataSz,
  30286. const byte* testResult, size_t testResultSz)
  30287. {
  30288. EXPECT_DECLS;
  30289. unsigned char check[WC_MAX_DIGEST_SIZE];
  30290. size_t checkSz = -1;
  30291. WOLFSSL_EVP_PKEY* key = NULL;
  30292. WOLFSSL_EVP_MD_CTX mdCtx;
  30293. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  30294. testKey, (int)testKeySz));
  30295. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30296. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30297. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30298. (unsigned int)testDataSz), 1);
  30299. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30300. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30301. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30302. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30303. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30304. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30305. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30306. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30307. (unsigned int)testDataSz), 1);
  30308. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30309. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30310. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30311. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30312. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30313. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30314. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30315. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30316. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30317. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30318. (unsigned int)testDataSz - 4), 1);
  30319. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30320. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30321. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30322. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30323. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30324. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30325. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30326. (unsigned int)testDataSz - 4), 1);
  30327. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30328. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30329. wolfSSL_EVP_PKEY_free(key);
  30330. return EXPECT_RESULT();
  30331. }
  30332. #endif
  30333. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  30334. {
  30335. EXPECT_DECLS;
  30336. #ifdef OPENSSL_EXTRA
  30337. static const unsigned char testKey[] =
  30338. {
  30339. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30340. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30341. 0x0b, 0x0b, 0x0b, 0x0b
  30342. };
  30343. static const char testData[] = "Hi There";
  30344. #ifdef WOLFSSL_SHA224
  30345. static const unsigned char testResultSha224[] =
  30346. {
  30347. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  30348. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  30349. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  30350. 0x53, 0x68, 0x4b, 0x22
  30351. };
  30352. #endif
  30353. #ifndef NO_SHA256
  30354. static const unsigned char testResultSha256[] =
  30355. {
  30356. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  30357. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  30358. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  30359. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  30360. };
  30361. #endif
  30362. #ifdef WOLFSSL_SHA384
  30363. static const unsigned char testResultSha384[] =
  30364. {
  30365. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  30366. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  30367. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  30368. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  30369. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  30370. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  30371. };
  30372. #endif
  30373. #ifdef WOLFSSL_SHA512
  30374. static const unsigned char testResultSha512[] =
  30375. {
  30376. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  30377. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  30378. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  30379. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  30380. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  30381. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  30382. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  30383. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  30384. };
  30385. #endif
  30386. #ifdef WOLFSSL_SHA3
  30387. #ifndef WOLFSSL_NOSHA3_224
  30388. static const unsigned char testResultSha3_224[] =
  30389. {
  30390. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  30391. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  30392. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  30393. 0xf3, 0xc8, 0x60, 0xf7
  30394. };
  30395. #endif
  30396. #ifndef WOLFSSL_NOSHA3_256
  30397. static const unsigned char testResultSha3_256[] =
  30398. {
  30399. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  30400. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  30401. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  30402. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  30403. };
  30404. #endif
  30405. #ifndef WOLFSSL_NOSHA3_384
  30406. static const unsigned char testResultSha3_384[] =
  30407. {
  30408. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  30409. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  30410. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  30411. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  30412. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  30413. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  30414. };
  30415. #endif
  30416. #ifndef WOLFSSL_NOSHA3_512
  30417. static const unsigned char testResultSha3_512[] =
  30418. {
  30419. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  30420. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  30421. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  30422. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  30423. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  30424. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  30425. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  30426. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  30427. };
  30428. #endif
  30429. #endif
  30430. #ifndef NO_SHA256
  30431. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  30432. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  30433. sizeof(testResultSha256)), TEST_SUCCESS);
  30434. #endif
  30435. #ifdef WOLFSSL_SHA224
  30436. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  30437. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  30438. sizeof(testResultSha224)), TEST_SUCCESS);
  30439. #endif
  30440. #ifdef WOLFSSL_SHA384
  30441. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  30442. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  30443. sizeof(testResultSha384)), TEST_SUCCESS);
  30444. #endif
  30445. #ifdef WOLFSSL_SHA512
  30446. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  30447. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  30448. sizeof(testResultSha512)), TEST_SUCCESS);
  30449. #endif
  30450. #ifdef WOLFSSL_SHA3
  30451. #ifndef WOLFSSL_NOSHA3_224
  30452. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  30453. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  30454. sizeof(testResultSha3_224)), TEST_SUCCESS);
  30455. #endif
  30456. #ifndef WOLFSSL_NOSHA3_256
  30457. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  30458. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  30459. sizeof(testResultSha3_256)), TEST_SUCCESS);
  30460. #endif
  30461. #ifndef WOLFSSL_NOSHA3_384
  30462. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  30463. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  30464. sizeof(testResultSha3_384)), TEST_SUCCESS);
  30465. #endif
  30466. #ifndef WOLFSSL_NOSHA3_512
  30467. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  30468. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  30469. sizeof(testResultSha3_512)), TEST_SUCCESS);
  30470. #endif
  30471. #endif
  30472. #endif /* OPENSSL_EXTRA */
  30473. return EXPECT_RESULT();
  30474. }
  30475. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  30476. {
  30477. EXPECT_DECLS;
  30478. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  30479. defined(USE_CERT_BUFFERS_2048)
  30480. WOLFSSL_EVP_PKEY* privKey = NULL;
  30481. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30482. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  30483. const char testData[] = "Hi There";
  30484. WOLFSSL_EVP_MD_CTX mdCtx;
  30485. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  30486. int ret;
  30487. size_t checkSz = -1;
  30488. int sz = 2048 / 8;
  30489. const unsigned char* cp;
  30490. const unsigned char* p;
  30491. unsigned char check[2048/8];
  30492. size_t i;
  30493. int paddings[] = {
  30494. RSA_PKCS1_PADDING,
  30495. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  30496. RSA_PKCS1_PSS_PADDING,
  30497. #endif
  30498. };
  30499. cp = client_key_der_2048;
  30500. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  30501. sizeof_client_key_der_2048)));
  30502. p = client_keypub_der_2048;
  30503. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30504. sizeof_client_keypub_der_2048)));
  30505. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30506. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  30507. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30508. NULL, privKey), 1);
  30509. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30510. (unsigned int)XSTRLEN(testData)), 1);
  30511. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30512. ExpectIntEQ((int)checkSz, sz);
  30513. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30514. ExpectIntEQ((int)checkSz,sz);
  30515. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30516. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30517. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  30518. ExpectIntEQ(ret, 1);
  30519. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30520. ExpectIntEQ(ret, 1);
  30521. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30522. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30523. NULL, pubKey), 1);
  30524. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30525. (unsigned int)XSTRLEN(testData)),
  30526. 1);
  30527. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30528. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30529. ExpectIntEQ(ret, 1);
  30530. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30531. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30532. NULL, privKey), 1);
  30533. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30534. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30535. ExpectIntEQ((int)checkSz, sz);
  30536. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30537. ExpectIntEQ((int)checkSz, sz);
  30538. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30539. (unsigned int)XSTRLEN(testData) - 4), 1);
  30540. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30541. ExpectIntEQ((int)checkSz, sz);
  30542. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30543. ExpectIntEQ(ret, 1);
  30544. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30545. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30546. NULL, pubKey), 1);
  30547. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30548. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30549. (unsigned int)XSTRLEN(testData) - 4),
  30550. 1);
  30551. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30552. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30553. ExpectIntEQ(ret, 1);
  30554. /* Check all signing padding types */
  30555. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  30556. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30557. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  30558. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  30559. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30560. paddings[i]), 1);
  30561. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30562. (unsigned int)XSTRLEN(testData)), 1);
  30563. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30564. ExpectIntEQ((int)checkSz, sz);
  30565. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30566. ExpectIntEQ((int)checkSz,sz);
  30567. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30568. ExpectIntEQ(ret, 1);
  30569. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30570. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  30571. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  30572. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30573. paddings[i]), 1);
  30574. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30575. (unsigned int)XSTRLEN(testData)), 1);
  30576. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30577. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30578. ExpectIntEQ(ret, 1);
  30579. }
  30580. wolfSSL_EVP_PKEY_free(pubKey);
  30581. wolfSSL_EVP_PKEY_free(privKey);
  30582. #endif
  30583. return EXPECT_RESULT();
  30584. }
  30585. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  30586. {
  30587. EXPECT_DECLS;
  30588. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30589. WOLFSSL_EVP_PKEY* privKey = NULL;
  30590. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30591. const char testData[] = "Hi There";
  30592. WOLFSSL_EVP_MD_CTX mdCtx;
  30593. int ret;
  30594. size_t checkSz = -1;
  30595. const unsigned char* cp;
  30596. const unsigned char* p;
  30597. unsigned char check[2048/8];
  30598. cp = ecc_clikey_der_256;
  30599. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  30600. sizeof_ecc_clikey_der_256));
  30601. p = ecc_clikeypub_der_256;
  30602. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30603. sizeof_ecc_clikeypub_der_256)));
  30604. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30605. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30606. NULL, privKey), 1);
  30607. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30608. (unsigned int)XSTRLEN(testData)), 1);
  30609. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30610. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30611. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30612. ExpectIntEQ(ret, 1);
  30613. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30614. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30615. NULL, pubKey), 1);
  30616. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30617. (unsigned int)XSTRLEN(testData)),
  30618. 1);
  30619. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30620. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30621. ExpectIntEQ(ret, 1);
  30622. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30623. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30624. NULL, privKey), 1);
  30625. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30626. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30627. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30628. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30629. (unsigned int)XSTRLEN(testData) - 4), 1);
  30630. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30631. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30632. ExpectIntEQ(ret, 1);
  30633. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30634. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30635. NULL, pubKey), 1);
  30636. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30637. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30638. (unsigned int)XSTRLEN(testData) - 4),
  30639. 1);
  30640. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30641. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30642. ExpectIntEQ(ret, 1);
  30643. wolfSSL_EVP_PKEY_free(pubKey);
  30644. wolfSSL_EVP_PKEY_free(privKey);
  30645. #endif
  30646. return EXPECT_RESULT();
  30647. }
  30648. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  30649. {
  30650. EXPECT_DECLS;
  30651. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30652. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  30653. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30654. char caFile[] = "./certs/client-ca.pem";
  30655. char clientFile[] = "./certs/client-cert.pem";
  30656. SSL_CTX* ctx = NULL;
  30657. X509* x509 = NULL;
  30658. BIO *bio = NULL;
  30659. X509 *cert = NULL;
  30660. X509 *ca = NULL;
  30661. STACK_OF(X509) *chain = NULL;
  30662. STACK_OF(X509) *chain2 = NULL;
  30663. #ifndef NO_WOLFSSL_SERVER
  30664. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30665. #else
  30666. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30667. #endif
  30668. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  30669. WOLFSSL_FILETYPE_PEM));
  30670. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  30671. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  30672. WOLFSSL_FILETYPE_PEM));
  30673. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  30674. /* additional test of getting EVP_PKEY key size from X509
  30675. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  30676. * allowed with user RSA */
  30677. {
  30678. EVP_PKEY* pkey = NULL;
  30679. #if defined(HAVE_ECC)
  30680. X509* ecX509 = NULL;
  30681. #endif /* HAVE_ECC */
  30682. ExpectNotNull(pkey = X509_get_pubkey(x509));
  30683. /* current RSA key is 2048 bit (256 bytes) */
  30684. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  30685. EVP_PKEY_free(pkey);
  30686. pkey = NULL;
  30687. #if defined(HAVE_ECC)
  30688. #if defined(USE_CERT_BUFFERS_256)
  30689. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  30690. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  30691. SSL_FILETYPE_ASN1));
  30692. #else
  30693. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  30694. cliEccCertFile, SSL_FILETYPE_PEM));
  30695. #endif
  30696. pkey = X509_get_pubkey(ecX509);
  30697. ExpectNotNull(pkey);
  30698. /* current ECC key is 256 bit (32 bytes) */
  30699. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  30700. X509_free(ecX509);
  30701. ecX509 = NULL;
  30702. EVP_PKEY_free(pkey);
  30703. pkey = NULL;
  30704. #endif /* HAVE_ECC */
  30705. }
  30706. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  30707. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  30708. if (EXPECT_SUCCESS()) {
  30709. x509 = NULL;
  30710. }
  30711. #ifdef WOLFSSL_ENCRYPTED_KEYS
  30712. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  30713. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30714. #endif
  30715. SSL_CTX_free(ctx);
  30716. ctx = NULL;
  30717. #ifndef NO_WOLFSSL_SERVER
  30718. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30719. #else
  30720. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30721. #endif
  30722. /* Test haproxy use case */
  30723. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  30724. /* Read Certificate */
  30725. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30726. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  30727. ExpectNotNull(chain = sk_X509_new_null());
  30728. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  30729. if (EXPECT_SUCCESS()) {
  30730. ca = NULL;
  30731. }
  30732. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  30733. ExpectNotNull(ca = sk_X509_shift(chain2));
  30734. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  30735. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  30736. if (EXPECT_SUCCESS()) {
  30737. ca = NULL;
  30738. }
  30739. BIO_free(bio);
  30740. X509_free(cert);
  30741. X509_free(ca);
  30742. X509_free(x509);
  30743. sk_X509_pop_free(chain, X509_free);
  30744. sk_X509_pop_free(chain2, X509_free);
  30745. SSL_CTX_free(ctx);
  30746. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30747. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30748. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  30749. return EXPECT_RESULT();
  30750. }
  30751. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30752. static int test_wolfSSL_ERR_peek_last_error_line(void)
  30753. {
  30754. EXPECT_DECLS;
  30755. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30756. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30757. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  30758. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  30759. callback_functions client_cb;
  30760. callback_functions server_cb;
  30761. int line = 0;
  30762. int flag = ERR_TXT_STRING;
  30763. const char* file = NULL;
  30764. const char* data = NULL;
  30765. /* create a failed connection and inspect the error */
  30766. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30767. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30768. client_cb.method = wolfTLSv1_1_client_method;
  30769. server_cb.method = wolfTLSv1_2_server_method;
  30770. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30771. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  30772. ExpectNotNull(data);
  30773. /* check clearing error state */
  30774. ERR_remove_state(0);
  30775. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30776. ERR_peek_last_error_line(NULL, &line);
  30777. ExpectIntEQ(line, 0);
  30778. ERR_peek_last_error_line(&file, NULL);
  30779. ExpectNull(file);
  30780. /* retry connection to fill error queue */
  30781. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30782. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30783. client_cb.method = wolfTLSv1_1_client_method;
  30784. server_cb.method = wolfTLSv1_2_server_method;
  30785. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30786. /* check that error code was stored */
  30787. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30788. ERR_peek_last_error_line(NULL, &line);
  30789. ExpectIntNE(line, 0);
  30790. ERR_peek_last_error_line(&file, NULL);
  30791. ExpectNotNull(file);
  30792. fprintf(stderr, "\nTesting error print out\n");
  30793. ERR_print_errors_fp(stderr);
  30794. fprintf(stderr, "Done testing print out\n\n");
  30795. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  30796. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  30797. return EXPECT_RESULT();
  30798. }
  30799. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  30800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30801. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30802. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  30803. {
  30804. (void) ok;
  30805. (void) ctx;
  30806. fprintf(stderr, "ENTER verify_cb\n");
  30807. return SSL_SUCCESS;
  30808. }
  30809. #endif
  30810. static int test_wolfSSL_X509_Name_canon(void)
  30811. {
  30812. EXPECT_DECLS;
  30813. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30814. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  30815. defined(WOLFSSL_CERT_GEN) && \
  30816. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  30817. const long ex_hash1 = 0x0fdb2da4;
  30818. const long ex_hash2 = 0x9f3e8c9e;
  30819. X509_NAME *name = NULL;
  30820. X509 *x509 = NULL;
  30821. XFILE file = XBADFILE;
  30822. unsigned long hash = 0;
  30823. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  30824. byte *pbuf = NULL;
  30825. word32 len = 0;
  30826. (void) ex_hash2;
  30827. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  30828. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30829. ExpectNotNull(name = X509_get_issuer_name(x509));
  30830. /* When output buffer is NULL, should return necessary output buffer
  30831. * length.*/
  30832. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  30833. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30834. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30835. hash = (((unsigned long)digest[3] << 24) |
  30836. ((unsigned long)digest[2] << 16) |
  30837. ((unsigned long)digest[1] << 8) |
  30838. ((unsigned long)digest[0]));
  30839. ExpectIntEQ(hash, ex_hash1);
  30840. if (file != XBADFILE) {
  30841. XFCLOSE(file);
  30842. file = XBADFILE;
  30843. }
  30844. X509_free(x509);
  30845. x509 = NULL;
  30846. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30847. pbuf = NULL;
  30848. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  30849. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30850. ExpectNotNull(name = X509_get_issuer_name(x509));
  30851. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30852. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30853. hash = (((unsigned long)digest[3] << 24) |
  30854. ((unsigned long)digest[2] << 16) |
  30855. ((unsigned long)digest[1] << 8) |
  30856. ((unsigned long)digest[0]));
  30857. ExpectIntEQ(hash, ex_hash2);
  30858. if (file != XBADFILE)
  30859. XFCLOSE(file);
  30860. X509_free(x509);
  30861. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30862. #endif
  30863. return EXPECT_RESULT();
  30864. }
  30865. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  30866. {
  30867. EXPECT_DECLS;
  30868. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  30869. const int MAX_DIR = 4;
  30870. const char paths[][32] = {
  30871. "./certs/ed25519",
  30872. "./certs/ecc",
  30873. "./certs/crl",
  30874. "./certs/",
  30875. };
  30876. char CertCrl_path[MAX_FILENAME_SZ];
  30877. char *p;
  30878. X509_STORE* str = NULL;
  30879. X509_LOOKUP* lookup = NULL;
  30880. WOLFSSL_STACK* sk = NULL;
  30881. int len, total_len, i;
  30882. (void)sk;
  30883. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  30884. /* illegal string */
  30885. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30886. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30887. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  30888. SSL_FILETYPE_PEM,NULL), 0);
  30889. /* free store */
  30890. X509_STORE_free(str);
  30891. str = NULL;
  30892. /* short folder string */
  30893. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30894. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30895. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  30896. SSL_FILETYPE_PEM,NULL), 1);
  30897. #if defined(WOLFSSL_INT_H)
  30898. /* only available when including internal.h */
  30899. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30900. #endif
  30901. /* free store */
  30902. X509_STORE_free(str);
  30903. str = NULL;
  30904. /* typical function check */
  30905. p = &CertCrl_path[0];
  30906. total_len = 0;
  30907. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  30908. len = (int)XSTRLEN((const char*)&paths[i]);
  30909. total_len += len;
  30910. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  30911. p += len;
  30912. if (i != 0) *(p++) = SEPARATOR_CHAR;
  30913. }
  30914. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30915. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30916. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  30917. SSL_FILETYPE_PEM,NULL), 1);
  30918. #if defined(WOLFSSL_INT_H)
  30919. /* only available when including internal.h */
  30920. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30921. #endif
  30922. X509_STORE_free(str);
  30923. #endif
  30924. return EXPECT_RESULT();
  30925. }
  30926. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  30927. {
  30928. EXPECT_DECLS;
  30929. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30930. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30931. defined(WOLFSSL_SIGNER_DER_CERT)
  30932. X509_STORE_CTX* ctx = NULL;
  30933. X509_STORE* str = NULL;
  30934. X509_LOOKUP* lookup = NULL;
  30935. X509* cert1 = NULL;
  30936. X509* x509Ca = NULL;
  30937. X509* x509Svr = NULL;
  30938. X509* issuer = NULL;
  30939. WOLFSSL_STACK* sk = NULL;
  30940. X509_NAME* caName = NULL;
  30941. X509_NAME* issuerName = NULL;
  30942. XFILE file1 = XBADFILE;
  30943. int i;
  30944. int cert_count = 0;
  30945. int cmp;
  30946. char der[] = "certs/ca-cert.der";
  30947. #ifdef HAVE_CRL
  30948. char pem[][100] = {
  30949. "./certs/crl/crl.pem",
  30950. "./certs/crl/crl2.pem",
  30951. "./certs/crl/caEccCrl.pem",
  30952. "./certs/crl/eccCliCRL.pem",
  30953. "./certs/crl/eccSrvCRL.pem",
  30954. ""
  30955. };
  30956. #endif
  30957. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  30958. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  30959. if (file1 != XBADFILE)
  30960. XFCLOSE(file1);
  30961. ExpectNotNull(ctx = X509_STORE_CTX_new());
  30962. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30963. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30964. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30965. SSL_FILETYPE_PEM,NULL), 1);
  30966. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30967. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  30968. /* check if CA cert is loaded into the store */
  30969. for (i = 0; i < cert_count; i++) {
  30970. x509Ca = sk_X509_value(sk, i);
  30971. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30972. }
  30973. ExpectNotNull((x509Svr =
  30974. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30975. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30976. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30977. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30978. ExpectNotNull(issuer);
  30979. caName = X509_get_subject_name(x509Ca);
  30980. ExpectNotNull(caName);
  30981. issuerName = X509_get_subject_name(issuer);
  30982. ExpectNotNull(issuerName);
  30983. cmp = X509_NAME_cmp(caName, issuerName);
  30984. ExpectIntEQ(cmp, 0);
  30985. /* load der format */
  30986. X509_free(issuer);
  30987. issuer = NULL;
  30988. X509_STORE_CTX_free(ctx);
  30989. ctx = NULL;
  30990. X509_STORE_free(str);
  30991. str = NULL;
  30992. sk_X509_pop_free(sk, NULL);
  30993. sk = NULL;
  30994. X509_free(x509Svr);
  30995. x509Svr = NULL;
  30996. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30997. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30998. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  30999. SSL_FILETYPE_ASN1,NULL), 1);
  31000. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31001. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31002. /* check if CA cert is loaded into the store */
  31003. for (i = 0; i < cert_count; i++) {
  31004. x509Ca = sk_X509_value(sk, i);
  31005. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31006. }
  31007. X509_STORE_free(str);
  31008. str = NULL;
  31009. sk_X509_pop_free(sk, NULL);
  31010. sk = NULL;
  31011. X509_free(cert1);
  31012. cert1 = NULL;
  31013. #ifdef HAVE_CRL
  31014. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  31015. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31016. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31017. SSL_FILETYPE_PEM,NULL), 1);
  31018. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  31019. "certs/server-revoked-cert.pem",
  31020. SSL_FILETYPE_PEM,NULL), 1);
  31021. if (str) {
  31022. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  31023. WOLFSSL_FILETYPE_PEM), 1);
  31024. /* since store hasn't yet known the revoked cert*/
  31025. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31026. "certs/server-revoked-cert.pem",
  31027. WOLFSSL_FILETYPE_PEM), 1);
  31028. }
  31029. for (i = 0; pem[i][0] != '\0'; i++)
  31030. {
  31031. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  31032. SSL_FILETYPE_PEM, NULL), 1);
  31033. }
  31034. if (str) {
  31035. /* since store knows crl list */
  31036. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31037. "certs/server-revoked-cert.pem",
  31038. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  31039. }
  31040. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  31041. X509_STORE_free(str);
  31042. #endif
  31043. #endif
  31044. return EXPECT_RESULT();
  31045. }
  31046. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  31047. {
  31048. int res = TEST_SKIPPED;
  31049. #if defined(OPENSSL_EXTRA)
  31050. X509_STORE_CTX_cleanup(NULL);
  31051. X509_STORE_CTX_trusted_stack(NULL, NULL);
  31052. res = TEST_SUCCESS;
  31053. #endif
  31054. return res;
  31055. }
  31056. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  31057. {
  31058. EXPECT_DECLS;
  31059. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31060. X509_STORE_CTX* ctx = NULL;
  31061. X509_STORE* str = NULL;
  31062. X509* x509Ca = NULL;
  31063. X509* x509Svr = NULL;
  31064. X509* issuer = NULL;
  31065. X509_NAME* caName = NULL;
  31066. X509_NAME* issuerName = NULL;
  31067. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31068. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31069. ExpectNotNull((x509Ca =
  31070. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  31071. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  31072. ExpectNotNull((x509Svr =
  31073. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31074. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31075. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31076. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  31077. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  31078. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  31079. #ifdef WOLFSSL_SIGNER_DER_CERT
  31080. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  31081. #endif
  31082. X509_free(issuer);
  31083. X509_STORE_CTX_free(ctx);
  31084. X509_free(x509Svr);
  31085. X509_STORE_free(str);
  31086. X509_free(x509Ca);
  31087. #endif
  31088. return EXPECT_RESULT();
  31089. }
  31090. static int test_wolfSSL_PKCS7_certs(void)
  31091. {
  31092. EXPECT_DECLS;
  31093. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  31094. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  31095. STACK_OF(X509)* sk = NULL;
  31096. STACK_OF(X509_INFO)* info_sk = NULL;
  31097. PKCS7 *p7 = NULL;
  31098. BIO* bio = NULL;
  31099. const byte* p = NULL;
  31100. int buflen = 0;
  31101. int i;
  31102. /* Test twice. Once with d2i and once without to test
  31103. * that everything is free'd correctly. */
  31104. for (i = 0; i < 2; i++) {
  31105. ExpectNotNull(p7 = PKCS7_new());
  31106. if (p7 != NULL) {
  31107. p7->version = 1;
  31108. #ifdef NO_SHA
  31109. p7->hashOID = SHA256h;
  31110. #else
  31111. p7->hashOID = SHAh;
  31112. #endif
  31113. }
  31114. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  31115. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  31116. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  31117. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  31118. ExpectNotNull(sk = sk_X509_new_null());
  31119. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  31120. X509_INFO* info = NULL;
  31121. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  31122. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  31123. if (EXPECT_SUCCESS() && (info != NULL)) {
  31124. info->x509 = NULL;
  31125. }
  31126. X509_INFO_free(info);
  31127. }
  31128. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  31129. info_sk = NULL;
  31130. BIO_free(bio);
  31131. bio = NULL;
  31132. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  31133. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  31134. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  31135. sk_X509_pop_free(sk, X509_free);
  31136. }
  31137. sk = NULL;
  31138. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  31139. if (i == 0) {
  31140. PKCS7_free(p7);
  31141. p7 = NULL;
  31142. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  31143. if (p7 != NULL) {
  31144. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  31145. * them */
  31146. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  31147. }
  31148. /* PKCS7_free free's the certs */
  31149. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  31150. }
  31151. BIO_free(bio);
  31152. bio = NULL;
  31153. PKCS7_free(p7);
  31154. p7 = NULL;
  31155. }
  31156. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31157. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  31158. return EXPECT_RESULT();
  31159. }
  31160. static int test_wolfSSL_X509_STORE_CTX(void)
  31161. {
  31162. EXPECT_DECLS;
  31163. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31164. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31165. X509_STORE_CTX* ctx = NULL;
  31166. X509_STORE* str = NULL;
  31167. X509* x509 = NULL;
  31168. #ifdef OPENSSL_ALL
  31169. X509* x5092 = NULL;
  31170. STACK_OF(X509) *sk = NULL;
  31171. STACK_OF(X509) *sk2 = NULL;
  31172. STACK_OF(X509) *sk3 = NULL;
  31173. #endif
  31174. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31175. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31176. ExpectNotNull((x509 =
  31177. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31178. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  31179. #ifdef OPENSSL_ALL
  31180. /* sk_X509_new only in OPENSSL_ALL */
  31181. sk = sk_X509_new_null();
  31182. ExpectNotNull(sk);
  31183. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  31184. #else
  31185. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  31186. #endif
  31187. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  31188. X509_STORE_CTX_set_error(ctx, -5);
  31189. X509_STORE_CTX_set_error(NULL, -5);
  31190. X509_STORE_CTX_free(ctx);
  31191. ctx = NULL;
  31192. #ifdef OPENSSL_ALL
  31193. sk_X509_pop_free(sk, NULL);
  31194. sk = NULL;
  31195. #endif
  31196. X509_STORE_free(str);
  31197. str = NULL;
  31198. X509_free(x509);
  31199. x509 = NULL;
  31200. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31201. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  31202. X509_STORE_CTX_free(ctx);
  31203. ctx = NULL;
  31204. #ifdef OPENSSL_ALL
  31205. /* test X509_STORE_CTX_get(1)_chain */
  31206. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  31207. SSL_FILETYPE_PEM)));
  31208. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  31209. SSL_FILETYPE_PEM)));
  31210. ExpectNotNull((sk = sk_X509_new_null()));
  31211. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  31212. if (EXPECT_FAIL()) {
  31213. X509_free(x509);
  31214. x509 = NULL;
  31215. }
  31216. ExpectNotNull((str = X509_STORE_new()));
  31217. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  31218. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  31219. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  31220. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  31221. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  31222. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  31223. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  31224. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  31225. X509_STORE_CTX_free(ctx);
  31226. ctx = NULL;
  31227. X509_STORE_free(str);
  31228. str = NULL;
  31229. /* CTX certs not freed yet */
  31230. X509_free(x5092);
  31231. x5092 = NULL;
  31232. sk_X509_pop_free(sk, NULL);
  31233. sk = NULL;
  31234. /* sk3 is dup so free here */
  31235. sk_X509_pop_free(sk3, NULL);
  31236. sk3 = NULL;
  31237. #endif
  31238. /* test X509_STORE_CTX_get/set_ex_data */
  31239. {
  31240. int i = 0, tmpData = 5;
  31241. void* tmpDataRet;
  31242. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31243. #ifdef HAVE_EX_DATA
  31244. for (i = 0; i < MAX_EX_DATA; i++) {
  31245. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31246. WOLFSSL_SUCCESS);
  31247. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31248. ExpectNotNull(tmpDataRet);
  31249. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31250. }
  31251. #else
  31252. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31253. WOLFSSL_FAILURE);
  31254. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31255. ExpectNull(tmpDataRet);
  31256. #endif
  31257. X509_STORE_CTX_free(ctx);
  31258. ctx = NULL;
  31259. }
  31260. /* test X509_STORE_get/set_ex_data */
  31261. {
  31262. int i = 0, tmpData = 99;
  31263. void* tmpDataRet;
  31264. ExpectNotNull(str = X509_STORE_new());
  31265. #ifdef HAVE_EX_DATA
  31266. for (i = 0; i < MAX_EX_DATA; i++) {
  31267. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31268. WOLFSSL_SUCCESS);
  31269. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31270. ExpectNotNull(tmpDataRet);
  31271. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31272. }
  31273. #else
  31274. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31275. WOLFSSL_FAILURE);
  31276. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31277. ExpectNull(tmpDataRet);
  31278. #endif
  31279. X509_STORE_free(str);
  31280. str = NULL;
  31281. }
  31282. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31283. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31284. return EXPECT_RESULT();
  31285. }
  31286. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31287. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  31288. STACK_OF(X509)* chain)
  31289. {
  31290. EXPECT_DECLS;
  31291. XFILE fp = XBADFILE;
  31292. X509* cert = NULL;
  31293. ExpectTrue((fp = XFOPEN(filename, "rb"))
  31294. != XBADFILE);
  31295. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31296. if (fp != XBADFILE) {
  31297. XFCLOSE(fp);
  31298. fp = XBADFILE;
  31299. }
  31300. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  31301. if (EXPECT_FAIL())
  31302. X509_free(cert);
  31303. return EXPECT_RESULT();
  31304. }
  31305. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  31306. int err, int loadCA)
  31307. {
  31308. EXPECT_DECLS;
  31309. X509_STORE_CTX* ctx = NULL;
  31310. X509_STORE* str = NULL;
  31311. XFILE fp = XBADFILE;
  31312. X509* cert = NULL;
  31313. STACK_OF(X509)* untrusted = NULL;
  31314. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  31315. != XBADFILE);
  31316. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31317. if (fp != XBADFILE) {
  31318. XFCLOSE(fp);
  31319. fp = XBADFILE;
  31320. }
  31321. ExpectNotNull(str = X509_STORE_new());
  31322. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31323. ExpectNotNull(untrusted = sk_X509_new_null());
  31324. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  31325. if (loadCA) {
  31326. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  31327. 1);
  31328. }
  31329. for (; *filenames; filenames++) {
  31330. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  31331. untrusted), TEST_SUCCESS);
  31332. }
  31333. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  31334. ExpectIntEQ(X509_verify_cert(ctx), ret);
  31335. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  31336. X509_free(cert);
  31337. X509_STORE_free(str);
  31338. X509_STORE_CTX_free(ctx);
  31339. sk_X509_pop_free(untrusted, NULL);
  31340. return EXPECT_RESULT();
  31341. }
  31342. #endif
  31343. static int test_X509_STORE_untrusted(void)
  31344. {
  31345. EXPECT_DECLS;
  31346. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31347. const char* untrusted1[] = {
  31348. "./certs/intermediate/ca-int2-cert.pem",
  31349. NULL
  31350. };
  31351. const char* untrusted2[] = {
  31352. "./certs/intermediate/ca-int-cert.pem",
  31353. "./certs/intermediate/ca-int2-cert.pem",
  31354. NULL
  31355. };
  31356. const char* untrusted3[] = {
  31357. "./certs/intermediate/ca-int-cert.pem",
  31358. "./certs/intermediate/ca-int2-cert.pem",
  31359. "./certs/ca-cert.pem",
  31360. NULL
  31361. };
  31362. /* Adding unrelated certs that should be ignored */
  31363. const char* untrusted4[] = {
  31364. "./certs/client-ca.pem",
  31365. "./certs/intermediate/ca-int-cert.pem",
  31366. "./certs/server-cert.pem",
  31367. "./certs/intermediate/ca-int2-cert.pem",
  31368. NULL
  31369. };
  31370. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  31371. * to loaded CA. */
  31372. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  31373. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  31374. /* Succeeds because path to loaded CA is available. */
  31375. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  31376. TEST_SUCCESS);
  31377. /* Fails because root CA is in the untrusted stack */
  31378. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  31379. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  31380. /* Succeeds because path to loaded CA is available. */
  31381. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  31382. TEST_SUCCESS);
  31383. #endif
  31384. return EXPECT_RESULT();
  31385. }
  31386. static int test_wolfSSL_X509_STORE_set_flags(void)
  31387. {
  31388. EXPECT_DECLS;
  31389. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31390. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31391. X509_STORE* store = NULL;
  31392. X509* x509 = NULL;
  31393. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  31394. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31395. WOLFSSL_FILETYPE_PEM)));
  31396. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  31397. #ifdef HAVE_CRL
  31398. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31399. WOLFSSL_SUCCESS);
  31400. #else
  31401. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31402. NOT_COMPILED_IN);
  31403. #endif
  31404. wolfSSL_X509_free(x509);
  31405. wolfSSL_X509_STORE_free(store);
  31406. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31407. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31408. return EXPECT_RESULT();
  31409. }
  31410. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  31411. {
  31412. EXPECT_DECLS;
  31413. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  31414. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31415. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  31416. WOLFSSL_X509_STORE* store = NULL;
  31417. WOLFSSL_X509_LOOKUP* lookup = NULL;
  31418. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  31419. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  31420. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  31421. X509_FILETYPE_PEM), 1);
  31422. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  31423. X509_FILETYPE_PEM), 1);
  31424. if (store != NULL) {
  31425. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  31426. WOLFSSL_FILETYPE_PEM), 1);
  31427. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31428. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  31429. }
  31430. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  31431. X509_FILETYPE_PEM), 1);
  31432. if (store != NULL) {
  31433. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31434. WOLFSSL_FILETYPE_PEM), 1);
  31435. }
  31436. wolfSSL_X509_STORE_free(store);
  31437. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  31438. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31439. return EXPECT_RESULT();
  31440. }
  31441. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  31442. {
  31443. EXPECT_DECLS;
  31444. #if defined(OPENSSL_EXTRA)
  31445. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  31446. time_t c_time;
  31447. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  31448. c_time = 365*24*60*60;
  31449. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  31450. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  31451. WOLFSSL_USE_CHECK_TIME);
  31452. ExpectTrue(ctx->param->check_time == c_time);
  31453. wolfSSL_X509_STORE_CTX_free(ctx);
  31454. #endif /* OPENSSL_EXTRA */
  31455. return EXPECT_RESULT();
  31456. }
  31457. static int test_wolfSSL_CTX_get0_set1_param(void)
  31458. {
  31459. EXPECT_DECLS;
  31460. #if defined(OPENSSL_EXTRA)
  31461. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31462. SSL_CTX* ctx = NULL;
  31463. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31464. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  31465. char testIPv4[] = "127.0.0.1";
  31466. char testhostName[] = "foo.hoge.com";
  31467. #ifndef NO_WOLFSSL_SERVER
  31468. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31469. #else
  31470. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31471. #endif
  31472. ExpectNull(SSL_CTX_get0_param(NULL));
  31473. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  31474. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  31475. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  31476. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  31477. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  31478. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  31479. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  31480. WOLFSSL_SUCCESS);
  31481. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  31482. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  31483. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  31484. (int)XSTRLEN(testhostName)));
  31485. ExpectIntEQ(0x01, pParam->hostFlags);
  31486. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31487. /* test for incorrect parameter */
  31488. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  31489. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  31490. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  31491. SSL_CTX_free(ctx);
  31492. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  31493. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31494. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31495. return EXPECT_RESULT();
  31496. }
  31497. static int test_wolfSSL_get0_param(void)
  31498. {
  31499. EXPECT_DECLS;
  31500. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31501. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31502. SSL_CTX* ctx = NULL;
  31503. SSL* ssl = NULL;
  31504. #ifndef NO_WOLFSSL_SERVER
  31505. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31506. #else
  31507. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31508. #endif
  31509. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31510. SSL_FILETYPE_PEM));
  31511. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31512. ExpectNotNull(ssl = SSL_new(ctx));
  31513. ExpectNotNull(SSL_get0_param(ssl));
  31514. SSL_free(ssl);
  31515. SSL_CTX_free(ctx);
  31516. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31517. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31518. return EXPECT_RESULT();
  31519. }
  31520. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  31521. {
  31522. EXPECT_DECLS;
  31523. #if defined(OPENSSL_EXTRA)
  31524. const char host[] = "www.example.com";
  31525. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31526. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  31527. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31528. if (pParam != NULL) {
  31529. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31530. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  31531. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31532. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31533. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31534. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31535. }
  31536. #endif /* OPENSSL_EXTRA */
  31537. return EXPECT_RESULT();
  31538. }
  31539. static int test_wolfSSL_set1_host(void)
  31540. {
  31541. EXPECT_DECLS;
  31542. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31543. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31544. const char host[] = "www.test_wolfSSL_set1_host.com";
  31545. const char emptyStr[] = "";
  31546. SSL_CTX* ctx = NULL;
  31547. SSL* ssl = NULL;
  31548. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31549. #ifndef NO_WOLFSSL_SERVER
  31550. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31551. #else
  31552. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31553. #endif
  31554. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31555. SSL_FILETYPE_PEM));
  31556. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31557. ExpectNotNull(ssl = SSL_new(ctx));
  31558. pParam = SSL_get0_param(ssl);
  31559. /* we should get back host string */
  31560. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31561. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31562. /* we should get back empty string */
  31563. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  31564. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31565. /* we should get back host string */
  31566. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31567. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31568. /* we should get back empty string */
  31569. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  31570. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31571. SSL_free(ssl);
  31572. SSL_CTX_free(ctx);
  31573. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31574. #endif /* OPENSSL_EXTRA */
  31575. return EXPECT_RESULT();
  31576. }
  31577. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  31578. {
  31579. EXPECT_DECLS;
  31580. #if defined(OPENSSL_EXTRA)
  31581. unsigned char buf[16] = {0};
  31582. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  31583. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  31584. /* test 127.0.0.1 */
  31585. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  31586. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  31587. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  31588. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  31589. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31590. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  31591. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  31592. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  31593. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31594. ExpectIntEQ(XSTRNCMP(param->ipasc,
  31595. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  31596. /* test 2001:db8:: */
  31597. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31598. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31599. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31600. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  31601. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31602. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  31603. /* test ::1234:5678 */
  31604. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  31605. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31606. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31607. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31608. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31609. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  31610. /* test 2001:db8::1234:5678 */
  31611. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31612. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31613. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31614. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31615. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31616. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  31617. sizeof(param->ipasc)), 0);
  31618. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  31619. /* 2001:db8:1::ab9:c0a8:102 */
  31620. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31621. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  31622. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  31623. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  31624. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31625. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  31626. sizeof(param->ipasc)), 0);
  31627. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31628. #endif /* OPENSSL_EXTRA */
  31629. return EXPECT_RESULT();
  31630. }
  31631. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  31632. {
  31633. EXPECT_DECLS;
  31634. #if defined(OPENSSL_EXTRA)
  31635. X509_STORE* store = NULL;
  31636. X509_STORE_CTX* ctx = NULL;
  31637. X509_STORE_CTX* ctx_no_init = NULL;
  31638. ExpectNotNull((store = X509_STORE_new()));
  31639. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31640. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  31641. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  31642. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  31643. /* should return NULL if ctx has not bee initialized */
  31644. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  31645. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  31646. wolfSSL_X509_STORE_CTX_free(ctx);
  31647. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  31648. X509_STORE_free(store);
  31649. #endif /* OPENSSL_EXTRA */
  31650. return EXPECT_RESULT();
  31651. }
  31652. static int test_wolfSSL_CTX_set_client_CA_list(void)
  31653. {
  31654. EXPECT_DECLS;
  31655. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31656. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  31657. WOLFSSL_CTX* ctx = NULL;
  31658. WOLFSSL* ssl = NULL;
  31659. X509_NAME* name = NULL;
  31660. STACK_OF(X509_NAME)* names = NULL;
  31661. STACK_OF(X509_NAME)* ca_list = NULL;
  31662. int names_len = 0;
  31663. int i;
  31664. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31665. /* Send two X501 names in cert request */
  31666. names = SSL_load_client_CA_file(cliCertFile);
  31667. ExpectNotNull(names);
  31668. ca_list = SSL_load_client_CA_file(caCertFile);
  31669. ExpectNotNull(ca_list);
  31670. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  31671. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  31672. if (EXPECT_FAIL()) {
  31673. wolfSSL_X509_NAME_free(name);
  31674. name = NULL;
  31675. }
  31676. SSL_CTX_set_client_CA_list(ctx, names);
  31677. /* This should only free the stack structure */
  31678. sk_X509_NAME_free(ca_list);
  31679. ca_list = NULL;
  31680. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31681. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31682. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31683. for (i = 0; i < names_len; i++) {
  31684. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31685. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31686. }
  31687. /* Needed to be able to create ssl object */
  31688. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31689. SSL_FILETYPE_PEM));
  31690. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31691. ExpectNotNull(ssl = wolfSSL_new(ctx));
  31692. /* load again as old names are responsibility of ctx to free*/
  31693. names = SSL_load_client_CA_file(cliCertFile);
  31694. ExpectNotNull(names);
  31695. SSL_set_client_CA_list(ssl, names);
  31696. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  31697. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31698. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31699. for (i = 0; i < names_len; i++) {
  31700. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31701. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31702. }
  31703. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  31704. {
  31705. tcp_ready ready;
  31706. func_args server_args;
  31707. callback_functions server_cb;
  31708. THREAD_TYPE serverThread;
  31709. WOLFSSL* ssl_client = NULL;
  31710. WOLFSSL_CTX* ctx_client = NULL;
  31711. SOCKET_T sockfd = 0;
  31712. /* wolfSSL_get_client_CA_list() with handshake */
  31713. StartTCP();
  31714. InitTcpReady(&ready);
  31715. XMEMSET(&server_args, 0, sizeof(func_args));
  31716. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31717. server_args.signal = &ready;
  31718. server_args.callbacks = &server_cb;
  31719. /* we are responsible for free'ing WOLFSSL_CTX */
  31720. server_cb.ctx = ctx;
  31721. server_cb.isSharedCtx = 1;
  31722. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  31723. cliCertFile, 0));
  31724. start_thread(test_server_nofail, &server_args, &serverThread);
  31725. wait_tcp_ready(&server_args);
  31726. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31727. ExpectNotNull(ctx_client =
  31728. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  31729. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  31730. ctx_client, caCertFile, 0));
  31731. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  31732. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  31733. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  31734. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  31735. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  31736. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  31737. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  31738. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  31739. /* We are expecting two cert names to be sent */
  31740. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  31741. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  31742. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  31743. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  31744. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  31745. }
  31746. wolfSSL_shutdown(ssl_client);
  31747. wolfSSL_free(ssl_client);
  31748. wolfSSL_CTX_free(ctx_client);
  31749. CloseSocket(sockfd);
  31750. join_thread(serverThread);
  31751. FreeTcpReady(&ready);
  31752. }
  31753. #endif
  31754. wolfSSL_free(ssl);
  31755. wolfSSL_CTX_free(ctx);
  31756. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  31757. * !NO_BIO */
  31758. return EXPECT_RESULT();
  31759. }
  31760. static int test_wolfSSL_CTX_add_client_CA(void)
  31761. {
  31762. EXPECT_DECLS;
  31763. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31764. !defined(NO_WOLFSSL_CLIENT)
  31765. WOLFSSL_CTX* ctx = NULL;
  31766. WOLFSSL_X509* x509 = NULL;
  31767. WOLFSSL_X509* x509_a = NULL;
  31768. STACK_OF(X509_NAME)* ca_list = NULL;
  31769. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31770. /* Add client cert */
  31771. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31772. SSL_FILETYPE_PEM));
  31773. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  31774. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31775. /* Add another client cert */
  31776. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  31777. SSL_FILETYPE_PEM));
  31778. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  31779. /* test for incorrect parameter */
  31780. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  31781. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  31782. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  31783. X509_free(x509);
  31784. X509_free(x509_a);
  31785. SSL_CTX_free(ctx);
  31786. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  31787. return EXPECT_RESULT();
  31788. }
  31789. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  31790. defined(HAVE_IO_TESTS_DEPENDENCIES)
  31791. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  31792. {
  31793. callback_functions* callbacks = ((func_args*)args)->callbacks;
  31794. WOLFSSL_CTX* ctx = callbacks->ctx;
  31795. WOLFSSL* ssl = NULL;
  31796. SOCKET_T sfd = 0;
  31797. SOCKET_T cfd = 0;
  31798. word16 port;
  31799. char input[1024];
  31800. int idx;
  31801. int ret, err = 0;
  31802. const char* privateName = "ech-private-name.com";
  31803. int privateNameLen = (int)XSTRLEN(privateName);
  31804. ((func_args*)args)->return_code = TEST_FAIL;
  31805. port = ((func_args*)args)->signal->port;
  31806. AssertIntEQ(WOLFSSL_SUCCESS,
  31807. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  31808. AssertIntEQ(WOLFSSL_SUCCESS,
  31809. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31810. WOLFSSL_FILETYPE_PEM));
  31811. AssertIntEQ(WOLFSSL_SUCCESS,
  31812. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31813. WOLFSSL_FILETYPE_PEM));
  31814. if (callbacks->ctx_ready)
  31815. callbacks->ctx_ready(ctx);
  31816. ssl = wolfSSL_new(ctx);
  31817. /* set the sni for the server */
  31818. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  31819. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  31820. CloseSocket(sfd);
  31821. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  31822. if (callbacks->ssl_ready)
  31823. callbacks->ssl_ready(ssl);
  31824. do {
  31825. err = 0; /* Reset error */
  31826. ret = wolfSSL_accept(ssl);
  31827. if (ret != WOLFSSL_SUCCESS) {
  31828. err = wolfSSL_get_error(ssl, 0);
  31829. }
  31830. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  31831. if (ret != WOLFSSL_SUCCESS) {
  31832. char buff[WOLFSSL_MAX_ERROR_SZ];
  31833. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  31834. }
  31835. else {
  31836. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  31837. input[idx] = 0;
  31838. fprintf(stderr, "Client message: %s\n", input);
  31839. }
  31840. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  31841. privateNameLen));
  31842. ((func_args*)args)->return_code = TEST_SUCCESS;
  31843. }
  31844. if (callbacks->on_result)
  31845. callbacks->on_result(ssl);
  31846. wolfSSL_shutdown(ssl);
  31847. wolfSSL_free(ssl);
  31848. wolfSSL_CTX_free(ctx);
  31849. CloseSocket(cfd);
  31850. #ifdef FP_ECC
  31851. wc_ecc_fp_free();
  31852. #endif
  31853. WOLFSSL_RETURN_FROM_THREAD(0);
  31854. }
  31855. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  31856. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31857. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  31858. {
  31859. AssertNotNull(ssl);
  31860. AssertNotNull(line);
  31861. XFILE fp;
  31862. const byte lf = '\n';
  31863. fp = XFOPEN("./MyKeyLog.txt", "a");
  31864. XFWRITE( line, 1, strlen(line),fp);
  31865. XFWRITE( (void*)&lf,1,1,fp);
  31866. XFFLUSH(fp);
  31867. XFCLOSE(fp);
  31868. }
  31869. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31870. static int test_wolfSSL_CTX_set_keylog_callback(void)
  31871. {
  31872. EXPECT_DECLS;
  31873. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31874. !defined(NO_WOLFSSL_CLIENT)
  31875. SSL_CTX* ctx = NULL;
  31876. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31877. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31878. SSL_CTX_free(ctx);
  31879. SSL_CTX_set_keylog_callback(NULL, NULL);
  31880. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31881. return EXPECT_RESULT();
  31882. }
  31883. static int test_wolfSSL_CTX_get_keylog_callback(void)
  31884. {
  31885. EXPECT_DECLS;
  31886. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31887. !defined(NO_WOLFSSL_CLIENT)
  31888. SSL_CTX* ctx = NULL;
  31889. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31890. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31891. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31892. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  31893. SSL_CTX_set_keylog_callback(ctx, NULL );
  31894. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31895. SSL_CTX_free(ctx);
  31896. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31897. return EXPECT_RESULT();
  31898. }
  31899. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31900. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31901. {
  31902. /* set keylog callback */
  31903. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31904. return TEST_SUCCESS;
  31905. }
  31906. #endif
  31907. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  31908. {
  31909. EXPECT_DECLS;
  31910. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31911. /* This test is intended for checking whether keylog callback is called
  31912. * in client during TLS handshake between the client and a server.
  31913. */
  31914. test_ssl_cbf server_cbf;
  31915. test_ssl_cbf client_cbf;
  31916. XFILE fp = XBADFILE;
  31917. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31918. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31919. server_cbf.method = wolfTLSv1_2_server_method;
  31920. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  31921. /* clean up keylog file */
  31922. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31923. if (fp != XBADFILE) {
  31924. XFCLOSE(fp);
  31925. fp = XBADFILE;
  31926. }
  31927. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31928. &server_cbf, NULL), TEST_SUCCESS);
  31929. /* check if the keylog file exists */
  31930. char buff[300] = {0};
  31931. int found = 0;
  31932. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  31933. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  31934. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  31935. found = 1;
  31936. break;
  31937. }
  31938. }
  31939. if (fp != XBADFILE) {
  31940. XFCLOSE(fp);
  31941. }
  31942. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  31943. ExpectIntEQ(found, 1);
  31944. /* clean up */
  31945. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  31946. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31947. return EXPECT_RESULT();
  31948. }
  31949. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31950. defined(HAVE_SECRET_CALLBACK)
  31951. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31952. {
  31953. /* set keylog callback */
  31954. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31955. return TEST_SUCCESS;
  31956. }
  31957. #endif
  31958. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  31959. {
  31960. EXPECT_DECLS;
  31961. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31962. defined(HAVE_SECRET_CALLBACK)
  31963. /* This test is intended for checking whether keylog callback is called
  31964. * in client during TLS handshake between the client and a server.
  31965. */
  31966. test_ssl_cbf server_cbf;
  31967. test_ssl_cbf client_cbf;
  31968. XFILE fp = XBADFILE;
  31969. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31970. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31971. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  31972. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  31973. /* clean up keylog file */
  31974. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31975. if (fp != XBADFILE) {
  31976. XFCLOSE(fp);
  31977. fp = XBADFILE;
  31978. }
  31979. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31980. &server_cbf, NULL), TEST_SUCCESS);
  31981. /* check if the keylog file exists */
  31982. {
  31983. char buff[300] = {0};
  31984. int found[4] = {0};
  31985. int numfnd = 0;
  31986. int i;
  31987. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  31988. while (EXPECT_SUCCESS() &&
  31989. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  31990. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  31991. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31992. found[0] = 1;
  31993. continue;
  31994. }
  31995. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  31996. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31997. found[1] = 1;
  31998. continue;
  31999. }
  32000. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  32001. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  32002. found[2] = 1;
  32003. continue;
  32004. }
  32005. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  32006. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  32007. found[3] = 1;
  32008. continue;
  32009. }
  32010. }
  32011. if (fp != XBADFILE)
  32012. XFCLOSE(fp);
  32013. for (i = 0; i < 4; i++) {
  32014. if (found[i] != 0)
  32015. numfnd++;
  32016. }
  32017. ExpectIntEQ(numfnd, 4);
  32018. }
  32019. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  32020. return EXPECT_RESULT();
  32021. }
  32022. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32023. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32024. static int test_wolfSSL_Tls13_ECH_params(void)
  32025. {
  32026. EXPECT_DECLS;
  32027. #if !defined(NO_WOLFSSL_CLIENT)
  32028. word32 outputLen = 0;
  32029. byte testBuf[72];
  32030. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  32031. WOLFSSL *ssl = wolfSSL_new(ctx);
  32032. ExpectNotNull(ctx);
  32033. ExpectNotNull(ssl);
  32034. /* invalid ctx */
  32035. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  32036. "ech-public-name.com", 0, 0, 0));
  32037. /* invalid public name */
  32038. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  32039. 0, 0));
  32040. /* invalid algorithms */
  32041. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  32042. "ech-public-name.com", 1000, 1000, 1000));
  32043. /* invalid ctx */
  32044. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  32045. &outputLen));
  32046. /* invalid output len */
  32047. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  32048. /* invalid ssl */
  32049. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  32050. (char*)testBuf, sizeof(testBuf)));
  32051. /* invalid configs64 */
  32052. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  32053. sizeof(testBuf)));
  32054. /* invalid size */
  32055. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  32056. (char*)testBuf, 0));
  32057. /* invalid ssl */
  32058. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  32059. sizeof(testBuf)));
  32060. /* invalid configs */
  32061. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  32062. sizeof(testBuf)));
  32063. /* invalid size */
  32064. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  32065. /* invalid ssl */
  32066. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  32067. /* invalid size */
  32068. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  32069. wolfSSL_free(ssl);
  32070. wolfSSL_CTX_free(ctx);
  32071. #endif /* !NO_WOLFSSL_CLIENT */
  32072. return EXPECT_RESULT();
  32073. }
  32074. static int test_wolfSSL_Tls13_ECH(void)
  32075. {
  32076. EXPECT_DECLS;
  32077. tcp_ready ready;
  32078. func_args client_args;
  32079. func_args server_args;
  32080. THREAD_TYPE serverThread;
  32081. callback_functions server_cbf;
  32082. callback_functions client_cbf;
  32083. SOCKET_T sockfd = 0;
  32084. WOLFSSL_CTX* ctx = NULL;
  32085. WOLFSSL* ssl = NULL;
  32086. const char* publicName = "ech-public-name.com";
  32087. const char* privateName = "ech-private-name.com";
  32088. int privateNameLen = 20;
  32089. char reply[1024];
  32090. int replyLen = 0;
  32091. byte rawEchConfig[128];
  32092. word32 rawEchConfigLen = sizeof(rawEchConfig);
  32093. InitTcpReady(&ready);
  32094. ready.port = 22222;
  32095. XMEMSET(&client_args, 0, sizeof(func_args));
  32096. XMEMSET(&server_args, 0, sizeof(func_args));
  32097. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  32098. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  32099. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32100. /* create the server context here so we can get the ech config */
  32101. ExpectNotNull(server_cbf.ctx =
  32102. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  32103. /* generate ech config */
  32104. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  32105. publicName, 0, 0, 0));
  32106. /* get the config for the client to use */
  32107. ExpectIntEQ(WOLFSSL_SUCCESS,
  32108. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  32109. &rawEchConfigLen));
  32110. server_args.callbacks = &server_cbf;
  32111. server_args.signal = &ready;
  32112. /* start server task */
  32113. start_thread(server_task_ech, &server_args, &serverThread);
  32114. wait_tcp_ready(&server_args);
  32115. /* run as a TLS1.3 client */
  32116. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  32117. ExpectIntEQ(WOLFSSL_SUCCESS,
  32118. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32119. ExpectIntEQ(WOLFSSL_SUCCESS,
  32120. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32121. ExpectIntEQ(WOLFSSL_SUCCESS,
  32122. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32123. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32124. /* get connected the server task */
  32125. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32126. /* set the ech configs for the client */
  32127. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  32128. rawEchConfigLen));
  32129. /* set the sni for the client */
  32130. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  32131. privateName, privateNameLen));
  32132. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32133. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32134. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  32135. privateNameLen);
  32136. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  32137. /* add th null terminator for string compare */
  32138. reply[replyLen] = 0;
  32139. /* check that the server replied with the private name */
  32140. ExpectStrEQ(privateName, reply);
  32141. wolfSSL_free(ssl);
  32142. wolfSSL_CTX_free(ctx);
  32143. CloseSocket(sockfd);
  32144. join_thread(serverThread);
  32145. FreeTcpReady(&ready);
  32146. return EXPECT_RESULT();
  32147. }
  32148. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32149. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32150. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32151. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32152. static int post_auth_version_cb(WOLFSSL* ssl)
  32153. {
  32154. EXPECT_DECLS;
  32155. /* do handshake and then test version error */
  32156. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32157. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32158. return EXPECT_RESULT();
  32159. }
  32160. static int post_auth_version_client_cb(WOLFSSL* ssl)
  32161. {
  32162. EXPECT_DECLS;
  32163. /* do handshake and then test version error */
  32164. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32165. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32166. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  32167. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  32168. /* check was added to error queue */
  32169. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  32170. /* check the string matches expected string */
  32171. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  32172. "WRONG_SSL_VERSION");
  32173. #endif
  32174. return EXPECT_RESULT();
  32175. }
  32176. static int post_auth_cb(WOLFSSL* ssl)
  32177. {
  32178. EXPECT_DECLS;
  32179. WOLFSSL_X509* x509 = NULL;
  32180. /* do handshake and then test version error */
  32181. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32182. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  32183. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  32184. wolfSSL_X509_free(x509);
  32185. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  32186. return EXPECT_RESULT();
  32187. }
  32188. static int set_post_auth_cb(WOLFSSL* ssl)
  32189. {
  32190. if (!wolfSSL_is_server(ssl)) {
  32191. EXPECT_DECLS;
  32192. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  32193. return EXPECT_RESULT();
  32194. }
  32195. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  32196. return TEST_SUCCESS;
  32197. }
  32198. #endif
  32199. static int test_wolfSSL_Tls13_postauth(void)
  32200. {
  32201. EXPECT_DECLS;
  32202. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32203. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32204. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32205. test_ssl_cbf server_cbf;
  32206. test_ssl_cbf client_cbf;
  32207. /* test version failure doing post auth with TLS 1.2 connection */
  32208. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32209. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32210. server_cbf.method = wolfTLSv1_2_server_method;
  32211. server_cbf.ssl_ready = set_post_auth_cb;
  32212. server_cbf.on_result = post_auth_version_cb;
  32213. client_cbf.ssl_ready = set_post_auth_cb;
  32214. client_cbf.on_result = post_auth_version_client_cb;
  32215. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32216. &server_cbf, NULL), TEST_SUCCESS);
  32217. /* tests on post auth with TLS 1.3 */
  32218. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32219. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32220. server_cbf.method = wolfTLSv1_3_server_method;
  32221. server_cbf.ssl_ready = set_post_auth_cb;
  32222. client_cbf.ssl_ready = set_post_auth_cb;
  32223. server_cbf.on_result = post_auth_cb;
  32224. client_cbf.on_result = NULL;
  32225. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32226. &server_cbf, NULL), TEST_SUCCESS);
  32227. #endif
  32228. return EXPECT_RESULT();
  32229. }
  32230. static int test_wolfSSL_X509_NID(void)
  32231. {
  32232. EXPECT_DECLS;
  32233. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  32234. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  32235. int sigType;
  32236. int nameSz;
  32237. X509* cert = NULL;
  32238. EVP_PKEY* pubKeyTmp = NULL;
  32239. X509_NAME* name = NULL;
  32240. char commonName[80];
  32241. char countryName[80];
  32242. char localityName[80];
  32243. char stateName[80];
  32244. char orgName[80];
  32245. char orgUnit[80];
  32246. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  32247. /* convert cert from DER to internal WOLFSSL_X509 struct */
  32248. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  32249. sizeof_client_cert_der_2048));
  32250. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  32251. /* extract PUBLIC KEY from cert */
  32252. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  32253. /* extract signatureType */
  32254. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  32255. /* extract subjectName info */
  32256. ExpectNotNull(name = X509_get_subject_name(cert));
  32257. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  32258. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32259. NULL, 0)), 0);
  32260. ExpectIntEQ(nameSz, 15);
  32261. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32262. commonName, sizeof(commonName))), 0);
  32263. ExpectIntEQ(nameSz, 15);
  32264. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  32265. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32266. commonName, 9)), 0);
  32267. ExpectIntEQ(nameSz, 8);
  32268. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  32269. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  32270. countryName, sizeof(countryName))), 0);
  32271. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  32272. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  32273. localityName, sizeof(localityName))), 0);
  32274. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  32275. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32276. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  32277. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  32278. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  32279. orgName, sizeof(orgName))), 0);
  32280. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  32281. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32282. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  32283. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  32284. EVP_PKEY_free(pubKeyTmp);
  32285. X509_free(cert);
  32286. #endif
  32287. return EXPECT_RESULT();
  32288. }
  32289. static int test_wolfSSL_CTX_set_srp_username(void)
  32290. {
  32291. EXPECT_DECLS;
  32292. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  32293. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32294. WOLFSSL_CTX* ctx = NULL;
  32295. WOLFSSL* ssl = NULL;
  32296. const char *username = "TESTUSER";
  32297. const char *password = "TESTPASSWORD";
  32298. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32299. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32300. SSL_SUCCESS);
  32301. wolfSSL_CTX_free(ctx);
  32302. ctx = NULL;
  32303. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32304. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32305. SSL_SUCCESS);
  32306. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32307. SSL_SUCCESS);
  32308. ExpectNotNull(ssl = SSL_new(ctx));
  32309. ExpectNotNull(SSL_get_srp_username(ssl));
  32310. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  32311. wolfSSL_free(ssl);
  32312. wolfSSL_CTX_free(ctx);
  32313. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32314. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32315. return EXPECT_RESULT();
  32316. }
  32317. static int test_wolfSSL_CTX_set_srp_password(void)
  32318. {
  32319. EXPECT_DECLS;
  32320. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  32321. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32322. WOLFSSL_CTX* ctx = NULL;
  32323. const char *username = "TESTUSER";
  32324. const char *password = "TESTPASSWORD";
  32325. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32326. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32327. SSL_SUCCESS);
  32328. wolfSSL_CTX_free(ctx);
  32329. ctx = NULL;
  32330. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32331. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32332. SSL_SUCCESS);
  32333. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32334. SSL_SUCCESS);
  32335. wolfSSL_CTX_free(ctx);
  32336. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32337. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32338. return EXPECT_RESULT();
  32339. }
  32340. static int test_wolfSSL_X509_STORE(void)
  32341. {
  32342. EXPECT_DECLS;
  32343. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32344. X509_STORE *store = NULL;
  32345. #ifdef HAVE_CRL
  32346. X509_STORE_CTX *storeCtx = NULL;
  32347. X509_CRL *crl = NULL;
  32348. X509 *ca = NULL;
  32349. X509 *cert = NULL;
  32350. const char crlPem[] = "./certs/crl/crl.revoked";
  32351. const char srvCert[] = "./certs/server-revoked-cert.pem";
  32352. const char caCert[] = "./certs/ca-cert.pem";
  32353. XFILE fp = XBADFILE;
  32354. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32355. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32356. SSL_FILETYPE_PEM)));
  32357. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32358. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32359. SSL_FILETYPE_PEM)));
  32360. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32361. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32362. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32363. X509_STORE_free(store);
  32364. store = NULL;
  32365. X509_STORE_CTX_free(storeCtx);
  32366. storeCtx = NULL;
  32367. X509_free(cert);
  32368. cert = NULL;
  32369. X509_free(ca);
  32370. ca = NULL;
  32371. /* should fail to verify now after adding in CRL */
  32372. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32373. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32374. SSL_FILETYPE_PEM)));
  32375. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32376. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  32377. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  32378. NULL, NULL));
  32379. if (fp != XBADFILE)
  32380. XFCLOSE(fp);
  32381. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  32382. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  32383. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32384. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32385. SSL_FILETYPE_PEM)));
  32386. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32387. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32388. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  32389. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  32390. X509_CRL_free(crl);
  32391. crl = NULL;
  32392. X509_STORE_free(store);
  32393. store = NULL;
  32394. X509_STORE_CTX_free(storeCtx);
  32395. storeCtx = NULL;
  32396. X509_free(cert);
  32397. cert = NULL;
  32398. X509_free(ca);
  32399. ca = NULL;
  32400. #endif /* HAVE_CRL */
  32401. #ifndef WOLFCRYPT_ONLY
  32402. {
  32403. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32404. SSL_CTX* ctx = NULL;
  32405. SSL* ssl = NULL;
  32406. int i;
  32407. for (i = 0; i < 2; i++) {
  32408. #ifndef NO_WOLFSSL_SERVER
  32409. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32410. #else
  32411. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32412. #endif
  32413. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32414. SSL_CTX_set_cert_store(ctx, store);
  32415. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32416. SSL_CTX_set_cert_store(ctx, store);
  32417. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32418. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32419. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32420. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32421. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32422. ExpectNotNull(ssl = SSL_new(ctx));
  32423. if (i == 0) {
  32424. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  32425. SSL_SUCCESS);
  32426. }
  32427. else {
  32428. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  32429. #ifdef OPENSSL_ALL
  32430. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  32431. #endif
  32432. }
  32433. if (EXPECT_FAIL() || (i == 1)) {
  32434. X509_STORE_free(store);
  32435. store = NULL;
  32436. }
  32437. SSL_free(ssl);
  32438. ssl = NULL;
  32439. SSL_CTX_free(ctx);
  32440. ctx = NULL;
  32441. }
  32442. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32443. }
  32444. #endif
  32445. #endif
  32446. return EXPECT_RESULT();
  32447. }
  32448. static int test_wolfSSL_X509_STORE_load_locations(void)
  32449. {
  32450. EXPECT_DECLS;
  32451. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32452. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32453. SSL_CTX *ctx = NULL;
  32454. X509_STORE *store = NULL;
  32455. const char ca_file[] = "./certs/ca-cert.pem";
  32456. const char client_pem_file[] = "./certs/client-cert.pem";
  32457. const char client_der_file[] = "./certs/client-cert.der";
  32458. const char ecc_file[] = "./certs/ecc-key.pem";
  32459. const char certs_path[] = "./certs/";
  32460. const char bad_path[] = "./bad-path/";
  32461. #ifdef HAVE_CRL
  32462. const char crl_path[] = "./certs/crl/";
  32463. const char crl_file[] = "./certs/crl/crl.pem";
  32464. #endif
  32465. #ifndef NO_WOLFSSL_SERVER
  32466. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32467. #else
  32468. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32469. #endif
  32470. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32471. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  32472. WOLFSSL_SUCCESS);
  32473. /* Test bad arguments */
  32474. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  32475. WOLFSSL_FAILURE);
  32476. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  32477. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  32478. WOLFSSL_FAILURE);
  32479. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  32480. WOLFSSL_FAILURE);
  32481. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  32482. WOLFSSL_FAILURE);
  32483. #ifdef HAVE_CRL
  32484. /* Test with CRL */
  32485. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  32486. WOLFSSL_SUCCESS);
  32487. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  32488. WOLFSSL_SUCCESS);
  32489. #endif
  32490. /* Test with CA */
  32491. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  32492. WOLFSSL_SUCCESS);
  32493. /* Test with client_cert and certs path */
  32494. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  32495. WOLFSSL_SUCCESS);
  32496. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  32497. WOLFSSL_SUCCESS);
  32498. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32499. /* Clear nodes */
  32500. ERR_clear_error();
  32501. #endif
  32502. SSL_CTX_free(ctx);
  32503. #endif
  32504. return EXPECT_RESULT();
  32505. }
  32506. static int test_X509_STORE_get0_objects(void)
  32507. {
  32508. EXPECT_DECLS;
  32509. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  32510. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32511. X509_STORE *store = NULL;
  32512. X509_STORE *store_cpy = NULL;
  32513. SSL_CTX *ctx = NULL;
  32514. X509_OBJECT *obj = NULL;
  32515. STACK_OF(X509_OBJECT) *objs = NULL;
  32516. int i;
  32517. /* Setup store */
  32518. #ifndef NO_WOLFSSL_SERVER
  32519. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32520. #else
  32521. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32522. #endif
  32523. ExpectNotNull(store_cpy = X509_STORE_new());
  32524. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32525. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  32526. WOLFSSL_SUCCESS);
  32527. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  32528. WOLFSSL_SUCCESS);
  32529. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  32530. WOLFSSL_SUCCESS);
  32531. #ifdef HAVE_CRL
  32532. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  32533. WOLFSSL_SUCCESS);
  32534. #endif
  32535. /* Store ready */
  32536. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  32537. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  32538. #ifdef HAVE_CRL
  32539. #ifdef WOLFSSL_SIGNER_DER_CERT
  32540. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  32541. #else
  32542. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  32543. #endif
  32544. #else
  32545. #ifdef WOLFSSL_SIGNER_DER_CERT
  32546. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  32547. #else
  32548. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  32549. #endif
  32550. #endif
  32551. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  32552. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  32553. switch (X509_OBJECT_get_type(obj)) {
  32554. case X509_LU_X509:
  32555. {
  32556. WOLFSSL_X509* x509;
  32557. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  32558. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  32559. break;
  32560. }
  32561. case X509_LU_CRL:
  32562. #ifdef HAVE_CRL
  32563. {
  32564. WOLFSSL_CRL* crl = NULL;
  32565. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  32566. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  32567. break;
  32568. }
  32569. #endif
  32570. case X509_LU_NONE:
  32571. default:
  32572. Fail(("X509_OBJECT_get_type should return x509 or crl "
  32573. "(when built with crl support)"),
  32574. ("Unrecognized X509_OBJECT type or none"));
  32575. }
  32576. }
  32577. X509_STORE_free(store_cpy);
  32578. SSL_CTX_free(ctx);
  32579. #endif
  32580. return EXPECT_RESULT();
  32581. }
  32582. static int test_wolfSSL_BN_CTX(void)
  32583. {
  32584. EXPECT_DECLS;
  32585. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32586. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32587. WOLFSSL_BN_CTX* bn_ctx = NULL;
  32588. WOLFSSL_BIGNUM* t = NULL;
  32589. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  32590. /* No implementation. */
  32591. BN_CTX_init(NULL);
  32592. ExpectNotNull(t = BN_CTX_get(NULL));
  32593. BN_free(t);
  32594. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  32595. BN_free(t);
  32596. #ifndef NO_WOLFSSL_STUB
  32597. /* No implementation. */
  32598. BN_CTX_start(NULL);
  32599. BN_CTX_start(bn_ctx);
  32600. #endif
  32601. BN_CTX_free(NULL);
  32602. BN_CTX_free(bn_ctx);
  32603. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32604. return EXPECT_RESULT();
  32605. }
  32606. static int test_wolfSSL_BN(void)
  32607. {
  32608. EXPECT_DECLS;
  32609. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32610. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32611. BIGNUM* a = NULL;
  32612. BIGNUM* b = NULL;
  32613. BIGNUM* c = NULL;
  32614. BIGNUM* d = NULL;
  32615. BIGNUM emptyBN;
  32616. /* Setup */
  32617. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32618. /* internal not set emptyBN. */
  32619. ExpectNotNull(a = BN_new());
  32620. ExpectNotNull(b = BN_new());
  32621. ExpectNotNull(c = BN_dup(b));
  32622. ExpectNotNull(d = BN_new());
  32623. /* Invalid parameter testing. */
  32624. BN_free(NULL);
  32625. ExpectNull(BN_dup(NULL));
  32626. ExpectNull(BN_dup(&emptyBN));
  32627. ExpectNull(BN_copy(NULL, NULL));
  32628. ExpectNull(BN_copy(b, NULL));
  32629. ExpectNull(BN_copy(NULL, c));
  32630. ExpectNull(BN_copy(b, &emptyBN));
  32631. ExpectNull(BN_copy(&emptyBN, c));
  32632. BN_clear(NULL);
  32633. BN_clear(&emptyBN);
  32634. ExpectIntEQ(BN_num_bytes(NULL), 0);
  32635. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  32636. ExpectIntEQ(BN_num_bits(NULL), 0);
  32637. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  32638. ExpectIntEQ(BN_is_negative(NULL), 0);
  32639. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  32640. /* END Invalid Parameters */
  32641. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  32642. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  32643. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  32644. ExpectIntEQ(BN_num_bits(a), 2);
  32645. ExpectIntEQ(BN_num_bytes(a), 1);
  32646. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  32647. defined(WOLFSSL_SP_INT_NEGATIVE))
  32648. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  32649. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  32650. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  32651. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  32652. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  32653. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32654. {
  32655. /* Do additional tests on negative BN conversions. */
  32656. char* ret = NULL;
  32657. ASN1_INTEGER* asn1 = NULL;
  32658. BIGNUM* tmp = NULL;
  32659. /* Sanity check we have a negative BN. */
  32660. ExpectIntEQ(BN_is_negative(c), 1);
  32661. ExpectNotNull(ret = BN_bn2dec(c));
  32662. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32663. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32664. ret = NULL;
  32665. /* Convert to ASN1_INTEGER and back to BN. */
  32666. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  32667. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  32668. /* After converting back BN should be negative and correct. */
  32669. ExpectIntEQ(BN_is_negative(tmp), 1);
  32670. ExpectNotNull(ret = BN_bn2dec(tmp));
  32671. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32672. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32673. ASN1_INTEGER_free(asn1);
  32674. BN_free(tmp);
  32675. }
  32676. #endif
  32677. ExpectIntEQ(BN_get_word(c), 4);
  32678. #endif
  32679. ExpectIntEQ(BN_set_word(a, 3), 1);
  32680. ExpectIntEQ(BN_set_word(b, 3), 1);
  32681. ExpectIntEQ(BN_set_word(c, 4), 1);
  32682. /* NULL == NULL, NULL < num, num > NULL */
  32683. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  32684. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  32685. ExpectIntLT(BN_cmp(NULL, b), 0);
  32686. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  32687. ExpectIntGT(BN_cmp(a, NULL), 0);
  32688. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  32689. ExpectIntEQ(BN_cmp(a, b), 0);
  32690. ExpectIntLT(BN_cmp(a, c), 0);
  32691. ExpectIntGT(BN_cmp(c, b), 0);
  32692. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  32693. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  32694. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  32695. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  32696. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  32697. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  32698. BN_clear(a);
  32699. BN_free(a);
  32700. BN_free(b);
  32701. BN_free(c);
  32702. BN_clear_free(d);
  32703. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32704. return EXPECT_RESULT();
  32705. }
  32706. static int test_wolfSSL_BN_init(void)
  32707. {
  32708. EXPECT_DECLS;
  32709. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32710. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32711. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  32712. BIGNUM* ap = NULL;
  32713. BIGNUM bv;
  32714. BIGNUM cv;
  32715. BIGNUM dv;
  32716. ExpectNotNull(ap = BN_new());
  32717. BN_init(NULL);
  32718. XMEMSET(&bv, 0, sizeof(bv));
  32719. ExpectNull(BN_dup(&bv));
  32720. BN_init(&bv);
  32721. BN_init(&cv);
  32722. BN_init(&dv);
  32723. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  32724. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  32725. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  32726. /* a^b mod c = */
  32727. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  32728. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  32729. /* check result 3^2 mod 5 */
  32730. ExpectIntEQ(BN_get_word(&dv), 4);
  32731. /* a*b mod c = */
  32732. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  32733. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  32734. /* check result 3*2 mod 5 */
  32735. ExpectIntEQ(BN_get_word(&dv), 1);
  32736. BN_free(ap);
  32737. #endif
  32738. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32739. return EXPECT_RESULT();
  32740. }
  32741. static int test_wolfSSL_BN_enc_dec(void)
  32742. {
  32743. EXPECT_DECLS;
  32744. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32745. BIGNUM* a = NULL;
  32746. BIGNUM* b = NULL;
  32747. BIGNUM* c = NULL;
  32748. BIGNUM emptyBN;
  32749. char* str = NULL;
  32750. const char* emptyStr = "";
  32751. const char* numberStr = "12345";
  32752. const char* badStr = "g12345";
  32753. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32754. const char* twoStr = "2";
  32755. #endif
  32756. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  32757. unsigned char outNum[5];
  32758. /* Setup */
  32759. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32760. ExpectNotNull(a = BN_new());
  32761. ExpectNotNull(b = BN_new());
  32762. ExpectIntEQ(BN_set_word(a, 2), 1);
  32763. /* Invalid parameters */
  32764. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  32765. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  32766. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  32767. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  32768. ExpectNull(BN_bn2hex(NULL));
  32769. ExpectNull(BN_bn2hex(&emptyBN));
  32770. ExpectNull(BN_bn2dec(NULL));
  32771. ExpectNull(BN_bn2dec(&emptyBN));
  32772. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  32773. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  32774. ExpectNull(BN_bin2bn(binNum, -1, a));
  32775. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  32776. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  32777. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  32778. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  32779. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  32780. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  32781. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  32782. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  32783. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  32784. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  32785. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  32786. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  32787. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  32788. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  32789. ExpectIntEQ(BN_set_word(a, 2), 1);
  32790. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  32791. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  32792. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  32793. ExpectIntEQ(BN_cmp(a, b), 0);
  32794. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  32795. ExpectIntEQ(BN_cmp(a, b), -1);
  32796. ExpectNotNull(str = BN_bn2hex(a));
  32797. ExpectNotNull(BN_hex2bn(&b, str));
  32798. ExpectIntEQ(BN_cmp(a, b), 0);
  32799. ExpectNotNull(BN_hex2bn(&b, numberStr));
  32800. ExpectIntEQ(BN_cmp(a, b), -1);
  32801. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32802. str = NULL;
  32803. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32804. ExpectNotNull(str = BN_bn2dec(a));
  32805. ExpectStrEQ(str, twoStr);
  32806. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32807. str = NULL;
  32808. #ifndef NO_RSA
  32809. ExpectNotNull(str = BN_bn2dec(a));
  32810. ExpectNotNull(BN_dec2bn(&b, str));
  32811. ExpectIntEQ(BN_cmp(a, b), 0);
  32812. ExpectNotNull(BN_dec2bn(&b, numberStr));
  32813. ExpectIntEQ(BN_cmp(a, b), -1);
  32814. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32815. str = NULL;
  32816. #else
  32817. /* No implementation - fail with good parameters. */
  32818. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  32819. #endif
  32820. #endif
  32821. BN_free(b);
  32822. BN_free(a);
  32823. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32824. return EXPECT_RESULT();
  32825. }
  32826. static int test_wolfSSL_BN_word(void)
  32827. {
  32828. EXPECT_DECLS;
  32829. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32830. BIGNUM* a = NULL;
  32831. BIGNUM* b = NULL;
  32832. BIGNUM* c = NULL;
  32833. BIGNUM av;
  32834. ExpectNotNull(a = BN_new());
  32835. ExpectNotNull(b = BN_new());
  32836. ExpectNotNull(c = BN_new());
  32837. XMEMSET(&av, 0, sizeof(av));
  32838. /* Invalid parameter. */
  32839. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  32840. ExpectIntEQ(BN_add_word(&av, 3), 0);
  32841. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  32842. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  32843. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  32844. ExpectIntEQ(BN_set_word(&av, 3), 0);
  32845. ExpectIntEQ(BN_get_word(NULL), 0);
  32846. ExpectIntEQ(BN_get_word(&av), 0);
  32847. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  32848. ExpectIntEQ(BN_is_word(&av, 3), 0);
  32849. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32850. !defined(NO_DSA))
  32851. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  32852. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  32853. #endif
  32854. ExpectIntEQ(BN_one(NULL), 0);
  32855. ExpectIntEQ(BN_one(&av), 0);
  32856. BN_zero(NULL);
  32857. BN_zero(&av);
  32858. ExpectIntEQ(BN_is_one(NULL), 0);
  32859. ExpectIntEQ(BN_is_one(&av), 0);
  32860. ExpectIntEQ(BN_is_zero(NULL), 0);
  32861. ExpectIntEQ(BN_is_zero(&av), 0);
  32862. ExpectIntEQ(BN_set_word(a, 3), 1);
  32863. ExpectIntEQ(BN_set_word(b, 2), 1);
  32864. ExpectIntEQ(BN_set_word(c, 5), 1);
  32865. /* a + 3 = */
  32866. ExpectIntEQ(BN_add_word(a, 3), 1);
  32867. /* check result 3 + 3*/
  32868. ExpectIntEQ(BN_get_word(a), 6);
  32869. ExpectIntEQ(BN_is_word(a, 6), 1);
  32870. ExpectIntEQ(BN_is_word(a, 5), 0);
  32871. /* set a back to 3 */
  32872. ExpectIntEQ(BN_set_word(a, 3), 1);
  32873. /* a - 3 = */
  32874. ExpectIntEQ(BN_sub_word(a, 3), 1);
  32875. /* check result 3 - 3*/
  32876. ExpectIntEQ(BN_get_word(a), 0);
  32877. ExpectIntEQ(BN_one(a), 1);
  32878. ExpectIntEQ(BN_is_word(a, 1), 1);
  32879. ExpectIntEQ(BN_is_word(a, 0), 0);
  32880. ExpectIntEQ(BN_is_one(a), 1);
  32881. ExpectIntEQ(BN_is_zero(a), 0);
  32882. BN_zero(a);
  32883. ExpectIntEQ(BN_is_word(a, 0), 1);
  32884. ExpectIntEQ(BN_is_word(a, 1), 0);
  32885. ExpectIntEQ(BN_is_zero(a), 1);
  32886. ExpectIntEQ(BN_is_one(a), 0);
  32887. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32888. !defined(NO_DSA))
  32889. ExpectIntEQ(BN_set_word(a, 5), 1);
  32890. ExpectIntEQ(BN_mod_word(a, 3), 2);
  32891. ExpectIntEQ(BN_mod_word(a, 0), -1);
  32892. #endif
  32893. BN_free(c);
  32894. BN_free(b);
  32895. BN_free(a);
  32896. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32897. return EXPECT_RESULT();
  32898. }
  32899. static int test_wolfSSL_BN_bits(void)
  32900. {
  32901. EXPECT_DECLS;
  32902. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32903. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32904. BIGNUM* a = NULL;
  32905. BIGNUM emptyBN;
  32906. /* Setup */
  32907. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32908. ExpectNotNull(a = BN_new());
  32909. /* Invalid parameters. */
  32910. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  32911. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  32912. ExpectIntEQ(BN_set_bit(a, -1), 0);
  32913. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  32914. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  32915. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  32916. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  32917. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  32918. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  32919. ExpectIntEQ(BN_is_odd(NULL), 0);
  32920. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  32921. ExpectIntEQ(BN_set_word(a, 0), 1);
  32922. ExpectIntEQ(BN_is_zero(a), 1);
  32923. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  32924. ExpectIntEQ(BN_is_zero(a), 0);
  32925. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  32926. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  32927. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  32928. ExpectIntEQ(BN_is_zero(a), 1);
  32929. ExpectIntEQ(BN_set_bit(a, 0), 1);
  32930. ExpectIntEQ(BN_is_odd(a), 1);
  32931. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  32932. ExpectIntEQ(BN_is_odd(a), 0);
  32933. ExpectIntEQ(BN_set_bit(a, 1), 1);
  32934. ExpectIntEQ(BN_is_odd(a), 0);
  32935. ExpectIntEQ(BN_set_bit(a, 129), 1);
  32936. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  32937. #ifndef NO_WOLFSSL_STUB
  32938. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  32939. #endif
  32940. BN_free(a);
  32941. #endif
  32942. return EXPECT_RESULT();
  32943. }
  32944. static int test_wolfSSL_BN_shift(void)
  32945. {
  32946. EXPECT_DECLS;
  32947. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32948. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32949. BIGNUM* a = NULL;
  32950. BIGNUM* b = NULL;
  32951. BIGNUM emptyBN;
  32952. /* Setup */
  32953. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32954. ExpectNotNull(a = BN_new());
  32955. ExpectNotNull(b = BN_new());
  32956. /* Invalid parameters. */
  32957. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  32958. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  32959. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  32960. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  32961. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  32962. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  32963. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  32964. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  32965. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  32966. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  32967. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  32968. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  32969. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  32970. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  32971. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  32972. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  32973. ExpectIntEQ(BN_set_word(a, 1), 1);
  32974. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  32975. ExpectIntEQ(BN_is_word(b, 2), 1);
  32976. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  32977. ExpectIntEQ(BN_is_word(a, 2), 1);
  32978. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  32979. ExpectIntEQ(BN_is_word(b, 1), 1);
  32980. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  32981. ExpectIntEQ(BN_is_word(a, 1), 1);
  32982. BN_free(b);
  32983. BN_free(a);
  32984. #endif
  32985. return EXPECT_RESULT();
  32986. }
  32987. static int test_wolfSSL_BN_math(void)
  32988. {
  32989. EXPECT_DECLS;
  32990. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32991. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32992. BIGNUM* a = NULL;
  32993. BIGNUM* b = NULL;
  32994. BIGNUM* r = NULL;
  32995. BIGNUM* rem = NULL;
  32996. BIGNUM emptyBN;
  32997. BN_ULONG val1;
  32998. BN_ULONG val2;
  32999. /* Setup */
  33000. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33001. ExpectNotNull(a = BN_new());
  33002. ExpectNotNull(b = BN_new());
  33003. ExpectNotNull(r = BN_new());
  33004. ExpectNotNull(rem = BN_new());
  33005. /* Invalid parameters. */
  33006. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  33007. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  33008. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  33009. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  33010. ExpectIntEQ(BN_add(r, a, NULL), 0);
  33011. ExpectIntEQ(BN_add(r, NULL, b), 0);
  33012. ExpectIntEQ(BN_add(NULL, a, b), 0);
  33013. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  33014. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  33015. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  33016. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  33017. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  33018. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  33019. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  33020. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  33021. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  33022. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  33023. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  33024. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  33025. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  33026. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  33027. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  33028. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  33029. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  33030. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  33031. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  33032. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  33033. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  33034. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  33035. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  33036. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  33037. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  33038. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  33039. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  33040. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  33041. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33042. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  33043. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  33044. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  33045. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  33046. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  33047. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  33048. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  33049. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  33050. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  33051. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  33052. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  33053. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  33054. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  33055. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  33056. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  33057. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33058. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33059. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  33060. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  33061. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  33062. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  33063. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  33064. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  33065. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  33066. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  33067. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  33068. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  33069. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  33070. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  33071. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  33072. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  33073. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33074. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  33075. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  33076. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  33077. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  33078. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  33079. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  33080. /* END Invalid parameters. */
  33081. val1 = 8;
  33082. val2 = 3;
  33083. ExpectIntEQ(BN_set_word(a, val1), 1);
  33084. ExpectIntEQ(BN_set_word(b, val2), 1);
  33085. ExpectIntEQ(BN_add(r, a, b), 1);
  33086. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  33087. ExpectIntEQ(BN_sub(r, a, b), 1);
  33088. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  33089. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  33090. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  33091. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  33092. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  33093. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  33094. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  33095. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  33096. BN_free(rem);
  33097. BN_free(r);
  33098. BN_free(b);
  33099. BN_free(a);
  33100. #endif
  33101. return EXPECT_RESULT();
  33102. }
  33103. static int test_wolfSSL_BN_math_mod(void)
  33104. {
  33105. EXPECT_DECLS;
  33106. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33107. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33108. BIGNUM* a = NULL;
  33109. BIGNUM* b = NULL;
  33110. BIGNUM* m = NULL;
  33111. BIGNUM* r = NULL;
  33112. BIGNUM* t = NULL;
  33113. BIGNUM emptyBN;
  33114. BN_ULONG val1;
  33115. BN_ULONG val2;
  33116. BN_ULONG val3;
  33117. /* Setup */
  33118. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33119. ExpectNotNull(a = BN_new());
  33120. ExpectNotNull(b = BN_new());
  33121. ExpectNotNull(m = BN_new());
  33122. ExpectNotNull(r = BN_new());
  33123. /* Invalid parameters. */
  33124. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  33125. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  33126. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  33127. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  33128. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  33129. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  33130. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  33131. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  33132. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  33133. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33134. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33135. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33136. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33137. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33138. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  33139. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  33140. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  33141. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  33142. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  33143. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  33144. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  33145. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  33146. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  33147. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  33148. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  33149. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  33150. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  33151. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33152. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33153. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33154. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33155. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33156. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  33157. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  33158. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  33159. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  33160. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  33161. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  33162. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  33163. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  33164. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  33165. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  33166. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  33167. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  33168. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  33169. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33170. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33171. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33172. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33173. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33174. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  33175. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  33176. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  33177. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  33178. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  33179. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33180. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33181. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33182. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33183. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  33184. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  33185. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  33186. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  33187. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  33188. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  33189. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  33190. /* END Invalid parameters. */
  33191. val1 = 9;
  33192. val2 = 13;
  33193. val3 = 5;
  33194. ExpectIntEQ(BN_set_word(a, val1), 1);
  33195. ExpectIntEQ(BN_set_word(b, val2), 1);
  33196. ExpectIntEQ(BN_set_word(m, val3), 1);
  33197. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  33198. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  33199. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  33200. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  33201. ExpectIntEQ(BN_set_word(a, 2), 1);
  33202. ExpectIntEQ(BN_set_word(b, 3), 1);
  33203. ExpectIntEQ(BN_set_word(m, 5), 1);
  33204. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  33205. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  33206. ExpectIntEQ(BN_is_word(r, 3), 1);
  33207. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  33208. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  33209. ExpectIntEQ(BN_is_word(r, 3), 1);
  33210. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  33211. ExpectIntEQ(BN_is_word(t, 3), 1);
  33212. BN_free(t);
  33213. /* No inverse case. No inverse when a divides b. */
  33214. ExpectIntEQ(BN_set_word(a, 3), 1);
  33215. ExpectIntEQ(BN_set_word(m, 9), 1);
  33216. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  33217. BN_free(r);
  33218. BN_free(m);
  33219. BN_free(b);
  33220. BN_free(a);
  33221. #endif
  33222. return EXPECT_RESULT();
  33223. }
  33224. static int test_wolfSSL_BN_math_other(void)
  33225. {
  33226. EXPECT_DECLS;
  33227. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33228. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33229. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33230. BIGNUM* a = NULL;
  33231. BIGNUM* b = NULL;
  33232. BIGNUM* r = NULL;
  33233. BIGNUM emptyBN;
  33234. /* Setup */
  33235. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33236. ExpectNotNull(a = BN_new());
  33237. ExpectNotNull(b = BN_new());
  33238. ExpectNotNull(r = BN_new());
  33239. /* Invalid parameters. */
  33240. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  33241. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  33242. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  33243. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  33244. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  33245. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  33246. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  33247. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33248. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  33249. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  33250. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  33251. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  33252. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  33253. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  33254. /* END Invalid parameters. */
  33255. /* No common factors between 2 and 3. */
  33256. ExpectIntEQ(BN_set_word(a, 2), 1);
  33257. ExpectIntEQ(BN_set_word(b, 3), 1);
  33258. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33259. ExpectIntEQ(BN_is_word(r, 1), 1);
  33260. /* 3 is largest value that divides both 6 and 9. */
  33261. ExpectIntEQ(BN_set_word(a, 6), 1);
  33262. ExpectIntEQ(BN_set_word(b, 9), 1);
  33263. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33264. ExpectIntEQ(BN_is_word(r, 3), 1);
  33265. /* GCD of 0 and 0 is undefined. */
  33266. ExpectIntEQ(BN_set_word(a, 0), 1);
  33267. ExpectIntEQ(BN_set_word(b, 0), 1);
  33268. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  33269. /* Teardown */
  33270. BN_free(r);
  33271. BN_free(b);
  33272. BN_free(a);
  33273. #endif
  33274. #endif
  33275. return EXPECT_RESULT();
  33276. }
  33277. static int test_wolfSSL_BN_rand(void)
  33278. {
  33279. EXPECT_DECLS;
  33280. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  33281. BIGNUM* bn = NULL;
  33282. BIGNUM* range = NULL;
  33283. BIGNUM emptyBN;
  33284. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33285. ExpectNotNull(bn = BN_new());
  33286. ExpectNotNull(range = BN_new());
  33287. /* Invalid parameters. */
  33288. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  33289. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33290. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  33291. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  33292. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33293. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  33294. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  33295. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33296. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  33297. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  33298. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33299. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  33300. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  33301. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  33302. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  33303. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  33304. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  33305. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  33306. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  33307. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33308. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33309. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33310. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33311. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33312. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33313. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33314. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33315. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33316. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33317. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33318. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33319. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33320. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33321. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33322. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33323. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33324. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33325. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33326. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33327. /* 1 bit random value must have no more than one top bit set. */
  33328. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33329. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33330. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33331. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33332. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33333. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33334. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33335. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33336. /* END Invalid parameters. */
  33337. /* 0 bit random: 0. */
  33338. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33339. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33340. ExpectIntEQ(BN_is_zero(bn), 1);
  33341. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  33342. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33343. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33344. ExpectIntEQ(BN_is_zero(bn), 1);
  33345. /* 1 bit random: 0 or 1. */
  33346. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33347. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33348. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33349. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33350. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33351. ExpectIntEQ(BN_get_word(bn), 1);
  33352. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33353. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33354. ExpectIntEQ(BN_get_word(bn), 1);
  33355. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33356. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33357. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33358. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33359. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33360. ExpectIntEQ(BN_get_word(bn), 1);
  33361. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33362. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33363. ExpectIntEQ(BN_get_word(bn), 1);
  33364. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33365. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33366. ExpectIntEQ(BN_num_bits(bn), 8);
  33367. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33368. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33369. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33370. ExpectIntEQ(BN_num_bits(bn), 8);
  33371. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33372. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33373. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33374. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33375. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33376. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33377. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33378. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33379. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33380. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33381. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33382. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33383. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33384. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33385. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33386. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33387. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33388. * case, requesting a 13-bit random number would actually return a 16-bit
  33389. * random number. */
  33390. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  33391. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33392. ExpectIntEQ(BN_num_bits(bn), 13);
  33393. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  33394. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33395. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33396. ExpectIntEQ(BN_set_word(range, 0), 1);
  33397. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33398. ExpectIntEQ(BN_set_word(range, 1), 1);
  33399. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33400. BN_free(bn);
  33401. BN_free(range);
  33402. #endif
  33403. return EXPECT_RESULT();
  33404. }
  33405. static int test_wolfSSL_BN_prime(void)
  33406. {
  33407. EXPECT_DECLS;
  33408. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33409. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33410. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  33411. BIGNUM* a = NULL;
  33412. BIGNUM* add = NULL;
  33413. BIGNUM* rem = NULL;
  33414. BIGNUM emptyBN;
  33415. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33416. ExpectNotNull(a = BN_new());
  33417. ExpectNotNull(add = BN_new());
  33418. ExpectNotNull(rem = BN_new());
  33419. /* Invalid parameters. */
  33420. /* BN_generate_prime_ex()
  33421. * prime - must have valid BIGNUM
  33422. * bits - Greater then 0
  33423. * safe - not supported, must be 0
  33424. * add - not supported, must be NULL
  33425. * rem - not supported, must be NULL
  33426. * cb - anything
  33427. */
  33428. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  33429. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  33430. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  33431. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  33432. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  33433. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  33434. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  33435. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  33436. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  33437. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  33438. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  33439. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  33440. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  33441. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  33442. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  33443. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  33444. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  33445. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  33446. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  33447. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  33448. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  33449. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  33450. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  33451. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  33452. /* END Invalid parameters. */
  33453. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  33454. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  33455. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33456. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  33457. BN_free(rem);
  33458. BN_free(add);
  33459. BN_free(a);
  33460. #endif
  33461. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33462. return EXPECT_RESULT();
  33463. }
  33464. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33465. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33466. #define TEST_ARG 0x1234
  33467. static void msg_cb(int write_p, int version, int content_type,
  33468. const void *buf, size_t len, SSL *ssl, void *arg)
  33469. {
  33470. (void)write_p;
  33471. (void)version;
  33472. (void)content_type;
  33473. (void)buf;
  33474. (void)len;
  33475. (void)ssl;
  33476. AssertTrue(arg == (void*)TEST_ARG);
  33477. }
  33478. #endif
  33479. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33480. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33481. #if defined(SESSION_CERTS)
  33482. #include "wolfssl/internal.h"
  33483. #endif
  33484. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  33485. {
  33486. EXPECT_DECLS;
  33487. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33488. STACK_OF(X509)* sk = NULL;
  33489. X509* x509 = NULL;
  33490. int i, num;
  33491. BIO* bio = NULL;
  33492. #endif
  33493. ExpectNotNull(ctx);
  33494. ExpectNotNull(ssl);
  33495. fprintf(stderr, "\n===== msgcb called ====\n");
  33496. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  33497. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  33498. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  33499. ExpectNotNull(SSL_get0_verified_chain(ssl));
  33500. #endif
  33501. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33502. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  33503. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  33504. if (sk == NULL) {
  33505. BIO_free(bio);
  33506. return TEST_FAIL;
  33507. }
  33508. num = sk_X509_num(sk);
  33509. ExpectTrue(num > 0);
  33510. for (i = 0; i < num; i++) {
  33511. ExpectNotNull(x509 = sk_X509_value(sk,i));
  33512. if (x509 == NULL)
  33513. break;
  33514. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  33515. X509_print(bio,x509);
  33516. fprintf(stderr, "\n\n");
  33517. }
  33518. BIO_free(bio);
  33519. #endif
  33520. return EXPECT_RESULT();
  33521. }
  33522. #endif
  33523. static int test_wolfSSL_msgCb(void)
  33524. {
  33525. EXPECT_DECLS;
  33526. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33527. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33528. test_ssl_cbf client_cb;
  33529. test_ssl_cbf server_cb;
  33530. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33531. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33532. #ifndef WOLFSSL_NO_TLS12
  33533. client_cb.method = wolfTLSv1_2_client_method;
  33534. server_cb.method = wolfTLSv1_2_server_method;
  33535. #else
  33536. client_cb.method = wolfTLSv1_3_client_method;
  33537. server_cb.method = wolfTLSv1_3_server_method;
  33538. #endif
  33539. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33540. &server_cb, msgCb), TEST_SUCCESS);
  33541. #endif
  33542. return EXPECT_RESULT();
  33543. }
  33544. static int test_wolfSSL_either_side(void)
  33545. {
  33546. EXPECT_DECLS;
  33547. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33548. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33549. test_ssl_cbf client_cb;
  33550. test_ssl_cbf server_cb;
  33551. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33552. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33553. /* Use different CTX for client and server */
  33554. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33555. ExpectNotNull(client_cb.ctx);
  33556. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33557. ExpectNotNull(server_cb.ctx);
  33558. /* we are responsible for free'ing WOLFSSL_CTX */
  33559. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33560. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33561. &server_cb, NULL), TEST_SUCCESS);
  33562. wolfSSL_CTX_free(client_cb.ctx);
  33563. wolfSSL_CTX_free(server_cb.ctx);
  33564. #endif
  33565. return EXPECT_RESULT();
  33566. }
  33567. static int test_wolfSSL_DTLS_either_side(void)
  33568. {
  33569. EXPECT_DECLS;
  33570. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33571. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  33572. test_ssl_cbf client_cb;
  33573. test_ssl_cbf server_cb;
  33574. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33575. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33576. /* Use different CTX for client and server */
  33577. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33578. ExpectNotNull(client_cb.ctx);
  33579. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33580. ExpectNotNull(server_cb.ctx);
  33581. /* we are responsible for free'ing WOLFSSL_CTX */
  33582. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33583. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33584. &server_cb, NULL), TEST_SUCCESS);
  33585. wolfSSL_CTX_free(client_cb.ctx);
  33586. wolfSSL_CTX_free(server_cb.ctx);
  33587. #endif
  33588. return EXPECT_RESULT();
  33589. }
  33590. static int test_generate_cookie(void)
  33591. {
  33592. EXPECT_DECLS;
  33593. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  33594. SSL_CTX* ctx = NULL;
  33595. SSL* ssl = NULL;
  33596. byte buf[FOURK_BUF] = {0};
  33597. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  33598. ExpectNotNull(ssl = SSL_new(ctx));
  33599. /* Test unconnected */
  33600. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  33601. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  33602. wolfSSL_SetCookieCtx(ssl, ctx);
  33603. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  33604. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  33605. SSL_free(ssl);
  33606. SSL_CTX_free(ctx);
  33607. #endif
  33608. return EXPECT_RESULT();
  33609. }
  33610. static int test_wolfSSL_set_options(void)
  33611. {
  33612. EXPECT_DECLS;
  33613. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33614. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33615. WOLFSSL* ssl = NULL;
  33616. WOLFSSL_CTX* ctx = NULL;
  33617. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33618. char appData[] = "extra msg";
  33619. #endif
  33620. #ifdef OPENSSL_EXTRA
  33621. unsigned char protos[] = {
  33622. 7, 't', 'l', 's', '/', '1', '.', '2',
  33623. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  33624. };
  33625. unsigned int len = sizeof(protos);
  33626. void *arg = (void *)TEST_ARG;
  33627. #endif
  33628. #ifndef NO_WOLFSSL_SERVER
  33629. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33630. #else
  33631. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33632. #endif
  33633. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33634. WOLFSSL_FILETYPE_PEM));
  33635. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33636. WOLFSSL_FILETYPE_PEM));
  33637. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  33638. == WOLFSSL_OP_NO_TLSv1);
  33639. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  33640. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33641. WOLFSSL_OP_NO_SSLv2)), 0);
  33642. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33643. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33644. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  33645. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33646. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33647. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33648. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33649. WOLFSSL_OP_NO_COMPRESSION));
  33650. wolfSSL_CTX_free(ctx);
  33651. ctx = NULL;
  33652. #ifndef NO_WOLFSSL_SERVER
  33653. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33654. #else
  33655. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33656. #endif
  33657. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33658. WOLFSSL_FILETYPE_PEM));
  33659. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33660. WOLFSSL_FILETYPE_PEM));
  33661. #ifdef OPENSSL_EXTRA
  33662. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  33663. #endif
  33664. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33665. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33666. #ifdef HAVE_EX_DATA
  33667. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  33668. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33669. if (ssl != NULL) {
  33670. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  33671. appData, sizeof(appData)), 0);
  33672. }
  33673. #else
  33674. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  33675. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33676. #endif
  33677. #endif
  33678. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  33679. WOLFSSL_OP_NO_TLSv1);
  33680. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  33681. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33682. WOLFSSL_OP_NO_SSLv2)), 0);
  33683. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33684. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33685. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  33686. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33687. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33688. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33689. #ifdef OPENSSL_EXTRA
  33690. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33691. WOLFSSL_OP_NO_COMPRESSION));
  33692. #endif
  33693. #ifdef OPENSSL_EXTRA
  33694. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  33695. wolfSSL_set_msg_callback_arg(ssl, arg);
  33696. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33697. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  33698. #else
  33699. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  33700. #endif
  33701. #endif
  33702. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  33703. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  33704. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  33705. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  33706. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33707. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  33708. #else
  33709. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  33710. #endif
  33711. #endif /* HAVE_ALPN && !NO_BIO */
  33712. #endif
  33713. wolfSSL_free(ssl);
  33714. wolfSSL_CTX_free(ctx);
  33715. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33716. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33717. return EXPECT_RESULT();
  33718. }
  33719. static int test_wolfSSL_sk_SSL_CIPHER(void)
  33720. {
  33721. EXPECT_DECLS;
  33722. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33723. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33724. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33725. SSL* ssl = NULL;
  33726. SSL_CTX* ctx = NULL;
  33727. STACK_OF(SSL_CIPHER) *sk = NULL;
  33728. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  33729. #ifndef NO_WOLFSSL_SERVER
  33730. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33731. #else
  33732. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33733. #endif
  33734. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33735. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33736. ExpectNotNull(ssl = SSL_new(ctx));
  33737. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  33738. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  33739. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  33740. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  33741. /* error case because connection has not been established yet */
  33742. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  33743. sk_SSL_CIPHER_free(dupSk);
  33744. /* sk is pointer to internal struct that should be free'd in SSL_free */
  33745. SSL_free(ssl);
  33746. SSL_CTX_free(ctx);
  33747. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33748. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33749. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33750. return EXPECT_RESULT();
  33751. }
  33752. static int test_wolfSSL_set1_curves_list(void)
  33753. {
  33754. EXPECT_DECLS;
  33755. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33756. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33757. SSL* ssl = NULL;
  33758. SSL_CTX* ctx = NULL;
  33759. #ifndef NO_WOLFSSL_SERVER
  33760. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33761. #else
  33762. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33763. #endif
  33764. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  33765. SSL_FILETYPE_PEM));
  33766. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  33767. ExpectNotNull(ssl = SSL_new(ctx));
  33768. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  33769. #ifdef HAVE_ECC
  33770. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  33771. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  33772. #endif
  33773. #ifdef HAVE_CURVE25519
  33774. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  33775. #else
  33776. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  33777. #endif
  33778. #ifdef HAVE_CURVE448
  33779. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  33780. #else
  33781. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  33782. #endif
  33783. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  33784. #ifdef HAVE_ECC
  33785. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  33786. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  33787. #endif
  33788. #ifdef HAVE_CURVE25519
  33789. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  33790. #else
  33791. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  33792. #endif
  33793. #ifdef HAVE_CURVE448
  33794. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  33795. #else
  33796. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  33797. #endif
  33798. SSL_free(ssl);
  33799. SSL_CTX_free(ctx);
  33800. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33801. #endif
  33802. return EXPECT_RESULT();
  33803. }
  33804. static int test_wolfSSL_set1_sigalgs_list(void)
  33805. {
  33806. EXPECT_DECLS;
  33807. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  33808. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33809. SSL* ssl = NULL;
  33810. SSL_CTX* ctx = NULL;
  33811. #ifndef NO_WOLFSSL_SERVER
  33812. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33813. #else
  33814. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33815. #endif
  33816. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33817. SSL_FILETYPE_PEM));
  33818. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33819. ExpectNotNull(ssl = SSL_new(ctx));
  33820. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33821. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  33822. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33823. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  33824. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  33825. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  33826. #ifndef NO_RSA
  33827. #ifndef NO_SHA256
  33828. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33829. WOLFSSL_FAILURE);
  33830. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33831. WOLFSSL_FAILURE);
  33832. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  33833. WOLFSSL_SUCCESS);
  33834. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  33835. WOLFSSL_SUCCESS);
  33836. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  33837. WOLFSSL_FAILURE);
  33838. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  33839. WOLFSSL_FAILURE);
  33840. #ifdef WC_RSA_PSS
  33841. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  33842. WOLFSSL_SUCCESS);
  33843. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  33844. WOLFSSL_SUCCESS);
  33845. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  33846. WOLFSSL_SUCCESS);
  33847. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  33848. WOLFSSL_SUCCESS);
  33849. #endif
  33850. #ifdef WOLFSSL_SHA512
  33851. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33852. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33853. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33854. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33855. #elif defined(WOLFSSL_SHA384)
  33856. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33857. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33858. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33859. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33860. #endif
  33861. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  33862. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  33863. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  33864. WOLFSSL_FAILURE);
  33865. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  33866. WOLFSSL_FAILURE);
  33867. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  33868. WOLFSSL_FAILURE);
  33869. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  33870. WOLFSSL_FAILURE);
  33871. #endif
  33872. #endif
  33873. #ifdef HAVE_ECC
  33874. #ifndef NO_SHA256
  33875. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  33876. WOLFSSL_SUCCESS);
  33877. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  33878. WOLFSSL_SUCCESS);
  33879. #ifdef WOLFSSL_SHA512
  33880. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33881. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33882. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33883. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33884. #elif defined(WOLFSSL_SHA384)
  33885. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33886. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33887. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33888. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33889. #endif
  33890. #endif
  33891. #endif
  33892. #ifdef HAVE_ED25519
  33893. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  33894. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  33895. #endif
  33896. #ifdef HAVE_ED448
  33897. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  33898. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  33899. #endif
  33900. #ifndef NO_DSA
  33901. #ifndef NO_SHA256
  33902. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  33903. WOLFSSL_SUCCESS);
  33904. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  33905. WOLFSSL_SUCCESS);
  33906. #endif
  33907. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  33908. defined(WOLFSSL_ALLOW_TLS_SHA1))
  33909. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  33910. WOLFSSL_SUCCESS);
  33911. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  33912. WOLFSSL_SUCCESS);
  33913. #endif
  33914. #endif
  33915. SSL_free(ssl);
  33916. SSL_CTX_free(ctx);
  33917. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33918. #endif
  33919. return EXPECT_RESULT();
  33920. }
  33921. /* Testing wolfSSL_set_tlsext_status_type function.
  33922. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  33923. */
  33924. static int test_wolfSSL_set_tlsext_status_type(void)
  33925. {
  33926. EXPECT_DECLS;
  33927. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  33928. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  33929. SSL* ssl = NULL;
  33930. SSL_CTX* ctx = NULL;
  33931. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33932. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33933. SSL_FILETYPE_PEM));
  33934. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33935. ExpectNotNull(ssl = SSL_new(ctx));
  33936. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  33937. SSL_SUCCESS);
  33938. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  33939. SSL_free(ssl);
  33940. SSL_CTX_free(ctx);
  33941. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  33942. return EXPECT_RESULT();
  33943. }
  33944. #ifndef NO_BIO
  33945. static int test_wolfSSL_PEM_read_bio(void)
  33946. {
  33947. EXPECT_DECLS;
  33948. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33949. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33950. byte buff[6000];
  33951. XFILE f = XBADFILE;
  33952. int bytes;
  33953. X509* x509 = NULL;
  33954. BIO* bio = NULL;
  33955. BUF_MEM* buf = NULL;
  33956. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  33957. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  33958. if (f != XBADFILE)
  33959. XFCLOSE(f);
  33960. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33961. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33962. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  33963. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33964. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  33965. /* BIO should return the set EOF value */
  33966. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  33967. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  33968. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  33969. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  33970. BIO_free(bio);
  33971. BUF_MEM_free(buf);
  33972. X509_free(x509);
  33973. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33974. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33975. return EXPECT_RESULT();
  33976. }
  33977. #if defined(OPENSSL_EXTRA)
  33978. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  33979. long argl, long ret)
  33980. {
  33981. (void)bio;
  33982. (void)cmd;
  33983. (void)argp;
  33984. (void)argi;
  33985. (void)argl;
  33986. return ret;
  33987. }
  33988. #endif
  33989. static int test_wolfSSL_BIO(void)
  33990. {
  33991. EXPECT_DECLS;
  33992. #if defined(OPENSSL_EXTRA)
  33993. const unsigned char* p = NULL;
  33994. byte buff[20];
  33995. BIO* bio1 = NULL;
  33996. BIO* bio2 = NULL;
  33997. BIO* bio3 = NULL;
  33998. char* bufPt = NULL;
  33999. int i;
  34000. for (i = 0; i < 20; i++) {
  34001. buff[i] = i;
  34002. }
  34003. /* test BIO_free with NULL */
  34004. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  34005. /* Creating and testing type BIO_s_bio */
  34006. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34007. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  34008. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  34009. /* read/write before set up */
  34010. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34011. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34012. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  34013. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  34014. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  34015. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  34016. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  34017. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  34018. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  34019. /* write buffer full */
  34020. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  34021. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  34022. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  34023. /* write the other direction with pair */
  34024. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  34025. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  34026. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  34027. /* try read */
  34028. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  34029. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  34030. /* try read using ctrl function */
  34031. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  34032. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  34033. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  34034. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  34035. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  34036. for (i = 0; i < 20; i++) {
  34037. ExpectIntEQ((int)bufPt[i], i);
  34038. }
  34039. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34040. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  34041. for (i = 0; i < 8; i++) {
  34042. ExpectIntEQ((int)bufPt[i], i);
  34043. }
  34044. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34045. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  34046. /* new pair */
  34047. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  34048. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  34049. bio2 = NULL;
  34050. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  34051. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34052. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  34053. /* test wrap around... */
  34054. ExpectIntEQ(BIO_reset(bio1), 0);
  34055. ExpectIntEQ(BIO_reset(bio3), 0);
  34056. /* fill write buffer, read only small amount then write again */
  34057. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34058. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34059. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  34060. for (i = 0; i < 4; i++) {
  34061. ExpectIntEQ(bufPt[i], i);
  34062. }
  34063. /* try writing over read index */
  34064. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  34065. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  34066. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  34067. /* read and write 0 bytes */
  34068. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34069. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  34070. /* should read only to end of write buffer then need to read again */
  34071. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  34072. for (i = 0; i < 16; i++) {
  34073. ExpectIntEQ(bufPt[i], buff[4 + i]);
  34074. }
  34075. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  34076. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  34077. for (i = 0; i < 4; i++) {
  34078. ExpectIntEQ(bufPt[i], 0);
  34079. }
  34080. /* read index should not have advanced with nread0 */
  34081. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  34082. for (i = 0; i < 4; i++) {
  34083. ExpectIntEQ(bufPt[i], 0);
  34084. }
  34085. /* write and fill up buffer checking reset of index state */
  34086. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34087. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34088. /* test reset on data in bio1 write buffer */
  34089. ExpectIntEQ(BIO_reset(bio1), 0);
  34090. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34091. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  34092. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34093. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  34094. ExpectNotNull(p);
  34095. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34096. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  34097. for (i = 0; i < 6; i++) {
  34098. ExpectIntEQ(bufPt[i], i);
  34099. }
  34100. /* test case of writing twice with offset read index */
  34101. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  34102. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  34103. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34104. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34105. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34106. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  34107. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  34108. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34109. BIO_free(bio1);
  34110. bio1 = NULL;
  34111. BIO_free(bio3);
  34112. bio3 = NULL;
  34113. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  34114. {
  34115. BIO* bioA = NULL;
  34116. BIO* bioB = NULL;
  34117. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  34118. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  34119. BIO_free(bioA);
  34120. bioA = NULL;
  34121. BIO_free(bioB);
  34122. bioB = NULL;
  34123. }
  34124. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34125. /* BIOs with file pointers */
  34126. #if !defined(NO_FILESYSTEM)
  34127. {
  34128. XFILE f1 = XBADFILE;
  34129. XFILE f2 = XBADFILE;
  34130. BIO* f_bio1 = NULL;
  34131. BIO* f_bio2 = NULL;
  34132. unsigned char cert[300];
  34133. char testFile[] = "tests/bio_write_test.txt";
  34134. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  34135. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  34136. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  34137. /* Failure due to wrong BIO type */
  34138. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34139. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  34140. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  34141. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  34142. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  34143. WOLFSSL_SUCCESS);
  34144. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34145. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  34146. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  34147. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  34148. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  34149. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  34150. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  34151. ExpectIntEQ(BIO_reset(f_bio2), 0);
  34152. ExpectIntEQ(BIO_tell(NULL),-1);
  34153. ExpectIntEQ(BIO_tell(f_bio2),0);
  34154. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  34155. ExpectIntEQ(BIO_tell(f_bio2),4);
  34156. BIO_free(f_bio1);
  34157. f_bio1 = NULL;
  34158. BIO_free(f_bio2);
  34159. f_bio2 = NULL;
  34160. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  34161. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34162. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34163. BIO_free(f_bio1);
  34164. f_bio1 = NULL;
  34165. }
  34166. #endif /* !defined(NO_FILESYSTEM) */
  34167. /* BIO info callback */
  34168. {
  34169. const char* testArg = "test";
  34170. BIO* cb_bio = NULL;
  34171. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  34172. BIO_set_callback(cb_bio, bioCallback);
  34173. ExpectNotNull(BIO_get_callback(cb_bio));
  34174. BIO_set_callback(cb_bio, NULL);
  34175. ExpectNull(BIO_get_callback(cb_bio));
  34176. BIO_set_callback_arg(cb_bio, (char*)testArg);
  34177. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  34178. ExpectNull(BIO_get_callback_arg(NULL));
  34179. BIO_free(cb_bio);
  34180. cb_bio = NULL;
  34181. }
  34182. /* BIO_vfree */
  34183. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34184. BIO_vfree(NULL);
  34185. BIO_vfree(bio1);
  34186. #endif
  34187. return EXPECT_RESULT();
  34188. }
  34189. #endif /* !NO_BIO */
  34190. static int test_wolfSSL_a2i_IPADDRESS(void)
  34191. {
  34192. EXPECT_DECLS;
  34193. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  34194. const unsigned char* data = NULL;
  34195. int dataSz = 0;
  34196. ASN1_OCTET_STRING *st = NULL;
  34197. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  34198. const unsigned char ipv6_exp[] = {
  34199. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34200. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  34201. };
  34202. const unsigned char ipv6_home[] = {
  34203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34204. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  34205. };
  34206. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  34207. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  34208. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34209. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  34210. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  34211. ASN1_STRING_free(st);
  34212. st = NULL;
  34213. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  34214. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34215. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34216. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  34217. ASN1_STRING_free(st);
  34218. st = NULL;
  34219. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  34220. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34221. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34222. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  34223. ASN1_STRING_free(st);
  34224. #endif
  34225. return EXPECT_RESULT();
  34226. }
  34227. static int test_wolfSSL_X509_cmp_time(void)
  34228. {
  34229. EXPECT_DECLS;
  34230. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  34231. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  34232. WOLFSSL_ASN1_TIME asn_time;
  34233. time_t t;
  34234. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  34235. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  34236. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  34237. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  34238. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  34239. #endif
  34240. return EXPECT_RESULT();
  34241. }
  34242. static int test_wolfSSL_X509_time_adj(void)
  34243. {
  34244. EXPECT_DECLS;
  34245. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  34246. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  34247. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  34248. !defined(NO_ASN_TIME)
  34249. X509* x509 = NULL;
  34250. time_t t;
  34251. time_t not_before;
  34252. time_t not_after;
  34253. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  34254. client_cert_der_2048, sizeof_client_cert_der_2048,
  34255. WOLFSSL_FILETYPE_ASN1));
  34256. t = 0;
  34257. not_before = wc_Time(0);
  34258. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  34259. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  34260. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  34261. /* Check X509_gmtime_adj, too. */
  34262. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  34263. X509_free(x509);
  34264. #endif
  34265. return EXPECT_RESULT();
  34266. }
  34267. static int test_wolfSSL_X509(void)
  34268. {
  34269. EXPECT_DECLS;
  34270. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34271. !defined(NO_RSA)
  34272. X509* x509 = NULL;
  34273. #ifndef NO_BIO
  34274. BIO* bio = NULL;
  34275. X509_STORE_CTX* ctx = NULL;
  34276. X509_STORE* store = NULL;
  34277. #endif
  34278. char der[] = "certs/ca-cert.der";
  34279. XFILE fp = XBADFILE;
  34280. ExpectNotNull(x509 = X509_new());
  34281. X509_free(x509);
  34282. x509 = NULL;
  34283. #ifndef NO_BIO
  34284. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34285. SSL_FILETYPE_PEM));
  34286. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34287. #ifdef WOLFSSL_CERT_GEN
  34288. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  34289. #endif
  34290. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34291. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  34292. ExpectNotNull(store = X509_STORE_new());
  34293. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  34294. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  34295. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  34296. X509_STORE_CTX_free(ctx);
  34297. X509_STORE_free(store);
  34298. X509_free(x509);
  34299. x509 = NULL;
  34300. BIO_free(bio);
  34301. #endif
  34302. /** d2i_X509_fp test **/
  34303. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34304. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  34305. ExpectNotNull(x509);
  34306. X509_free(x509);
  34307. x509 = NULL;
  34308. if (fp != XBADFILE) {
  34309. XFCLOSE(fp);
  34310. fp = XBADFILE;
  34311. }
  34312. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34313. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  34314. ExpectNotNull(x509);
  34315. X509_free(x509);
  34316. if (fp != XBADFILE)
  34317. XFCLOSE(fp);
  34318. /* X509_up_ref test */
  34319. ExpectIntEQ(X509_up_ref(NULL), 0);
  34320. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  34321. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  34322. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  34323. X509_free(x509); /* refCount = 2 */
  34324. X509_free(x509); /* refCount = 1 */
  34325. X509_free(x509); /* refCount = 0, free */
  34326. #endif
  34327. return EXPECT_RESULT();
  34328. }
  34329. static int test_wolfSSL_X509_get_ext_count(void)
  34330. {
  34331. EXPECT_DECLS;
  34332. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34333. !defined(NO_RSA)
  34334. int ret = 0;
  34335. WOLFSSL_X509* x509 = NULL;
  34336. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  34337. XFILE f = XBADFILE;
  34338. /* NULL parameter check */
  34339. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  34340. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34341. SSL_FILETYPE_PEM));
  34342. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34343. wolfSSL_X509_free(x509);
  34344. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  34345. SSL_FILETYPE_PEM));
  34346. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34347. wolfSSL_X509_free(x509);
  34348. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  34349. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34350. if (f != XBADFILE)
  34351. XFCLOSE(f);
  34352. /* wolfSSL_X509_get_ext_count() valid input */
  34353. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  34354. /* wolfSSL_X509_get_ext_count() NULL argument */
  34355. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  34356. wolfSSL_X509_free(x509);
  34357. #endif
  34358. return EXPECT_RESULT();
  34359. }
  34360. static int test_wolfSSL_X509_sign2(void)
  34361. {
  34362. EXPECT_DECLS;
  34363. /* test requires WOLFSSL_AKID_NAME to match expected output */
  34364. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34365. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  34366. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  34367. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  34368. defined(WOLFSSL_IP_ALT_NAME))
  34369. WOLFSSL_X509 *x509 = NULL;
  34370. WOLFSSL_X509 *ca = NULL;
  34371. const unsigned char *der = NULL;
  34372. const unsigned char *pt = NULL;
  34373. WOLFSSL_EVP_PKEY *priv = NULL;
  34374. WOLFSSL_X509_NAME *name = NULL;
  34375. int derSz;
  34376. #ifndef NO_ASN_TIME
  34377. WOLFSSL_ASN1_TIME *notBefore = NULL;
  34378. WOLFSSL_ASN1_TIME *notAfter = NULL;
  34379. const int year = 365*24*60*60;
  34380. const int day = 24*60*60;
  34381. const int hour = 60*60;
  34382. const int mini = 60;
  34383. time_t t;
  34384. #endif
  34385. const unsigned char expected[] = {
  34386. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  34387. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  34388. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  34389. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  34390. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  34391. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  34392. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  34393. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  34394. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  34395. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  34396. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  34397. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  34398. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  34399. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  34400. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34401. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  34402. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  34403. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  34404. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  34405. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  34406. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  34407. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  34408. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  34409. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  34410. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  34411. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  34412. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  34413. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  34414. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  34415. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  34416. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  34417. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  34418. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  34419. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34420. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  34421. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  34422. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  34423. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  34424. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  34425. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  34426. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  34427. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  34428. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  34429. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  34430. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  34431. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  34432. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  34433. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  34434. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  34435. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  34436. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  34437. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  34438. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  34439. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  34440. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  34441. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  34442. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  34443. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  34444. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  34445. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  34446. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  34447. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  34448. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34449. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34450. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  34451. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34452. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34453. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  34454. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  34455. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  34456. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  34457. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  34458. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  34459. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  34460. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  34461. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  34462. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  34463. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34464. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  34465. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  34466. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34467. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  34468. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  34469. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  34470. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  34471. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  34472. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  34473. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  34474. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  34475. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  34476. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  34477. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  34478. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  34479. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  34480. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  34481. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  34482. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  34483. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  34484. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  34485. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  34486. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  34487. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  34488. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  34489. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  34490. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  34491. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  34492. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  34493. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  34494. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  34495. };
  34496. pt = ca_key_der_2048;
  34497. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  34498. sizeof_ca_key_der_2048));
  34499. pt = client_cert_der_2048;
  34500. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  34501. sizeof_client_cert_der_2048));
  34502. pt = ca_cert_der_2048;
  34503. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  34504. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  34505. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34506. #ifndef NO_ASN_TIME
  34507. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  34508. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  34509. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  34510. ExpectIntEQ(notAfter->length, 13);
  34511. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  34512. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  34513. #endif
  34514. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  34515. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  34516. ExpectIntEQ(derSz, sizeof(expected));
  34517. #ifndef NO_ASN_TIME
  34518. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  34519. #endif
  34520. wolfSSL_X509_free(ca);
  34521. wolfSSL_X509_free(x509);
  34522. wolfSSL_EVP_PKEY_free(priv);
  34523. #ifndef NO_ASN_TIME
  34524. wolfSSL_ASN1_TIME_free(notBefore);
  34525. wolfSSL_ASN1_TIME_free(notAfter);
  34526. #endif
  34527. #endif
  34528. return EXPECT_RESULT();
  34529. }
  34530. static int test_wolfSSL_X509_sign(void)
  34531. {
  34532. EXPECT_DECLS;
  34533. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  34534. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  34535. int ret;
  34536. char *cn = NULL;
  34537. word32 cnSz;
  34538. X509_NAME *name = NULL;
  34539. X509 *x509 = NULL;
  34540. X509 *ca = NULL;
  34541. DecodedCert dCert;
  34542. EVP_PKEY *pub = NULL;
  34543. EVP_PKEY *priv = NULL;
  34544. EVP_MD_CTX *mctx = NULL;
  34545. #if defined(USE_CERT_BUFFERS_1024)
  34546. const unsigned char* rsaPriv = client_key_der_1024;
  34547. const unsigned char* rsaPub = client_keypub_der_1024;
  34548. const unsigned char* certIssuer = client_cert_der_1024;
  34549. long clientKeySz = (long)sizeof_client_key_der_1024;
  34550. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  34551. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  34552. #elif defined(USE_CERT_BUFFERS_2048)
  34553. const unsigned char* rsaPriv = client_key_der_2048;
  34554. const unsigned char* rsaPub = client_keypub_der_2048;
  34555. const unsigned char* certIssuer = client_cert_der_2048;
  34556. long clientKeySz = (long)sizeof_client_key_der_2048;
  34557. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  34558. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  34559. #endif
  34560. byte sn[16];
  34561. int snSz = sizeof(sn);
  34562. /* Set X509_NAME fields */
  34563. ExpectNotNull(name = X509_NAME_new());
  34564. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  34565. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  34566. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34567. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  34568. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34569. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  34570. /* Get private and public keys */
  34571. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  34572. clientKeySz));
  34573. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  34574. ExpectNotNull(x509 = X509_new());
  34575. /* Set version 3 */
  34576. ExpectIntNE(X509_set_version(x509, 2L), 0);
  34577. /* Set subject name, add pubkey, and sign certificate */
  34578. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  34579. X509_NAME_free(name);
  34580. name = NULL;
  34581. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  34582. #ifdef WOLFSSL_ALT_NAMES
  34583. /* Add some subject alt names */
  34584. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  34585. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  34586. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34587. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  34588. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34589. "sphygmomanometer",
  34590. ASN_DNS_TYPE), SSL_SUCCESS);
  34591. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34592. "supercalifragilisticexpialidocious",
  34593. ASN_DNS_TYPE), SSL_SUCCESS);
  34594. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34595. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  34596. ASN_DNS_TYPE), SSL_SUCCESS);
  34597. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34598. {
  34599. unsigned char ip4_type[] = {127,128,0,255};
  34600. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  34601. 0xff, 0xee, 0x99, 0x88,
  34602. 0x77, 0x66, 0x55, 0x44,
  34603. 0x00, 0x33, 0x22, 0x11};
  34604. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  34605. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  34606. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  34607. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  34608. }
  34609. #endif
  34610. #endif /* WOLFSSL_ALT_NAMES */
  34611. /* test valid sign case */
  34612. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  34613. /* test valid X509_sign_ctx case */
  34614. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34615. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  34616. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  34617. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  34618. ExpectIntEQ(X509_get_ext_count(x509), 1);
  34619. #endif
  34620. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  34621. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  34622. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  34623. #endif
  34624. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  34625. WOLFSSL_SUCCESS);
  34626. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  34627. /* Variation in size depends on ASN.1 encoding when MSB is set.
  34628. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  34629. * with the MSB set. See GenerateInteger in asn.c */
  34630. #ifndef USE_CERT_BUFFERS_1024
  34631. #ifndef WOLFSSL_ALT_NAMES
  34632. /* Valid case - size should be 781-786 with 16 byte serial number */
  34633. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  34634. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34635. /* Valid case - size should be 955-960 with 16 byte serial number */
  34636. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  34637. #else
  34638. /* Valid case - size should be 926-931 with 16 byte serial number */
  34639. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  34640. #endif
  34641. #else
  34642. #ifndef WOLFSSL_ALT_NAMES
  34643. /* Valid case - size should be 537-542 with 16 byte serial number */
  34644. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  34645. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34646. /* Valid case - size should be 695-670 with 16 byte serial number */
  34647. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  34648. #else
  34649. /* Valid case - size should be 666-671 with 16 byte serial number */
  34650. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  34651. #endif
  34652. #endif
  34653. /* check that issuer name is as expected after signature */
  34654. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  34655. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  34656. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  34657. ExpectNotNull(name = X509_get_subject_name(ca));
  34658. cnSz = X509_NAME_get_sz(name);
  34659. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34660. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34661. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  34662. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34663. cn = NULL;
  34664. #ifdef WOLFSSL_MULTI_ATTRIB
  34665. /* test adding multiple OU's to the signer */
  34666. ExpectNotNull(name = X509_get_subject_name(ca));
  34667. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34668. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  34669. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34670. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  34671. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  34672. #endif
  34673. ExpectNotNull(name = X509_get_subject_name(ca));
  34674. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  34675. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34676. ExpectNotNull(name = X509_get_issuer_name(x509));
  34677. cnSz = X509_NAME_get_sz(name);
  34678. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34679. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34680. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  34681. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  34682. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34683. cn = NULL;
  34684. FreeDecodedCert(&dCert);
  34685. /* Test invalid parameters */
  34686. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  34687. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  34688. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  34689. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  34690. EVP_MD_CTX_free(mctx);
  34691. mctx = NULL;
  34692. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34693. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  34694. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  34695. /* test invalid version number */
  34696. #if defined(OPENSSL_ALL)
  34697. ExpectIntNE(X509_set_version(x509, 6L), 0);
  34698. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34699. /* uses ParseCert which fails on bad version number */
  34700. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  34701. #endif
  34702. EVP_MD_CTX_free(mctx);
  34703. EVP_PKEY_free(priv);
  34704. EVP_PKEY_free(pub);
  34705. X509_free(x509);
  34706. X509_free(ca);
  34707. #endif
  34708. return EXPECT_RESULT();
  34709. }
  34710. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  34711. {
  34712. EXPECT_DECLS;
  34713. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34714. X509* x509 = NULL;
  34715. const X509_ALGOR* alg;
  34716. ExpectNotNull(x509 = X509_new());
  34717. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  34718. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34719. X509_free(x509);
  34720. #endif
  34721. return EXPECT_RESULT();
  34722. }
  34723. static int test_wolfSSL_X509_ALGOR_get0(void)
  34724. {
  34725. EXPECT_DECLS;
  34726. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34727. !defined(NO_SHA256) && !defined(NO_RSA)
  34728. X509* x509 = NULL;
  34729. const ASN1_OBJECT* obj = NULL;
  34730. const X509_ALGOR* alg = NULL;
  34731. int pptype = 0;
  34732. const void *ppval = NULL;
  34733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34734. SSL_FILETYPE_PEM));
  34735. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34736. /* Invalid case */
  34737. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  34738. ExpectNull(obj);
  34739. /* Valid case */
  34740. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  34741. ExpectNotNull(obj);
  34742. ExpectNull(ppval);
  34743. ExpectIntNE(pptype, 0);
  34744. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  34745. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  34746. X509_free(x509);
  34747. #endif
  34748. return EXPECT_RESULT();
  34749. }
  34750. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  34751. {
  34752. EXPECT_DECLS;
  34753. #if defined(OPENSSL_EXTRA)
  34754. X509_VERIFY_PARAM *paramTo = NULL;
  34755. X509_VERIFY_PARAM *paramFrom = NULL;
  34756. char testIPv4[] = "127.0.0.1";
  34757. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  34758. char testhostName1[] = "foo.hoge.com";
  34759. char testhostName2[] = "foobar.hoge.com";
  34760. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  34761. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  34762. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  34763. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  34764. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  34765. (int)XSTRLEN(testhostName1)), 1);
  34766. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  34767. (int)XSTRLEN(testhostName1)));
  34768. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  34769. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  34770. ExpectIntEQ(0x01, paramFrom->hostFlags);
  34771. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  34772. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  34773. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34774. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  34775. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  34776. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34777. /* null pointer */
  34778. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  34779. /* in the case of "from" null, returns success */
  34780. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  34781. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  34782. /* inherit flags test : VPARAM_DEFAULT */
  34783. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34784. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34785. (int)XSTRLEN(testhostName1)));
  34786. ExpectIntEQ(0x01, paramTo->hostFlags);
  34787. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34788. /* inherit flags test : VPARAM OVERWRITE */
  34789. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34790. (int)XSTRLEN(testhostName2)), 1);
  34791. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34792. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34793. if (paramTo != NULL) {
  34794. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  34795. }
  34796. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34797. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34798. (int)XSTRLEN(testhostName1)));
  34799. ExpectIntEQ(0x01, paramTo->hostFlags);
  34800. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34801. /* inherit flags test : VPARAM_RESET_FLAGS */
  34802. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34803. (int)XSTRLEN(testhostName2)), 1);
  34804. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34805. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  34806. if (paramTo != NULL) {
  34807. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  34808. }
  34809. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34810. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34811. (int)XSTRLEN(testhostName1)));
  34812. ExpectIntEQ(0x01, paramTo->hostFlags);
  34813. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34814. /* inherit flags test : VPARAM_LOCKED */
  34815. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34816. (int)XSTRLEN(testhostName2)), 1);
  34817. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34818. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34819. if (paramTo != NULL) {
  34820. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  34821. }
  34822. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34823. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  34824. (int)XSTRLEN(testhostName2)));
  34825. ExpectIntEQ(0x00, paramTo->hostFlags);
  34826. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34827. /* test for incorrect parameters */
  34828. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  34829. 0);
  34830. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  34831. /* inherit flags test : VPARAM_ONCE, not testable yet */
  34832. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  34833. 1);
  34834. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  34835. X509_V_FLAG_CRL_CHECK_ALL);
  34836. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  34837. X509_V_FLAG_CRL_CHECK_ALL), 1);
  34838. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  34839. X509_VERIFY_PARAM_free(paramTo);
  34840. X509_VERIFY_PARAM_free(paramFrom);
  34841. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  34842. #endif
  34843. return EXPECT_RESULT();
  34844. }
  34845. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34846. static int test_wolfSSL_check_domain_verify_count = 0;
  34847. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  34848. WOLFSSL_X509_STORE_CTX* store)
  34849. {
  34850. EXPECT_DECLS;
  34851. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  34852. ExpectIntEQ(preverify, 1);
  34853. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  34854. return EXPECT_SUCCESS();
  34855. }
  34856. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  34857. {
  34858. EXPECT_DECLS;
  34859. X509_VERIFY_PARAM *param = NULL;
  34860. ExpectNotNull(param = SSL_get0_param(ssl));
  34861. /* Domain check should only be done on the leaf cert */
  34862. X509_VERIFY_PARAM_set_hostflags(param,
  34863. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  34864. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  34865. "wolfSSL Server Chain", 0), 1);
  34866. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  34867. test_wolfSSL_check_domain_verify_cb);
  34868. return EXPECT_RESULT();
  34869. }
  34870. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  34871. {
  34872. EXPECT_DECLS;
  34873. /* Use a cert with different domains in chain */
  34874. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  34875. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  34876. return EXPECT_RESULT();
  34877. }
  34878. static int test_wolfSSL_check_domain(void)
  34879. {
  34880. EXPECT_DECLS;
  34881. test_ssl_cbf func_cb_client;
  34882. test_ssl_cbf func_cb_server;
  34883. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  34884. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  34885. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  34886. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  34887. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  34888. &func_cb_server, NULL), TEST_SUCCESS);
  34889. /* Should have been called once for each cert in sent chain */
  34890. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  34891. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  34892. #else
  34893. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  34894. #endif
  34895. return EXPECT_RESULT();
  34896. }
  34897. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  34898. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  34899. {
  34900. EXPECT_DECLS;
  34901. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34902. X509* x509 = NULL;
  34903. X509_PUBKEY* pubKey;
  34904. ExpectNotNull(x509 = X509_new());
  34905. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  34906. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  34907. X509_free(x509);
  34908. #endif
  34909. return EXPECT_RESULT();
  34910. }
  34911. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  34912. {
  34913. EXPECT_DECLS;
  34914. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34915. !defined(NO_SHA256) && !defined(NO_RSA)
  34916. X509* x509 = NULL;
  34917. ASN1_OBJECT* obj = NULL;
  34918. const ASN1_OBJECT* pa_oid = NULL;
  34919. X509_PUBKEY* pubKey = NULL;
  34920. X509_PUBKEY* pubKey2 = NULL;
  34921. EVP_PKEY* evpKey = NULL;
  34922. const unsigned char *pk = NULL;
  34923. int ppklen;
  34924. int pptype;
  34925. X509_ALGOR *pa = NULL;
  34926. const void *pval;
  34927. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  34928. SSL_FILETYPE_PEM));
  34929. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  34930. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  34931. ExpectNotNull(pk);
  34932. ExpectNotNull(pa);
  34933. ExpectNotNull(pubKey);
  34934. ExpectIntGT(ppklen, 0);
  34935. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  34936. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  34937. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  34938. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  34939. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  34940. ExpectNotNull(pk);
  34941. ExpectNotNull(pa);
  34942. ExpectIntGT(ppklen, 0);
  34943. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  34944. ExpectNotNull(pa_oid);
  34945. ExpectNull(pval);
  34946. ExpectIntEQ(pptype, V_ASN1_NULL);
  34947. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  34948. X509_PUBKEY_free(pubKey2);
  34949. X509_free(x509);
  34950. EVP_PKEY_free(evpKey);
  34951. #endif
  34952. return EXPECT_RESULT();
  34953. }
  34954. static int test_wolfSSL_X509_PUBKEY_EC(void)
  34955. {
  34956. EXPECT_DECLS;
  34957. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  34958. X509* x509 = NULL;
  34959. ASN1_OBJECT* obj = NULL;
  34960. ASN1_OBJECT* poid = NULL;
  34961. const ASN1_OBJECT* pa_oid = NULL;
  34962. X509_PUBKEY* pubKey = NULL;
  34963. X509_PUBKEY* pubKey2 = NULL;
  34964. EVP_PKEY* evpKey = NULL;
  34965. const unsigned char *pk = NULL;
  34966. int ppklen;
  34967. int pptype;
  34968. X509_ALGOR *pa = NULL;
  34969. const void *pval;
  34970. char buf[50];
  34971. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  34972. SSL_FILETYPE_PEM));
  34973. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  34974. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  34975. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  34976. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  34977. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  34978. ExpectNotNull(pk);
  34979. ExpectNotNull(pa);
  34980. ExpectIntGT(ppklen, 0);
  34981. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  34982. ExpectNotNull(pa_oid);
  34983. ExpectNotNull(pval);
  34984. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  34985. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  34986. poid = (ASN1_OBJECT *)pval;
  34987. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  34988. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  34989. X509_PUBKEY_free(pubKey2);
  34990. X509_free(x509);
  34991. EVP_PKEY_free(evpKey);
  34992. #endif
  34993. return EXPECT_RESULT();
  34994. }
  34995. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  34996. {
  34997. EXPECT_DECLS;
  34998. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  34999. word32 bytes;
  35000. #ifdef USE_CERT_BUFFERS_1024
  35001. byte tmp[ONEK_BUF];
  35002. #elif defined(USE_CERT_BUFFERS_2048)
  35003. byte tmp[TWOK_BUF];
  35004. #else
  35005. byte tmp[TWOK_BUF];
  35006. #endif /* END USE_CERT_BUFFERS_1024 */
  35007. const unsigned char* dsaKeyDer = tmp;
  35008. ASN1_OBJECT* obj = NULL;
  35009. ASN1_STRING* str;
  35010. const ASN1_OBJECT* pa_oid = NULL;
  35011. X509_PUBKEY* pubKey = NULL;
  35012. EVP_PKEY* evpKey = NULL;
  35013. const unsigned char *pk = NULL;
  35014. int ppklen, pptype;
  35015. X509_ALGOR *pa = NULL;
  35016. const void *pval;
  35017. #ifdef USE_CERT_BUFFERS_1024
  35018. XMEMSET(tmp, 0, sizeof(tmp));
  35019. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  35020. bytes = sizeof_dsa_key_der_1024;
  35021. #elif defined(USE_CERT_BUFFERS_2048)
  35022. XMEMSET(tmp, 0, sizeof(tmp));
  35023. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  35024. bytes = sizeof_dsa_key_der_2048;
  35025. #else
  35026. {
  35027. XFILE fp = XBADFILE;
  35028. XMEMSET(tmp, 0, sizeof(tmp));
  35029. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  35030. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  35031. if (fp != XBADFILE)
  35032. XFCLOSE(fp);
  35033. }
  35034. #endif
  35035. /* Initialize pkey with der format dsa key */
  35036. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  35037. ExpectNotNull(pubKey = X509_PUBKEY_new());
  35038. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  35039. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35040. ExpectNotNull(pk);
  35041. ExpectNotNull(pa);
  35042. ExpectIntGT(ppklen, 0);
  35043. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35044. ExpectNotNull(pa_oid);
  35045. ExpectNotNull(pval);
  35046. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  35047. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  35048. str = (ASN1_STRING *)pval;
  35049. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  35050. #ifdef USE_CERT_BUFFERS_1024
  35051. ExpectIntEQ(ASN1_STRING_length(str), 291);
  35052. #else
  35053. ExpectIntEQ(ASN1_STRING_length(str), 549);
  35054. #endif /* END USE_CERT_BUFFERS_1024 */
  35055. X509_PUBKEY_free(pubKey);
  35056. EVP_PKEY_free(evpKey);
  35057. #endif
  35058. return EXPECT_RESULT();
  35059. }
  35060. static int test_wolfSSL_BUF(void)
  35061. {
  35062. EXPECT_DECLS;
  35063. #if defined(OPENSSL_EXTRA)
  35064. BUF_MEM* buf = NULL;
  35065. ExpectNotNull(buf = BUF_MEM_new());
  35066. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  35067. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  35068. BUF_MEM_free(buf);
  35069. #endif
  35070. return EXPECT_RESULT();
  35071. }
  35072. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35073. static int stub_rand_seed(const void *buf, int num)
  35074. {
  35075. (void)buf;
  35076. (void)num;
  35077. return 123;
  35078. }
  35079. static int stub_rand_bytes(unsigned char *buf, int num)
  35080. {
  35081. (void)buf;
  35082. (void)num;
  35083. return 456;
  35084. }
  35085. static byte* was_stub_rand_cleanup_called(void)
  35086. {
  35087. static byte was_called = 0;
  35088. return &was_called;
  35089. }
  35090. static void stub_rand_cleanup(void)
  35091. {
  35092. byte* was_called = was_stub_rand_cleanup_called();
  35093. *was_called = 1;
  35094. return;
  35095. }
  35096. static byte* was_stub_rand_add_called(void)
  35097. {
  35098. static byte was_called = 0;
  35099. return &was_called;
  35100. }
  35101. static int stub_rand_add(const void *buf, int num, double entropy)
  35102. {
  35103. byte* was_called = was_stub_rand_add_called();
  35104. (void)buf;
  35105. (void)num;
  35106. (void)entropy;
  35107. *was_called = 1;
  35108. return 0;
  35109. }
  35110. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  35111. {
  35112. (void)buf;
  35113. (void)num;
  35114. return 9876;
  35115. }
  35116. static int stub_rand_status(void)
  35117. {
  35118. return 5432;
  35119. }
  35120. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35121. static int test_wolfSSL_RAND_set_rand_method(void)
  35122. {
  35123. EXPECT_DECLS;
  35124. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35125. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  35126. unsigned char* buf = NULL;
  35127. int num = 0;
  35128. double entropy = 0;
  35129. int ret;
  35130. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  35131. byte* was_add_called = was_stub_rand_add_called();
  35132. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  35133. DYNAMIC_TYPE_TMP_BUFFER));
  35134. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  35135. ExpectIntEQ(*was_cleanup_called, 0);
  35136. RAND_cleanup();
  35137. ExpectIntEQ(*was_cleanup_called, 0);
  35138. rand_methods.seed = &stub_rand_seed;
  35139. rand_methods.bytes = &stub_rand_bytes;
  35140. rand_methods.cleanup = &stub_rand_cleanup;
  35141. rand_methods.add = &stub_rand_add;
  35142. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  35143. rand_methods.status = &stub_rand_status;
  35144. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  35145. ExpectIntEQ(RAND_seed(buf, num), 123);
  35146. ExpectIntEQ(RAND_bytes(buf, num), 456);
  35147. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  35148. ExpectIntEQ(RAND_status(), 5432);
  35149. ExpectIntEQ(*was_add_called, 0);
  35150. /* The function pointer for RAND_add returns int, but RAND_add itself
  35151. * returns void. */
  35152. RAND_add(buf, num, entropy);
  35153. ExpectIntEQ(*was_add_called, 1);
  35154. was_add_called = 0;
  35155. ExpectIntEQ(*was_cleanup_called, 0);
  35156. RAND_cleanup();
  35157. ExpectIntEQ(*was_cleanup_called, 1);
  35158. *was_cleanup_called = 0;
  35159. ret = RAND_set_rand_method(NULL);
  35160. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  35161. ExpectIntNE(RAND_status(), 5432);
  35162. ExpectIntEQ(*was_cleanup_called, 0);
  35163. RAND_cleanup();
  35164. ExpectIntEQ(*was_cleanup_called, 0);
  35165. RAND_set_rand_method(NULL);
  35166. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35167. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35168. return EXPECT_RESULT();
  35169. }
  35170. static int test_wolfSSL_RAND_bytes(void)
  35171. {
  35172. EXPECT_DECLS;
  35173. #if defined(OPENSSL_EXTRA)
  35174. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  35175. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  35176. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  35177. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  35178. int max_bufsize;
  35179. byte *my_buf = NULL;
  35180. /* sanity check */
  35181. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  35182. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  35183. max_bufsize = size4;
  35184. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  35185. DYNAMIC_TYPE_TMP_BUFFER));
  35186. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  35187. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  35188. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  35189. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  35190. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  35191. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  35192. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  35193. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35194. #endif
  35195. return EXPECT_RESULT();
  35196. }
  35197. static int test_wolfSSL_RAND(void)
  35198. {
  35199. EXPECT_DECLS;
  35200. #if defined(OPENSSL_EXTRA)
  35201. byte seed[16];
  35202. XMEMSET(seed, 0, sizeof(seed));
  35203. /* No global methods set. */
  35204. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  35205. ExpectIntEQ(RAND_poll(), 1);
  35206. RAND_cleanup();
  35207. ExpectIntEQ(RAND_egd(NULL), -1);
  35208. #ifndef NO_FILESYSTEM
  35209. {
  35210. char fname[100];
  35211. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  35212. ExpectIntEQ(RAND_write_file(NULL), 0);
  35213. }
  35214. #endif
  35215. #endif
  35216. return EXPECT_RESULT();
  35217. }
  35218. static int test_wolfSSL_PKCS8_Compat(void)
  35219. {
  35220. EXPECT_DECLS;
  35221. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  35222. !defined(NO_BIO)
  35223. PKCS8_PRIV_KEY_INFO* pt = NULL;
  35224. BIO* bio = NULL;
  35225. XFILE f = XBADFILE;
  35226. int bytes;
  35227. char pkcs8_buffer[512];
  35228. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35229. EVP_PKEY *pkey = NULL;
  35230. #endif
  35231. /* file from wolfssl/certs/ directory */
  35232. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  35233. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  35234. 0);
  35235. if (f != XBADFILE)
  35236. XFCLOSE(f);
  35237. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35238. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  35239. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35240. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  35241. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  35242. /* gets PKCS8 pointer to pkey */
  35243. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  35244. EVP_PKEY_free(pkey);
  35245. #endif
  35246. BIO_free(bio);
  35247. PKCS8_PRIV_KEY_INFO_free(pt);
  35248. #endif
  35249. return EXPECT_RESULT();
  35250. }
  35251. static int test_wolfSSL_PKCS8_d2i(void)
  35252. {
  35253. EXPECT_DECLS;
  35254. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  35255. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  35256. * requires a 12 byte password in FIPS mode. This test ends up
  35257. * trying to use an 8 byte password. */
  35258. #ifndef NO_FILESYSTEM
  35259. unsigned char pkcs8_buffer[2048];
  35260. const unsigned char* p;
  35261. int bytes;
  35262. XFILE file = XBADFILE;
  35263. WOLFSSL_EVP_PKEY* pkey = NULL;
  35264. #ifndef NO_BIO
  35265. BIO* bio = NULL;
  35266. #if defined(OPENSSL_ALL) && \
  35267. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  35268. defined(HAVE_ECC)) && \
  35269. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35270. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  35271. #endif
  35272. #endif
  35273. #ifndef NO_RSA
  35274. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  35275. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  35276. #ifndef NO_DES3
  35277. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  35278. #endif
  35279. #endif /* NO_RSA */
  35280. #ifdef HAVE_ECC
  35281. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  35282. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  35283. #ifndef NO_DES3
  35284. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  35285. #endif
  35286. #endif /* HAVE_ECC */
  35287. #endif /* !NO_FILESYSTEM */
  35288. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  35289. #ifndef NO_RSA
  35290. #ifdef USE_CERT_BUFFERS_1024
  35291. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  35292. int rsaSz = sizeof_server_key_der_1024;
  35293. #else
  35294. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  35295. int rsaSz = sizeof_server_key_der_2048;
  35296. #endif
  35297. #endif
  35298. #ifdef HAVE_ECC
  35299. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  35300. int ecSz = sizeof_ecc_key_der_256;
  35301. #endif
  35302. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  35303. #ifndef NO_FILESYSTEM
  35304. (void)pkcs8_buffer;
  35305. (void)p;
  35306. (void)bytes;
  35307. (void)file;
  35308. #ifndef NO_BIO
  35309. (void)bio;
  35310. #endif
  35311. #endif
  35312. #ifdef OPENSSL_ALL
  35313. #ifndef NO_RSA
  35314. /* Try to auto-detect normal RSA private key */
  35315. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  35316. EVP_PKEY_free(pkey);
  35317. pkey = NULL;
  35318. #endif
  35319. #ifdef HAVE_ECC
  35320. /* Try to auto-detect normal EC private key */
  35321. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  35322. EVP_PKEY_free(pkey);
  35323. pkey = NULL;
  35324. #endif
  35325. #endif /* OPENSSL_ALL */
  35326. #ifndef NO_FILESYSTEM
  35327. #ifndef NO_RSA
  35328. /* Get DER encoded RSA PKCS#8 data. */
  35329. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  35330. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35331. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35332. file)), 0);
  35333. if (file != XBADFILE) {
  35334. XFCLOSE(file);
  35335. file = XBADFILE;
  35336. }
  35337. p = pkcs8_buffer;
  35338. #ifdef OPENSSL_ALL
  35339. /* Try to decode - auto-detect key type. */
  35340. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35341. #else
  35342. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  35343. #endif
  35344. /* Get PEM encoded RSA PKCS#8 data. */
  35345. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  35346. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35347. file)), 0);
  35348. if (file != XBADFILE) {
  35349. XFCLOSE(file);
  35350. file = XBADFILE;
  35351. }
  35352. #if defined(OPENSSL_ALL) && \
  35353. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35354. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35355. /* Write PKCS#8 PEM to BIO. */
  35356. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35357. NULL), bytes);
  35358. /* Compare file and written data */
  35359. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35360. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35361. BIO_free(bio);
  35362. bio = NULL;
  35363. #if !defined(NO_DES3) && !defined(NO_SHA)
  35364. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35365. /* Write Encrypted PKCS#8 PEM to BIO. */
  35366. bytes = 1834;
  35367. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  35368. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35369. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35370. (void*)"yassl123"));
  35371. EVP_PKEY_free(evpPkey);
  35372. evpPkey = NULL;
  35373. BIO_free(bio);
  35374. bio = NULL;
  35375. #endif /* !NO_DES3 && !NO_SHA */
  35376. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35377. EVP_PKEY_free(pkey);
  35378. pkey = NULL;
  35379. /* PKCS#8 encrypted RSA key */
  35380. #ifndef NO_DES3
  35381. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  35382. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35383. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35384. file)), 0);
  35385. if (file != XBADFILE) {
  35386. XFCLOSE(file);
  35387. file = XBADFILE;
  35388. }
  35389. #if defined(OPENSSL_ALL) && \
  35390. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35391. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35392. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35393. (void*)"yassl123"));
  35394. EVP_PKEY_free(pkey);
  35395. pkey = NULL;
  35396. BIO_free(bio);
  35397. bio = NULL;
  35398. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35399. #endif /* !NO_DES3 */
  35400. #endif /* NO_RSA */
  35401. #ifdef HAVE_ECC
  35402. /* PKCS#8 encode EC key */
  35403. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  35404. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35405. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35406. file)), 0);
  35407. if (file != XBADFILE) {
  35408. XFCLOSE(file);
  35409. file = XBADFILE;
  35410. }
  35411. p = pkcs8_buffer;
  35412. #ifdef OPENSSL_ALL
  35413. /* Try to decode - auto-detect key type. */
  35414. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35415. #else
  35416. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  35417. #endif
  35418. /* Get PEM encoded RSA PKCS#8 data. */
  35419. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  35420. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35421. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35422. file)), 0);
  35423. if (file != XBADFILE) {
  35424. XFCLOSE(file);
  35425. file = XBADFILE;
  35426. }
  35427. #if defined(OPENSSL_ALL) && \
  35428. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  35429. defined(HAVE_AES_CBC)
  35430. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35431. /* Write PKCS#8 PEM to BIO. */
  35432. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35433. NULL), bytes);
  35434. /* Compare file and written data */
  35435. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35436. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35437. BIO_free(bio);
  35438. bio = NULL;
  35439. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35440. /* Write Encrypted PKCS#8 PEM to BIO. */
  35441. bytes = 379;
  35442. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  35443. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35444. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35445. (void*)"yassl123"));
  35446. EVP_PKEY_free(evpPkey);
  35447. evpPkey = NULL;
  35448. BIO_free(bio);
  35449. bio = NULL;
  35450. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  35451. EVP_PKEY_free(pkey);
  35452. pkey = NULL;
  35453. /* PKCS#8 encrypted EC key */
  35454. #ifndef NO_DES3
  35455. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  35456. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35457. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35458. file)), 0);
  35459. if (file != XBADFILE) {
  35460. XFCLOSE(file);
  35461. file = XBADFILE;
  35462. }
  35463. #if defined(OPENSSL_ALL) && \
  35464. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35465. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35466. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35467. (void*)"yassl123"));
  35468. EVP_PKEY_free(pkey);
  35469. pkey = NULL;
  35470. BIO_free(bio);
  35471. bio = NULL;
  35472. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35473. #endif /* !NO_DES3 */
  35474. #endif /* HAVE_ECC */
  35475. #endif /* !NO_FILESYSTEM */
  35476. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  35477. return EXPECT_RESULT();
  35478. }
  35479. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35480. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35481. #define LOGGING_THREADS 5
  35482. #define ERROR_COUNT 10
  35483. /* copied from logging.c since this is not exposed otherwise */
  35484. #ifndef ERROR_QUEUE_MAX
  35485. #ifdef ERROR_QUEUE_PER_THREAD
  35486. #define ERROR_QUEUE_MAX 16
  35487. #else
  35488. /* this breaks from compat of unlimited error queue size */
  35489. #define ERROR_QUEUE_MAX 100
  35490. #endif
  35491. #endif
  35492. static volatile int loggingThreadsReady;
  35493. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  35494. {
  35495. const char* file;
  35496. int line;
  35497. unsigned long err;
  35498. int errorCount = 0;
  35499. int i;
  35500. (void)args;
  35501. while (!loggingThreadsReady);
  35502. for (i = 0; i < ERROR_COUNT; i++)
  35503. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35504. while ((err = ERR_get_error_line(&file, &line))) {
  35505. AssertIntEQ(err, 990 + errorCount);
  35506. errorCount++;
  35507. }
  35508. AssertIntEQ(errorCount, ERROR_COUNT);
  35509. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  35510. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  35511. errorCount = 0;
  35512. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  35513. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35514. while ((err = ERR_get_error_line(&file, &line))) {
  35515. AssertIntEQ(err, 990 + errorCount);
  35516. errorCount++;
  35517. }
  35518. /* test that the 3 errors over the max were dropped */
  35519. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  35520. WOLFSSL_RETURN_FROM_THREAD(0);
  35521. }
  35522. #endif
  35523. static int test_error_queue_per_thread(void)
  35524. {
  35525. int res = TEST_SKIPPED;
  35526. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35527. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35528. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  35529. int i;
  35530. ERR_clear_error(); /* clear out any error nodes */
  35531. loggingThreadsReady = 0;
  35532. for (i = 0; i < LOGGING_THREADS; i++)
  35533. start_thread(test_logging, NULL, &loggingThreads[i]);
  35534. loggingThreadsReady = 1;
  35535. for (i = 0; i < LOGGING_THREADS; i++)
  35536. join_thread(loggingThreads[i]);
  35537. res = TEST_SUCCESS;
  35538. #endif
  35539. return res;
  35540. }
  35541. static int test_wolfSSL_ERR_put_error(void)
  35542. {
  35543. EXPECT_DECLS;
  35544. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35545. defined(DEBUG_WOLFSSL)
  35546. const char* file;
  35547. int line;
  35548. ERR_clear_error(); /* clear out any error nodes */
  35549. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35550. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35551. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  35552. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  35553. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  35554. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  35555. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  35556. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  35557. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  35558. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  35559. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  35560. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  35561. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  35562. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  35563. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  35564. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  35565. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  35566. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  35567. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  35568. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  35569. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  35570. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  35571. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  35572. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  35573. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  35574. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  35575. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  35576. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  35577. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  35578. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  35579. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  35580. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  35581. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  35582. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  35583. "this file", 100);
  35584. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  35585. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35586. ExpectIntEQ(line, 100);
  35587. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  35588. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35589. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  35590. #endif
  35591. /* try reading past end of error queue */
  35592. file = NULL;
  35593. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35594. ExpectNull(file);
  35595. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  35596. PEMerr(4,4);
  35597. ExpectIntEQ(ERR_get_error(), 4);
  35598. /* Empty and free up all error nodes */
  35599. ERR_clear_error();
  35600. /* Verify all nodes are cleared */
  35601. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35602. ERR_clear_error();
  35603. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35604. #endif
  35605. return EXPECT_RESULT();
  35606. }
  35607. /*
  35608. * This is a regression test for a bug where the peek/get error functions were
  35609. * drawing from the end of the queue rather than the front.
  35610. */
  35611. static int test_wolfSSL_ERR_get_error_order(void)
  35612. {
  35613. EXPECT_DECLS;
  35614. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  35615. /* Empty the queue. */
  35616. wolfSSL_ERR_clear_error();
  35617. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  35618. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  35619. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  35620. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  35621. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  35622. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  35623. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  35624. return EXPECT_RESULT();
  35625. }
  35626. #ifndef NO_BIO
  35627. static int test_wolfSSL_ERR_print_errors(void)
  35628. {
  35629. EXPECT_DECLS;
  35630. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35631. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  35632. BIO* bio = NULL;
  35633. char buf[1024];
  35634. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35635. ERR_clear_error(); /* clear out any error nodes */
  35636. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35637. /* Choosing -600 as an unused errno. */
  35638. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  35639. ERR_print_errors(bio);
  35640. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  35641. ExpectIntEQ(XSTRNCMP(
  35642. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  35643. buf, 55), 0);
  35644. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  35645. ExpectIntEQ(XSTRNCMP(
  35646. "error:600:wolfSSL library:unknown error number:asn.c:100",
  35647. buf, 56), 0);
  35648. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  35649. ExpectIntEQ(buf[0], '\0');
  35650. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  35651. BIO_free(bio);
  35652. #endif
  35653. return EXPECT_RESULT();
  35654. }
  35655. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35656. defined(DEBUG_WOLFSSL)
  35657. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  35658. {
  35659. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  35660. return 0;
  35661. }
  35662. #endif
  35663. static int test_wolfSSL_ERR_print_errors_cb(void)
  35664. {
  35665. EXPECT_DECLS;
  35666. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35667. defined(DEBUG_WOLFSSL)
  35668. BIO* bio = NULL;
  35669. char buf[1024];
  35670. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35671. ERR_clear_error(); /* clear out any error nodes */
  35672. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35673. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  35674. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  35675. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  35676. ExpectIntEQ(XSTRNCMP(
  35677. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  35678. buf, 53), 0);
  35679. ExpectIntEQ(XSTRNCMP(
  35680. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  35681. buf + 53, 55), 0);
  35682. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  35683. BIO_free(bio);
  35684. #endif
  35685. return EXPECT_RESULT();
  35686. }
  35687. /*
  35688. * Testing WOLFSSL_ERROR_MSG
  35689. */
  35690. static int test_WOLFSSL_ERROR_MSG(void)
  35691. {
  35692. int res = TEST_SKIPPED;
  35693. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  35694. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  35695. const char* msg = TEST_STRING;
  35696. WOLFSSL_ERROR_MSG(msg);
  35697. res = TEST_SUCCESS;
  35698. #endif
  35699. return res;
  35700. } /* End test_WOLFSSL_ERROR_MSG */
  35701. /*
  35702. * Testing wc_ERR_remove_state
  35703. */
  35704. static int test_wc_ERR_remove_state(void)
  35705. {
  35706. int res = TEST_SKIPPED;
  35707. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35708. wc_ERR_remove_state();
  35709. res = TEST_SUCCESS;
  35710. #endif
  35711. return res;
  35712. } /* End test_wc_ERR_remove_state */
  35713. /*
  35714. * Testing wc_ERR_print_errors_fp
  35715. */
  35716. static int test_wc_ERR_print_errors_fp(void)
  35717. {
  35718. EXPECT_DECLS;
  35719. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  35720. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  35721. long sz;
  35722. XFILE fp = XBADFILE;
  35723. WOLFSSL_ERROR(BAD_FUNC_ARG);
  35724. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  35725. XBADFILE);
  35726. wc_ERR_print_errors_fp(fp);
  35727. #if defined(DEBUG_WOLFSSL)
  35728. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  35729. #ifdef NO_ERROR_QUEUE
  35730. ExpectIntEQ(sz = XFTELL(fp), 0);
  35731. #else
  35732. ExpectIntNE(sz = XFTELL(fp), 0);
  35733. #endif
  35734. #endif
  35735. if (fp != XBADFILE)
  35736. XFCLOSE(fp);
  35737. (void)sz;
  35738. #endif
  35739. return EXPECT_RESULT();
  35740. } /* End test_wc_ERR_print_errors_fp */
  35741. #ifdef DEBUG_WOLFSSL
  35742. static void Logging_cb(const int logLevel, const char *const logMessage)
  35743. {
  35744. (void)logLevel;
  35745. (void)logMessage;
  35746. }
  35747. #endif
  35748. /*
  35749. * Testing wolfSSL_GetLoggingCb
  35750. */
  35751. static int test_wolfSSL_GetLoggingCb(void)
  35752. {
  35753. EXPECT_DECLS;
  35754. #ifdef DEBUG_WOLFSSL
  35755. /* Testing without wolfSSL_SetLoggingCb() */
  35756. ExpectNull(wolfSSL_GetLoggingCb());
  35757. /* Testing with wolfSSL_SetLoggingCb() */
  35758. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  35759. ExpectNotNull(wolfSSL_GetLoggingCb());
  35760. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  35761. #endif
  35762. ExpectNull(wolfSSL_GetLoggingCb());
  35763. return EXPECT_RESULT();
  35764. } /* End test_wolfSSL_GetLoggingCb */
  35765. #endif /* !NO_BIO */
  35766. static int test_wolfSSL_MD4(void)
  35767. {
  35768. EXPECT_DECLS;
  35769. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  35770. MD4_CTX md4;
  35771. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  35772. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  35773. "789012345678901234567890";
  35774. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  35775. "\xcc\x05\x36";
  35776. int msgSz = (int)XSTRLEN(msg);
  35777. XMEMSET(out, 0, sizeof(out));
  35778. MD4_Init(&md4);
  35779. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  35780. MD4_Final(out, &md4);
  35781. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  35782. #endif
  35783. return EXPECT_RESULT();
  35784. }
  35785. static int test_wolfSSL_MD5(void)
  35786. {
  35787. EXPECT_DECLS;
  35788. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35789. byte input1[] = "";
  35790. byte input2[] = "message digest";
  35791. byte hash[WC_MD5_DIGEST_SIZE];
  35792. unsigned char output1[] =
  35793. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  35794. unsigned char output2[] =
  35795. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  35796. WOLFSSL_MD5_CTX md5;
  35797. XMEMSET(&md5, 0, sizeof(md5));
  35798. /* Test cases for illegal parameters */
  35799. ExpectIntEQ(MD5_Init(NULL), 0);
  35800. ExpectIntEQ(MD5_Init(&md5), 1);
  35801. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  35802. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  35803. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  35804. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  35805. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  35806. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  35807. /* Init MD5 CTX */
  35808. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35809. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  35810. 1);
  35811. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35812. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35813. /* Init MD5 CTX */
  35814. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35815. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  35816. (int)XSTRLEN((const char*)input2)), 1);
  35817. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35818. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35819. #if !defined(NO_OLD_NAMES) && \
  35820. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  35821. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  35822. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  35823. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  35824. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  35825. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  35826. &hash);
  35827. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35828. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  35829. &hash);
  35830. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35831. {
  35832. byte data[] = "Data to be hashed.";
  35833. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  35834. ExpectNotNull(MD5(data, sizeof(data), NULL));
  35835. ExpectNotNull(MD5(data, sizeof(data), hash));
  35836. ExpectNotNull(MD5(NULL, 0, hash));
  35837. ExpectNull(MD5(NULL, sizeof(data), hash));
  35838. }
  35839. #endif
  35840. #endif
  35841. return EXPECT_RESULT();
  35842. }
  35843. static int test_wolfSSL_MD5_Transform(void)
  35844. {
  35845. EXPECT_DECLS;
  35846. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35847. byte input1[] = "";
  35848. byte input2[] = "abc";
  35849. byte local[WC_MD5_BLOCK_SIZE];
  35850. word32 sLen = 0;
  35851. #ifdef BIG_ENDIAN_ORDER
  35852. unsigned char output1[] =
  35853. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  35854. unsigned char output2[] =
  35855. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  35856. #else
  35857. unsigned char output1[] =
  35858. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  35859. unsigned char output2[] =
  35860. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  35861. #endif
  35862. union {
  35863. wc_Md5 native;
  35864. MD5_CTX compat;
  35865. } md5;
  35866. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  35867. XMEMSET(&local, 0, sizeof(local));
  35868. /* sanity check */
  35869. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  35870. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  35871. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  35872. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  35873. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35874. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  35875. /* Init MD5 CTX */
  35876. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  35877. /* Do Transform*/
  35878. sLen = (word32)XSTRLEN((char*)input1);
  35879. XMEMCPY(local, input1, sLen);
  35880. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35881. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  35882. /* Init MD5 CTX */
  35883. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  35884. sLen = (word32)XSTRLEN((char*)input2);
  35885. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  35886. XMEMCPY(local, input2, sLen);
  35887. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  35888. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  35889. #endif
  35890. return EXPECT_RESULT();
  35891. }
  35892. static int test_wolfSSL_SHA(void)
  35893. {
  35894. EXPECT_DECLS;
  35895. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  35896. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  35897. (!defined(HAVE_FIPS) || \
  35898. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35899. {
  35900. const unsigned char in[] = "abc";
  35901. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  35902. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  35903. unsigned char out[WC_SHA_DIGEST_SIZE];
  35904. unsigned char* p;
  35905. WOLFSSL_SHA_CTX sha;
  35906. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35907. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  35908. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35909. /* SHA interface test */
  35910. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35911. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  35912. ExpectNotNull(SHA(in, 0, out));
  35913. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  35914. ExpectNotNull(SHA(NULL, 0, out));
  35915. ExpectNotNull(SHA(NULL, 0, NULL));
  35916. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  35917. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35918. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  35919. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  35920. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  35921. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  35922. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  35923. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35924. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  35925. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  35926. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  35927. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35928. }
  35929. #endif
  35930. #if !defined(NO_SHA256)
  35931. {
  35932. const unsigned char in[] = "abc";
  35933. unsigned char expected[] =
  35934. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  35935. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  35936. "\x15\xAD";
  35937. unsigned char out[WC_SHA256_DIGEST_SIZE];
  35938. unsigned char* p;
  35939. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  35940. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35941. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  35942. #else
  35943. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  35944. #endif
  35945. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  35946. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35947. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  35948. #else
  35949. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  35950. #endif
  35951. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  35952. }
  35953. #endif
  35954. #if defined(WOLFSSL_SHA384)
  35955. {
  35956. const unsigned char in[] = "abc";
  35957. unsigned char expected[] =
  35958. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  35959. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  35960. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  35961. "\xc8\x25\xa7";
  35962. unsigned char out[WC_SHA384_DIGEST_SIZE];
  35963. unsigned char* p;
  35964. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  35965. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35966. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  35967. #else
  35968. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  35969. #endif
  35970. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  35971. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35972. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  35973. #else
  35974. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  35975. #endif
  35976. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  35977. }
  35978. #endif
  35979. #if defined(WOLFSSL_SHA512)
  35980. {
  35981. const unsigned char in[] = "abc";
  35982. unsigned char expected[] =
  35983. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  35984. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  35985. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  35986. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  35987. "\xa5\x4c\xa4\x9f";
  35988. unsigned char out[WC_SHA512_DIGEST_SIZE];
  35989. unsigned char* p;
  35990. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  35991. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35992. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  35993. #else
  35994. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  35995. #endif
  35996. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  35997. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35998. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  35999. #else
  36000. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  36001. #endif
  36002. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  36003. }
  36004. #endif
  36005. #endif
  36006. return EXPECT_RESULT();
  36007. }
  36008. static int test_wolfSSL_SHA_Transform(void)
  36009. {
  36010. EXPECT_DECLS;
  36011. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  36012. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36013. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36014. byte input1[] = "";
  36015. byte input2[] = "abc";
  36016. byte local[WC_SHA_BLOCK_SIZE];
  36017. word32 sLen = 0;
  36018. #ifdef BIG_ENDIAN_ORDER
  36019. unsigned char output1[] =
  36020. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  36021. "\x09\xf7\x3a\x93";
  36022. unsigned char output2[] =
  36023. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  36024. "\x7c\x6e\x08\xb8";
  36025. #else
  36026. unsigned char output1[] =
  36027. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  36028. "\x93\x3a\xf7\x09";
  36029. unsigned char output2[] =
  36030. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  36031. "\xb8\x08\x6e\x7c";
  36032. #endif
  36033. union {
  36034. wc_Sha native;
  36035. SHA_CTX compat;
  36036. } sha;
  36037. union {
  36038. wc_Sha native;
  36039. SHA_CTX compat;
  36040. } sha1;
  36041. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36042. XMEMSET(&local, 0, sizeof(local));
  36043. /* sanity check */
  36044. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  36045. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36046. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36047. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  36048. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36049. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36050. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36051. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36052. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36053. /* Init SHA CTX */
  36054. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36055. /* Do Transform*/
  36056. sLen = (word32)XSTRLEN((char*)input1);
  36057. XMEMCPY(local, input1, sLen);
  36058. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36059. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36060. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36061. /* Init SHA CTX */
  36062. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36063. sLen = (word32)XSTRLEN((char*)input2);
  36064. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36065. XMEMCPY(local, input2, sLen);
  36066. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36067. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36068. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36069. /* SHA1 */
  36070. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36071. /* Init SHA CTX */
  36072. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36073. /* Do Transform*/
  36074. sLen = (word32)XSTRLEN((char*)input1);
  36075. XMEMCPY(local, input1, sLen);
  36076. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36077. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36078. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  36079. /* Init SHA CTX */
  36080. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36081. sLen = (word32)XSTRLEN((char*)input2);
  36082. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36083. XMEMCPY(local, input2, sLen);
  36084. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36085. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36086. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36087. #endif
  36088. #endif
  36089. return EXPECT_RESULT();
  36090. }
  36091. static int test_wolfSSL_SHA224(void)
  36092. {
  36093. EXPECT_DECLS;
  36094. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36095. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36096. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36097. unsigned char input[] =
  36098. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36099. unsigned char output[] =
  36100. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36101. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36102. size_t inLen;
  36103. byte hash[WC_SHA224_DIGEST_SIZE];
  36104. unsigned char* p;
  36105. inLen = XSTRLEN((char*)input);
  36106. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36107. ExpectNull(SHA224(NULL, inLen, hash));
  36108. ExpectNotNull(SHA224(input, 0, hash));
  36109. ExpectNotNull(SHA224(input, inLen, NULL));
  36110. ExpectNotNull(SHA224(NULL, 0, hash));
  36111. ExpectNotNull(SHA224(NULL, 0, NULL));
  36112. ExpectNotNull(SHA224(input, inLen, hash));
  36113. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36114. ExpectNotNull(p = SHA224(input, inLen, NULL));
  36115. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  36116. #endif
  36117. return EXPECT_RESULT();
  36118. }
  36119. static int test_wolfSSL_SHA256(void)
  36120. {
  36121. EXPECT_DECLS;
  36122. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36123. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36124. unsigned char input[] =
  36125. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36126. unsigned char output[] =
  36127. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36128. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36129. "\x06\xC1";
  36130. size_t inLen;
  36131. byte hash[WC_SHA256_DIGEST_SIZE];
  36132. inLen = XSTRLEN((char*)input);
  36133. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36134. ExpectNotNull(SHA256(input, inLen, hash));
  36135. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36136. #endif
  36137. return EXPECT_RESULT();
  36138. }
  36139. static int test_wolfSSL_SHA256_Transform(void)
  36140. {
  36141. EXPECT_DECLS;
  36142. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36143. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36144. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36145. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  36146. !defined(WOLFSSL_KCAPI_HASH)
  36147. byte input1[] = "";
  36148. byte input2[] = "abc";
  36149. byte local[WC_SHA256_BLOCK_SIZE];
  36150. word32 sLen = 0;
  36151. #ifdef BIG_ENDIAN_ORDER
  36152. unsigned char output1[] =
  36153. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36154. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36155. unsigned char output2[] =
  36156. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36157. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36158. #else
  36159. unsigned char output1[] =
  36160. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36161. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36162. unsigned char output2[] =
  36163. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36164. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36165. #endif
  36166. union {
  36167. wc_Sha256 native;
  36168. SHA256_CTX compat;
  36169. } sha256;
  36170. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36171. XMEMSET(&local, 0, sizeof(local));
  36172. /* sanity check */
  36173. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  36174. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36175. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36176. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36177. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36178. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36179. /* Init SHA256 CTX */
  36180. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36181. /* Do Transform*/
  36182. sLen = (word32)XSTRLEN((char*)input1);
  36183. XMEMCPY(local, input1, sLen);
  36184. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36185. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  36186. 0);
  36187. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36188. /* Init SHA256 CTX */
  36189. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36190. sLen = (word32)XSTRLEN((char*)input2);
  36191. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36192. XMEMCPY(local, input2, sLen);
  36193. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36194. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  36195. 0);
  36196. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36197. #endif
  36198. #endif
  36199. return EXPECT_RESULT();
  36200. }
  36201. static int test_wolfSSL_SHA512_Transform(void)
  36202. {
  36203. EXPECT_DECLS;
  36204. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36205. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36206. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36207. !defined(WOLFSSL_KCAPI_HASH)
  36208. byte input1[] = "";
  36209. byte input2[] = "abc";
  36210. byte local[WC_SHA512_BLOCK_SIZE];
  36211. word32 sLen = 0;
  36212. #ifdef BIG_ENDIAN_ORDER
  36213. unsigned char output1[] =
  36214. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36215. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36216. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36217. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36218. unsigned char output2[] =
  36219. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36220. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36221. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36222. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36223. #else
  36224. unsigned char output1[] =
  36225. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36226. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36227. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36228. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36229. unsigned char output2[] =
  36230. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36231. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36232. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36233. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36234. #endif
  36235. union {
  36236. wc_Sha512 native;
  36237. SHA512_CTX compat;
  36238. } sha512;
  36239. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36240. XMEMSET(&local, 0, sizeof(local));
  36241. /* sanity check */
  36242. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  36243. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36244. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36245. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36246. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36247. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36248. /* Init SHA512 CTX */
  36249. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36250. /* Do Transform*/
  36251. sLen = (word32)XSTRLEN((char*)input1);
  36252. XMEMCPY(local, input1, sLen);
  36253. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36254. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36255. WC_SHA512_DIGEST_SIZE), 0);
  36256. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36257. /* Init SHA512 CTX */
  36258. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  36259. sLen = (word32)XSTRLEN((char*)input2);
  36260. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36261. XMEMCPY(local, input2, sLen);
  36262. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36263. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36264. WC_SHA512_DIGEST_SIZE), 0);
  36265. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36266. (void)input1;
  36267. #endif
  36268. #endif
  36269. return EXPECT_RESULT();
  36270. }
  36271. static int test_wolfSSL_SHA512_224_Transform(void)
  36272. {
  36273. EXPECT_DECLS;
  36274. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36275. !defined(WOLFSSL_NOSHA512_224)
  36276. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36277. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36278. !defined(WOLFSSL_KCAPI_HASH)
  36279. byte input1[] = "";
  36280. byte input2[] = "abc";
  36281. byte local[WC_SHA512_BLOCK_SIZE];
  36282. word32 sLen = 0;
  36283. unsigned char output1[] =
  36284. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  36285. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  36286. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  36287. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  36288. unsigned char output2[] =
  36289. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  36290. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  36291. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  36292. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  36293. union {
  36294. wc_Sha512 native;
  36295. SHA512_CTX compat;
  36296. } sha512;
  36297. #ifdef BIG_ENDIAN_ORDER
  36298. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36299. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36300. #endif
  36301. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36302. XMEMSET(&local, 0, sizeof(local));
  36303. /* sanity check */
  36304. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  36305. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  36306. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  36307. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  36308. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  36309. BAD_FUNC_ARG);
  36310. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36311. /* Init SHA512 CTX */
  36312. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  36313. /* Do Transform*/
  36314. sLen = (word32)XSTRLEN((char*)input1);
  36315. XMEMCPY(local, input1, sLen);
  36316. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36317. 1);
  36318. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36319. WC_SHA512_DIGEST_SIZE), 0);
  36320. /* frees resources */
  36321. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36322. /* Init SHA512 CTX */
  36323. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  36324. sLen = (word32)XSTRLEN((char*)input2);
  36325. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36326. XMEMCPY(local, input2, sLen);
  36327. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36328. 1);
  36329. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36330. WC_SHA512_DIGEST_SIZE), 0);
  36331. /* frees resources */
  36332. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36333. #endif
  36334. #endif
  36335. return EXPECT_RESULT();
  36336. }
  36337. static int test_wolfSSL_SHA512_256_Transform(void)
  36338. {
  36339. EXPECT_DECLS;
  36340. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36341. !defined(WOLFSSL_NOSHA512_256)
  36342. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36343. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36344. !defined(WOLFSSL_KCAPI_HASH)
  36345. byte input1[] = "";
  36346. byte input2[] = "abc";
  36347. byte local[WC_SHA512_BLOCK_SIZE];
  36348. word32 sLen = 0;
  36349. unsigned char output1[] =
  36350. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  36351. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  36352. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  36353. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  36354. unsigned char output2[] =
  36355. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  36356. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  36357. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  36358. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  36359. union {
  36360. wc_Sha512 native;
  36361. SHA512_CTX compat;
  36362. } sha512;
  36363. #ifdef BIG_ENDIAN_ORDER
  36364. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36365. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36366. #endif
  36367. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36368. XMEMSET(&local, 0, sizeof(local));
  36369. /* sanity check */
  36370. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  36371. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  36372. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  36373. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  36374. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  36375. BAD_FUNC_ARG);
  36376. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36377. /* Init SHA512 CTX */
  36378. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  36379. /* Do Transform*/
  36380. sLen = (word32)XSTRLEN((char*)input1);
  36381. XMEMCPY(local, input1, sLen);
  36382. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36383. 1);
  36384. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36385. WC_SHA512_DIGEST_SIZE), 0);
  36386. /* frees resources */
  36387. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36388. /* Init SHA512 CTX */
  36389. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  36390. sLen = (word32)XSTRLEN((char*)input2);
  36391. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36392. XMEMCPY(local, input2, sLen);
  36393. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36394. 1);
  36395. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36396. WC_SHA512_DIGEST_SIZE), 0);
  36397. /* frees resources */
  36398. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36399. #endif
  36400. #endif
  36401. return EXPECT_RESULT();
  36402. }
  36403. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36404. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36405. * buffer of 64 bytes.
  36406. *
  36407. * returns the size of the digest buffer on success and a negative value on
  36408. * failure.
  36409. */
  36410. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  36411. int* sz)
  36412. {
  36413. EXPECT_DECLS;
  36414. HMAC_CTX ctx1;
  36415. HMAC_CTX ctx2;
  36416. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36417. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36418. unsigned char long_key[] =
  36419. "0123456789012345678901234567890123456789"
  36420. "0123456789012345678901234567890123456789"
  36421. "0123456789012345678901234567890123456789"
  36422. "0123456789012345678901234567890123456789";
  36423. unsigned char msg[] = "message to hash";
  36424. unsigned int digestSz = 64;
  36425. int keySz = sizeof(key);
  36426. int long_keySz = sizeof(long_key);
  36427. int msgSz = sizeof(msg);
  36428. unsigned char digest2[64];
  36429. unsigned int digestSz2 = 64;
  36430. HMAC_CTX_init(&ctx1);
  36431. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36432. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36433. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36434. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36435. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36436. HMAC_CTX_cleanup(&ctx1);
  36437. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36438. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36439. HMAC_CTX_cleanup(&ctx2);
  36440. ExpectIntEQ(digestSz, digestSz2);
  36441. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36442. /* test HMAC_Init with NULL key */
  36443. /* init after copy */
  36444. HMAC_CTX_init(&ctx1);
  36445. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36446. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36447. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36448. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36449. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36450. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36451. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36452. HMAC_CTX_cleanup(&ctx1);
  36453. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36454. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36455. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36456. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36457. HMAC_CTX_cleanup(&ctx2);
  36458. ExpectIntEQ(digestSz, digestSz2);
  36459. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36460. /* long key */
  36461. HMAC_CTX_init(&ctx1);
  36462. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  36463. SSL_SUCCESS);
  36464. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36465. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36466. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36467. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36468. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36469. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36470. HMAC_CTX_cleanup(&ctx1);
  36471. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36472. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36473. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36474. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36475. HMAC_CTX_cleanup(&ctx2);
  36476. ExpectIntEQ(digestSz, digestSz2);
  36477. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36478. /* init before copy */
  36479. HMAC_CTX_init(&ctx1);
  36480. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36481. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36482. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36483. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36484. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36485. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36486. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36487. HMAC_CTX_cleanup(&ctx1);
  36488. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36489. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36490. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36491. HMAC_CTX_cleanup(&ctx2);
  36492. ExpectIntEQ(digestSz, digestSz2);
  36493. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36494. *sz = digestSz;
  36495. return EXPECT_RESULT();
  36496. }
  36497. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36498. static int test_wolfSSL_HMAC_CTX(void)
  36499. {
  36500. EXPECT_DECLS;
  36501. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36502. unsigned char digest[64];
  36503. int digestSz;
  36504. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  36505. WOLFSSL_HMAC_CTX ctx1;
  36506. WOLFSSL_HMAC_CTX ctx2;
  36507. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  36508. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  36509. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  36510. wolfSSL_HMAC_CTX_free(NULL);
  36511. wolfSSL_HMAC_CTX_free(hmac_ctx);
  36512. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  36513. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  36514. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  36515. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  36516. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  36517. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  36518. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36519. ((! defined(HAVE_FIPS_VERSION)) || \
  36520. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36521. /* Copy object that hasn't had a digest set - MD5. */
  36522. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  36523. #else
  36524. /* Copy object that hasn't had a digest set. */
  36525. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  36526. #endif
  36527. HMAC_CTX_cleanup(NULL);
  36528. HMAC_CTX_cleanup(&ctx2);
  36529. ExpectNull(HMAC_CTX_get_md(NULL));
  36530. #ifndef NO_SHA
  36531. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  36532. TEST_SUCCESS);
  36533. ExpectIntEQ(digestSz, 20);
  36534. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36535. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36536. #endif /* !NO_SHA */
  36537. #ifdef WOLFSSL_SHA224
  36538. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  36539. TEST_SUCCESS);
  36540. ExpectIntEQ(digestSz, 28);
  36541. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36542. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36543. "\x02\x0E", digest, digestSz), 0);
  36544. #endif /* WOLFSSL_SHA224 */
  36545. #ifndef NO_SHA256
  36546. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  36547. TEST_SUCCESS);
  36548. ExpectIntEQ(digestSz, 32);
  36549. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36550. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36551. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36552. #endif /* !NO_SHA256 */
  36553. #ifdef WOLFSSL_SHA384
  36554. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  36555. TEST_SUCCESS);
  36556. ExpectIntEQ(digestSz, 48);
  36557. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36558. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36559. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36560. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36561. digest, digestSz), 0);
  36562. #endif /* WOLFSSL_SHA384 */
  36563. #ifdef WOLFSSL_SHA512
  36564. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  36565. TEST_SUCCESS);
  36566. ExpectIntEQ(digestSz, 64);
  36567. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36568. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36569. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36570. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36571. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36572. digest, digestSz), 0);
  36573. #endif /* WOLFSSL_SHA512 */
  36574. #ifdef WOLFSSL_SHA3
  36575. #ifndef WOLFSSL_NOSHA3_224
  36576. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  36577. TEST_SUCCESS);
  36578. ExpectIntEQ(digestSz, 28);
  36579. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  36580. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  36581. "\x29\x0f", digest, digestSz), 0);
  36582. #endif
  36583. #ifndef WOLFSSL_NOSHA3_256
  36584. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  36585. TEST_SUCCESS);
  36586. ExpectIntEQ(digestSz, 32);
  36587. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  36588. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  36589. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  36590. #endif
  36591. #ifndef WOLFSSL_NOSHA3_384
  36592. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  36593. TEST_SUCCESS);
  36594. ExpectIntEQ(digestSz, 48);
  36595. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  36596. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  36597. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  36598. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  36599. digest, digestSz), 0);
  36600. #endif
  36601. #ifndef WOLFSSL_NOSHA3_512
  36602. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  36603. TEST_SUCCESS);
  36604. ExpectIntEQ(digestSz, 64);
  36605. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  36606. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  36607. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  36608. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  36609. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  36610. digest, digestSz), 0);
  36611. #endif
  36612. #endif
  36613. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  36614. HAVE_FIPS_VERSION <= 2)
  36615. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  36616. TEST_SUCCESS);
  36617. ExpectIntEQ(digestSz, 16);
  36618. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36619. "\xE4\x98\xDD", digest, digestSz), 0);
  36620. #endif /* !NO_MD5 */
  36621. #endif
  36622. return EXPECT_RESULT();
  36623. }
  36624. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36625. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36626. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36627. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  36628. {
  36629. EXPECT_DECLS;
  36630. static const unsigned char key[] = "simple test key";
  36631. HMAC_CTX* hmac = NULL;
  36632. ENGINE* e = NULL;
  36633. unsigned char hash[WC_MAX_DIGEST_SIZE];
  36634. unsigned int len;
  36635. ExpectNotNull(hmac = HMAC_CTX_new());
  36636. HMAC_CTX_init(hmac);
  36637. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36638. ((! defined(HAVE_FIPS_VERSION)) || \
  36639. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36640. /* Get size on object that hasn't had a digest set - MD5. */
  36641. ExpectIntEQ(HMAC_size(hmac), 16);
  36642. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  36643. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  36644. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  36645. #else
  36646. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  36647. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  36648. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  36649. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  36650. #endif
  36651. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  36652. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  36653. /* re-using test key as data to hash */
  36654. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  36655. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  36656. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  36657. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  36658. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  36659. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  36660. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  36661. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  36662. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  36663. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  36664. ExpectIntEQ(len, md_len);
  36665. ExpectIntEQ(HMAC_size(NULL), 0);
  36666. ExpectIntEQ(HMAC_size(hmac), md_len);
  36667. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  36668. HMAC_cleanup(NULL);
  36669. HMAC_cleanup(hmac);
  36670. HMAC_CTX_free(hmac);
  36671. len = 0;
  36672. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  36673. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  36674. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  36675. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  36676. ExpectIntEQ(len, md_len);
  36677. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  36678. /* With data. */
  36679. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  36680. &len));
  36681. /* With NULL data. */
  36682. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  36683. &len));
  36684. /* With zero length data. */
  36685. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  36686. return EXPECT_RESULT();
  36687. }
  36688. #endif
  36689. static int test_wolfSSL_HMAC(void)
  36690. {
  36691. EXPECT_DECLS;
  36692. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36693. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36694. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36695. #ifndef NO_SHA256
  36696. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  36697. TEST_SUCCESS);
  36698. #endif
  36699. #ifdef WOLFSSL_SHA224
  36700. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  36701. TEST_SUCCESS);
  36702. #endif
  36703. #ifdef WOLFSSL_SHA384
  36704. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  36705. TEST_SUCCESS);
  36706. #endif
  36707. #ifdef WOLFSSL_SHA512
  36708. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  36709. TEST_SUCCESS);
  36710. #endif
  36711. #ifdef WOLFSSL_SHA3
  36712. #ifndef WOLFSSL_NOSHA3_224
  36713. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  36714. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  36715. #endif
  36716. #ifndef WOLFSSL_NOSHA3_256
  36717. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  36718. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  36719. #endif
  36720. #ifndef WOLFSSL_NOSHA3_384
  36721. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  36722. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  36723. #endif
  36724. #ifndef WOLFSSL_NOSHA3_512
  36725. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  36726. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  36727. #endif
  36728. #endif
  36729. #ifndef NO_SHA
  36730. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  36731. TEST_SUCCESS);
  36732. #endif
  36733. #endif
  36734. return EXPECT_RESULT();
  36735. }
  36736. static int test_wolfSSL_CMAC(void)
  36737. {
  36738. EXPECT_DECLS;
  36739. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  36740. defined(WOLFSSL_AES_DIRECT)
  36741. int i;
  36742. byte key[AES_256_KEY_SIZE];
  36743. CMAC_CTX* cmacCtx = NULL;
  36744. byte out[AES_BLOCK_SIZE];
  36745. size_t outLen = AES_BLOCK_SIZE;
  36746. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  36747. key[i] = i;
  36748. }
  36749. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36750. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  36751. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36752. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36753. NULL), 1);
  36754. /* re-using test key as data to hash */
  36755. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36756. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36757. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  36758. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  36759. /* No Update works. */
  36760. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36761. NULL), 1);
  36762. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36763. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36764. NULL), 1);
  36765. /* Test parameters with CMAC_Update. */
  36766. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  36767. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  36768. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  36769. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  36770. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  36771. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36772. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  36773. /* Test parameters with CMAC_Final. */
  36774. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  36775. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  36776. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  36777. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  36778. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  36779. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  36780. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36781. CMAC_CTX_free(cmacCtx);
  36782. /* Test parameters with CMAC Init. */
  36783. cmacCtx = NULL;
  36784. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36785. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36786. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  36787. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36788. NULL), 0);
  36789. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36790. NULL), 0);
  36791. /* give a key too small for the cipher, verify we get failure */
  36792. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  36793. NULL), 0);
  36794. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  36795. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  36796. /* Only AES-CBC supported. */
  36797. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  36798. NULL), 0);
  36799. #endif
  36800. CMAC_CTX_free(cmacCtx);
  36801. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  36802. cmacCtx = NULL;
  36803. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36804. /* No Init. */
  36805. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  36806. CMAC_CTX_free(cmacCtx);
  36807. /* Test AES-256-CBC */
  36808. cmacCtx = NULL;
  36809. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36810. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  36811. NULL), 1);
  36812. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36813. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36814. CMAC_CTX_free(cmacCtx);
  36815. /* Test AES-192-CBC */
  36816. cmacCtx = NULL;
  36817. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36818. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36819. NULL), 1);
  36820. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36821. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36822. CMAC_CTX_free(cmacCtx);
  36823. cmacCtx = NULL;
  36824. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36825. CMAC_CTX_free(cmacCtx);
  36826. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  36827. return EXPECT_RESULT();
  36828. }
  36829. static int test_wolfSSL_DES(void)
  36830. {
  36831. EXPECT_DECLS;
  36832. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36833. const_DES_cblock myDes;
  36834. DES_cblock iv;
  36835. DES_key_schedule key;
  36836. word32 i;
  36837. DES_LONG dl;
  36838. unsigned char msg[] = "hello wolfssl";
  36839. unsigned char weakKey[][8] = {
  36840. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  36841. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  36842. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  36843. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  36844. };
  36845. unsigned char semiWeakKey[][8] = {
  36846. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  36847. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  36848. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  36849. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  36850. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  36851. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  36852. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  36853. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  36854. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  36855. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  36856. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  36857. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  36858. };
  36859. DES_check_key(1);
  36860. DES_set_key(&myDes, &key);
  36861. /* check, check of odd parity */
  36862. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  36863. myDes[0] = 6; /* set even parity */
  36864. XMEMSET(key, 5, sizeof(DES_key_schedule));
  36865. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  36866. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  36867. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  36868. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  36869. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  36870. /* set odd parity for success case */
  36871. DES_set_odd_parity(&myDes);
  36872. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  36873. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  36874. myDes[3]);
  36875. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  36876. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36877. ExpectIntEQ(key[i], myDes[i]);
  36878. }
  36879. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  36880. /* check weak key */
  36881. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  36882. XMEMSET(key, 5, sizeof(DES_key_schedule));
  36883. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  36884. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  36885. DES_set_key_unchecked(NULL, NULL);
  36886. DES_set_key_unchecked(&myDes, NULL);
  36887. DES_set_key_unchecked(NULL, &key);
  36888. /* compare arrays, should be the same */
  36889. /* now do unchecked copy of a weak key over */
  36890. DES_set_key_unchecked(&myDes, &key);
  36891. /* compare arrays, should be the same */
  36892. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36893. ExpectIntEQ(key[i], myDes[i]);
  36894. }
  36895. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  36896. myDes[7] = 2;
  36897. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  36898. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  36899. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  36900. /* Test all weak keys. */
  36901. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  36902. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  36903. }
  36904. /* Test all semi-weak keys. */
  36905. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  36906. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  36907. }
  36908. /* check DES_key_sched API */
  36909. XMEMSET(key, 1, sizeof(DES_key_schedule));
  36910. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  36911. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  36912. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  36913. /* compare arrays, should be the same */
  36914. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  36915. ExpectIntEQ(key[i], myDes[i]);
  36916. }
  36917. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  36918. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  36919. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  36920. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  36921. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  36922. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  36923. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  36924. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  36925. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  36926. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  36927. * DES_cbc_encrypt on the input */
  36928. XMEMSET(iv, 0, sizeof(DES_cblock));
  36929. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  36930. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  36931. ExpectIntEQ(dl, 480052723);
  36932. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  36933. return EXPECT_RESULT();
  36934. }
  36935. static int test_wolfSSL_DES_ncbc(void)
  36936. {
  36937. EXPECT_DECLS;
  36938. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  36939. const_DES_cblock myDes;
  36940. DES_cblock iv = {1};
  36941. DES_key_schedule key = {0};
  36942. unsigned char msg[] = "hello wolfssl";
  36943. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  36944. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  36945. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  36946. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  36947. /* partial block test */
  36948. DES_set_key(&key, &myDes);
  36949. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  36950. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  36951. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36952. DES_set_key(&key, &myDes);
  36953. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36954. *((byte*)&iv) = 1;
  36955. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  36956. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  36957. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  36958. /* full block test */
  36959. DES_set_key(&key, &myDes);
  36960. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  36961. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36962. *((byte*)&iv) = 1;
  36963. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  36964. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  36965. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36966. DES_set_key(&key, &myDes);
  36967. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  36968. *((byte*)&iv) = 1;
  36969. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  36970. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  36971. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  36972. #endif
  36973. return EXPECT_RESULT();
  36974. }
  36975. static int test_wolfSSL_DES_ecb_encrypt(void)
  36976. {
  36977. EXPECT_DECLS;
  36978. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  36979. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  36980. WOLFSSL_DES_key_schedule key;
  36981. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  36982. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  36983. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  36984. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  36985. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  36986. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  36987. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  36988. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  36989. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  36990. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  36991. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  36992. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  36993. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  36994. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  36995. /* Encrypt messages */
  36996. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  36997. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  36998. {
  36999. /* Decrypt messages */
  37000. int ret1 = 0;
  37001. int ret2 = 0;
  37002. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  37003. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  37004. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  37005. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  37006. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  37007. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  37008. }
  37009. #endif
  37010. return EXPECT_RESULT();
  37011. }
  37012. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  37013. {
  37014. EXPECT_DECLS;
  37015. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37016. unsigned char input1[8], input2[8];
  37017. unsigned char output1[8], output2[8];
  37018. unsigned char back1[8], back2[8];
  37019. WOLFSSL_DES_cblock iv1, iv2;
  37020. WOLFSSL_DES_key_schedule key1, key2, key3;
  37021. int i;
  37022. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  37023. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  37024. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  37025. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  37026. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  37027. XMEMSET(output1, 0, sizeof(output1));
  37028. XMEMSET(output2, 0, sizeof(output2));
  37029. XMEMSET(back1, 0, sizeof(back1));
  37030. XMEMSET(back2, 0, sizeof(back2));
  37031. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37032. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37033. /* Encrypt messages */
  37034. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  37035. DES_ENCRYPT);
  37036. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  37037. DES_ENCRYPT);
  37038. {
  37039. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37040. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37041. /* Decrypt messages */
  37042. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  37043. &iv1, DES_DECRYPT);
  37044. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  37045. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  37046. &iv2, DES_DECRYPT);
  37047. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  37048. }
  37049. for (i = 0; i < 8; i++) {
  37050. XMEMSET(output1, 0, sizeof(output1));
  37051. XMEMSET(output2, 0, sizeof(output2));
  37052. XMEMSET(back1, 0, sizeof(back1));
  37053. XMEMSET(back2, 0, sizeof(back2));
  37054. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37055. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37056. /* Encrypt partial messages */
  37057. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  37058. &iv1, DES_ENCRYPT);
  37059. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  37060. &iv2, DES_ENCRYPT);
  37061. {
  37062. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37063. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37064. /* Decrypt messages */
  37065. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  37066. &key3, &iv1, DES_DECRYPT);
  37067. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  37068. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  37069. &key3, &iv2, DES_DECRYPT);
  37070. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  37071. }
  37072. }
  37073. #endif
  37074. return EXPECT_RESULT();
  37075. }
  37076. static int test_wolfSSL_AES_encrypt(void)
  37077. {
  37078. EXPECT_DECLS;
  37079. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37080. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37081. AES_KEY enc;
  37082. AES_KEY dec;
  37083. const byte msg[] = {
  37084. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37085. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37086. };
  37087. const byte exp[] = {
  37088. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  37089. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  37090. };
  37091. const byte key[] = {
  37092. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37093. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37094. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37095. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37096. };
  37097. byte eout[sizeof(msg)];
  37098. byte dout[sizeof(msg)];
  37099. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  37100. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  37101. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  37102. wolfSSL_AES_encrypt(msg, NULL, NULL);
  37103. wolfSSL_AES_encrypt(NULL, eout, NULL);
  37104. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  37105. wolfSSL_AES_encrypt(msg, eout, NULL);
  37106. wolfSSL_AES_encrypt(msg, NULL, &enc);
  37107. wolfSSL_AES_encrypt(NULL, eout, &enc);
  37108. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  37109. wolfSSL_AES_decrypt(eout, NULL, NULL);
  37110. wolfSSL_AES_decrypt(NULL, dout, NULL);
  37111. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  37112. wolfSSL_AES_decrypt(eout, dout, NULL);
  37113. wolfSSL_AES_decrypt(eout, NULL, &dec);
  37114. wolfSSL_AES_decrypt(NULL, dout, &dec);
  37115. wolfSSL_AES_encrypt(msg, eout, &enc);
  37116. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  37117. wolfSSL_AES_decrypt(eout, dout, &dec);
  37118. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  37119. #endif
  37120. return EXPECT_RESULT();
  37121. }
  37122. static int test_wolfSSL_AES_ecb_encrypt(void)
  37123. {
  37124. EXPECT_DECLS;
  37125. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37126. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37127. AES_KEY aes;
  37128. const byte msg[] =
  37129. {
  37130. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37131. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37132. };
  37133. const byte verify[] =
  37134. {
  37135. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37136. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37137. };
  37138. const byte key[] =
  37139. {
  37140. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37141. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37142. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37143. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37144. };
  37145. byte out[AES_BLOCK_SIZE];
  37146. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37147. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37148. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37149. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37150. #ifdef HAVE_AES_DECRYPT
  37151. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37152. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37153. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37154. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37155. #endif
  37156. /* test bad arguments */
  37157. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37158. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37159. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37160. #endif
  37161. return EXPECT_RESULT();
  37162. }
  37163. static int test_wolfSSL_AES_cbc_encrypt(void)
  37164. {
  37165. EXPECT_DECLS;
  37166. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37167. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37168. AES_KEY aes;
  37169. AES_KEY* aesN = NULL;
  37170. size_t len = 0;
  37171. size_t lenB = 0;
  37172. int keySz0 = 0;
  37173. int keySzN = -1;
  37174. byte out[AES_BLOCK_SIZE] = {0};
  37175. byte* outN = NULL;
  37176. /* Test vectors retrieved from:
  37177. * <begin URL>
  37178. * https://csrc.nist.gov/
  37179. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37180. * documents/aes/KAT_AES.zip
  37181. * </end URL>
  37182. */
  37183. const byte* pt128N = NULL;
  37184. byte* key128N = NULL;
  37185. byte* iv128N = NULL;
  37186. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37187. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37189. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37190. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37191. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37193. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37194. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37195. len = sizeof(pt128);
  37196. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37197. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37198. ExpectIntNE(XMEMCMP(b, g, h), i)
  37199. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37200. /* Stressing wolfSSL_AES_cbc_encrypt() */
  37201. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37202. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37203. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37204. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37205. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37206. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37207. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37208. /* Stressing wolfSSL_AES_set_encrypt_key */
  37209. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37210. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37211. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37212. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37213. /* Stressing wolfSSL_AES_set_decrypt_key */
  37214. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37215. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37216. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37217. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37218. #ifdef WOLFSSL_AES_128
  37219. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  37220. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37221. RESET_IV(iv128tmp, iv128);
  37222. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37223. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37224. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37225. wc_AesFree((Aes*)&aes);
  37226. #ifdef HAVE_AES_DECRYPT
  37227. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  37228. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37229. RESET_IV(iv128tmp, iv128);
  37230. len = sizeof(ct128);
  37231. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37232. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37233. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37234. wc_AesFree((Aes*)&aes);
  37235. #endif
  37236. #endif /* WOLFSSL_AES_128 */
  37237. #ifdef WOLFSSL_AES_192
  37238. {
  37239. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37240. * Appendix F.2.3 */
  37241. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37242. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37243. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37244. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37245. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37246. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37247. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37248. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37249. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37250. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37251. len = sizeof(pt192);
  37252. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  37253. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37254. RESET_IV(iv192tmp, iv192);
  37255. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37256. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37257. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37258. wc_AesFree((Aes*)&aes);
  37259. #ifdef HAVE_AES_DECRYPT
  37260. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  37261. len = sizeof(ct192);
  37262. RESET_IV(iv192tmp, iv192);
  37263. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37264. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37265. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37266. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37267. wc_AesFree((Aes*)&aes);
  37268. #endif
  37269. }
  37270. #endif /* WOLFSSL_AES_192 */
  37271. #ifdef WOLFSSL_AES_256
  37272. {
  37273. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37274. * Appendix F.2.5 */
  37275. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37276. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37277. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37278. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37279. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37280. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37281. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37282. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37283. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37284. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37285. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37286. len = sizeof(pt256);
  37287. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  37288. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37289. RESET_IV(iv256tmp, iv256);
  37290. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37291. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37292. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37293. wc_AesFree((Aes*)&aes);
  37294. #ifdef HAVE_AES_DECRYPT
  37295. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  37296. len = sizeof(ct256);
  37297. RESET_IV(iv256tmp, iv256);
  37298. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37299. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37300. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37301. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37302. wc_AesFree((Aes*)&aes);
  37303. #endif
  37304. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  37305. !defined(HAVE_SELFTEST)
  37306. {
  37307. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37308. byte wrapPlain[sizeof(key256)] = { 0 };
  37309. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37310. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  37311. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37312. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37313. 15), WOLFSSL_FAILURE);
  37314. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37315. sizeof(key256)), sizeof(wrapCipher));
  37316. wc_AesFree((Aes*)&aes);
  37317. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  37318. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37319. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37320. 23), WOLFSSL_FAILURE);
  37321. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37322. sizeof(wrapCipher)), sizeof(wrapPlain));
  37323. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37324. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37325. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37326. wc_AesFree((Aes*)&aes);
  37327. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  37328. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37329. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37330. sizeof(key256)), sizeof(wrapCipher));
  37331. wc_AesFree((Aes*)&aes);
  37332. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  37333. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37334. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37335. sizeof(wrapCipher)), sizeof(wrapPlain));
  37336. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37337. wc_AesFree((Aes*)&aes);
  37338. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37339. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37340. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37341. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  37342. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  37343. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  37344. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  37345. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  37346. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  37347. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37348. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37349. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37350. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  37351. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  37352. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  37353. 0);
  37354. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  37355. 0);
  37356. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  37357. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  37358. }
  37359. #endif /* HAVE_AES_KEYWRAP */
  37360. }
  37361. #endif /* WOLFSSL_AES_256 */
  37362. #endif
  37363. return EXPECT_RESULT();
  37364. }
  37365. static int test_wolfSSL_AES_cfb128_encrypt(void)
  37366. {
  37367. EXPECT_DECLS;
  37368. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  37369. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37370. AES_KEY aesEnc;
  37371. AES_KEY aesDec;
  37372. const byte msg[] = {
  37373. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37374. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37375. };
  37376. const byte exp[] = {
  37377. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  37378. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  37379. };
  37380. const byte key[] = {
  37381. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37382. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37383. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37384. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37385. };
  37386. const byte ivData[] = {
  37387. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37388. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37389. };
  37390. byte out[AES_BLOCK_SIZE];
  37391. byte iv[AES_BLOCK_SIZE];
  37392. word32 i;
  37393. int num;
  37394. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37395. XMEMCPY(iv, ivData, sizeof(iv));
  37396. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37397. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  37398. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  37399. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37400. #ifdef HAVE_AES_DECRYPT
  37401. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37402. XMEMCPY(iv, ivData, sizeof(iv));
  37403. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37404. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  37405. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37406. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37407. #endif
  37408. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  37409. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37410. XMEMCPY(iv, ivData, sizeof(iv));
  37411. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37412. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  37413. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37414. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  37415. if (i == 0) {
  37416. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37417. }
  37418. else {
  37419. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37420. }
  37421. #ifdef HAVE_AES_DECRYPT
  37422. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37423. XMEMCPY(iv, ivData, sizeof(iv));
  37424. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37425. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  37426. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37427. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  37428. if (i == 0) {
  37429. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37430. }
  37431. else {
  37432. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37433. }
  37434. #endif
  37435. }
  37436. if (EXPECT_SUCCESS()) {
  37437. /* test bad arguments */
  37438. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37439. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37440. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  37441. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37442. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  37443. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37444. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37445. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  37446. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37447. }
  37448. #endif
  37449. return EXPECT_RESULT();
  37450. }
  37451. static int test_wolfSSL_CRYPTO_cts128(void)
  37452. {
  37453. EXPECT_DECLS;
  37454. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37455. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37456. byte tmp[64]; /* Largest vector size */
  37457. /* Test vectors taken form RFC3962 Appendix B */
  37458. const testVector vects[] = {
  37459. {
  37460. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37461. "\x20",
  37462. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37463. "\x97",
  37464. 17, 17
  37465. },
  37466. {
  37467. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37468. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37469. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37470. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37471. 31, 31
  37472. },
  37473. {
  37474. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37475. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  37476. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37477. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  37478. 32, 32
  37479. },
  37480. {
  37481. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37482. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37483. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  37484. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37485. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  37486. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  37487. 47, 47
  37488. },
  37489. {
  37490. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37491. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37492. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  37493. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37494. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  37495. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  37496. 48, 48
  37497. },
  37498. {
  37499. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37500. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37501. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  37502. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  37503. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37504. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37505. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  37506. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  37507. 64, 64
  37508. }
  37509. };
  37510. byte keyBytes[AES_128_KEY_SIZE] = {
  37511. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  37512. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  37513. };
  37514. size_t i;
  37515. AES_KEY encKey;
  37516. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  37517. XMEMSET(tmp, 0, sizeof(tmp));
  37518. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  37519. AES_KEY decKey;
  37520. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37521. &encKey), 0);
  37522. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37523. &decKey), 0);
  37524. XMEMSET(iv, 0, sizeof(iv));
  37525. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  37526. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  37527. vects[i].outLen);
  37528. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  37529. XMEMSET(iv, 0, sizeof(iv));
  37530. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  37531. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  37532. vects[i].inLen);
  37533. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  37534. }
  37535. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37536. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37537. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37538. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37539. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37540. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  37541. (cbc128_f)AES_cbc_encrypt), 0);
  37542. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  37543. (cbc128_f)AES_cbc_encrypt), 0);
  37544. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  37545. (cbc128_f)AES_cbc_encrypt), 0);
  37546. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  37547. (cbc128_f)AES_cbc_encrypt), 0);
  37548. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  37549. (cbc128_f)AES_cbc_encrypt), 0);
  37550. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37551. /* Length too small. */
  37552. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  37553. (cbc128_f)AES_cbc_encrypt), 0);
  37554. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37555. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37556. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37557. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37558. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37559. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  37560. (cbc128_f)AES_cbc_encrypt), 0);
  37561. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  37562. (cbc128_f)AES_cbc_encrypt), 0);
  37563. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  37564. (cbc128_f)AES_cbc_encrypt), 0);
  37565. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  37566. (cbc128_f)AES_cbc_encrypt), 0);
  37567. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  37568. (cbc128_f)AES_cbc_encrypt), 0);
  37569. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37570. /* Length too small. */
  37571. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  37572. (cbc128_f)AES_cbc_encrypt), 0);
  37573. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  37574. return EXPECT_RESULT();
  37575. }
  37576. static int test_wolfSSL_RC4(void)
  37577. {
  37578. EXPECT_DECLS;
  37579. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  37580. WOLFSSL_RC4_KEY rc4Key;
  37581. unsigned char key[] = {
  37582. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37583. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37584. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37585. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37586. };
  37587. unsigned char data[] = {
  37588. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37589. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37590. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37591. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37592. };
  37593. unsigned char enc[sizeof(data)];
  37594. unsigned char dec[sizeof(data)];
  37595. word32 i;
  37596. word32 j;
  37597. wolfSSL_RC4_set_key(NULL, -1, NULL);
  37598. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  37599. wolfSSL_RC4_set_key(NULL, 0, NULL);
  37600. wolfSSL_RC4_set_key(NULL, -1, key);
  37601. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  37602. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  37603. wolfSSL_RC4_set_key(NULL, 0, key);
  37604. wolfSSL_RC4(NULL, 0, NULL, NULL);
  37605. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  37606. wolfSSL_RC4(NULL, 0, data, NULL);
  37607. wolfSSL_RC4(NULL, 0, NULL, enc);
  37608. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  37609. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  37610. wolfSSL_RC4(NULL, 0, data, enc);
  37611. ExpectIntEQ(1, 1);
  37612. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  37613. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  37614. XMEMSET(enc, 0, sizeof(enc));
  37615. XMEMSET(dec, 0, sizeof(dec));
  37616. /* Encrypt */
  37617. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37618. wolfSSL_RC4(&rc4Key, j, data, enc);
  37619. /* Decrypt */
  37620. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37621. wolfSSL_RC4(&rc4Key, j, enc, dec);
  37622. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  37623. }
  37624. }
  37625. #endif
  37626. return EXPECT_RESULT();
  37627. }
  37628. static int test_wolfSSL_OBJ(void)
  37629. {
  37630. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  37631. * mode
  37632. */
  37633. EXPECT_DECLS;
  37634. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  37635. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  37636. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  37637. ASN1_OBJECT *obj = NULL;
  37638. ASN1_OBJECT *obj2 = NULL;
  37639. char buf[50];
  37640. XFILE fp = XBADFILE;
  37641. X509 *x509 = NULL;
  37642. X509_NAME *x509Name = NULL;
  37643. X509_NAME_ENTRY *x509NameEntry = NULL;
  37644. ASN1_OBJECT *asn1Name = NULL;
  37645. int numNames = 0;
  37646. BIO *bio = NULL;
  37647. int nid;
  37648. int i, j;
  37649. const char *f[] = {
  37650. #ifndef NO_RSA
  37651. "./certs/ca-cert.der",
  37652. #endif
  37653. #ifdef HAVE_ECC
  37654. "./certs/ca-ecc-cert.der",
  37655. "./certs/ca-ecc384-cert.der",
  37656. #endif
  37657. NULL};
  37658. ASN1_OBJECT *field_name_obj = NULL;
  37659. int lastpos = -1;
  37660. int tmp = -1;
  37661. ASN1_STRING *asn1 = NULL;
  37662. unsigned char *buf_dyn = NULL;
  37663. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  37664. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37665. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  37666. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  37667. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37668. ASN1_OBJECT_free(obj);
  37669. obj = NULL;
  37670. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  37671. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  37672. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  37673. #ifdef WOLFSSL_CERT_EXT
  37674. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  37675. #endif
  37676. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37677. ExpectNotNull(obj2 = OBJ_dup(obj));
  37678. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  37679. ASN1_OBJECT_free(obj);
  37680. obj = NULL;
  37681. ASN1_OBJECT_free(obj2);
  37682. obj2 = NULL;
  37683. for (i = 0; f[i] != NULL; i++)
  37684. {
  37685. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  37686. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  37687. if (fp != XBADFILE) {
  37688. XFCLOSE(fp);
  37689. fp = XBADFILE;
  37690. }
  37691. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  37692. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37693. /* Get the Common Name by using OBJ_txt2obj */
  37694. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  37695. do
  37696. {
  37697. lastpos = tmp;
  37698. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  37699. } while (tmp > -1);
  37700. ExpectIntNE(lastpos, -1);
  37701. ASN1_OBJECT_free(field_name_obj);
  37702. field_name_obj = NULL;
  37703. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  37704. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  37705. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  37706. /*
  37707. * All Common Names should be www.wolfssl.com
  37708. * This makes testing easier as we can test for the expected value.
  37709. */
  37710. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  37711. OPENSSL_free(buf_dyn);
  37712. buf_dyn = NULL;
  37713. bio = BIO_new(BIO_s_mem());
  37714. ExpectTrue(bio != NULL);
  37715. for (j = 0; j < numNames; j++)
  37716. {
  37717. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37718. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  37719. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37720. }
  37721. BIO_free(bio);
  37722. bio = NULL;
  37723. X509_free(x509);
  37724. x509 = NULL;
  37725. }
  37726. #ifdef HAVE_PKCS12
  37727. {
  37728. PKCS12 *p12 = NULL;
  37729. int boolRet;
  37730. EVP_PKEY *pkey = NULL;
  37731. const char *p12_f[] = {
  37732. #if !defined(NO_DES3) && !defined(NO_RSA)
  37733. "./certs/test-servercert.p12",
  37734. #endif
  37735. NULL};
  37736. for (i = 0; p12_f[i] != NULL; i++)
  37737. {
  37738. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  37739. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  37740. if (fp != XBADFILE) {
  37741. XFCLOSE(fp);
  37742. fp = XBADFILE;
  37743. }
  37744. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  37745. &pkey, &x509, NULL)) > 0);
  37746. wc_PKCS12_free(p12);
  37747. p12 = NULL;
  37748. EVP_PKEY_free(pkey);
  37749. x509Name = X509_get_issuer_name(x509);
  37750. ExpectNotNull(x509Name);
  37751. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37752. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  37753. for (j = 0; j < numNames; j++)
  37754. {
  37755. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37756. ExpectNotNull(asn1Name =
  37757. X509_NAME_ENTRY_get_object(x509NameEntry));
  37758. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37759. }
  37760. BIO_free(bio);
  37761. bio = NULL;
  37762. X509_free(x509);
  37763. x509 = NULL;
  37764. }
  37765. }
  37766. #endif /* HAVE_PKCS12 */
  37767. #endif
  37768. return EXPECT_RESULT();
  37769. }
  37770. static int test_wolfSSL_OBJ_cmp(void)
  37771. {
  37772. EXPECT_DECLS;
  37773. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37774. ASN1_OBJECT *obj = NULL;
  37775. ASN1_OBJECT *obj2 = NULL;
  37776. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37777. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  37778. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  37779. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  37780. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  37781. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  37782. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  37783. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  37784. ASN1_OBJECT_free(obj);
  37785. ASN1_OBJECT_free(obj2);
  37786. #endif
  37787. return EXPECT_RESULT();
  37788. }
  37789. static int test_wolfSSL_OBJ_txt2nid(void)
  37790. {
  37791. EXPECT_DECLS;
  37792. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  37793. defined(WOLFSSL_APACHE_HTTPD)
  37794. int i;
  37795. static const struct {
  37796. const char* sn;
  37797. const char* ln;
  37798. const char* oid;
  37799. int nid;
  37800. } testVals[] = {
  37801. #ifdef WOLFSSL_APACHE_HTTPD
  37802. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  37803. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  37804. NID_id_on_dnsSRV },
  37805. { "msUPN", "Microsoft User Principal Name",
  37806. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  37807. #endif
  37808. { NULL, NULL, NULL, NID_undef }
  37809. };
  37810. /* Invalid cases */
  37811. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  37812. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  37813. /* Valid cases */
  37814. for (i = 0; testVals[i].sn != NULL; i++) {
  37815. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  37816. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  37817. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  37818. }
  37819. #endif
  37820. return EXPECT_RESULT();
  37821. }
  37822. static int test_wolfSSL_OBJ_txt2obj(void)
  37823. {
  37824. EXPECT_DECLS;
  37825. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  37826. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  37827. int i;
  37828. char buf[50];
  37829. ASN1_OBJECT* obj = NULL;
  37830. static const struct {
  37831. const char* oidStr;
  37832. const char* sn;
  37833. const char* ln;
  37834. } objs_list[] = {
  37835. #if defined(WOLFSSL_APACHE_HTTPD)
  37836. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  37837. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  37838. #endif
  37839. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  37840. { NULL, NULL, NULL }
  37841. };
  37842. static const struct {
  37843. const char* numeric;
  37844. const char* name;
  37845. } objs_named[] = {
  37846. /* In dictionary but not in normal list. */
  37847. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  37848. /* Made up OID. */
  37849. { "1.3.5.7", "1.3.5.7" },
  37850. { NULL, NULL }
  37851. };
  37852. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  37853. ASN1_OBJECT_free(obj);
  37854. obj = NULL;
  37855. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  37856. ASN1_OBJECT_free(obj);
  37857. obj = NULL;
  37858. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  37859. /* Test numerical value of oid (oidStr) */
  37860. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  37861. /* Convert object back to text to confirm oid is correct */
  37862. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37863. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37864. ASN1_OBJECT_free(obj);
  37865. obj = NULL;
  37866. XMEMSET(buf, 0, sizeof(buf));
  37867. /* Test short name (sn) */
  37868. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  37869. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  37870. /* Convert object back to text to confirm oid is correct */
  37871. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37872. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37873. ASN1_OBJECT_free(obj);
  37874. obj = NULL;
  37875. XMEMSET(buf, 0, sizeof(buf));
  37876. /* Test long name (ln) - should fail when no_name = 1 */
  37877. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  37878. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  37879. /* Convert object back to text to confirm oid is correct */
  37880. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37881. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37882. ASN1_OBJECT_free(obj);
  37883. obj = NULL;
  37884. XMEMSET(buf, 0, sizeof(buf));
  37885. }
  37886. for (i = 0; objs_named[i].numeric != NULL; i++) {
  37887. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  37888. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  37889. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  37890. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37891. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  37892. ASN1_OBJECT_free(obj);
  37893. obj = NULL;
  37894. }
  37895. #endif
  37896. return EXPECT_RESULT();
  37897. }
  37898. static int test_wolfSSL_PEM_write_bio_X509(void)
  37899. {
  37900. EXPECT_DECLS;
  37901. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  37902. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  37903. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  37904. !defined(NO_FILESYSTEM)
  37905. /* This test contains the hard coded expected
  37906. * lengths. Update if necessary */
  37907. XFILE fp = XBADFILE;
  37908. WOLFSSL_EVP_PKEY *priv = NULL;
  37909. BIO* input = NULL;
  37910. BIO* output = NULL;
  37911. X509* x509a = NULL;
  37912. X509* x509b = NULL;
  37913. ASN1_TIME* notBeforeA = NULL;
  37914. ASN1_TIME* notAfterA = NULL;
  37915. #ifndef NO_ASN_TIME
  37916. ASN1_TIME* notBeforeB = NULL;
  37917. ASN1_TIME* notAfterB = NULL;
  37918. #endif
  37919. int expectedLen;
  37920. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  37921. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  37922. if (fp != XBADFILE) {
  37923. XFCLOSE(fp);
  37924. fp = XBADFILE;
  37925. }
  37926. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  37927. "rb"));
  37928. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  37929. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  37930. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37931. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  37932. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  37933. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  37934. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37935. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37936. /* compare length against expected */
  37937. expectedLen = 2000;
  37938. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  37939. #ifndef NO_ASN_TIME
  37940. /* read exported X509 PEM back into struct, sanity check on export,
  37941. * make sure notBefore/notAfter are the same and certs are identical. */
  37942. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  37943. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  37944. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  37945. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  37946. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  37947. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  37948. X509_free(x509b);
  37949. x509b = NULL;
  37950. #endif
  37951. /* Reset output buffer */
  37952. BIO_free(output);
  37953. output = NULL;
  37954. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37955. /* Test forcing the AKID to be generated just from KeyIdentifier */
  37956. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  37957. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  37958. x509a->authKeyId = x509a->authKeyIdSrc;
  37959. x509a->authKeyIdSrc = NULL;
  37960. x509a->authKeyIdSrcSz = 0;
  37961. }
  37962. /* Resign to re-generate the der */
  37963. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  37964. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37965. /* Check that we generate a smaller output since the AKID will
  37966. * only contain the KeyIdentifier without any additional
  37967. * information */
  37968. /* Here we copy the validity struct from the original */
  37969. expectedLen = 1688;
  37970. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  37971. /* Reset buffers and x509 */
  37972. BIO_free(input);
  37973. input = NULL;
  37974. BIO_free(output);
  37975. output = NULL;
  37976. X509_free(x509a);
  37977. x509a = NULL;
  37978. /* test CA and basicConstSet values are encoded when
  37979. * the cert is a CA */
  37980. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  37981. /* read PEM into X509 struct */
  37982. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37983. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  37984. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37985. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37986. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  37987. * values are maintained and certs are identical.*/
  37988. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  37989. ExpectIntEQ(x509b->isCa, 1);
  37990. ExpectIntEQ(x509b->basicConstSet, 1);
  37991. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  37992. X509_free(x509a);
  37993. x509a = NULL;
  37994. X509_free(x509b);
  37995. x509b = NULL;
  37996. BIO_free(input);
  37997. input = NULL;
  37998. BIO_free(output);
  37999. output = NULL;
  38000. /* test CA and basicConstSet values are encoded when
  38001. * the cert is not CA */
  38002. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  38003. /* read PEM into X509 struct */
  38004. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38005. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38006. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38007. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38008. /* read exported X509 PEM back into struct, ensure isCa and
  38009. * basicConstSet values are maintained and certs are identical */
  38010. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38011. ExpectIntEQ(x509b->isCa, 0);
  38012. ExpectIntEQ(x509b->basicConstSet, 1);
  38013. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38014. wolfSSL_EVP_PKEY_free(priv);
  38015. X509_free(x509a);
  38016. X509_free(x509b);
  38017. BIO_free(input);
  38018. BIO_free(output);
  38019. #endif
  38020. return EXPECT_RESULT();
  38021. }
  38022. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38023. {
  38024. EXPECT_DECLS;
  38025. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38026. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38027. X509* x509 = NULL;
  38028. #ifndef NO_BIO
  38029. BIO* bio = NULL;
  38030. #endif
  38031. X509_NAME* nm = NULL;
  38032. X509_NAME_ENTRY* entry = NULL;
  38033. unsigned char cn[] = "another name to add";
  38034. #ifdef OPENSSL_ALL
  38035. int i;
  38036. int names_len = 0;
  38037. #endif
  38038. ExpectNotNull(x509 =
  38039. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38040. #ifndef NO_BIO
  38041. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38042. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38043. #endif
  38044. #ifdef WOLFSSL_CERT_REQ
  38045. {
  38046. X509_REQ* req = NULL;
  38047. #ifndef NO_BIO
  38048. BIO* bReq = NULL;
  38049. #endif
  38050. ExpectNotNull(req =
  38051. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38052. #ifndef NO_BIO
  38053. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  38054. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38055. BIO_free(bReq);
  38056. #endif
  38057. X509_free(req);
  38058. }
  38059. #endif
  38060. ExpectNotNull(nm = X509_get_subject_name(x509));
  38061. /* Test add entry */
  38062. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38063. 0x0c, cn, (int)sizeof(cn)));
  38064. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38065. #ifdef WOLFSSL_CERT_EXT
  38066. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38067. (byte*)"support@wolfssl.com", 19, -1,
  38068. 1), WOLFSSL_SUCCESS);
  38069. #endif
  38070. X509_NAME_ENTRY_free(entry);
  38071. entry = NULL;
  38072. #ifdef WOLFSSL_CERT_REQ
  38073. {
  38074. unsigned char srv_pkcs9p[] = "Server";
  38075. unsigned char fvrtDrnk[] = "tequila";
  38076. unsigned char* der = NULL;
  38077. char* subject = NULL;
  38078. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38079. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38080. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38081. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38082. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38083. ExpectNotNull(der);
  38084. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  38085. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38086. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  38087. #ifdef DEBUG_WOLFSSL
  38088. if (subject != NULL) {
  38089. fprintf(stderr, "\n\t%s\n", subject);
  38090. }
  38091. #endif
  38092. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38093. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38094. }
  38095. #endif
  38096. /* Test add entry by text */
  38097. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38098. 0x0c, cn, (int)sizeof(cn)));
  38099. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38100. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38101. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38102. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38103. #endif
  38104. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38105. X509_NAME_ENTRY_free(entry);
  38106. entry = NULL;
  38107. /* Test add entry by NID */
  38108. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38109. cn, -1, -1, 0), SSL_SUCCESS);
  38110. #ifdef OPENSSL_ALL
  38111. /* stack of name entry */
  38112. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38113. for (i = 0; i < names_len; i++) {
  38114. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38115. }
  38116. #endif
  38117. #ifndef NO_BIO
  38118. BIO_free(bio);
  38119. #endif
  38120. X509_free(x509); /* free's nm */
  38121. #endif
  38122. return EXPECT_RESULT();
  38123. }
  38124. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38125. static int test_GENERAL_NAME_set0_othername(void) {
  38126. EXPECT_DECLS;
  38127. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38128. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38129. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38130. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38131. defined(WOLFSSL_FPKI)
  38132. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38133. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38134. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38135. const char * cert_fname = "./certs/server-cert.der";
  38136. const char * key_fname = "./certs/server-key.der";
  38137. X509* x509 = NULL;
  38138. GENERAL_NAME* gn = NULL;
  38139. GENERAL_NAMES* gns = NULL;
  38140. ASN1_OBJECT* upn_oid = NULL;
  38141. ASN1_UTF8STRING *utf8str = NULL;
  38142. ASN1_TYPE *value = NULL;
  38143. X509_EXTENSION * ext = NULL;
  38144. byte* pt = NULL;
  38145. byte der[4096];
  38146. int derSz = 0;
  38147. EVP_PKEY* priv = NULL;
  38148. XFILE f = XBADFILE;
  38149. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  38150. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  38151. if (f != XBADFILE) {
  38152. XFCLOSE(f);
  38153. f = XBADFILE;
  38154. }
  38155. ExpectNotNull(gn = GENERAL_NAME_new());
  38156. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38157. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38158. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38159. ExpectNotNull(value = ASN1_TYPE_new());
  38160. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38161. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  38162. wolfSSL_ASN1_STRING_free(utf8str);
  38163. }
  38164. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38165. if (EXPECT_FAIL()) {
  38166. ASN1_TYPE_free(value);
  38167. }
  38168. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38169. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38170. if (EXPECT_FAIL()) {
  38171. GENERAL_NAME_free(gn);
  38172. gn = NULL;
  38173. }
  38174. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38175. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  38176. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38177. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38178. if (f != XBADFILE) {
  38179. XFCLOSE(f);
  38180. f = XBADFILE;
  38181. }
  38182. pt = der;
  38183. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38184. (const unsigned char**)&pt, derSz));
  38185. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38186. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38187. gns = NULL;
  38188. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38189. NULL));
  38190. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  38191. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  38192. ExpectIntEQ(gn->type, 0);
  38193. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38194. ASN1_OBJECT_free(upn_oid);
  38195. X509_EXTENSION_free(ext);
  38196. X509_free(x509);
  38197. EVP_PKEY_free(priv);
  38198. #endif
  38199. return EXPECT_RESULT();
  38200. }
  38201. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38202. static int test_othername_and_SID_ext(void) {
  38203. EXPECT_DECLS;
  38204. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38205. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38206. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38207. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38208. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  38209. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38210. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38211. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38212. const char* csr_fname = "./certs/csr.signed.der";
  38213. const char* key_fname = "./certs/server-key.der";
  38214. byte der[4096];
  38215. int derSz = 0;
  38216. X509_REQ* x509 = NULL;
  38217. STACK_OF(X509_EXTENSION) *exts = NULL;
  38218. X509_EXTENSION * san_ext = NULL;
  38219. X509_EXTENSION * ext = NULL;
  38220. GENERAL_NAME* gn = NULL;
  38221. GENERAL_NAMES* gns = NULL;
  38222. ASN1_OBJECT* upn_oid = NULL;
  38223. ASN1_UTF8STRING *utf8str = NULL;
  38224. ASN1_TYPE *value = NULL;
  38225. ASN1_STRING *extval = NULL;
  38226. /* SID extension. SID data format explained here:
  38227. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38228. */
  38229. uint8_t SidExtension[] = {
  38230. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38231. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38232. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38233. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38234. uint8_t expectedAltName[] = {
  38235. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  38236. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  38237. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38238. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  38239. X509_EXTENSION *sid_ext = NULL;
  38240. ASN1_OBJECT* sid_oid = NULL;
  38241. ASN1_OCTET_STRING *sid_data = NULL;
  38242. EVP_PKEY* priv = NULL;
  38243. XFILE f = XBADFILE;
  38244. byte* pt = NULL;
  38245. BIO* bio = NULL;
  38246. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  38247. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38248. if (f != XBADFILE) {
  38249. XFCLOSE(f);
  38250. f = XBADFILE;
  38251. }
  38252. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  38253. ExpectNotNull(gn = GENERAL_NAME_new());
  38254. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38255. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38256. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38257. ExpectNotNull(value = ASN1_TYPE_new());
  38258. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38259. if (EXPECT_FAIL()) {
  38260. ASN1_UTF8STRING_free(utf8str);
  38261. }
  38262. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38263. if (EXPECT_FAIL()) {
  38264. ASN1_TYPE_free(value);
  38265. GENERAL_NAME_free(gn);
  38266. gn = NULL;
  38267. }
  38268. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38269. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38270. if (EXPECT_FAIL()) {
  38271. GENERAL_NAME_free(gn);
  38272. }
  38273. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38274. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38275. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  38276. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38277. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38278. sid_data));
  38279. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  38280. /* Ensure an empty stack doesn't raise an error. */
  38281. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38282. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38283. if (EXPECT_FAIL()) {
  38284. X509_EXTENSION_free(san_ext);
  38285. }
  38286. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38287. if (EXPECT_FAIL()) {
  38288. X509_EXTENSION_free(sid_ext);
  38289. }
  38290. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38291. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38292. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38293. if (f != XBADFILE)
  38294. XFCLOSE(f);
  38295. pt = der;
  38296. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38297. (const unsigned char**)&pt, derSz));
  38298. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38299. pt = der;
  38300. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38301. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38302. gns = NULL;
  38303. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38304. exts = NULL;
  38305. ASN1_OBJECT_free(upn_oid);
  38306. ASN1_OBJECT_free(sid_oid);
  38307. ASN1_OCTET_STRING_free(sid_data);
  38308. X509_REQ_free(x509);
  38309. x509 = NULL;
  38310. EVP_PKEY_free(priv);
  38311. /* At this point everything used to generate what is in der is cleaned up.
  38312. * We now read back from der to confirm the extensions were inserted
  38313. * correctly. */
  38314. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  38315. ExpectNotNull(bio);
  38316. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  38317. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  38318. ExpectNotNull(x509);
  38319. BIO_free(bio);
  38320. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  38321. x509));
  38322. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  38323. /* Check the SID extension. */
  38324. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  38325. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38326. ExpectIntEQ(extval->length, sizeof(SidExtension));
  38327. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  38328. /* Check the AltNames extension. */
  38329. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  38330. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38331. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  38332. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  38333. 0);
  38334. /* Cleanup */
  38335. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38336. NULL));
  38337. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  38338. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  38339. ExpectIntEQ(gn->type, 0);
  38340. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38341. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38342. X509_REQ_free(x509);
  38343. #endif
  38344. return EXPECT_RESULT();
  38345. }
  38346. static int test_wolfSSL_X509_set_name(void)
  38347. {
  38348. EXPECT_DECLS;
  38349. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38350. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38351. X509* x509 = NULL;
  38352. X509_NAME* name = NULL;
  38353. ExpectNotNull(name = X509_NAME_new());
  38354. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38355. (byte*)"wolfssl.com", 11, 0, 1),
  38356. WOLFSSL_SUCCESS);
  38357. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38358. (byte*)"support@wolfssl.com", 19, -1,
  38359. 1), WOLFSSL_SUCCESS);
  38360. ExpectNotNull(x509 = X509_new());
  38361. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38362. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38363. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38364. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38365. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38366. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38367. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38368. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38369. X509_free(x509);
  38370. X509_NAME_free(name);
  38371. #endif /* OPENSSL_ALL && !NO_CERTS */
  38372. return EXPECT_RESULT();
  38373. }
  38374. static int test_wolfSSL_X509_set_notAfter(void)
  38375. {
  38376. EXPECT_DECLS;
  38377. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38378. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38379. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38380. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38381. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38382. /* Generalized time will overflow time_t if not long */
  38383. X509* x = NULL;
  38384. BIO* bio = NULL;
  38385. ASN1_TIME *asn_time = NULL;
  38386. ASN1_TIME *time_check = NULL;
  38387. const int year = 365*24*60*60;
  38388. const int day = 24*60*60;
  38389. const int hour = 60*60;
  38390. const int mini = 60;
  38391. int offset_day;
  38392. unsigned char buf[25];
  38393. time_t t;
  38394. /*
  38395. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38396. */
  38397. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38398. offset_day = 7;
  38399. /*
  38400. * Free these.
  38401. */
  38402. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38403. ExpectNotNull(asn_time);
  38404. ExpectNotNull(x = X509_new());
  38405. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38406. /*
  38407. * Tests
  38408. */
  38409. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38410. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38411. ExpectNotNull(time_check = X509_get_notAfter(x));
  38412. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38413. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38414. /* Convert to human readable format and compare to intended date */
  38415. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38416. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38417. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38418. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  38419. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  38420. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  38421. /*
  38422. * Cleanup
  38423. */
  38424. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38425. X509_free(x);
  38426. BIO_free(bio);
  38427. #endif
  38428. return EXPECT_RESULT();
  38429. }
  38430. static int test_wolfSSL_X509_set_notBefore(void)
  38431. {
  38432. EXPECT_DECLS;
  38433. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38434. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38435. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38436. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38437. X509* x = NULL;
  38438. BIO* bio = NULL;
  38439. ASN1_TIME *asn_time = NULL;
  38440. ASN1_TIME *time_check = NULL;
  38441. const int year = 365*24*60*60;
  38442. const int day = 24*60*60;
  38443. const int hour = 60*60;
  38444. const int mini = 60;
  38445. int offset_day;
  38446. unsigned char buf[25];
  38447. time_t t;
  38448. /*
  38449. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38450. */
  38451. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38452. offset_day = 7;
  38453. /*
  38454. * Free these.
  38455. */
  38456. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38457. ExpectNotNull(asn_time);
  38458. ExpectNotNull(x = X509_new());
  38459. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38460. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38461. /*
  38462. * Main Tests
  38463. */
  38464. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38465. /* time_check == (ANS1_TIME*)x->notBefore */
  38466. ExpectNotNull(time_check = X509_get_notBefore(x));
  38467. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38468. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38469. /* Convert to human readable format and compare to intended date */
  38470. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38471. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38472. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38473. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  38474. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  38475. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  38476. /*
  38477. * Cleanup
  38478. */
  38479. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38480. X509_free(x);
  38481. BIO_free(bio);
  38482. #endif
  38483. return EXPECT_RESULT();
  38484. }
  38485. static int test_wolfSSL_X509_set_version(void)
  38486. {
  38487. EXPECT_DECLS;
  38488. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38489. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38490. X509* x509 = NULL;
  38491. long v = 2L;
  38492. long maxInt = INT_MAX;
  38493. ExpectNotNull(x509 = X509_new());
  38494. /* These should pass. */
  38495. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  38496. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  38497. /* Fail Case: When v(long) is greater than x509->version(int). */
  38498. v = maxInt+1;
  38499. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  38500. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  38501. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  38502. /* Cleanup */
  38503. X509_free(x509);
  38504. #endif
  38505. return EXPECT_RESULT();
  38506. }
  38507. #ifndef NO_BIO
  38508. static int test_wolfSSL_BIO_gets(void)
  38509. {
  38510. EXPECT_DECLS;
  38511. #if defined(OPENSSL_EXTRA)
  38512. BIO* bio = NULL;
  38513. BIO* bio2 = NULL;
  38514. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  38515. char emp[] = "";
  38516. char bio_buffer[20];
  38517. int bufferSz = 20;
  38518. #ifdef OPENSSL_ALL
  38519. BUF_MEM* emp_bm = NULL;
  38520. BUF_MEM* msg_bm = NULL;
  38521. #endif
  38522. /* try with bad args */
  38523. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  38524. #ifdef OPENSSL_ALL
  38525. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  38526. #endif
  38527. /* try with real msg */
  38528. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38529. XMEMSET(bio_buffer, 0, bufferSz);
  38530. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  38531. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  38532. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  38533. ExpectFalse(bio2 != BIO_next(bio));
  38534. /* make buffer filled with no terminating characters */
  38535. XMEMSET(bio_buffer, 1, bufferSz);
  38536. /* BIO_gets reads a line of data */
  38537. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38538. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38539. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38540. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38541. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38542. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38543. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38544. #ifdef OPENSSL_ALL
  38545. /* test setting the mem_buf manually */
  38546. BIO_free(bio);
  38547. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38548. ExpectNotNull(emp_bm = BUF_MEM_new());
  38549. ExpectNotNull(msg_bm = BUF_MEM_new());
  38550. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  38551. if (EXPECT_SUCCESS()) {
  38552. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38553. msg_bm->data = NULL;
  38554. }
  38555. /* emp size is 1 for terminator */
  38556. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  38557. if (EXPECT_SUCCESS()) {
  38558. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38559. emp_bm->data = emp;
  38560. msg_bm->data = msg;
  38561. }
  38562. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  38563. /* check reading an empty string */
  38564. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38565. ExpectStrEQ(emp, bio_buffer);
  38566. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38567. /* BIO_gets reads a line of data */
  38568. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  38569. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38570. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38571. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38572. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38573. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38574. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38575. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38576. if (EXPECT_SUCCESS())
  38577. emp_bm->data = NULL;
  38578. BUF_MEM_free(emp_bm);
  38579. if (EXPECT_SUCCESS())
  38580. msg_bm->data = NULL;
  38581. BUF_MEM_free(msg_bm);
  38582. #endif
  38583. /* check not null terminated string */
  38584. BIO_free(bio);
  38585. bio = NULL;
  38586. msg[0] = 0x33;
  38587. msg[1] = 0x33;
  38588. msg[2] = 0x33;
  38589. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38590. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  38591. ExpectIntEQ(bio_buffer[0], msg[0]);
  38592. ExpectIntEQ(bio_buffer[1], msg[1]);
  38593. ExpectIntNE(bio_buffer[2], msg[2]);
  38594. BIO_free(bio);
  38595. bio = NULL;
  38596. msg[3] = 0x33;
  38597. bio_buffer[3] = 0x33;
  38598. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38599. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  38600. ExpectIntEQ(bio_buffer[0], msg[0]);
  38601. ExpectIntEQ(bio_buffer[1], msg[1]);
  38602. ExpectIntEQ(bio_buffer[2], msg[2]);
  38603. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  38604. /* check reading an empty string */
  38605. BIO_free(bio);
  38606. bio = NULL;
  38607. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  38608. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38609. ExpectStrEQ(emp, bio_buffer);
  38610. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38611. /* check error cases */
  38612. BIO_free(bio);
  38613. bio = NULL;
  38614. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  38615. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38616. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38617. #if !defined(NO_FILESYSTEM)
  38618. {
  38619. BIO* f_bio = NULL;
  38620. XFILE f = XBADFILE;
  38621. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  38622. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38623. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  38624. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  38625. if (EXPECT_FAIL() && (f != XBADFILE)) {
  38626. XFCLOSE(f);
  38627. }
  38628. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38629. BIO_free(f_bio);
  38630. f_bio = NULL;
  38631. }
  38632. #endif /* NO_FILESYSTEM */
  38633. BIO_free(bio);
  38634. bio = NULL;
  38635. BIO_free(bio2);
  38636. bio2 = NULL;
  38637. /* try with type BIO */
  38638. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  38639. sizeof(msg));
  38640. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38641. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38642. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  38643. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  38644. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  38645. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  38646. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  38647. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38648. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38649. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38650. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38651. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38652. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38653. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38654. BIO_free(bio);
  38655. bio = NULL;
  38656. BIO_free(bio2);
  38657. bio2 = NULL;
  38658. /* check reading an empty string */
  38659. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38660. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  38661. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38662. ExpectStrEQ(emp, bio_buffer);
  38663. BIO_free(bio);
  38664. bio = NULL;
  38665. #endif
  38666. return EXPECT_RESULT();
  38667. }
  38668. static int test_wolfSSL_BIO_puts(void)
  38669. {
  38670. EXPECT_DECLS;
  38671. #if defined(OPENSSL_EXTRA)
  38672. BIO* bio = NULL;
  38673. char input[] = "hello\0world\n.....ok\n\0";
  38674. char output[128];
  38675. XMEMSET(output, 0, sizeof(output));
  38676. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38677. ExpectIntEQ(BIO_puts(bio, input), 5);
  38678. ExpectIntEQ(BIO_pending(bio), 5);
  38679. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  38680. ExpectIntEQ(BIO_pending(bio), 19);
  38681. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  38682. ExpectStrEQ(output, "helloworld\n");
  38683. ExpectIntEQ(BIO_pending(bio), 8);
  38684. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  38685. ExpectStrEQ(output, ".....ok\n");
  38686. ExpectIntEQ(BIO_pending(bio), 0);
  38687. ExpectIntEQ(BIO_puts(bio, ""), -1);
  38688. BIO_free(bio);
  38689. #endif
  38690. return EXPECT_RESULT();
  38691. }
  38692. static int test_wolfSSL_BIO_dump(void)
  38693. {
  38694. EXPECT_DECLS;
  38695. #if defined(OPENSSL_EXTRA)
  38696. BIO* bio;
  38697. static const unsigned char data[] = {
  38698. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  38699. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  38700. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  38701. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  38702. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  38703. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  38704. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  38705. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  38706. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  38707. 0xB4
  38708. };
  38709. /* Generated with OpenSSL. */
  38710. static const char expected[] =
  38711. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  38712. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  38713. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  38714. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  38715. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  38716. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  38717. static const char expectedAll[] =
  38718. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  38719. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  38720. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  38721. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  38722. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  38723. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  38724. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  38725. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  38726. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  38727. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  38728. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  38729. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  38730. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  38731. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  38732. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  38733. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  38734. char output[16 * 80];
  38735. int i;
  38736. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38737. /* Example key dumped. */
  38738. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  38739. sizeof(expected) - 1);
  38740. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  38741. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  38742. /* Try every possible value for a character. */
  38743. for (i = 0; i < 256; i++)
  38744. output[i] = i;
  38745. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  38746. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  38747. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  38748. BIO_free(bio);
  38749. #endif
  38750. return EXPECT_RESULT();
  38751. }
  38752. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38753. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38754. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38755. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  38756. {
  38757. (void)ssl;
  38758. (void)buf;
  38759. (void)sz;
  38760. (void)ctx;
  38761. return WOLFSSL_CBIO_ERR_WANT_READ;
  38762. }
  38763. #endif
  38764. static int test_wolfSSL_BIO_should_retry(void)
  38765. {
  38766. EXPECT_DECLS;
  38767. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38768. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38769. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38770. tcp_ready ready;
  38771. func_args server_args;
  38772. THREAD_TYPE serverThread;
  38773. SOCKET_T sockfd = 0;
  38774. WOLFSSL_CTX* ctx = NULL;
  38775. WOLFSSL* ssl = NULL;
  38776. char msg[64] = "hello wolfssl!";
  38777. char reply[1024];
  38778. int msgSz = (int)XSTRLEN(msg);
  38779. int ret;
  38780. BIO* bio = NULL;
  38781. XMEMSET(&server_args, 0, sizeof(func_args));
  38782. #ifdef WOLFSSL_TIRTOS
  38783. fdOpenSession(Task_self());
  38784. #endif
  38785. StartTCP();
  38786. InitTcpReady(&ready);
  38787. #if defined(USE_WINDOWS_API)
  38788. /* use RNG to get random port if using windows */
  38789. ready.port = GetRandomPort();
  38790. #endif
  38791. server_args.signal = &ready;
  38792. start_thread(test_server_nofail, &server_args, &serverThread);
  38793. wait_tcp_ready(&server_args);
  38794. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38795. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  38796. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  38797. #endif
  38798. ExpectIntEQ(WOLFSSL_SUCCESS,
  38799. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38800. ExpectIntEQ(WOLFSSL_SUCCESS,
  38801. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38802. ExpectIntEQ(WOLFSSL_SUCCESS,
  38803. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38804. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  38805. /* force retry */
  38806. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  38807. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  38808. ExpectNotNull(ssl);
  38809. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  38810. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  38811. if (EXPECT_FAIL()) {
  38812. wolfSSL_free(ssl);
  38813. ssl = NULL;
  38814. }
  38815. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  38816. ExpectIntNE(BIO_should_retry(bio), 0);
  38817. ExpectIntEQ(BIO_should_read(bio), 0);
  38818. ExpectIntEQ(BIO_should_write(bio), 0);
  38819. /* now perform successful connection */
  38820. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  38821. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  38822. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  38823. ret = wolfSSL_get_error(ssl, -1);
  38824. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  38825. ExpectIntNE(BIO_should_retry(bio), 0);
  38826. if (ret == WOLFSSL_ERROR_WANT_READ)
  38827. ExpectIntEQ(BIO_should_read(bio), 1);
  38828. else
  38829. ExpectIntEQ(BIO_should_read(bio), 0);
  38830. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  38831. ExpectIntEQ(BIO_should_write(bio), 1);
  38832. else
  38833. ExpectIntEQ(BIO_should_write(bio), 0);
  38834. }
  38835. else {
  38836. ExpectIntEQ(BIO_should_retry(bio), 0);
  38837. ExpectIntEQ(BIO_should_read(bio), 0);
  38838. ExpectIntEQ(BIO_should_write(bio), 0);
  38839. }
  38840. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  38841. XSTRLEN("I hear you fa shizzle!")), 0);
  38842. BIO_free(bio);
  38843. wolfSSL_CTX_free(ctx);
  38844. CloseSocket(sockfd);
  38845. join_thread(serverThread);
  38846. FreeTcpReady(&ready);
  38847. #ifdef WOLFSSL_TIRTOS
  38848. fdOpenSession(Task_self());
  38849. #endif
  38850. #endif
  38851. return EXPECT_RESULT();
  38852. }
  38853. static int test_wolfSSL_BIO_connect(void)
  38854. {
  38855. EXPECT_DECLS;
  38856. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38857. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  38858. tcp_ready ready;
  38859. func_args server_args;
  38860. THREAD_TYPE serverThread;
  38861. BIO *tcpBio = NULL;
  38862. BIO *sslBio = NULL;
  38863. SSL_CTX* ctx = NULL;
  38864. SSL *ssl = NULL;
  38865. SSL *sslPtr;
  38866. char msg[] = "hello wolfssl!";
  38867. char reply[30];
  38868. char buff[10] = {0};
  38869. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38870. ExpectIntEQ(WOLFSSL_SUCCESS,
  38871. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38872. ExpectIntEQ(WOLFSSL_SUCCESS,
  38873. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38874. ExpectIntEQ(WOLFSSL_SUCCESS,
  38875. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38876. /* Setup server */
  38877. XMEMSET(&server_args, 0, sizeof(func_args));
  38878. StartTCP();
  38879. InitTcpReady(&ready);
  38880. #if defined(USE_WINDOWS_API)
  38881. /* use RNG to get random port if using windows */
  38882. ready.port = GetRandomPort();
  38883. #endif
  38884. server_args.signal = &ready;
  38885. start_thread(test_server_nofail, &server_args, &serverThread);
  38886. wait_tcp_ready(&server_args);
  38887. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38888. /* Start the test proper */
  38889. /* Setup the TCP BIO */
  38890. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  38891. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  38892. /* Setup the SSL object */
  38893. ExpectNotNull(ssl = SSL_new(ctx));
  38894. SSL_set_connect_state(ssl);
  38895. /* Setup the SSL BIO */
  38896. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  38897. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  38898. if (EXPECT_FAIL()) {
  38899. wolfSSL_free(ssl);
  38900. }
  38901. /* Verify that BIO_get_ssl works. */
  38902. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  38903. ExpectPtrEq(ssl, sslPtr);
  38904. /* Link BIO's so that sslBio uses tcpBio for IO */
  38905. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  38906. /* Do TCP connect */
  38907. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  38908. /* Do TLS handshake */
  38909. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  38910. /* Test writing */
  38911. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38912. /* Expect length of default wolfSSL reply */
  38913. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38914. /* Clean it all up */
  38915. BIO_free_all(sslBio);
  38916. /* Server clean up */
  38917. join_thread(serverThread);
  38918. FreeTcpReady(&ready);
  38919. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  38920. * after. */
  38921. XMEMSET(&server_args, 0, sizeof(func_args));
  38922. StartTCP();
  38923. InitTcpReady(&ready);
  38924. #if defined(USE_WINDOWS_API)
  38925. /* use RNG to get random port if using windows */
  38926. ready.port = GetRandomPort();
  38927. #endif
  38928. server_args.signal = &ready;
  38929. start_thread(test_server_nofail, &server_args, &serverThread);
  38930. wait_tcp_ready(&server_args);
  38931. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38932. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  38933. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  38934. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  38935. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  38936. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  38937. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38938. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38939. /* Attempt to close the TLS connection gracefully. */
  38940. BIO_ssl_shutdown(sslBio);
  38941. BIO_free_all(sslBio);
  38942. join_thread(serverThread);
  38943. FreeTcpReady(&ready);
  38944. SSL_CTX_free(ctx);
  38945. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38946. wc_ecc_fp_free(); /* free per thread cache */
  38947. #endif
  38948. #endif
  38949. return EXPECT_RESULT();
  38950. }
  38951. static int test_wolfSSL_BIO_tls(void)
  38952. {
  38953. EXPECT_DECLS;
  38954. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  38955. SSL_CTX* ctx = NULL;
  38956. SSL *ssl = NULL;
  38957. BIO *readBio = NULL;
  38958. BIO *writeBio = NULL;
  38959. int ret;
  38960. int err = 0;
  38961. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38962. ExpectNotNull(ssl = SSL_new(ctx));
  38963. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  38964. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  38965. /* Qt reads data from write-bio,
  38966. * then writes the read data into plain packet.
  38967. * Qt reads data from plain packet,
  38968. * then writes the read data into read-bio.
  38969. */
  38970. SSL_set_bio(ssl, readBio, writeBio);
  38971. do {
  38972. #ifdef WOLFSSL_ASYNC_CRYPT
  38973. if (err == WC_PENDING_E) {
  38974. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  38975. if (ret < 0) { break; } else if (ret == 0) { continue; }
  38976. }
  38977. #endif
  38978. ret = SSL_connect(ssl);
  38979. err = SSL_get_error(ssl, 0);
  38980. } while (err == WC_PENDING_E);
  38981. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  38982. /* in this use case, should return WANT READ
  38983. * so that Qt will read the data from plain packet for next state.
  38984. */
  38985. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  38986. SSL_free(ssl);
  38987. SSL_CTX_free(ctx);
  38988. #endif
  38989. return EXPECT_RESULT();
  38990. }
  38991. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38992. defined(HAVE_HTTP_CLIENT)
  38993. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  38994. {
  38995. BIO* clientBio;
  38996. SSL* sslClient;
  38997. SSL_CTX* ctx;
  38998. char connectAddr[20]; /* IP + port */;
  38999. (void)args;
  39000. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  39001. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  39002. AssertIntEQ(BIO_do_connect(clientBio), 1);
  39003. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39004. AssertNotNull(sslClient = SSL_new(ctx));
  39005. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  39006. SSL_set_bio(sslClient, clientBio, clientBio);
  39007. AssertIntEQ(SSL_connect(sslClient), 1);
  39008. SSL_free(sslClient);
  39009. SSL_CTX_free(ctx);
  39010. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39011. wc_ecc_fp_free(); /* free per thread cache */
  39012. #endif
  39013. WOLFSSL_RETURN_FROM_THREAD(0);
  39014. }
  39015. #endif
  39016. static int test_wolfSSL_BIO_accept(void)
  39017. {
  39018. EXPECT_DECLS;
  39019. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39020. defined(HAVE_HTTP_CLIENT)
  39021. BIO* serverBindBio = NULL;
  39022. BIO* serverAcceptBio = NULL;
  39023. SSL* sslServer = NULL;
  39024. SSL_CTX* ctx = NULL;
  39025. func_args args;
  39026. THREAD_TYPE thread;
  39027. char port[10]; /* 10 bytes should be enough to store the string
  39028. * representation of the port */
  39029. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  39030. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  39031. /* First BIO_do_accept binds the port */
  39032. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39033. XMEMSET(&args, 0, sizeof(func_args));
  39034. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  39035. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39036. /* Let's plug it into SSL to test */
  39037. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39038. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39039. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39040. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39041. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39042. ExpectNotNull(sslServer = SSL_new(ctx));
  39043. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  39044. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  39045. ExpectIntEQ(SSL_accept(sslServer), 1);
  39046. join_thread(thread);
  39047. BIO_free(serverBindBio);
  39048. SSL_free(sslServer);
  39049. SSL_CTX_free(ctx);
  39050. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39051. wc_ecc_fp_free(); /* free per thread cache */
  39052. #endif
  39053. #endif
  39054. return EXPECT_RESULT();
  39055. }
  39056. static int test_wolfSSL_BIO_write(void)
  39057. {
  39058. EXPECT_DECLS;
  39059. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  39060. BIO* bio = NULL;
  39061. BIO* bio64 = NULL;
  39062. BIO* bio_mem = NULL;
  39063. BIO* ptr = NULL;
  39064. int sz;
  39065. char msg[] = "conversion test";
  39066. char out[40];
  39067. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  39068. void* bufPtr = NULL;
  39069. BUF_MEM* buf = NULL;
  39070. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39071. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  39072. if (EXPECT_FAIL()) {
  39073. BIO_free(bio64);
  39074. }
  39075. /* now should convert to base64 then write to memory */
  39076. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39077. BIO_flush(bio);
  39078. /* test BIO chain */
  39079. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  39080. ExpectNotNull(buf);
  39081. ExpectIntEQ(buf->length, 25);
  39082. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  39083. ExpectPtrEq(buf->data, bufPtr);
  39084. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  39085. sz = sizeof(out);
  39086. XMEMSET(out, 0, sz);
  39087. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  39088. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39089. /* write then read should return the same message */
  39090. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39091. sz = sizeof(out);
  39092. XMEMSET(out, 0, sz);
  39093. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  39094. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39095. /* now try encoding with no line ending */
  39096. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  39097. #ifdef HAVE_EX_DATA
  39098. BIO_set_ex_data(bio64, 0, (void*) "data");
  39099. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  39100. #endif
  39101. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39102. BIO_flush(bio);
  39103. sz = sizeof(out);
  39104. XMEMSET(out, 0, sz);
  39105. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  39106. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39107. BIO_free_all(bio); /* frees bio64 also */
  39108. bio = NULL;
  39109. /* test with more than one bio64 in list */
  39110. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39111. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  39112. if (EXPECT_FAIL()) {
  39113. BIO_free(bio64);
  39114. bio64 = NULL;
  39115. }
  39116. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  39117. ExpectNotNull(BIO_push(bio64, bio_mem));
  39118. if (EXPECT_FAIL()) {
  39119. BIO_free(bio_mem);
  39120. }
  39121. /* now should convert to base64 when stored and then decode with read */
  39122. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  39123. BIO_flush(bio);
  39124. sz = sizeof(out);
  39125. XMEMSET(out, 0, sz);
  39126. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  39127. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  39128. BIO_clear_flags(bio64, ~0);
  39129. BIO_set_retry_read(bio);
  39130. BIO_free_all(bio); /* frees bio64s also */
  39131. bio = NULL;
  39132. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  39133. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39134. BIO_free(bio);
  39135. #endif
  39136. return EXPECT_RESULT();
  39137. }
  39138. static int test_wolfSSL_BIO_printf(void)
  39139. {
  39140. EXPECT_DECLS;
  39141. #if defined(OPENSSL_ALL)
  39142. BIO* bio = NULL;
  39143. int sz = 7;
  39144. char msg[] = "TLS 1.3 for the world";
  39145. char out[60];
  39146. char expected[] = "TLS 1.3 for the world : sz = 7";
  39147. XMEMSET(out, 0, sizeof(out));
  39148. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39149. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39150. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39151. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39152. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39153. BIO_free(bio);
  39154. #endif
  39155. return EXPECT_RESULT();
  39156. }
  39157. static int test_wolfSSL_BIO_f_md(void)
  39158. {
  39159. EXPECT_DECLS;
  39160. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39161. BIO* bio = NULL;
  39162. BIO* mem = NULL;
  39163. char msg[] = "message to hash";
  39164. char out[60];
  39165. EVP_MD_CTX* ctx = NULL;
  39166. const unsigned char testKey[] =
  39167. {
  39168. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39169. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39170. 0x0b, 0x0b, 0x0b, 0x0b
  39171. };
  39172. const char testData[] = "Hi There";
  39173. const unsigned char testResult[] =
  39174. {
  39175. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39176. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39177. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39178. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39179. };
  39180. const unsigned char expectedHash[] =
  39181. {
  39182. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39183. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39184. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39185. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39186. };
  39187. const unsigned char emptyHash[] =
  39188. {
  39189. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39190. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39191. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39192. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39193. };
  39194. unsigned char check[sizeof(testResult) + 1];
  39195. size_t checkSz = -1;
  39196. EVP_PKEY* key = NULL;
  39197. XMEMSET(out, 0, sizeof(out));
  39198. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39199. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39200. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39201. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39202. /* should not be able to write/read yet since just digest wrapper and no
  39203. * data is passing through the bio */
  39204. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  39205. ExpectIntEQ(BIO_pending(bio), 0);
  39206. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39207. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  39208. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39209. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39210. BIO_reset(bio);
  39211. /* append BIO mem to bio in order to read/write */
  39212. ExpectNotNull(bio = BIO_push(bio, mem));
  39213. XMEMSET(out, 0, sizeof(out));
  39214. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39215. ExpectIntEQ(BIO_pending(bio), 16);
  39216. /* this just reads the message and does not hash it (gets calls final) */
  39217. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39218. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39219. /* create a message digest using BIO */
  39220. XMEMSET(out, 0, sizeof(out));
  39221. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39222. ExpectIntEQ(BIO_pending(mem), 16);
  39223. ExpectIntEQ(BIO_pending(bio), 16);
  39224. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39225. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39226. BIO_free(bio);
  39227. bio = NULL;
  39228. BIO_free(mem);
  39229. mem = NULL;
  39230. /* test with HMAC */
  39231. XMEMSET(out, 0, sizeof(out));
  39232. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39233. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39234. BIO_get_md_ctx(bio, &ctx);
  39235. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  39236. (int)sizeof(testKey)));
  39237. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39238. ExpectNotNull(bio = BIO_push(bio, mem));
  39239. BIO_write(bio, testData, (int)strlen(testData));
  39240. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  39241. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  39242. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39243. EVP_PKEY_free(key);
  39244. BIO_free(bio);
  39245. BIO_free(mem);
  39246. #endif
  39247. return EXPECT_RESULT();
  39248. }
  39249. static int test_wolfSSL_BIO_up_ref(void)
  39250. {
  39251. EXPECT_DECLS;
  39252. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39253. BIO* bio = NULL;
  39254. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39255. ExpectIntEQ(BIO_up_ref(NULL), 0);
  39256. ExpectIntEQ(BIO_up_ref(bio), 1);
  39257. BIO_free(bio);
  39258. ExpectIntEQ(BIO_up_ref(bio), 1);
  39259. BIO_free(bio);
  39260. BIO_free(bio);
  39261. #endif
  39262. return EXPECT_RESULT();
  39263. }
  39264. static int test_wolfSSL_BIO_reset(void)
  39265. {
  39266. EXPECT_DECLS;
  39267. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39268. BIO* bio = NULL;
  39269. byte buf[16];
  39270. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  39271. (word32)XSTRLEN("secure your data")));
  39272. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  39273. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39274. XMEMSET(buf, 0, 16);
  39275. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  39276. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39277. /* You cannot write to MEM BIO with read-only mode. */
  39278. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39279. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  39280. XMEMSET(buf, 0, 16);
  39281. ExpectIntEQ(BIO_reset(bio), 0);
  39282. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  39283. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39284. BIO_free(bio);
  39285. #endif
  39286. return EXPECT_RESULT();
  39287. }
  39288. #endif /* !NO_BIO */
  39289. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39290. /* test that the callback arg is correct */
  39291. static int certCbArg = 0;
  39292. static int clientCertCb(WOLFSSL* ssl, void* arg)
  39293. {
  39294. if (ssl == NULL || arg != &certCbArg)
  39295. return 0;
  39296. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39297. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39298. return 0;
  39299. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39300. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39301. return 0;
  39302. return 1;
  39303. }
  39304. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  39305. {
  39306. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  39307. return TEST_SUCCESS;
  39308. }
  39309. /**
  39310. * This is only done because test_client_nofail has no way to stop
  39311. * certificate and key loading
  39312. */
  39313. static int clientCertClearCb(WOLFSSL* ssl)
  39314. {
  39315. /* Clear the loaded certs to force the callbacks to set them up */
  39316. SSL_certs_clear(ssl);
  39317. return TEST_SUCCESS;
  39318. }
  39319. static int serverCertCb(WOLFSSL* ssl, void* arg)
  39320. {
  39321. if (ssl == NULL || arg != &certCbArg)
  39322. return 0;
  39323. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39325. return 0;
  39326. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39327. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39328. return 0;
  39329. return 1;
  39330. }
  39331. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  39332. {
  39333. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  39334. return TEST_SUCCESS;
  39335. }
  39336. /**
  39337. * This is only done because test_server_nofail has no way to stop
  39338. * certificate and key loading
  39339. */
  39340. static int serverCertClearCb(WOLFSSL* ssl)
  39341. {
  39342. /* Clear the loaded certs to force the callbacks to set them up */
  39343. SSL_certs_clear(ssl);
  39344. return TEST_SUCCESS;
  39345. }
  39346. #endif
  39347. static int test_wolfSSL_cert_cb(void)
  39348. {
  39349. EXPECT_DECLS;
  39350. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39351. test_ssl_cbf func_cb_client;
  39352. test_ssl_cbf func_cb_server;
  39353. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39354. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  39355. func_cb_client.ctx_ready = clientCertSetupCb;
  39356. func_cb_client.ssl_ready = clientCertClearCb;
  39357. func_cb_server.ctx_ready = serverCertSetupCb;
  39358. func_cb_server.ssl_ready = serverCertClearCb;
  39359. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  39360. &func_cb_server, NULL), TEST_SUCCESS);
  39361. #endif
  39362. return EXPECT_RESULT();
  39363. }
  39364. static int test_wolfSSL_SESSION(void)
  39365. {
  39366. EXPECT_DECLS;
  39367. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39368. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39369. !defined(NO_SESSION_CACHE)
  39370. WOLFSSL* ssl = NULL;
  39371. WOLFSSL_CTX* ctx = NULL;
  39372. WOLFSSL_SESSION* sess = NULL;
  39373. WOLFSSL_SESSION* sess_copy = NULL;
  39374. #ifdef OPENSSL_EXTRA
  39375. #ifdef HAVE_EXT_CACHE
  39376. unsigned char* sessDer = NULL;
  39377. unsigned char* ptr = NULL;
  39378. int sz;
  39379. #endif
  39380. const unsigned char context[] = "user app context";
  39381. unsigned int contextSz = (unsigned int)sizeof(context);
  39382. #endif
  39383. int ret, err;
  39384. SOCKET_T sockfd;
  39385. tcp_ready ready;
  39386. func_args server_args;
  39387. THREAD_TYPE serverThread;
  39388. char msg[80];
  39389. const char* sendGET = "GET";
  39390. /* TLS v1.3 requires session tickets */
  39391. /* CHACHA and POLY1305 required for myTicketEncCb */
  39392. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  39393. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  39394. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  39395. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  39396. #else
  39397. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39398. #endif
  39399. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  39400. WOLFSSL_FILETYPE_PEM));
  39401. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  39402. WOLFSSL_FILETYPE_PEM));
  39403. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  39404. WOLFSSL_SUCCESS);
  39405. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39406. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  39407. #endif
  39408. #ifdef HAVE_SESSION_TICKET
  39409. /* Use session tickets, for ticket tests below */
  39410. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  39411. #endif
  39412. XMEMSET(&server_args, 0, sizeof(func_args));
  39413. #ifdef WOLFSSL_TIRTOS
  39414. fdOpenSession(Task_self());
  39415. #endif
  39416. StartTCP();
  39417. InitTcpReady(&ready);
  39418. #if defined(USE_WINDOWS_API)
  39419. /* use RNG to get random port if using windows */
  39420. ready.port = GetRandomPort();
  39421. #endif
  39422. server_args.signal = &ready;
  39423. start_thread(test_server_nofail, &server_args, &serverThread);
  39424. wait_tcp_ready(&server_args);
  39425. /* client connection */
  39426. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39427. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  39428. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39429. #ifdef WOLFSSL_ASYNC_CRYPT
  39430. err = 0; /* Reset error */
  39431. #endif
  39432. do {
  39433. #ifdef WOLFSSL_ASYNC_CRYPT
  39434. if (err == WC_PENDING_E) {
  39435. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39436. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39437. }
  39438. #endif
  39439. ret = wolfSSL_connect(ssl);
  39440. err = wolfSSL_get_error(ssl, 0);
  39441. } while (err == WC_PENDING_E);
  39442. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  39443. #ifdef WOLFSSL_ASYNC_CRYPT
  39444. err = 0; /* Reset error */
  39445. #endif
  39446. do {
  39447. #ifdef WOLFSSL_ASYNC_CRYPT
  39448. if (err == WC_PENDING_E) {
  39449. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39450. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39451. }
  39452. #endif
  39453. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  39454. err = wolfSSL_get_error(ssl, 0);
  39455. } while (err == WC_PENDING_E);
  39456. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  39457. #ifdef WOLFSSL_ASYNC_CRYPT
  39458. err = 0; /* Reset error */
  39459. #endif
  39460. do {
  39461. #ifdef WOLFSSL_ASYNC_CRYPT
  39462. if (err == WC_PENDING_E) {
  39463. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39464. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39465. }
  39466. #endif
  39467. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  39468. err = wolfSSL_get_error(ssl, 0);
  39469. } while (err == WC_PENDING_E);
  39470. ExpectIntEQ(ret, 23);
  39471. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  39472. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  39473. #ifdef HAVE_EXT_CACHE
  39474. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  39475. * HAVE_EXT_CACHE we get new objects each time */
  39476. #endif
  39477. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39478. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  39479. sess = wolfSSL_get_session(ssl);
  39480. #ifdef OPENSSL_EXTRA
  39481. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  39482. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  39483. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  39484. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  39485. #ifdef HAVE_SESSION_TICKET
  39486. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  39487. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  39488. SESSION_TICKET_HINT_DEFAULT);
  39489. #else
  39490. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  39491. #endif
  39492. #else
  39493. (void)sess;
  39494. #endif /* OPENSSL_EXTRA */
  39495. /* Retain copy of the session for later testing */
  39496. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  39497. wolfSSL_shutdown(ssl);
  39498. wolfSSL_free(ssl); ssl = NULL;
  39499. join_thread(serverThread);
  39500. FreeTcpReady(&ready);
  39501. #ifdef WOLFSSL_TIRTOS
  39502. fdOpenSession(Task_self());
  39503. #endif
  39504. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  39505. {
  39506. X509 *x509 = NULL;
  39507. char buf[30];
  39508. int bufSz = 0;
  39509. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  39510. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  39511. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  39512. sizeof(buf))), 0);
  39513. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  39514. if (bufSz == 7) {
  39515. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  39516. }
  39517. if (bufSz == 16) {
  39518. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  39519. }
  39520. }
  39521. #endif
  39522. #ifdef HAVE_EXT_CACHE
  39523. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  39524. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39525. sess_copy = NULL;
  39526. #endif
  39527. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  39528. /* get session from DER and update the timeout */
  39529. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  39530. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  39531. wolfSSL_SESSION_free(sess); sess = NULL;
  39532. sess = NULL;
  39533. ptr = sessDer;
  39534. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  39535. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  39536. (const unsigned char**)&ptr, sz));
  39537. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  39538. sessDer = NULL;
  39539. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  39540. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39541. #endif
  39542. /* successful set session test */
  39543. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39544. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  39545. #ifdef HAVE_SESSION_TICKET
  39546. /* Test set/get session ticket */
  39547. {
  39548. const char* ticket = "This is a session ticket";
  39549. char buf[64] = {0};
  39550. word32 bufSz = (word32)sizeof(buf);
  39551. ExpectIntEQ(SSL_SUCCESS,
  39552. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  39553. (word32)XSTRLEN(ticket)));
  39554. ExpectIntEQ(SSL_SUCCESS,
  39555. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  39556. ExpectStrEQ(ticket, buf);
  39557. }
  39558. #endif
  39559. #ifdef OPENSSL_EXTRA
  39560. /* session timeout case */
  39561. /* make the session to be expired */
  39562. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  39563. XSLEEP_MS(1200);
  39564. /* SSL_set_session should reject specified session but return success
  39565. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  39566. */
  39567. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39568. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  39569. #else
  39570. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  39571. #endif
  39572. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39573. #ifdef WOLFSSL_SESSION_ID_CTX
  39574. /* fail case with miss match session context IDs (use compatibility API) */
  39575. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  39576. SSL_SUCCESS);
  39577. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39578. wolfSSL_free(ssl); ssl = NULL;
  39579. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  39580. SSL_FAILURE);
  39581. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  39582. SSL_SUCCESS);
  39583. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39584. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39585. #endif
  39586. #endif /* OPENSSL_EXTRA */
  39587. wolfSSL_free(ssl);
  39588. wolfSSL_SESSION_free(sess);
  39589. wolfSSL_CTX_free(ctx);
  39590. #endif
  39591. return EXPECT_RESULT();
  39592. }
  39593. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39594. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39595. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  39596. !defined(WOLFSSL_NO_TLS12)
  39597. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  39598. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  39599. {
  39600. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  39601. /* returns previous timeout value */
  39602. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  39603. #else
  39604. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  39605. #endif
  39606. }
  39607. /* set the session to timeout in a second */
  39608. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  39609. {
  39610. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  39611. }
  39612. /* store the client side session from the first successful connection */
  39613. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  39614. {
  39615. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  39616. NULL); /* ref count 1 */
  39617. }
  39618. /* wait till session is expired then set it in the WOLFSSL struct for use */
  39619. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  39620. {
  39621. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  39622. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39623. WOLFSSL_SUCCESS);
  39624. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  39625. }
  39626. /* set expired session in the WOLFSSL struct for use */
  39627. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  39628. {
  39629. XSLEEP_MS(1200); /* wait a second for session to expire */
  39630. /* set the expired session, call to set session fails but continuing on
  39631. after failure should be handled here */
  39632. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39633. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39634. WOLFSSL_SUCCESS);
  39635. #else
  39636. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  39637. WOLFSSL_SUCCESS);
  39638. #endif
  39639. }
  39640. /* check that the expired session was not reused */
  39641. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  39642. {
  39643. /* since the session has expired it should not have been reused */
  39644. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  39645. }
  39646. #endif
  39647. static int test_wolfSSL_SESSION_expire_downgrade(void)
  39648. {
  39649. EXPECT_DECLS;
  39650. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39651. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39652. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  39653. !defined(WOLFSSL_NO_TLS12)
  39654. WOLFSSL_CTX* ctx = NULL;
  39655. callback_functions server_cbf, client_cbf;
  39656. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  39657. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  39658. /* force server side to use TLS 1.2 */
  39659. server_cbf.ctx = ctx;
  39660. server_cbf.method = wolfTLSv1_2_server_method;
  39661. client_cbf.method = wolfSSLv23_client_method;
  39662. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39663. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  39664. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  39665. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39666. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39667. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39668. /* set the previously created session and wait till expired */
  39669. server_cbf.ctx = ctx;
  39670. client_cbf.method = wolfSSLv23_client_method;
  39671. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39672. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  39673. client_cbf.on_result =
  39674. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  39675. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39676. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39677. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39678. /* set the previously created expired session */
  39679. server_cbf.ctx = ctx;
  39680. client_cbf.method = wolfSSLv23_client_method;
  39681. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  39682. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  39683. client_cbf.on_result =
  39684. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  39685. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  39686. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  39687. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  39688. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  39689. wolfSSL_CTX_free(ctx);
  39690. #endif
  39691. return EXPECT_RESULT();
  39692. }
  39693. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  39694. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39695. static int clientSessRemCountMalloc = 0;
  39696. static int serverSessRemCountMalloc = 0;
  39697. static int clientSessRemCountFree = 0;
  39698. static int serverSessRemCountFree = 0;
  39699. static WOLFSSL_CTX* serverSessCtx = NULL;
  39700. static WOLFSSL_SESSION* serverSess = NULL;
  39701. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39702. !defined(NO_SESSION_CACHE_REF)
  39703. static WOLFSSL_CTX* clientSessCtx = NULL;
  39704. static WOLFSSL_SESSION* clientSess = NULL;
  39705. #endif
  39706. static int serverSessRemIdx = 3;
  39707. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  39708. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  39709. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  39710. {
  39711. int* side;
  39712. (void)ctx;
  39713. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  39714. if (side != NULL) {
  39715. if (*side == WOLFSSL_CLIENT_END)
  39716. clientSessRemCountFree++;
  39717. else
  39718. serverSessRemCountFree++;
  39719. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  39720. }
  39721. }
  39722. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  39723. {
  39724. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  39725. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  39726. !defined(NO_SESSION_CACHE_REF)
  39727. {
  39728. EXPECT_DECLS;
  39729. /* Allow downgrade, set min version, and disable TLS 1.3.
  39730. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  39731. * reference to the session cache. But with WOLFSSL_TLS13 and without
  39732. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  39733. * the session in the cache. In this case we need to downgrade to
  39734. * previous versions to just use the legacy session ID field. */
  39735. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  39736. SSL_SUCCESS);
  39737. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  39738. SSL_SUCCESS);
  39739. return EXPECT_RESULT();
  39740. }
  39741. #else
  39742. return TEST_SUCCESS;
  39743. #endif
  39744. }
  39745. static int SessRemSslSetupCb(WOLFSSL* ssl)
  39746. {
  39747. EXPECT_DECLS;
  39748. int* side;
  39749. if (SSL_is_server(ssl)) {
  39750. side = &sessRemCtx_Server;
  39751. serverSessRemCountMalloc++;
  39752. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  39753. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  39754. SSL_SUCCESS);
  39755. }
  39756. else {
  39757. side = &sessRemCtx_Client;
  39758. clientSessRemCountMalloc++;
  39759. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39760. !defined(NO_SESSION_CACHE_REF)
  39761. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  39762. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  39763. SSL_SUCCESS);
  39764. #endif
  39765. }
  39766. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  39767. serverSessRemIdx, side), SSL_SUCCESS);
  39768. return EXPECT_RESULT();
  39769. }
  39770. #endif
  39771. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  39772. {
  39773. EXPECT_DECLS;
  39774. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  39775. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39776. /* Check that the remove callback gets called for external data in a
  39777. * session object */
  39778. test_ssl_cbf func_cb;
  39779. XMEMSET(&func_cb, 0, sizeof(func_cb));
  39780. func_cb.ctx_ready = SessRemCtxSetupCb;
  39781. func_cb.on_result = SessRemSslSetupCb;
  39782. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  39783. NULL), TEST_SUCCESS);
  39784. /* Both should have been allocated */
  39785. ExpectIntEQ(clientSessRemCountMalloc, 1);
  39786. ExpectIntEQ(serverSessRemCountMalloc, 1);
  39787. /* This should not be called yet. Session wasn't evicted from cache yet. */
  39788. ExpectIntEQ(clientSessRemCountFree, 0);
  39789. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39790. !defined(NO_SESSION_CACHE_REF)
  39791. /* Force a cache lookup */
  39792. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39793. /* Force a cache update */
  39794. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  39795. /* This should set the timeout to 0 and call the remove callback from within
  39796. * the session cache. */
  39797. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  39798. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39799. ExpectIntEQ(clientSessRemCountFree, 1);
  39800. #endif
  39801. /* Server session is in the cache so ex_data isn't free'd with the SSL
  39802. * object */
  39803. ExpectIntEQ(serverSessRemCountFree, 0);
  39804. /* Force a cache lookup */
  39805. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39806. /* Force a cache update */
  39807. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  39808. /* This should set the timeout to 0 and call the remove callback from within
  39809. * the session cache. */
  39810. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  39811. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39812. ExpectIntEQ(serverSessRemCountFree, 1);
  39813. /* Need to free the references that we kept */
  39814. SSL_CTX_free(serverSessCtx);
  39815. SSL_SESSION_free(serverSess);
  39816. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39817. !defined(NO_SESSION_CACHE_REF)
  39818. SSL_CTX_free(clientSessCtx);
  39819. SSL_SESSION_free(clientSess);
  39820. #endif
  39821. #endif
  39822. return EXPECT_RESULT();
  39823. }
  39824. static int test_wolfSSL_ticket_keys(void)
  39825. {
  39826. EXPECT_DECLS;
  39827. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  39828. !defined(NO_WOLFSSL_SERVER)
  39829. WOLFSSL_CTX* ctx = NULL;
  39830. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  39831. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  39832. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  39833. WOLFSSL_FAILURE);
  39834. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  39835. WOLFSSL_FAILURE);
  39836. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  39837. WOLFSSL_FAILURE);
  39838. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  39839. WOLFSSL_FAILURE);
  39840. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39841. WOLFSSL_FAILURE);
  39842. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39843. WOLFSSL_FAILURE);
  39844. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39845. WOLFSSL_FAILURE);
  39846. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  39847. WOLFSSL_FAILURE);
  39848. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  39849. WOLFSSL_FAILURE);
  39850. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  39851. WOLFSSL_FAILURE);
  39852. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  39853. WOLFSSL_FAILURE);
  39854. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39855. WOLFSSL_FAILURE);
  39856. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39857. WOLFSSL_FAILURE);
  39858. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39859. WOLFSSL_FAILURE);
  39860. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39861. WOLFSSL_SUCCESS);
  39862. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39863. WOLFSSL_SUCCESS);
  39864. wolfSSL_CTX_free(ctx);
  39865. #endif
  39866. return EXPECT_RESULT();
  39867. }
  39868. #ifndef NO_BIO
  39869. static int test_wolfSSL_d2i_PUBKEY(void)
  39870. {
  39871. EXPECT_DECLS;
  39872. #if defined(OPENSSL_EXTRA)
  39873. BIO* bio = NULL;
  39874. EVP_PKEY* pkey = NULL;
  39875. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39876. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  39877. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  39878. /* RSA PUBKEY test */
  39879. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  39880. sizeof_client_keypub_der_2048), 0);
  39881. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39882. EVP_PKEY_free(pkey);
  39883. pkey = NULL;
  39884. #endif
  39885. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  39886. /* ECC PUBKEY test */
  39887. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  39888. sizeof_ecc_clikeypub_der_256), 0);
  39889. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39890. EVP_PKEY_free(pkey);
  39891. pkey = NULL;
  39892. #endif
  39893. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  39894. /* DSA PUBKEY test */
  39895. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  39896. sizeof_dsa_pub_key_der_2048), 0);
  39897. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39898. EVP_PKEY_free(pkey);
  39899. pkey = NULL;
  39900. #endif
  39901. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  39902. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  39903. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39904. (HAVE_FIPS_VERSION > 2))
  39905. /* DH PUBKEY test */
  39906. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  39907. sizeof_dh_pub_key_der_2048), 0);
  39908. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39909. EVP_PKEY_free(pkey);
  39910. pkey = NULL;
  39911. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39912. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  39913. BIO_free(bio);
  39914. (void)pkey;
  39915. #endif
  39916. return EXPECT_RESULT();
  39917. }
  39918. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  39919. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  39920. {
  39921. EXPECT_DECLS;
  39922. BIO* bio = NULL;
  39923. EVP_PKEY* pkey = NULL;
  39924. WOLFSSL_CTX* ctx = NULL;
  39925. #if defined(WOLFSSL_KEY_GEN)
  39926. unsigned char buff[4096];
  39927. unsigned char* bufPtr = buff;
  39928. #endif
  39929. /* test creating new EVP_PKEY with bad arg */
  39930. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  39931. /* test loading RSA key using BIO */
  39932. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39933. {
  39934. XFILE file = XBADFILE;
  39935. const char* fname = "./certs/server-key.der";
  39936. size_t sz;
  39937. byte* buf = NULL;
  39938. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  39939. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39940. ExpectTrue((sz = XFTELL(file)) != 0);
  39941. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39942. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39943. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  39944. if (file != XBADFILE) {
  39945. XFCLOSE(file);
  39946. }
  39947. /* Test using BIO new mem and loading DER private key */
  39948. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39949. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39950. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39951. BIO_free(bio);
  39952. bio = NULL;
  39953. EVP_PKEY_free(pkey);
  39954. pkey = NULL;
  39955. }
  39956. #endif
  39957. /* test loading ECC key using BIO */
  39958. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  39959. {
  39960. XFILE file = XBADFILE;
  39961. const char* fname = "./certs/ecc-key.der";
  39962. size_t sz;
  39963. byte* buf = NULL;
  39964. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  39965. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39966. ExpectTrue((sz = XFTELL(file)) != 0);
  39967. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39968. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39969. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  39970. if (file != XBADFILE)
  39971. XFCLOSE(file);
  39972. /* Test using BIO new mem and loading DER private key */
  39973. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39974. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39975. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39976. BIO_free(bio);
  39977. bio = NULL;
  39978. EVP_PKEY_free(pkey);
  39979. pkey = NULL;
  39980. }
  39981. #endif
  39982. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39983. #ifndef NO_WOLFSSL_SERVER
  39984. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  39985. #else
  39986. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  39987. #endif
  39988. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  39989. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  39990. {
  39991. RSA* rsa = NULL;
  39992. /* Tests bad parameters */
  39993. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  39994. /* RSA not set yet, expecting to fail*/
  39995. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  39996. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  39997. /* set RSA using bio*/
  39998. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  39999. sizeof_client_key_der_2048), 0);
  40000. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  40001. ExpectNotNull(rsa);
  40002. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  40003. /* i2d RSAprivate key tests */
  40004. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  40005. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  40006. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40007. sizeof_client_key_der_2048);
  40008. bufPtr -= sizeof_client_key_der_2048;
  40009. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40010. sizeof_client_key_der_2048), 0);
  40011. bufPtr = NULL;
  40012. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40013. sizeof_client_key_der_2048);
  40014. ExpectNotNull(bufPtr);
  40015. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40016. sizeof_client_key_der_2048), 0);
  40017. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  40018. RSA_free(rsa);
  40019. rsa = RSA_new();
  40020. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  40021. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  40022. RSA_free(rsa);
  40023. }
  40024. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  40025. SSL_CTX_free(ctx);
  40026. ctx = NULL;
  40027. BIO_free(bio);
  40028. bio = NULL;
  40029. return EXPECT_RESULT();
  40030. }
  40031. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  40032. #endif /* !NO_BIO */
  40033. static int test_wolfSSL_sk_GENERAL_NAME(void)
  40034. {
  40035. EXPECT_DECLS;
  40036. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40037. !defined(NO_RSA)
  40038. X509* x509 = NULL;
  40039. GENERAL_NAME* gn = NULL;
  40040. unsigned char buf[4096];
  40041. const unsigned char* bufPt = NULL;
  40042. int bytes = 0;
  40043. int i;
  40044. int j;
  40045. XFILE f = XBADFILE;
  40046. STACK_OF(GENERAL_NAME)* sk = NULL;
  40047. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40048. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40049. if (f != XBADFILE)
  40050. XFCLOSE(f);
  40051. for (j = 0; j < 2; ++j) {
  40052. bufPt = buf;
  40053. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40054. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40055. NID_subject_alt_name, NULL, NULL));
  40056. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  40057. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  40058. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  40059. if (gn != NULL) {
  40060. switch (gn->type) {
  40061. case GEN_DNS:
  40062. fprintf(stderr, "found type GEN_DNS\n");
  40063. break;
  40064. case GEN_EMAIL:
  40065. fprintf(stderr, "found type GEN_EMAIL\n");
  40066. break;
  40067. case GEN_URI:
  40068. fprintf(stderr, "found type GEN_URI\n");
  40069. break;
  40070. }
  40071. }
  40072. }
  40073. X509_free(x509);
  40074. x509 = NULL;
  40075. if (j == 0) {
  40076. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40077. }
  40078. else {
  40079. /*
  40080. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  40081. * it was supposed to. This is a regression test for that bug.
  40082. */
  40083. GENERAL_NAMES_free(sk);
  40084. }
  40085. sk = NULL;
  40086. }
  40087. #endif
  40088. return EXPECT_RESULT();
  40089. }
  40090. static int test_wolfSSL_GENERAL_NAME_print(void)
  40091. {
  40092. EXPECT_DECLS;
  40093. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  40094. X509* x509 = NULL;
  40095. GENERAL_NAME* gn = NULL;
  40096. unsigned char buf[4096];
  40097. const unsigned char* bufPt = NULL;
  40098. int bytes;
  40099. XFILE f = XBADFILE;
  40100. STACK_OF(GENERAL_NAME)* sk = NULL;
  40101. BIO* out = NULL;
  40102. unsigned char outbuf[128];
  40103. X509_EXTENSION* ext = NULL;
  40104. AUTHORITY_INFO_ACCESS* aia = NULL;
  40105. ACCESS_DESCRIPTION* ad = NULL;
  40106. ASN1_IA5STRING *dnsname = NULL;
  40107. const unsigned char v4Addr[] = {192,168,53,1};
  40108. const unsigned char v6Addr[] =
  40109. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  40110. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  40111. const unsigned char email[] =
  40112. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  40113. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  40114. const char* dnsStr = "DNS:example.com";
  40115. const char* uriStr = "URI:http://127.0.0.1:22220";
  40116. const char* v4addStr = "IP Address:192.168.53.1";
  40117. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  40118. const char* emailStr = "email:info@wolfssl.com";
  40119. const char* othrStr = "othername:<unsupported>";
  40120. const char* x400Str = "X400Name:<unsupported>";
  40121. const char* ediStr = "EdiPartyName:<unsupported>";
  40122. /* BIO to output */
  40123. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  40124. /* test for NULL param */
  40125. gn = NULL;
  40126. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  40127. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  40128. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  40129. /* test for GEN_DNS */
  40130. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40131. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40132. if (f != XBADFILE) {
  40133. XFCLOSE(f);
  40134. f = XBADFILE;
  40135. }
  40136. bufPt = buf;
  40137. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40138. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40139. NID_subject_alt_name, NULL, NULL));
  40140. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  40141. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40142. XMEMSET(outbuf, 0, sizeof(outbuf));
  40143. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40144. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40145. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40146. gn = NULL;
  40147. sk = NULL;
  40148. X509_free(x509);
  40149. x509 = NULL;
  40150. /* Lets test for setting as well. */
  40151. ExpectNotNull(gn = GENERAL_NAME_new());
  40152. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  40153. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  40154. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  40155. dnsname = NULL;
  40156. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40157. XMEMSET(outbuf, 0, sizeof(outbuf));
  40158. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40159. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40160. GENERAL_NAME_free(gn);
  40161. /* test for GEN_URI */
  40162. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40163. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40164. if (f != XBADFILE) {
  40165. XFCLOSE(f);
  40166. f = XBADFILE;
  40167. }
  40168. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  40169. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40170. ext));
  40171. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  40172. if (ad != NULL) {
  40173. gn = ad->location;
  40174. }
  40175. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40176. gn = NULL;
  40177. XMEMSET(outbuf,0,sizeof(outbuf));
  40178. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40179. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  40180. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40181. aia = NULL;
  40182. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40183. ExpectNotNull(aia);
  40184. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  40185. aia = NULL;
  40186. X509_free(x509);
  40187. x509 = NULL;
  40188. /* test for GEN_IPADD */
  40189. /* ip v4 address */
  40190. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40191. if (gn != NULL) {
  40192. gn->type = GEN_IPADD;
  40193. if (gn->d.iPAddress != NULL) {
  40194. gn->d.iPAddress->length = sizeof(v4Addr);
  40195. }
  40196. }
  40197. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  40198. sizeof(v4Addr)), 1);
  40199. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40200. XMEMSET(outbuf,0,sizeof(outbuf));
  40201. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40202. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  40203. GENERAL_NAME_free(gn);
  40204. gn = NULL;
  40205. /* ip v6 address */
  40206. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40207. if (gn != NULL) {
  40208. gn->type = GEN_IPADD;
  40209. if (gn->d.iPAddress != NULL) {
  40210. gn->d.iPAddress->length = sizeof(v6Addr);
  40211. }
  40212. }
  40213. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  40214. sizeof(v6Addr)), 1);
  40215. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40216. XMEMSET(outbuf,0,sizeof(outbuf));
  40217. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40218. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  40219. GENERAL_NAME_free(gn);
  40220. gn = NULL;
  40221. /* test for GEN_EMAIL */
  40222. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40223. if (gn != NULL) {
  40224. gn->type = GEN_EMAIL;
  40225. if (gn->d.rfc822Name != NULL) {
  40226. gn->d.rfc822Name->length = sizeof(email);
  40227. }
  40228. }
  40229. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  40230. 1);
  40231. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40232. XMEMSET(outbuf,0,sizeof(outbuf));
  40233. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40234. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  40235. GENERAL_NAME_free(gn);
  40236. gn = NULL;
  40237. /* test for GEN_OTHERNAME */
  40238. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40239. if (gn != NULL) {
  40240. gn->type = GEN_OTHERNAME;
  40241. }
  40242. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40243. XMEMSET(outbuf,0,sizeof(outbuf));
  40244. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40245. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  40246. GENERAL_NAME_free(gn);
  40247. gn = NULL;
  40248. /* test for GEN_X400 */
  40249. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40250. if (gn != NULL) {
  40251. gn->type = GEN_X400;
  40252. }
  40253. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40254. XMEMSET(outbuf,0,sizeof(outbuf));
  40255. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40256. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  40257. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40258. if (gn != NULL) {
  40259. gn->type = GEN_IA5;
  40260. }
  40261. GENERAL_NAME_free(gn);
  40262. gn = NULL;
  40263. /* test for GEN_EDIPARTY */
  40264. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40265. if (gn != NULL) {
  40266. gn->type = GEN_EDIPARTY;
  40267. }
  40268. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40269. XMEMSET(outbuf,0,sizeof(outbuf));
  40270. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40271. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  40272. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40273. if (gn != NULL) {
  40274. gn->type = GEN_IA5;
  40275. }
  40276. GENERAL_NAME_free(gn);
  40277. gn = NULL;
  40278. BIO_free(out);
  40279. #endif /* OPENSSL_ALL */
  40280. return EXPECT_RESULT();
  40281. }
  40282. static int test_wolfSSL_sk_DIST_POINT(void)
  40283. {
  40284. EXPECT_DECLS;
  40285. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40286. !defined(NO_RSA)
  40287. X509* x509 = NULL;
  40288. unsigned char buf[4096];
  40289. const unsigned char* bufPt;
  40290. int bytes;
  40291. int i;
  40292. int j;
  40293. XFILE f = XBADFILE;
  40294. DIST_POINT* dp;
  40295. DIST_POINT_NAME* dpn;
  40296. GENERAL_NAME* gn;
  40297. ASN1_IA5STRING* uri;
  40298. STACK_OF(DIST_POINT)* dps = NULL;
  40299. STACK_OF(GENERAL_NAME)* gns = NULL;
  40300. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  40301. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  40302. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40303. if (f != XBADFILE)
  40304. XFCLOSE(f);
  40305. bufPt = buf;
  40306. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40307. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  40308. NID_crl_distribution_points, NULL, NULL));
  40309. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  40310. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  40311. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  40312. ExpectNotNull(dpn = dp->distpoint);
  40313. /* this should be type 0, fullname */
  40314. ExpectIntEQ(dpn->type, 0);
  40315. ExpectNotNull(gns = dp->distpoint->name.fullname);
  40316. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40317. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  40318. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  40319. ExpectIntEQ(gn->type, GEN_URI);
  40320. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  40321. ExpectNotNull(uri->data);
  40322. ExpectIntGT(uri->length, 0);
  40323. }
  40324. }
  40325. X509_free(x509);
  40326. CRL_DIST_POINTS_free(dps);
  40327. #endif
  40328. return EXPECT_RESULT();
  40329. }
  40330. static int test_wolfSSL_verify_mode(void)
  40331. {
  40332. EXPECT_DECLS;
  40333. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40334. WOLFSSL* ssl = NULL;
  40335. WOLFSSL_CTX* ctx = NULL;
  40336. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40337. ExpectNotNull(ssl = SSL_new(ctx));
  40338. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40339. SSL_free(ssl);
  40340. ssl = NULL;
  40341. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40342. ExpectNotNull(ssl = SSL_new(ctx));
  40343. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40344. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40345. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40346. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  40347. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40348. SSL_free(ssl);
  40349. ssl = NULL;
  40350. wolfSSL_CTX_set_verify(ctx,
  40351. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40352. ExpectNotNull(ssl = SSL_new(ctx));
  40353. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40354. ExpectIntEQ(SSL_get_verify_mode(ssl),
  40355. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40356. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  40357. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40358. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40359. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40360. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40361. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40362. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40363. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40364. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  40365. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  40366. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  40367. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  40368. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  40369. #endif
  40370. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40371. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40372. SSL_free(ssl);
  40373. SSL_CTX_free(ctx);
  40374. #endif
  40375. return EXPECT_RESULT();
  40376. }
  40377. static int test_wolfSSL_verify_depth(void)
  40378. {
  40379. EXPECT_DECLS;
  40380. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40381. WOLFSSL* ssl = NULL;
  40382. WOLFSSL_CTX* ctx = NULL;
  40383. long depth;
  40384. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40385. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  40386. ExpectNotNull(ssl = SSL_new(ctx));
  40387. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  40388. SSL_free(ssl);
  40389. ssl = NULL;
  40390. SSL_CTX_set_verify_depth(ctx, -1);
  40391. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  40392. SSL_CTX_set_verify_depth(ctx, 2);
  40393. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  40394. ExpectNotNull(ssl = SSL_new(ctx));
  40395. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  40396. SSL_free(ssl);
  40397. SSL_CTX_free(ctx);
  40398. #endif
  40399. return EXPECT_RESULT();
  40400. }
  40401. static int test_wolfSSL_verify_result(void)
  40402. {
  40403. EXPECT_DECLS;
  40404. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  40405. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  40406. WOLFSSL* ssl = NULL;
  40407. WOLFSSL_CTX* ctx = NULL;
  40408. long result = 0xDEADBEEF;
  40409. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  40410. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40411. ExpectNotNull(ssl = SSL_new(ctx));
  40412. wolfSSL_set_verify_result(ssl, result);
  40413. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  40414. SSL_free(ssl);
  40415. SSL_CTX_free(ctx);
  40416. #endif
  40417. return EXPECT_RESULT();
  40418. }
  40419. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40420. static void sslMsgCb(int w, int version, int type, const void* buf,
  40421. size_t sz, SSL* ssl, void* arg)
  40422. {
  40423. int i;
  40424. unsigned char* pt = (unsigned char*)buf;
  40425. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  40426. (w)?"Writing":"Reading", (int)sz, version, type);
  40427. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  40428. fprintf(stderr, "\n");
  40429. (void)ssl;
  40430. (void)arg;
  40431. }
  40432. #endif /* OPENSSL_EXTRA */
  40433. static int test_wolfSSL_msg_callback(void)
  40434. {
  40435. EXPECT_DECLS;
  40436. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40437. WOLFSSL* ssl = NULL;
  40438. WOLFSSL_CTX* ctx = NULL;
  40439. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40440. ExpectNotNull(ssl = SSL_new(ctx));
  40441. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  40442. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  40443. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  40444. SSL_free(ssl);
  40445. SSL_CTX_free(ctx);
  40446. #endif
  40447. return EXPECT_RESULT();
  40448. }
  40449. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  40450. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40451. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40452. static void binary_dump(void *ptr, int size)
  40453. {
  40454. #ifdef WOLFSSL_EVP_PRINT
  40455. int i = 0;
  40456. unsigned char *p = (unsigned char *) ptr;
  40457. fprintf(stderr, "{");
  40458. while ((p != NULL) && (i < size)) {
  40459. if ((i % 8) == 0) {
  40460. fprintf(stderr, "\n");
  40461. fprintf(stderr, " ");
  40462. }
  40463. fprintf(stderr, "0x%02x, ", p[i]);
  40464. i++;
  40465. }
  40466. fprintf(stderr, "\n};\n");
  40467. #else
  40468. (void) ptr;
  40469. (void) size;
  40470. #endif
  40471. }
  40472. static int last_val = 0x0f;
  40473. static int check_result(unsigned char *data, int len)
  40474. {
  40475. int i;
  40476. for ( ; len; ) {
  40477. last_val = (last_val + 1) % 16;
  40478. for (i = 0; i < 16; len--, i++, data++)
  40479. if (*data != last_val) {
  40480. return -1;
  40481. }
  40482. }
  40483. return 0;
  40484. }
  40485. static int r_offset;
  40486. static int w_offset;
  40487. static void init_offset(void)
  40488. {
  40489. r_offset = 0;
  40490. w_offset = 0;
  40491. }
  40492. static void get_record(unsigned char *data, unsigned char *buf, int len)
  40493. {
  40494. XMEMCPY(buf, data+r_offset, len);
  40495. r_offset += len;
  40496. }
  40497. static void set_record(unsigned char *data, unsigned char *buf, int len)
  40498. {
  40499. XMEMCPY(data+w_offset, buf, len);
  40500. w_offset += len;
  40501. }
  40502. static void set_plain(unsigned char *plain, int rec)
  40503. {
  40504. int i, j;
  40505. unsigned char *p = plain;
  40506. #define BLOCKSZ 16
  40507. for (i=0; i<(rec/BLOCKSZ); i++) {
  40508. for (j=0; j<BLOCKSZ; j++)
  40509. *p++ = (i % 16);
  40510. }
  40511. }
  40512. #endif
  40513. static int test_wolfSSL_EVP_Cipher_extra(void)
  40514. {
  40515. EXPECT_DECLS;
  40516. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40517. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40518. /* aes128-cbc, keylen=16, ivlen=16 */
  40519. byte aes128_cbc_key[] = {
  40520. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40521. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40522. };
  40523. byte aes128_cbc_iv[] = {
  40524. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  40525. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  40526. };
  40527. /* teset data size table */
  40528. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  40529. int test_drive2[] = {8, 3, 8, 512, 0};
  40530. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  40531. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  40532. int test_drive_len[100];
  40533. int ret = 0;
  40534. EVP_CIPHER_CTX *evp = NULL;
  40535. int ilen = 0;
  40536. int klen = 0;
  40537. int i, j;
  40538. const EVP_CIPHER *type;
  40539. byte *iv;
  40540. byte *key;
  40541. int ivlen;
  40542. int keylen;
  40543. #define RECORDS 16
  40544. #define BUFFSZ 512
  40545. byte plain [BUFFSZ * RECORDS];
  40546. byte cipher[BUFFSZ * RECORDS];
  40547. byte inb[BUFFSZ];
  40548. byte outb[BUFFSZ+16];
  40549. int outl = 0;
  40550. int inl;
  40551. iv = aes128_cbc_iv;
  40552. ivlen = sizeof(aes128_cbc_iv);
  40553. key = aes128_cbc_key;
  40554. keylen = sizeof(aes128_cbc_key);
  40555. type = EVP_aes_128_cbc();
  40556. set_plain(plain, BUFFSZ * RECORDS);
  40557. SSL_library_init();
  40558. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  40559. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40560. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40561. klen = EVP_CIPHER_CTX_key_length(evp);
  40562. if (klen > 0 && keylen != klen) {
  40563. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40564. }
  40565. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40566. if (ilen > 0 && ivlen != ilen) {
  40567. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40568. }
  40569. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40570. for (j = 0; j<RECORDS; j++)
  40571. {
  40572. inl = BUFFSZ;
  40573. get_record(plain, inb, inl);
  40574. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  40575. set_record(cipher, outb, outl);
  40576. }
  40577. for (i = 0; test_drive[i]; i++) {
  40578. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40579. init_offset();
  40580. test_drive_len[i] = 0;
  40581. for (j = 0; test_drive[i][j]; j++)
  40582. {
  40583. inl = test_drive[i][j];
  40584. test_drive_len[i] += inl;
  40585. get_record(plain, inb, inl);
  40586. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  40587. 0);
  40588. /* output to cipher buffer, so that following Dec test can detect
  40589. if any error */
  40590. set_record(cipher, outb, outl);
  40591. }
  40592. EVP_CipherFinal(evp, outb, &outl);
  40593. if (outl > 0)
  40594. set_record(cipher, outb, outl);
  40595. }
  40596. for (i = 0; test_drive[i]; i++) {
  40597. last_val = 0x0f;
  40598. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  40599. init_offset();
  40600. for (j = 0; test_drive[i][j]; j++) {
  40601. inl = test_drive[i][j];
  40602. get_record(cipher, inb, inl);
  40603. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  40604. 0);
  40605. binary_dump(outb, outl);
  40606. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  40607. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  40608. }
  40609. ret = EVP_CipherFinal(evp, outb, &outl);
  40610. binary_dump(outb, outl);
  40611. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  40612. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  40613. ExpectTrue(ret);
  40614. }
  40615. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  40616. EVP_CIPHER_CTX_free(evp);
  40617. evp = NULL;
  40618. /* Do an extra test to verify correct behavior with empty input. */
  40619. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  40620. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40621. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40622. klen = EVP_CIPHER_CTX_key_length(evp);
  40623. if (klen > 0 && keylen != klen) {
  40624. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40625. }
  40626. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40627. if (ilen > 0 && ivlen != ilen) {
  40628. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40629. }
  40630. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40631. /* outl should be set to 0 after passing NULL, 0 for input args. */
  40632. outl = -1;
  40633. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  40634. ExpectIntEQ(outl, 0);
  40635. EVP_CIPHER_CTX_free(evp);
  40636. #endif /* test_EVP_Cipher */
  40637. return EXPECT_RESULT();
  40638. }
  40639. static int test_wolfSSL_PEM_read_DHparams(void)
  40640. {
  40641. EXPECT_DECLS;
  40642. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  40643. !defined(NO_FILESYSTEM)
  40644. DH* dh = NULL;
  40645. XFILE fp = XBADFILE;
  40646. unsigned char derOut[300];
  40647. unsigned char* derOutBuf = derOut;
  40648. int derOutSz = 0;
  40649. unsigned char derExpected[300];
  40650. int derExpectedSz = 0;
  40651. XMEMSET(derOut, 0, sizeof(derOut));
  40652. XMEMSET(derExpected, 0, sizeof(derExpected));
  40653. /* open DH param file, read into DH struct */
  40654. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  40655. /* bad args */
  40656. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  40657. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  40658. /* good args */
  40659. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40660. if (fp != XBADFILE) {
  40661. XFCLOSE(fp);
  40662. fp = XBADFILE;
  40663. }
  40664. /* read in certs/dh2048.der for comparison against exported params */
  40665. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  40666. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  40667. fp), 0);
  40668. if (fp != XBADFILE) {
  40669. XFCLOSE(fp);
  40670. fp = XBADFILE;
  40671. }
  40672. /* export DH back to DER and compare */
  40673. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  40674. ExpectIntEQ(derOutSz, derExpectedSz);
  40675. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  40676. DH_free(dh);
  40677. dh = NULL;
  40678. /* Test parsing with X9.42 header */
  40679. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  40680. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40681. if (fp != XBADFILE)
  40682. XFCLOSE(fp);
  40683. DH_free(dh);
  40684. #endif
  40685. return EXPECT_RESULT();
  40686. }
  40687. static int test_wolfSSL_X509_get_serialNumber(void)
  40688. {
  40689. EXPECT_DECLS;
  40690. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  40691. ASN1_INTEGER* a = NULL;
  40692. BIGNUM* bn = NULL;
  40693. X509* x509 = NULL;
  40694. char *serialHex = NULL;
  40695. byte serial[3];
  40696. int serialSz;
  40697. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40698. SSL_FILETYPE_PEM));
  40699. ExpectNotNull(a = X509_get_serialNumber(x509));
  40700. /* check on value of ASN1 Integer */
  40701. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  40702. a = NULL;
  40703. /* test setting serial number and then retrieving it */
  40704. ExpectNotNull(a = ASN1_INTEGER_new());
  40705. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  40706. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  40707. serialSz = sizeof(serial);
  40708. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  40709. WOLFSSL_SUCCESS);
  40710. ExpectIntEQ(serialSz, 1);
  40711. ExpectIntEQ(serial[0], 3);
  40712. ASN1_INTEGER_free(a);
  40713. a = NULL;
  40714. /* test setting serial number with 0's in it */
  40715. serial[0] = 0x01;
  40716. serial[1] = 0x00;
  40717. serial[2] = 0x02;
  40718. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  40719. if (a != NULL) {
  40720. a->data[0] = ASN_INTEGER;
  40721. a->data[1] = sizeof(serial);
  40722. XMEMCPY(&a->data[2], serial, sizeof(serial));
  40723. a->length = sizeof(serial) + 2;
  40724. }
  40725. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  40726. XMEMSET(serial, 0, sizeof(serial));
  40727. serialSz = sizeof(serial);
  40728. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  40729. WOLFSSL_SUCCESS);
  40730. ExpectIntEQ(serialSz, 3);
  40731. ExpectIntEQ(serial[0], 0x01);
  40732. ExpectIntEQ(serial[1], 0x00);
  40733. ExpectIntEQ(serial[2], 0x02);
  40734. ASN1_INTEGER_free(a);
  40735. a = NULL;
  40736. X509_free(x509); /* free's a */
  40737. ExpectNotNull(serialHex = BN_bn2hex(bn));
  40738. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  40739. ExpectStrEQ(serialHex, "01");
  40740. #else
  40741. ExpectStrEQ(serialHex, "1");
  40742. #endif
  40743. OPENSSL_free(serialHex);
  40744. ExpectIntEQ(BN_get_word(bn), 1);
  40745. BN_free(bn);
  40746. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  40747. ExpectNotNull(a = ASN1_INTEGER_new());
  40748. if (a != NULL) {
  40749. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  40750. DYNAMIC_TYPE_OPENSSL));
  40751. a->isDynamic = 1;
  40752. ASN1_INTEGER_free(a);
  40753. }
  40754. #endif
  40755. return EXPECT_RESULT();
  40756. }
  40757. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  40758. {
  40759. EXPECT_DECLS;
  40760. #if defined(OPENSSL_EXTRA)
  40761. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  40762. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  40763. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  40764. #endif
  40765. return EXPECT_RESULT();
  40766. }
  40767. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  40768. {
  40769. EXPECT_DECLS;
  40770. #if defined(OPENSSL_EXTRA)
  40771. #define MAX_HEXSTR_BUFSZ 9
  40772. #define NUM_CASES 5
  40773. struct Output {
  40774. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  40775. long ret;
  40776. };
  40777. int i;
  40778. int j;
  40779. const char* inputs[NUM_CASES] = {
  40780. "aabcd1357e",
  40781. "01:12:23:34:a5:b6:c7:d8:e9",
  40782. ":01:02",
  40783. "012",
  40784. ":ab:ac:d"
  40785. };
  40786. struct Output expectedOutputs[NUM_CASES] = {
  40787. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  40788. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  40789. {{0x01, 0x02}, 2},
  40790. {{0x00}, 0},
  40791. {{0x00}, 0}
  40792. };
  40793. long len = 0;
  40794. unsigned char* returnedBuf = NULL;
  40795. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  40796. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  40797. if (returnedBuf == NULL) {
  40798. ExpectIntEQ(expectedOutputs[i].ret, 0);
  40799. continue;
  40800. }
  40801. ExpectIntEQ(expectedOutputs[i].ret, len);
  40802. for (j = 0; j < len; ++j) {
  40803. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  40804. }
  40805. OPENSSL_free(returnedBuf);
  40806. returnedBuf = NULL;
  40807. }
  40808. #endif
  40809. return EXPECT_RESULT();
  40810. }
  40811. static int test_wolfSSL_X509_CA_num(void)
  40812. {
  40813. EXPECT_DECLS;
  40814. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40815. defined(HAVE_ECC) && !defined(NO_RSA)
  40816. WOLFSSL_X509_STORE *store = NULL;
  40817. WOLFSSL_X509 *x509_1 = NULL;
  40818. WOLFSSL_X509 *x509_2 = NULL;
  40819. int ca_num = 0;
  40820. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  40821. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40822. WOLFSSL_FILETYPE_PEM));
  40823. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  40824. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  40825. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  40826. WOLFSSL_FILETYPE_PEM));
  40827. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  40828. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  40829. wolfSSL_X509_free(x509_1);
  40830. wolfSSL_X509_free(x509_2);
  40831. wolfSSL_X509_STORE_free(store);
  40832. #endif
  40833. return EXPECT_RESULT();
  40834. }
  40835. static int test_wolfSSL_X509_check_ca(void)
  40836. {
  40837. EXPECT_DECLS;
  40838. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40839. WOLFSSL_X509 *x509 = NULL;
  40840. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  40841. WOLFSSL_FILETYPE_PEM));
  40842. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  40843. wolfSSL_X509_free(x509);
  40844. #endif
  40845. return EXPECT_RESULT();
  40846. }
  40847. static int test_wolfSSL_X509_check_ip_asc(void)
  40848. {
  40849. EXPECT_DECLS;
  40850. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40851. WOLFSSL_X509 *x509 = NULL;
  40852. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  40853. WOLFSSL_FILETYPE_PEM));
  40854. #if 0
  40855. /* TODO: add cert gen for testing positive case */
  40856. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  40857. #endif
  40858. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  40859. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  40860. wolfSSL_X509_free(x509);
  40861. #endif
  40862. return EXPECT_RESULT();
  40863. }
  40864. static int test_wolfSSL_make_cert(void)
  40865. {
  40866. EXPECT_DECLS;
  40867. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  40868. defined(WOLFSSL_CERT_EXT)
  40869. int ret;
  40870. Cert cert;
  40871. CertName name;
  40872. RsaKey key;
  40873. WC_RNG rng;
  40874. byte der[FOURK_BUF];
  40875. word32 idx;
  40876. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  40877. #ifdef OPENSSL_EXTRA
  40878. const unsigned char* pt;
  40879. int certSz;
  40880. X509* x509 = NULL;
  40881. X509_NAME* x509name;
  40882. X509_NAME_ENTRY* entry;
  40883. ASN1_STRING* entryValue;
  40884. #endif
  40885. XMEMSET(&name, 0, sizeof(CertName));
  40886. /* set up cert name */
  40887. XMEMCPY(name.country, "US", sizeof("US"));
  40888. name.countryEnc = CTC_PRINTABLE;
  40889. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  40890. name.stateEnc = CTC_UTF8;
  40891. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  40892. name.localityEnc = CTC_UTF8;
  40893. XMEMCPY(name.sur, "Test", sizeof("Test"));
  40894. name.surEnc = CTC_UTF8;
  40895. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  40896. name.orgEnc = CTC_UTF8;
  40897. XMEMCPY(name.unit, "Development", sizeof("Development"));
  40898. name.unitEnc = CTC_UTF8;
  40899. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  40900. name.commonNameEnc = CTC_UTF8;
  40901. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  40902. name.serialDevEnc = CTC_PRINTABLE;
  40903. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  40904. name.userIdEnc = CTC_PRINTABLE;
  40905. #ifdef WOLFSSL_MULTI_ATTRIB
  40906. #if CTC_MAX_ATTRIB > 2
  40907. {
  40908. NameAttrib* n;
  40909. n = &name.name[0];
  40910. n->id = ASN_DOMAIN_COMPONENT;
  40911. n->type = CTC_UTF8;
  40912. n->sz = sizeof("com");
  40913. XMEMCPY(n->value, "com", sizeof("com"));
  40914. n = &name.name[1];
  40915. n->id = ASN_DOMAIN_COMPONENT;
  40916. n->type = CTC_UTF8;
  40917. n->sz = sizeof("wolfssl");
  40918. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  40919. }
  40920. #endif
  40921. #endif /* WOLFSSL_MULTI_ATTRIB */
  40922. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  40923. #ifndef HAVE_FIPS
  40924. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  40925. #else
  40926. ExpectIntEQ(wc_InitRng(&rng), 0);
  40927. #endif
  40928. /* load test RSA key */
  40929. idx = 0;
  40930. #if defined(USE_CERT_BUFFERS_1024)
  40931. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  40932. sizeof_server_key_der_1024), 0);
  40933. #elif defined(USE_CERT_BUFFERS_2048)
  40934. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  40935. sizeof_server_key_der_2048), 0);
  40936. #else
  40937. /* error case, no RSA key loaded, happens later */
  40938. (void)idx;
  40939. #endif
  40940. XMEMSET(&cert, 0 , sizeof(Cert));
  40941. ExpectIntEQ(wc_InitCert(&cert), 0);
  40942. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  40943. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  40944. cert.serialSz = (int)sizeof(mySerial);
  40945. cert.isCA = 1;
  40946. #ifndef NO_SHA256
  40947. cert.sigType = CTC_SHA256wRSA;
  40948. #else
  40949. cert.sigType = CTC_SHAwRSA;
  40950. #endif
  40951. /* add SKID from the Public Key */
  40952. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  40953. /* add AKID from the Public Key */
  40954. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  40955. ret = 0;
  40956. do {
  40957. #if defined(WOLFSSL_ASYNC_CRYPT)
  40958. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  40959. #endif
  40960. if (ret >= 0) {
  40961. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  40962. }
  40963. } while (ret == WC_PENDING_E);
  40964. ExpectIntGT(ret, 0);
  40965. #ifdef OPENSSL_EXTRA
  40966. /* der holds a certificate with DC's now check X509 parsing of it */
  40967. certSz = ret;
  40968. pt = der;
  40969. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  40970. ExpectNotNull(x509name = X509_get_subject_name(x509));
  40971. #ifdef WOLFSSL_MULTI_ATTRIB
  40972. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40973. -1)), 5);
  40974. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40975. idx)), 6);
  40976. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40977. idx)), -1);
  40978. #endif /* WOLFSSL_MULTI_ATTRIB */
  40979. /* compare DN at index 0 */
  40980. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  40981. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40982. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  40983. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  40984. #ifndef WOLFSSL_MULTI_ATTRIB
  40985. /* compare Serial Number */
  40986. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  40987. -1)), 7);
  40988. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  40989. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40990. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  40991. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  40992. #endif
  40993. #ifdef WOLFSSL_MULTI_ATTRIB
  40994. /* get first and second DC and compare result */
  40995. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  40996. -1)), 5);
  40997. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  40998. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  40999. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  41000. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41001. idx)), 6);
  41002. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41003. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41004. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  41005. #endif /* WOLFSSL_MULTI_ATTRIB */
  41006. /* try invalid index locations for regression test and sanity check */
  41007. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  41008. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  41009. X509_free(x509);
  41010. #endif /* OPENSSL_EXTRA */
  41011. wc_FreeRsaKey(&key);
  41012. wc_FreeRng(&rng);
  41013. #endif
  41014. return EXPECT_RESULT();
  41015. }
  41016. static int test_wolfSSL_X509_get_version(void)
  41017. {
  41018. EXPECT_DECLS;
  41019. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41020. WOLFSSL_X509 *x509 = NULL;
  41021. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41022. WOLFSSL_FILETYPE_PEM));
  41023. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  41024. wolfSSL_X509_free(x509);
  41025. #endif
  41026. return EXPECT_RESULT();
  41027. }
  41028. #if defined(OPENSSL_ALL)
  41029. static int test_wolfSSL_sk_CIPHER_description(void)
  41030. {
  41031. EXPECT_DECLS;
  41032. #if !defined(NO_RSA)
  41033. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41034. int i;
  41035. int numCiphers = 0;
  41036. const SSL_METHOD *method = NULL;
  41037. const SSL_CIPHER *cipher = NULL;
  41038. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41039. SSL_CTX *ctx = NULL;
  41040. SSL *ssl = NULL;
  41041. char buf[256];
  41042. char test_str[9] = "0000000";
  41043. const char badStr[] = "unknown";
  41044. const char certPath[] = "./certs/client-cert.pem";
  41045. XMEMSET(buf, 0, sizeof(buf));
  41046. ExpectNotNull(method = TLSv1_2_client_method());
  41047. ExpectNotNull(ctx = SSL_CTX_new(method));
  41048. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41049. SSL_CTX_set_verify_depth(ctx, 4);
  41050. SSL_CTX_set_options(ctx, flags);
  41051. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41052. WOLFSSL_SUCCESS);
  41053. ExpectNotNull(ssl = SSL_new(ctx));
  41054. /* SSL_get_ciphers returns a stack of all configured ciphers
  41055. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41056. */
  41057. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41058. /* loop through the amount of supportedCiphers */
  41059. numCiphers = sk_num(supportedCiphers);
  41060. for (i = 0; i < numCiphers; ++i) {
  41061. int j;
  41062. /* sk_value increments "sk->data.cipher->cipherOffset".
  41063. * wolfSSL_sk_CIPHER_description sets the description for
  41064. * the cipher based on the provided offset.
  41065. */
  41066. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41067. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41068. }
  41069. /* Search cipher description string for "unknown" descriptor */
  41070. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41071. int k = 0;
  41072. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41073. test_str[k] = badStr[k];
  41074. j++;
  41075. k++;
  41076. }
  41077. }
  41078. /* Fail if test_str == badStr == "unknown" */
  41079. ExpectStrNE(test_str,badStr);
  41080. }
  41081. SSL_free(ssl);
  41082. SSL_CTX_free(ctx);
  41083. #endif
  41084. return EXPECT_RESULT();
  41085. }
  41086. static int test_wolfSSL_get_ciphers_compat(void)
  41087. {
  41088. EXPECT_DECLS;
  41089. #if !defined(NO_RSA)
  41090. const SSL_METHOD *method = NULL;
  41091. const char certPath[] = "./certs/client-cert.pem";
  41092. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41093. SSL_CTX *ctx = NULL;
  41094. WOLFSSL *ssl = NULL;
  41095. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41096. ExpectNotNull(method = SSLv23_client_method());
  41097. ExpectNotNull(ctx = SSL_CTX_new(method));
  41098. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41099. SSL_CTX_set_verify_depth(ctx, 4);
  41100. SSL_CTX_set_options(ctx, flags);
  41101. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41102. WOLFSSL_SUCCESS);
  41103. ExpectNotNull(ssl = SSL_new(ctx));
  41104. /* Test Bad NULL input */
  41105. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  41106. /* Test for Good input */
  41107. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41108. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41109. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41110. SSL_free(ssl);
  41111. SSL_CTX_free(ctx);
  41112. #endif
  41113. return EXPECT_RESULT();
  41114. }
  41115. static int test_wolfSSL_X509_PUBKEY_get(void)
  41116. {
  41117. EXPECT_DECLS;
  41118. WOLFSSL_X509_PUBKEY pubkey;
  41119. WOLFSSL_X509_PUBKEY* key;
  41120. WOLFSSL_EVP_PKEY evpkey ;
  41121. WOLFSSL_EVP_PKEY* evpPkey;
  41122. WOLFSSL_EVP_PKEY* retEvpPkey;
  41123. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41124. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41125. key = &pubkey;
  41126. evpPkey = &evpkey;
  41127. evpPkey->type = WOLFSSL_SUCCESS;
  41128. key->pkey = evpPkey;
  41129. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41130. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41131. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41132. key->pkey = NULL;
  41133. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41134. return EXPECT_RESULT();
  41135. }
  41136. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41137. {
  41138. EXPECT_DECLS;
  41139. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41140. DSA *dsa = NULL;
  41141. DSA *setDsa = NULL;
  41142. EVP_PKEY *pkey = NULL;
  41143. EVP_PKEY *set1Pkey = NULL;
  41144. SHA_CTX sha;
  41145. byte signature[DSA_SIG_SIZE];
  41146. byte hash[WC_SHA_DIGEST_SIZE];
  41147. word32 bytes;
  41148. int answer;
  41149. #ifdef USE_CERT_BUFFERS_1024
  41150. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41151. int dsaKeySz = sizeof_dsa_key_der_1024;
  41152. byte tmp[ONEK_BUF];
  41153. XMEMSET(tmp, 0, sizeof(tmp));
  41154. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41155. bytes = dsaKeySz;
  41156. #elif defined(USE_CERT_BUFFERS_2048)
  41157. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41158. int dsaKeySz = sizeof_dsa_key_der_2048;
  41159. byte tmp[TWOK_BUF];
  41160. XMEMSET(tmp, 0, sizeof(tmp));
  41161. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41162. bytes = dsaKeySz;
  41163. #else
  41164. byte tmp[TWOK_BUF];
  41165. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41166. int dsaKeySz;
  41167. XFILE fp = XBADFILE;
  41168. XMEMSET(tmp, 0, sizeof(tmp));
  41169. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  41170. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  41171. if (fp != XBADFILE)
  41172. XFCLOSE(fp);
  41173. #endif /* END USE_CERT_BUFFERS_1024 */
  41174. /* Create hash to later Sign and Verify */
  41175. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41176. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41177. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41178. /* Initialize pkey with der format dsa key */
  41179. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41180. (long)dsaKeySz));
  41181. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41182. /* Should Fail: NULL argument */
  41183. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41184. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41185. /* Should Pass: Initialized pkey argument */
  41186. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41187. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41188. #ifdef USE_CERT_BUFFERS_1024
  41189. ExpectIntEQ(DSA_bits(dsa), 1024);
  41190. #else
  41191. ExpectIntEQ(DSA_bits(dsa), 2048);
  41192. #endif
  41193. /* Sign */
  41194. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41195. /* Verify. */
  41196. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41197. WOLFSSL_SUCCESS);
  41198. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41199. /* Should Fail: set1Pkey not initialized */
  41200. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41201. /* Initialize set1Pkey */
  41202. set1Pkey = EVP_PKEY_new();
  41203. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41204. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41205. WOLFSSL_SUCCESS);
  41206. /* Should Pass: set dsa into set1Pkey */
  41207. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41208. DSA_free(dsa);
  41209. DSA_free(setDsa);
  41210. EVP_PKEY_free(pkey);
  41211. EVP_PKEY_free(set1Pkey);
  41212. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41213. return EXPECT_RESULT();
  41214. } /* END test_EVP_PKEY_set1_get1_DSA */
  41215. static int test_wolfSSL_DSA_generate_parameters(void)
  41216. {
  41217. EXPECT_DECLS;
  41218. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41219. !defined(HAVE_FIPS)
  41220. DSA *dsa = NULL;
  41221. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  41222. NULL));
  41223. DSA_free(dsa);
  41224. #endif
  41225. return EXPECT_RESULT();
  41226. }
  41227. static int test_wolfSSL_DSA_SIG(void)
  41228. {
  41229. EXPECT_DECLS;
  41230. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41231. !defined(HAVE_FIPS)
  41232. DSA *dsa = NULL;
  41233. DSA *dsa2 = NULL;
  41234. DSA_SIG *sig = NULL;
  41235. const BIGNUM *p = NULL;
  41236. const BIGNUM *q = NULL;
  41237. const BIGNUM *g = NULL;
  41238. const BIGNUM *pub = NULL;
  41239. const BIGNUM *priv = NULL;
  41240. BIGNUM *dup_p = NULL;
  41241. BIGNUM *dup_q = NULL;
  41242. BIGNUM *dup_g = NULL;
  41243. BIGNUM *dup_pub = NULL;
  41244. BIGNUM *dup_priv = NULL;
  41245. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  41246. ExpectNotNull(dsa = DSA_new());
  41247. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  41248. NULL), 1);
  41249. ExpectIntEQ(DSA_generate_key(dsa), 1);
  41250. DSA_get0_pqg(dsa, &p, &q, &g);
  41251. DSA_get0_key(dsa, &pub, &priv);
  41252. ExpectNotNull(dup_p = BN_dup(p));
  41253. ExpectNotNull(dup_q = BN_dup(q));
  41254. ExpectNotNull(dup_g = BN_dup(g));
  41255. ExpectNotNull(dup_pub = BN_dup(pub));
  41256. ExpectNotNull(dup_priv = BN_dup(priv));
  41257. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  41258. ExpectNotNull(dsa2 = DSA_new());
  41259. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  41260. if (EXPECT_FAIL()) {
  41261. BN_free(dup_p);
  41262. BN_free(dup_q);
  41263. BN_free(dup_g);
  41264. }
  41265. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  41266. if (EXPECT_FAIL()) {
  41267. BN_free(dup_pub);
  41268. BN_free(dup_priv);
  41269. }
  41270. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  41271. DSA_free(dsa);
  41272. DSA_free(dsa2);
  41273. DSA_SIG_free(sig);
  41274. #endif
  41275. return EXPECT_RESULT();
  41276. }
  41277. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  41278. {
  41279. EXPECT_DECLS;
  41280. #ifdef HAVE_ECC
  41281. WOLFSSL_EC_KEY* ecKey = NULL;
  41282. WOLFSSL_EC_KEY* ecGet1 = NULL;
  41283. EVP_PKEY* pkey = NULL;
  41284. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41285. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41286. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  41287. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41288. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41289. /* Should fail since ecKey is empty */
  41290. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41291. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41292. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41293. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  41294. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  41295. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  41296. wolfSSL_EC_KEY_free(ecKey);
  41297. wolfSSL_EC_KEY_free(ecGet1);
  41298. EVP_PKEY_free(pkey);
  41299. #endif /* HAVE_ECC */
  41300. return EXPECT_RESULT();
  41301. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  41302. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  41303. {
  41304. EXPECT_DECLS;
  41305. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41306. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41307. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41308. DH *dh = NULL;
  41309. DH *setDh = NULL;
  41310. EVP_PKEY *pkey = NULL;
  41311. XFILE f = XBADFILE;
  41312. unsigned char buf[4096];
  41313. const unsigned char* pt = buf;
  41314. const char* dh2048 = "./certs/dh2048.der";
  41315. long len = 0;
  41316. int code = -1;
  41317. XMEMSET(buf, 0, sizeof(buf));
  41318. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  41319. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41320. if (f != XBADFILE)
  41321. XFCLOSE(f);
  41322. /* Load dh2048.der into DH with internal format */
  41323. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41324. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  41325. ExpectIntEQ(code, 0);
  41326. code = -1;
  41327. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41328. /* Set DH into PKEY */
  41329. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  41330. /* Get DH from PKEY */
  41331. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  41332. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  41333. ExpectIntEQ(code, 0);
  41334. EVP_PKEY_free(pkey);
  41335. DH_free(setDh);
  41336. DH_free(dh);
  41337. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  41338. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41339. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41340. return EXPECT_RESULT();
  41341. } /* END test_EVP_PKEY_set1_get1_DH */
  41342. static int test_wolfSSL_CTX_ctrl(void)
  41343. {
  41344. EXPECT_DECLS;
  41345. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41346. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41347. char caFile[] = "./certs/client-ca.pem";
  41348. char clientFile[] = "./certs/client-cert.pem";
  41349. SSL_CTX* ctx = NULL;
  41350. X509* x509 = NULL;
  41351. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41352. byte buf[6000];
  41353. char file[] = "./certs/dsaparams.pem";
  41354. XFILE f = XBADFILE;
  41355. int bytes;
  41356. BIO* bio = NULL;
  41357. DSA* dsa = NULL;
  41358. DH* dh = NULL;
  41359. #endif
  41360. #ifdef HAVE_ECC
  41361. WOLFSSL_EC_KEY* ecKey = NULL;
  41362. #endif
  41363. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41364. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  41365. WOLFSSL_FILETYPE_PEM));
  41366. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  41367. if (EXPECT_FAIL()) {
  41368. wolfSSL_X509_free(x509);
  41369. }
  41370. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  41371. WOLFSSL_FILETYPE_PEM));
  41372. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41373. /* Initialize DH */
  41374. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  41375. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  41376. if (f != XBADFILE)
  41377. XFCLOSE(f);
  41378. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  41379. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  41380. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  41381. #endif
  41382. #ifdef HAVE_ECC
  41383. /* Initialize WOLFSSL_EC_KEY */
  41384. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41385. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41386. #endif
  41387. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  41388. /* additional test of getting EVP_PKEY key size from X509
  41389. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  41390. * allowed with user RSA */
  41391. {
  41392. EVP_PKEY* pkey = NULL;
  41393. #if defined(HAVE_ECC)
  41394. X509* ecX509 = NULL;
  41395. #endif /* HAVE_ECC */
  41396. ExpectNotNull(pkey = X509_get_pubkey(x509));
  41397. /* current RSA key is 2048 bit (256 bytes) */
  41398. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  41399. EVP_PKEY_free(pkey);
  41400. pkey = NULL;
  41401. #if defined(HAVE_ECC)
  41402. #if defined(USE_CERT_BUFFERS_256)
  41403. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  41404. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  41405. SSL_FILETYPE_ASN1));
  41406. #else
  41407. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  41408. cliEccCertFile, SSL_FILETYPE_PEM));
  41409. #endif
  41410. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  41411. /* current ECC key is 256 bit (32 bytes) */
  41412. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  41413. X509_free(ecX509);
  41414. EVP_PKEY_free(pkey);
  41415. #endif /* HAVE_ECC */
  41416. }
  41417. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  41418. /* Tests should fail with passed in NULL pointer */
  41419. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  41420. SSL_FAILURE);
  41421. #if !defined(NO_DH) && !defined(NO_DSA)
  41422. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  41423. SSL_FAILURE);
  41424. #endif
  41425. #ifdef HAVE_ECC
  41426. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  41427. SSL_FAILURE);
  41428. #endif
  41429. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  41430. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  41431. */
  41432. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  41433. SSL_SUCCESS);
  41434. if (EXPECT_FAIL()) {
  41435. wolfSSL_X509_free(x509);
  41436. }
  41437. /* Test with SSL_CTRL_OPTIONS
  41438. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  41439. */
  41440. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  41441. NULL) == SSL_OP_NO_TLSv1);
  41442. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  41443. /* Test with SSL_CTRL_SET_TMP_DH
  41444. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  41445. */
  41446. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41447. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  41448. SSL_SUCCESS);
  41449. #endif
  41450. /* Test with SSL_CTRL_SET_TMP_ECDH
  41451. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  41452. */
  41453. #ifdef HAVE_ECC
  41454. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  41455. SSL_SUCCESS);
  41456. #endif
  41457. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41458. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  41459. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  41460. #endif
  41461. /* Test for min/max proto */
  41462. #ifndef WOLFSSL_NO_TLS12
  41463. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41464. 0, NULL), SSL_SUCCESS);
  41465. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41466. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41467. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  41468. #endif
  41469. #ifdef WOLFSSL_TLS13
  41470. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41471. 0, NULL), SSL_SUCCESS);
  41472. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41473. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  41474. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  41475. #ifndef WOLFSSL_NO_TLS12
  41476. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41477. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41478. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  41479. #endif
  41480. #endif
  41481. /* Cleanup and Pass */
  41482. #if !defined(NO_DH) && !defined(NO_DSA)
  41483. #ifndef NO_BIO
  41484. BIO_free(bio);
  41485. DSA_free(dsa);
  41486. DH_free(dh);
  41487. #endif
  41488. #endif
  41489. #ifdef HAVE_ECC
  41490. wolfSSL_EC_KEY_free(ecKey);
  41491. #endif
  41492. SSL_CTX_free(ctx);
  41493. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41494. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  41495. return EXPECT_RESULT();
  41496. }
  41497. static int test_wolfSSL_EVP_PKEY_assign(void)
  41498. {
  41499. EXPECT_DECLS;
  41500. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  41501. int type;
  41502. WOLFSSL_EVP_PKEY* pkey = NULL;
  41503. #ifndef NO_RSA
  41504. WOLFSSL_RSA* rsa = NULL;
  41505. #endif
  41506. #ifndef NO_DSA
  41507. WOLFSSL_DSA* dsa = NULL;
  41508. #endif
  41509. #ifdef HAVE_ECC
  41510. WOLFSSL_EC_KEY* ecKey = NULL;
  41511. #endif
  41512. #ifndef NO_RSA
  41513. type = EVP_PKEY_RSA;
  41514. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41515. ExpectNotNull(rsa = wolfSSL_RSA_new());
  41516. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  41517. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41518. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  41519. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  41520. if (EXPECT_FAIL()) {
  41521. wolfSSL_RSA_free(rsa);
  41522. }
  41523. wolfSSL_EVP_PKEY_free(pkey);
  41524. pkey = NULL;
  41525. #endif /* NO_RSA */
  41526. #ifndef NO_DSA
  41527. type = EVP_PKEY_DSA;
  41528. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41529. ExpectNotNull(dsa = wolfSSL_DSA_new());
  41530. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  41531. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41532. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  41533. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  41534. if (EXPECT_FAIL()) {
  41535. wolfSSL_DSA_free(dsa);
  41536. }
  41537. wolfSSL_EVP_PKEY_free(pkey);
  41538. pkey = NULL;
  41539. #endif /* NO_DSA */
  41540. #ifdef HAVE_ECC
  41541. type = EVP_PKEY_EC;
  41542. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41543. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41544. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  41545. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41546. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  41547. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  41548. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41549. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  41550. if (EXPECT_FAIL()) {
  41551. wolfSSL_EC_KEY_free(ecKey);
  41552. }
  41553. wolfSSL_EVP_PKEY_free(pkey);
  41554. pkey = NULL;
  41555. #endif /* HAVE_ECC */
  41556. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  41557. return EXPECT_RESULT();
  41558. }
  41559. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  41560. {
  41561. EXPECT_DECLS;
  41562. #if !defined(NO_DH) && \
  41563. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41564. XFILE f = XBADFILE;
  41565. unsigned char buf[4096];
  41566. const unsigned char* pt = buf;
  41567. const char* params1 = "./certs/dh2048.der";
  41568. long len = 0;
  41569. WOLFSSL_DH* dh = NULL;
  41570. WOLFSSL_EVP_PKEY* pkey = NULL;
  41571. XMEMSET(buf, 0, sizeof(buf));
  41572. /* Load DH parameters DER. */
  41573. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  41574. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41575. if (f != XBADFILE)
  41576. XFCLOSE(f);
  41577. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41578. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  41579. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41580. /* Bad cases */
  41581. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  41582. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  41583. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  41584. /* Good case */
  41585. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  41586. if (EXPECT_FAIL()) {
  41587. wolfSSL_DH_free(dh);
  41588. }
  41589. EVP_PKEY_free(pkey);
  41590. #endif
  41591. return EXPECT_RESULT();
  41592. }
  41593. static int test_wolfSSL_EVP_PKEY_base_id(void)
  41594. {
  41595. EXPECT_DECLS;
  41596. WOLFSSL_EVP_PKEY* pkey = NULL;
  41597. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41598. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  41599. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  41600. EVP_PKEY_free(pkey);
  41601. return EXPECT_RESULT();
  41602. }
  41603. static int test_wolfSSL_EVP_PKEY_id(void)
  41604. {
  41605. EXPECT_DECLS;
  41606. WOLFSSL_EVP_PKEY* pkey = NULL;
  41607. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41608. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  41609. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  41610. EVP_PKEY_free(pkey);
  41611. return EXPECT_RESULT();
  41612. }
  41613. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  41614. {
  41615. EXPECT_DECLS;
  41616. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  41617. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  41618. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  41619. ECC_MIN_KEY_SZ <= 256)
  41620. EVP_PKEY_CTX* ctx = NULL;
  41621. EVP_PKEY* pkey = NULL;
  41622. /* Test error conditions. */
  41623. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  41624. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  41625. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  41626. #ifndef NO_RSA
  41627. EVP_PKEY_CTX_free(ctx);
  41628. /* Parameter generation for RSA not supported yet. */
  41629. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  41630. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  41631. #endif
  41632. #ifdef HAVE_ECC
  41633. EVP_PKEY_CTX_free(ctx);
  41634. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  41635. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  41636. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  41637. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  41638. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  41639. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  41640. WOLFSSL_SUCCESS);
  41641. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41642. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  41643. #endif
  41644. EVP_PKEY_CTX_free(ctx);
  41645. EVP_PKEY_free(pkey);
  41646. #endif
  41647. return EXPECT_RESULT();
  41648. }
  41649. static int test_wolfSSL_EVP_PKEY_keygen(void)
  41650. {
  41651. EXPECT_DECLS;
  41652. WOLFSSL_EVP_PKEY* pkey = NULL;
  41653. EVP_PKEY_CTX* ctx = NULL;
  41654. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  41655. WOLFSSL_EVP_PKEY* params = NULL;
  41656. DH* dh = NULL;
  41657. const BIGNUM* pubkey = NULL;
  41658. const BIGNUM* privkey = NULL;
  41659. ASN1_INTEGER* asn1int = NULL;
  41660. unsigned int length = 0;
  41661. byte* derBuffer = NULL;
  41662. #endif
  41663. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41664. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41665. /* Bad cases */
  41666. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  41667. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  41668. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  41669. /* Good case */
  41670. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  41671. EVP_PKEY_CTX_free(ctx);
  41672. ctx = NULL;
  41673. EVP_PKEY_free(pkey);
  41674. pkey = NULL;
  41675. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  41676. /* Test DH keygen */
  41677. {
  41678. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  41679. ExpectNotNull(dh = DH_get_2048_256());
  41680. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  41681. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  41682. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41683. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  41684. DH_free(dh);
  41685. dh = NULL;
  41686. EVP_PKEY_CTX_free(ctx);
  41687. EVP_PKEY_free(params);
  41688. /* try exporting generated key to DER, to verify */
  41689. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  41690. DH_get0_key(dh, &pubkey, &privkey);
  41691. ExpectNotNull(pubkey);
  41692. ExpectNotNull(privkey);
  41693. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  41694. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  41695. ASN1_INTEGER_free(asn1int);
  41696. DH_free(dh);
  41697. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41698. EVP_PKEY_free(pkey);
  41699. }
  41700. #endif
  41701. return EXPECT_RESULT();
  41702. }
  41703. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  41704. {
  41705. EXPECT_DECLS;
  41706. WOLFSSL_EVP_PKEY* pkey = NULL;
  41707. EVP_PKEY_CTX *ctx = NULL;
  41708. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41709. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41710. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  41711. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  41712. EVP_PKEY_CTX_free(ctx);
  41713. EVP_PKEY_free(pkey);
  41714. return EXPECT_RESULT();
  41715. }
  41716. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  41717. {
  41718. EXPECT_DECLS;
  41719. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  41720. WOLFSSL_EVP_PKEY* pkey = NULL;
  41721. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41722. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  41723. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  41724. EVP_PKEY_free(pkey);
  41725. #endif
  41726. return EXPECT_RESULT();
  41727. }
  41728. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  41729. {
  41730. EXPECT_DECLS;
  41731. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  41732. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  41733. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  41734. !defined(NO_FILESYSTEM)
  41735. WOLFSSL_EVP_PKEY* params = NULL;
  41736. WOLFSSL_EVP_PKEY* copy = NULL;
  41737. DH* dh = NULL;
  41738. BIGNUM* p1;
  41739. BIGNUM* g1;
  41740. BIGNUM* q1;
  41741. BIGNUM* p2;
  41742. BIGNUM* g2;
  41743. BIGNUM* q2;
  41744. /* create DH with DH_get_2048_256 params */
  41745. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  41746. ExpectNotNull(dh = DH_get_2048_256());
  41747. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  41748. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  41749. (const BIGNUM**)&q1,
  41750. (const BIGNUM**)&g1);
  41751. DH_free(dh);
  41752. dh = NULL;
  41753. /* create DH with random generated DH params */
  41754. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  41755. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  41756. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  41757. DH_free(dh);
  41758. dh = NULL;
  41759. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  41760. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  41761. ExpectNotNull(dh->p);
  41762. ExpectNotNull(dh->g);
  41763. ExpectNotNull(dh->q);
  41764. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  41765. (const BIGNUM**)&q2,
  41766. (const BIGNUM**)&g2);
  41767. ExpectIntEQ(BN_cmp(p1, p2), 0);
  41768. ExpectIntEQ(BN_cmp(q1, q2), 0);
  41769. ExpectIntEQ(BN_cmp(g1, g2), 0);
  41770. DH_free(dh);
  41771. EVP_PKEY_free(copy);
  41772. EVP_PKEY_free(params);
  41773. #endif
  41774. return EXPECT_RESULT();
  41775. }
  41776. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  41777. {
  41778. EXPECT_DECLS;
  41779. WOLFSSL_EVP_PKEY* pkey = NULL;
  41780. EVP_PKEY_CTX* ctx = NULL;
  41781. int bits = 2048;
  41782. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41783. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41784. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  41785. WOLFSSL_SUCCESS);
  41786. EVP_PKEY_CTX_free(ctx);
  41787. EVP_PKEY_free(pkey);
  41788. return EXPECT_RESULT();
  41789. }
  41790. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  41791. {
  41792. EXPECT_DECLS;
  41793. /* This is large enough to be used for all key sizes */
  41794. byte key[AES_256_KEY_SIZE] = {0};
  41795. byte iv[AES_BLOCK_SIZE] = {0};
  41796. int i;
  41797. int nids[] = {
  41798. #ifdef HAVE_AES_CBC
  41799. NID_aes_128_cbc,
  41800. #endif
  41801. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41802. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41803. #ifdef HAVE_AESGCM
  41804. NID_aes_128_gcm,
  41805. #endif
  41806. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41807. #ifdef WOLFSSL_AES_COUNTER
  41808. NID_aes_128_ctr,
  41809. #endif
  41810. #ifndef NO_DES3
  41811. NID_des_cbc,
  41812. NID_des_ede3_cbc,
  41813. #endif
  41814. };
  41815. int iv_lengths[] = {
  41816. #ifdef HAVE_AES_CBC
  41817. AES_BLOCK_SIZE,
  41818. #endif
  41819. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41820. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41821. #ifdef HAVE_AESGCM
  41822. GCM_NONCE_MID_SZ,
  41823. #endif
  41824. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41825. #ifdef WOLFSSL_AES_COUNTER
  41826. AES_BLOCK_SIZE,
  41827. #endif
  41828. #ifndef NO_DES3
  41829. DES_BLOCK_SIZE,
  41830. DES_BLOCK_SIZE,
  41831. #endif
  41832. };
  41833. int nidsLen = (sizeof(nids)/sizeof(int));
  41834. for (i = 0; i < nidsLen; i++) {
  41835. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41836. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41837. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41838. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41839. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  41840. EVP_CIPHER_CTX_free(ctx);
  41841. }
  41842. return EXPECT_RESULT();
  41843. }
  41844. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  41845. {
  41846. EXPECT_DECLS;
  41847. byte key[AES_256_KEY_SIZE] = {0};
  41848. byte iv[AES_BLOCK_SIZE] = {0};
  41849. int i;
  41850. int nids[] = {
  41851. #ifdef HAVE_AES_CBC
  41852. NID_aes_128_cbc,
  41853. NID_aes_256_cbc,
  41854. #endif
  41855. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41856. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41857. #ifdef HAVE_AESGCM
  41858. NID_aes_128_gcm,
  41859. NID_aes_256_gcm,
  41860. #endif
  41861. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41862. #ifdef WOLFSSL_AES_COUNTER
  41863. NID_aes_128_ctr,
  41864. NID_aes_256_ctr,
  41865. #endif
  41866. #ifndef NO_DES3
  41867. NID_des_cbc,
  41868. NID_des_ede3_cbc,
  41869. #endif
  41870. };
  41871. int key_lengths[] = {
  41872. #ifdef HAVE_AES_CBC
  41873. AES_128_KEY_SIZE,
  41874. AES_256_KEY_SIZE,
  41875. #endif
  41876. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41877. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41878. #ifdef HAVE_AESGCM
  41879. AES_128_KEY_SIZE,
  41880. AES_256_KEY_SIZE,
  41881. #endif
  41882. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41883. #ifdef WOLFSSL_AES_COUNTER
  41884. AES_128_KEY_SIZE,
  41885. AES_256_KEY_SIZE,
  41886. #endif
  41887. #ifndef NO_DES3
  41888. DES_KEY_SIZE,
  41889. DES3_KEY_SIZE,
  41890. #endif
  41891. };
  41892. int nidsLen = (sizeof(nids)/sizeof(int));
  41893. for (i = 0; i < nidsLen; i++) {
  41894. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41895. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41896. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41897. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41898. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  41899. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  41900. WOLFSSL_SUCCESS);
  41901. EVP_CIPHER_CTX_free(ctx);
  41902. }
  41903. return EXPECT_RESULT();
  41904. }
  41905. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  41906. {
  41907. EXPECT_DECLS;
  41908. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  41909. int ivLen, keyLen;
  41910. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  41911. #ifdef HAVE_AESGCM
  41912. byte key[AES_128_KEY_SIZE] = {0};
  41913. byte iv[AES_BLOCK_SIZE] = {0};
  41914. const EVP_CIPHER *init = EVP_aes_128_gcm();
  41915. #else
  41916. byte key[DES3_KEY_SIZE] = {0};
  41917. byte iv[DES_BLOCK_SIZE] = {0};
  41918. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  41919. #endif
  41920. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41921. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41922. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  41923. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  41924. /* Bad cases */
  41925. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  41926. WOLFSSL_FAILURE);
  41927. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  41928. WOLFSSL_FAILURE);
  41929. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  41930. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  41931. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  41932. WOLFSSL_FAILURE);
  41933. /* Good case */
  41934. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  41935. EVP_CIPHER_CTX_free(ctx);
  41936. #endif
  41937. return EXPECT_RESULT();
  41938. }
  41939. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  41940. {
  41941. EXPECT_DECLS;
  41942. WOLFSSL_ENGINE* e = NULL;
  41943. int id = 0;
  41944. EVP_PKEY_CTX *ctx = NULL;
  41945. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  41946. EVP_PKEY_CTX_free(ctx);
  41947. return EXPECT_RESULT();
  41948. }
  41949. static int test_wolfSSL_EVP_rc4(void)
  41950. {
  41951. EXPECT_DECLS;
  41952. #if !defined(NO_RC4)
  41953. ExpectNotNull(wolfSSL_EVP_rc4());
  41954. #endif
  41955. return EXPECT_RESULT();
  41956. }
  41957. static int test_wolfSSL_EVP_enc_null(void)
  41958. {
  41959. EXPECT_DECLS;
  41960. ExpectNotNull(wolfSSL_EVP_enc_null());
  41961. return EXPECT_RESULT();
  41962. }
  41963. static int test_wolfSSL_EVP_rc2_cbc(void)
  41964. {
  41965. EXPECT_DECLS;
  41966. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  41967. ExpectNull(wolfSSL_EVP_rc2_cbc());
  41968. #endif
  41969. return EXPECT_RESULT();
  41970. }
  41971. static int test_wolfSSL_EVP_mdc2(void)
  41972. {
  41973. EXPECT_DECLS;
  41974. #if !defined(NO_WOLFSSL_STUB)
  41975. ExpectNull(wolfSSL_EVP_mdc2());
  41976. #endif
  41977. return EXPECT_RESULT();
  41978. }
  41979. static int test_wolfSSL_EVP_md4(void)
  41980. {
  41981. EXPECT_DECLS;
  41982. #if !defined(NO_MD4)
  41983. ExpectNotNull(wolfSSL_EVP_md4());
  41984. #endif
  41985. return EXPECT_RESULT();
  41986. }
  41987. static int test_wolfSSL_EVP_aes_256_gcm(void)
  41988. {
  41989. EXPECT_DECLS;
  41990. #ifdef HAVE_AESGCM
  41991. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  41992. #endif
  41993. return EXPECT_RESULT();
  41994. }
  41995. static int test_wolfSSL_EVP_aes_192_gcm(void)
  41996. {
  41997. EXPECT_DECLS;
  41998. #ifdef HAVE_AESGCM
  41999. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  42000. #endif
  42001. return EXPECT_RESULT();
  42002. }
  42003. static int test_wolfSSL_EVP_aes_256_ccm(void)
  42004. {
  42005. EXPECT_DECLS;
  42006. #ifdef HAVE_AESCCM
  42007. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  42008. #endif
  42009. return EXPECT_RESULT();
  42010. }
  42011. static int test_wolfSSL_EVP_aes_192_ccm(void)
  42012. {
  42013. EXPECT_DECLS;
  42014. #ifdef HAVE_AESCCM
  42015. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  42016. #endif
  42017. return EXPECT_RESULT();
  42018. }
  42019. static int test_wolfSSL_EVP_aes_128_ccm(void)
  42020. {
  42021. EXPECT_DECLS;
  42022. #ifdef HAVE_AESCCM
  42023. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  42024. #endif
  42025. return EXPECT_RESULT();
  42026. }
  42027. static int test_wolfSSL_EVP_ripemd160(void)
  42028. {
  42029. EXPECT_DECLS;
  42030. #if !defined(NO_WOLFSSL_STUB)
  42031. ExpectNull(wolfSSL_EVP_ripemd160());
  42032. #endif
  42033. return EXPECT_RESULT();
  42034. }
  42035. static int test_wolfSSL_EVP_get_digestbynid(void)
  42036. {
  42037. EXPECT_DECLS;
  42038. #ifndef NO_MD5
  42039. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42040. #endif
  42041. #ifndef NO_SHA
  42042. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42043. #endif
  42044. #ifndef NO_SHA256
  42045. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42046. #endif
  42047. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  42048. return EXPECT_RESULT();
  42049. }
  42050. static int test_wolfSSL_EVP_MD_nid(void)
  42051. {
  42052. EXPECT_DECLS;
  42053. #ifndef NO_MD5
  42054. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42055. #endif
  42056. #ifndef NO_SHA
  42057. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42058. #endif
  42059. #ifndef NO_SHA256
  42060. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42061. #endif
  42062. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  42063. return EXPECT_RESULT();
  42064. }
  42065. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42066. {
  42067. EXPECT_DECLS;
  42068. #if defined(HAVE_ECC)
  42069. WOLFSSL_EVP_PKEY* pkey = NULL;
  42070. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  42071. ExpectNotNull(pkey = EVP_PKEY_new());
  42072. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  42073. EVP_PKEY_free(pkey);
  42074. #endif
  42075. return EXPECT_RESULT();
  42076. }
  42077. static int test_wolfSSL_EVP_X_STATE(void)
  42078. {
  42079. EXPECT_DECLS;
  42080. #if !defined(NO_DES3) && !defined(NO_RC4)
  42081. byte key[DES3_KEY_SIZE] = {0};
  42082. byte iv[DES_IV_SIZE] = {0};
  42083. EVP_CIPHER_CTX *ctx = NULL;
  42084. const EVP_CIPHER *init = NULL;
  42085. /* Bad test cases */
  42086. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42087. ExpectNotNull(init = EVP_des_ede3_cbc());
  42088. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42089. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42090. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  42091. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  42092. EVP_CIPHER_CTX_free(ctx);
  42093. ctx = NULL;
  42094. /* Good test case */
  42095. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42096. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42097. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42098. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42099. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  42100. EVP_CIPHER_CTX_free(ctx);
  42101. #endif
  42102. return EXPECT_RESULT();
  42103. }
  42104. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42105. {
  42106. EXPECT_DECLS;
  42107. #if !defined(NO_DES3) && !defined(NO_RC4)
  42108. byte key[DES3_KEY_SIZE] = {0};
  42109. byte iv[DES_IV_SIZE] = {0};
  42110. EVP_CIPHER_CTX *ctx = NULL;
  42111. const EVP_CIPHER *init = NULL;
  42112. /* Bad test cases */
  42113. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42114. ExpectNotNull(init = EVP_des_ede3_cbc());
  42115. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42116. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42117. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42118. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42119. EVP_CIPHER_CTX_free(ctx);
  42120. ctx = NULL;
  42121. /* Good test case */
  42122. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42123. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42124. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42125. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42126. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42127. EVP_CIPHER_CTX_free(ctx);
  42128. #endif
  42129. return EXPECT_RESULT();
  42130. }
  42131. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42132. {
  42133. EXPECT_DECLS;
  42134. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  42135. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  42136. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  42137. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  42138. #ifdef HAVE_AES_CBC
  42139. #ifdef WOLFSSL_AES_128
  42140. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42141. #endif
  42142. #ifdef WOLFSSL_AES_192
  42143. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42144. #endif
  42145. #ifdef WOLFSSL_AES_256
  42146. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42147. #endif
  42148. #endif
  42149. #ifdef HAVE_AESGCM
  42150. #ifdef WOLFSSL_AES_128
  42151. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42152. #endif
  42153. #ifdef WOLFSSL_AES_192
  42154. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42155. #endif
  42156. #ifdef WOLFSSL_AES_256
  42157. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42158. #endif
  42159. #endif
  42160. #ifdef HAVE_AESCCM
  42161. #ifdef WOLFSSL_AES_128
  42162. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42163. #endif
  42164. #ifdef WOLFSSL_AES_192
  42165. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42166. #endif
  42167. #ifdef WOLFSSL_AES_256
  42168. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42169. #endif
  42170. #endif
  42171. #ifdef WOLFSSL_AES_COUNTER
  42172. #ifdef WOLFSSL_AES_128
  42173. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42174. #endif
  42175. #ifdef WOLFSSL_AES_192
  42176. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42177. #endif
  42178. #ifdef WOLFSSL_AES_256
  42179. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42180. #endif
  42181. #endif
  42182. #ifdef HAVE_AES_ECB
  42183. #ifdef WOLFSSL_AES_128
  42184. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42185. #endif
  42186. #ifdef WOLFSSL_AES_192
  42187. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42188. #endif
  42189. #ifdef WOLFSSL_AES_256
  42190. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42191. #endif
  42192. #endif
  42193. #ifdef WOLFSSL_AES_OFB
  42194. #ifdef WOLFSSL_AES_128
  42195. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42196. #endif
  42197. #ifdef WOLFSSL_AES_192
  42198. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42199. #endif
  42200. #ifdef WOLFSSL_AES_256
  42201. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42202. #endif
  42203. #endif
  42204. #ifndef NO_RC4
  42205. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42206. #endif
  42207. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42208. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42209. #endif
  42210. #endif
  42211. #ifdef WOLFSSL_SM4_ECB
  42212. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  42213. #endif
  42214. #ifdef WOLFSSL_SM4_CBC
  42215. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  42216. #endif
  42217. #ifdef WOLFSSL_SM4_CTR
  42218. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  42219. #endif
  42220. #ifdef WOLFSSL_SM4_GCM
  42221. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  42222. #endif
  42223. #ifdef WOLFSSL_SM4_CCM
  42224. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  42225. #endif
  42226. return EXPECT_RESULT();
  42227. }
  42228. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42229. {
  42230. EXPECT_DECLS;
  42231. int nids[] = {
  42232. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42233. #ifdef WOLFSSL_AES_128
  42234. NID_aes_128_cbc,
  42235. #endif
  42236. #ifdef WOLFSSL_AES_192
  42237. NID_aes_192_cbc,
  42238. #endif
  42239. #ifdef WOLFSSL_AES_256
  42240. NID_aes_256_cbc,
  42241. #endif
  42242. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42243. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42244. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42245. #ifdef HAVE_AESGCM
  42246. #ifdef WOLFSSL_AES_128
  42247. NID_aes_128_gcm,
  42248. #endif
  42249. #ifdef WOLFSSL_AES_192
  42250. NID_aes_192_gcm,
  42251. #endif
  42252. #ifdef WOLFSSL_AES_256
  42253. NID_aes_256_gcm,
  42254. #endif
  42255. #endif /* HAVE_AESGCM */
  42256. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42257. #ifdef WOLFSSL_AES_COUNTER
  42258. #ifdef WOLFSSL_AES_128
  42259. NID_aes_128_ctr,
  42260. #endif
  42261. #ifdef WOLFSSL_AES_192
  42262. NID_aes_192_ctr,
  42263. #endif
  42264. #ifdef WOLFSSL_AES_256
  42265. NID_aes_256_ctr,
  42266. #endif
  42267. #endif
  42268. #ifndef NO_DES3
  42269. NID_des_cbc,
  42270. NID_des_ede3_cbc,
  42271. #endif
  42272. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42273. NID_chacha20_poly1305,
  42274. #endif
  42275. };
  42276. int iv_lengths[] = {
  42277. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42278. #ifdef WOLFSSL_AES_128
  42279. AES_BLOCK_SIZE,
  42280. #endif
  42281. #ifdef WOLFSSL_AES_192
  42282. AES_BLOCK_SIZE,
  42283. #endif
  42284. #ifdef WOLFSSL_AES_256
  42285. AES_BLOCK_SIZE,
  42286. #endif
  42287. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42288. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42289. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42290. #ifdef HAVE_AESGCM
  42291. #ifdef WOLFSSL_AES_128
  42292. GCM_NONCE_MID_SZ,
  42293. #endif
  42294. #ifdef WOLFSSL_AES_192
  42295. GCM_NONCE_MID_SZ,
  42296. #endif
  42297. #ifdef WOLFSSL_AES_256
  42298. GCM_NONCE_MID_SZ,
  42299. #endif
  42300. #endif /* HAVE_AESGCM */
  42301. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42302. #ifdef WOLFSSL_AES_COUNTER
  42303. #ifdef WOLFSSL_AES_128
  42304. AES_BLOCK_SIZE,
  42305. #endif
  42306. #ifdef WOLFSSL_AES_192
  42307. AES_BLOCK_SIZE,
  42308. #endif
  42309. #ifdef WOLFSSL_AES_256
  42310. AES_BLOCK_SIZE,
  42311. #endif
  42312. #endif
  42313. #ifndef NO_DES3
  42314. DES_BLOCK_SIZE,
  42315. DES_BLOCK_SIZE,
  42316. #endif
  42317. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42318. CHACHA20_POLY1305_AEAD_IV_SIZE,
  42319. #endif
  42320. };
  42321. int i;
  42322. int nidsLen = (sizeof(nids)/sizeof(int));
  42323. for (i = 0; i < nidsLen; i++) {
  42324. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  42325. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  42326. }
  42327. return EXPECT_RESULT();
  42328. }
  42329. static int test_wolfSSL_EVP_SignInit_ex(void)
  42330. {
  42331. EXPECT_DECLS;
  42332. WOLFSSL_EVP_MD_CTX mdCtx;
  42333. WOLFSSL_ENGINE* e = 0;
  42334. const EVP_MD* md = EVP_sha256();
  42335. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42336. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  42337. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42338. return EXPECT_RESULT();
  42339. }
  42340. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  42341. {
  42342. EXPECT_DECLS;
  42343. #if !defined(NO_SHA256)
  42344. WOLFSSL_EVP_MD_CTX mdCtx;
  42345. unsigned int s = 0;
  42346. unsigned char md[WC_SHA256_DIGEST_SIZE];
  42347. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  42348. /* Bad Case */
  42349. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42350. (HAVE_FIPS_VERSION > 2))
  42351. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42352. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  42353. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42354. #else
  42355. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42356. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  42357. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42358. #endif
  42359. /* Good Case */
  42360. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42361. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  42362. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  42363. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42364. #endif
  42365. return EXPECT_RESULT();
  42366. }
  42367. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  42368. {
  42369. EXPECT_DECLS;
  42370. #if defined(OPENSSL_EXTRA)
  42371. EVP_PKEY* pkey = NULL;
  42372. EVP_PKEY_CTX* ctx = NULL;
  42373. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42374. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42375. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  42376. /* void */
  42377. EVP_PKEY_CTX_free(ctx);
  42378. #else
  42379. /* int */
  42380. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  42381. #endif
  42382. EVP_PKEY_free(pkey);
  42383. #endif
  42384. return EXPECT_RESULT();
  42385. }
  42386. static int test_wolfSSL_EVP_PKEY_param_check(void)
  42387. {
  42388. EXPECT_DECLS;
  42389. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  42390. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42391. DH *dh = NULL;
  42392. DH *setDh = NULL;
  42393. EVP_PKEY *pkey = NULL;
  42394. EVP_PKEY_CTX* ctx = NULL;
  42395. FILE* f = NULL;
  42396. unsigned char buf[512];
  42397. const unsigned char* pt = buf;
  42398. const char* dh2048 = "./certs/dh2048.der";
  42399. long len = 0;
  42400. int code = -1;
  42401. XMEMSET(buf, 0, sizeof(buf));
  42402. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42403. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42404. if (f != XBADFILE)
  42405. XFCLOSE(f);
  42406. /* Load dh2048.der into DH with internal format */
  42407. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  42408. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42409. ExpectIntEQ(code, 0);
  42410. code = -1;
  42411. pkey = wolfSSL_EVP_PKEY_new();
  42412. /* Set DH into PKEY */
  42413. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42414. /* create ctx from pkey */
  42415. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42416. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  42417. /* TODO: more invalid cases */
  42418. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  42419. EVP_PKEY_CTX_free(ctx);
  42420. EVP_PKEY_free(pkey);
  42421. DH_free(setDh);
  42422. DH_free(dh);
  42423. #endif
  42424. #endif
  42425. return EXPECT_RESULT();
  42426. }
  42427. static int test_wolfSSL_EVP_BytesToKey(void)
  42428. {
  42429. EXPECT_DECLS;
  42430. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42431. byte key[AES_BLOCK_SIZE] = {0};
  42432. byte iv[AES_BLOCK_SIZE] = {0};
  42433. int count = 0;
  42434. const EVP_MD* md = EVP_sha256();
  42435. const EVP_CIPHER *type;
  42436. const unsigned char *salt = (unsigned char *)"salt1234";
  42437. int sz = 5;
  42438. const byte data[] = {
  42439. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42440. 0x72,0x6c,0x64
  42441. };
  42442. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  42443. /* Bad cases */
  42444. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  42445. 0);
  42446. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  42447. 16);
  42448. md = "2";
  42449. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42450. WOLFSSL_FAILURE);
  42451. /* Good case */
  42452. md = EVP_sha256();
  42453. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42454. 16);
  42455. #endif
  42456. return EXPECT_RESULT();
  42457. }
  42458. static int test_evp_cipher_aes_gcm(void)
  42459. {
  42460. EXPECT_DECLS;
  42461. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  42462. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  42463. (HAVE_FIPS_VERSION >= 2)))
  42464. /*
  42465. * This test checks data at various points in the encrypt/decrypt process
  42466. * against known values produced using the same test with OpenSSL. This
  42467. * interop testing is critical for verifying the correctness of our
  42468. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  42469. * a flow supported by OpenSSL that uses the control command
  42470. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  42471. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  42472. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  42473. * wolfSSL OpenSSH clients because there was a bug in this flow that
  42474. * happened to "cancel out" if both sides of the connection had the bug.
  42475. */
  42476. enum {
  42477. NUM_ENCRYPTIONS = 3,
  42478. AAD_SIZE = 4
  42479. };
  42480. byte plainText1[] = {
  42481. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  42482. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  42483. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  42484. };
  42485. byte plainText2[] = {
  42486. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  42487. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  42488. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  42489. };
  42490. byte plainText3[] = {
  42491. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  42492. 0x5b, 0x57, 0x10
  42493. };
  42494. byte* plainTexts[NUM_ENCRYPTIONS] = {
  42495. plainText1,
  42496. plainText2,
  42497. plainText3
  42498. };
  42499. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  42500. sizeof(plainText1),
  42501. sizeof(plainText2),
  42502. sizeof(plainText3)
  42503. };
  42504. byte aad1[AAD_SIZE] = {
  42505. 0x00, 0x00, 0x00, 0x01
  42506. };
  42507. byte aad2[AAD_SIZE] = {
  42508. 0x00, 0x00, 0x00, 0x10
  42509. };
  42510. byte aad3[AAD_SIZE] = {
  42511. 0x00, 0x00, 0x01, 0x00
  42512. };
  42513. byte* aads[NUM_ENCRYPTIONS] = {
  42514. aad1,
  42515. aad2,
  42516. aad3
  42517. };
  42518. const byte iv[GCM_NONCE_MID_SZ] = {
  42519. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  42520. };
  42521. byte currentIv[GCM_NONCE_MID_SZ];
  42522. const byte key[] = {
  42523. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  42524. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  42525. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  42526. };
  42527. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  42528. {
  42529. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42530. 0xEF
  42531. },
  42532. {
  42533. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42534. 0xF0
  42535. },
  42536. {
  42537. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42538. 0xF1
  42539. }
  42540. };
  42541. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  42542. {
  42543. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  42544. 0x3D, 0x32, 0x18, 0x34, 0xA9
  42545. },
  42546. {
  42547. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  42548. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  42549. },
  42550. {
  42551. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  42552. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  42553. }
  42554. };
  42555. const byte expCipherText1[] = {
  42556. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  42557. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  42558. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  42559. };
  42560. const byte expCipherText2[] = {
  42561. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  42562. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  42563. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  42564. };
  42565. const byte expCipherText3[] = {
  42566. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  42567. 0x80, 0x5E, 0x6B,
  42568. };
  42569. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  42570. expCipherText1,
  42571. expCipherText2,
  42572. expCipherText3
  42573. };
  42574. byte* cipherText = NULL;
  42575. byte* calcPlainText = NULL;
  42576. byte tag[AES_BLOCK_SIZE];
  42577. EVP_CIPHER_CTX* encCtx = NULL;
  42578. EVP_CIPHER_CTX* decCtx = NULL;
  42579. int i, j, outl;
  42580. /****************************************************/
  42581. for (i = 0; i < 3; ++i) {
  42582. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  42583. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  42584. /* First iteration, set key before IV. */
  42585. if (i == 0) {
  42586. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  42587. SSL_SUCCESS);
  42588. /*
  42589. * The call to EVP_CipherInit below (with NULL key) should clear the
  42590. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  42591. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  42592. * behavior.
  42593. */
  42594. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42595. (void*)iv), SSL_SUCCESS);
  42596. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  42597. SSL_SUCCESS);
  42598. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42599. currentIv), SSL_FAILURE);
  42600. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  42601. SSL_SUCCESS);
  42602. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  42603. SSL_SUCCESS);
  42604. }
  42605. /* Second iteration, IV before key. */
  42606. else {
  42607. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  42608. SSL_SUCCESS);
  42609. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  42610. SSL_SUCCESS);
  42611. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  42612. SSL_SUCCESS);
  42613. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  42614. SSL_SUCCESS);
  42615. }
  42616. /*
  42617. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  42618. * been issued first.
  42619. */
  42620. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42621. currentIv), SSL_FAILURE);
  42622. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42623. (void*)iv), SSL_SUCCESS);
  42624. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  42625. (void*)iv), SSL_SUCCESS);
  42626. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  42627. /*************** Encrypt ***************/
  42628. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42629. currentIv), SSL_SUCCESS);
  42630. /* Check current IV against expected. */
  42631. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  42632. /* Add AAD. */
  42633. if (i == 2) {
  42634. /* Test streaming API. */
  42635. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  42636. AAD_SIZE), SSL_SUCCESS);
  42637. }
  42638. else {
  42639. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  42640. AAD_SIZE);
  42641. }
  42642. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  42643. DYNAMIC_TYPE_TMP_BUFFER));
  42644. /* Encrypt plaintext. */
  42645. if (i == 2) {
  42646. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  42647. plainTexts[j], plainTextSzs[j]),
  42648. SSL_SUCCESS);
  42649. }
  42650. else {
  42651. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  42652. plainTextSzs[j]), plainTextSzs[j]);
  42653. }
  42654. if (i == 2) {
  42655. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  42656. SSL_SUCCESS);
  42657. }
  42658. else {
  42659. /*
  42660. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  42661. * akin to calling EVP_CipherFinal.
  42662. */
  42663. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  42664. }
  42665. /* Check ciphertext against expected. */
  42666. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  42667. 0);
  42668. /* Get and check tag against expected. */
  42669. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  42670. sizeof(tag), tag), SSL_SUCCESS);
  42671. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  42672. /*************** Decrypt ***************/
  42673. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  42674. currentIv), SSL_SUCCESS);
  42675. /* Check current IV against expected. */
  42676. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  42677. /* Add AAD. */
  42678. if (i == 2) {
  42679. /* Test streaming API. */
  42680. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  42681. AAD_SIZE), SSL_SUCCESS);
  42682. }
  42683. else {
  42684. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  42685. AAD_SIZE);
  42686. }
  42687. /* Set expected tag. */
  42688. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  42689. sizeof(tag), tag), SSL_SUCCESS);
  42690. /* Decrypt ciphertext. */
  42691. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  42692. DYNAMIC_TYPE_TMP_BUFFER));
  42693. if (i == 2) {
  42694. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  42695. cipherText, plainTextSzs[j]),
  42696. SSL_SUCCESS);
  42697. }
  42698. else {
  42699. /* This first EVP_Cipher call will check the tag, too. */
  42700. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  42701. plainTextSzs[j]), plainTextSzs[j]);
  42702. }
  42703. if (i == 2) {
  42704. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  42705. SSL_SUCCESS);
  42706. }
  42707. else {
  42708. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  42709. }
  42710. /* Check plaintext against expected. */
  42711. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  42712. 0);
  42713. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42714. cipherText = NULL;
  42715. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42716. calcPlainText = NULL;
  42717. }
  42718. EVP_CIPHER_CTX_free(encCtx);
  42719. encCtx = NULL;
  42720. EVP_CIPHER_CTX_free(decCtx);
  42721. decCtx = NULL;
  42722. }
  42723. #endif
  42724. return EXPECT_RESULT();
  42725. }
  42726. static int test_wolfSSL_OBJ_ln(void)
  42727. {
  42728. EXPECT_DECLS;
  42729. const int nid_set[] = {
  42730. NID_commonName,
  42731. NID_serialNumber,
  42732. NID_countryName,
  42733. NID_localityName,
  42734. NID_stateOrProvinceName,
  42735. NID_organizationName,
  42736. NID_organizationalUnitName,
  42737. NID_domainComponent,
  42738. NID_businessCategory,
  42739. NID_jurisdictionCountryName,
  42740. NID_jurisdictionStateOrProvinceName,
  42741. NID_emailAddress
  42742. };
  42743. const char* ln_set[] = {
  42744. "commonName",
  42745. "serialNumber",
  42746. "countryName",
  42747. "localityName",
  42748. "stateOrProvinceName",
  42749. "organizationName",
  42750. "organizationalUnitName",
  42751. "domainComponent",
  42752. "businessCategory",
  42753. "jurisdictionCountryName",
  42754. "jurisdictionStateOrProvinceName",
  42755. "emailAddress",
  42756. };
  42757. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  42758. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  42759. #ifdef HAVE_ECC
  42760. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42761. {
  42762. EC_builtin_curve r[27];
  42763. size_t nCurves = sizeof(r) / sizeof(r[0]);
  42764. nCurves = EC_get_builtin_curves(r, nCurves);
  42765. for (i = 0; i < nCurves; i++) {
  42766. /* skip ECC_CURVE_INVALID */
  42767. if (r[i].nid != ECC_CURVE_INVALID) {
  42768. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  42769. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  42770. }
  42771. }
  42772. }
  42773. #endif
  42774. #endif
  42775. for (i = 0; i < maxIdx; i++) {
  42776. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  42777. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  42778. }
  42779. return EXPECT_RESULT();
  42780. }
  42781. static int test_wolfSSL_OBJ_sn(void)
  42782. {
  42783. EXPECT_DECLS;
  42784. int i = 0, maxIdx = 7;
  42785. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  42786. NID_stateOrProvinceName,NID_organizationName,
  42787. NID_organizationalUnitName,NID_emailAddress};
  42788. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  42789. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  42790. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  42791. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  42792. WOLFSSL_EMAIL_ADDR};
  42793. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  42794. for (i = 0; i < maxIdx; i++) {
  42795. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  42796. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  42797. }
  42798. return EXPECT_RESULT();
  42799. }
  42800. #if !defined(NO_BIO)
  42801. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  42802. {
  42803. return lh_strhash(s[3]);
  42804. }
  42805. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  42806. {
  42807. return XSTRCMP(a[3], b[3]);
  42808. }
  42809. #endif
  42810. static int test_wolfSSL_TXT_DB(void)
  42811. {
  42812. EXPECT_DECLS;
  42813. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42814. BIO *bio = NULL;
  42815. TXT_DB *db = NULL;
  42816. const int columns = 6;
  42817. const char *fields[6] = {
  42818. "V",
  42819. "320926161116Z",
  42820. "",
  42821. "12BD",
  42822. "unknown",
  42823. "/CN=rsa doe",
  42824. };
  42825. char** fields_copy = NULL;
  42826. /* Test read */
  42827. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  42828. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  42829. ExpectNotNull(db = TXT_DB_read(bio, columns));
  42830. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  42831. DYNAMIC_TYPE_OPENSSL));
  42832. if (fields_copy != NULL) {
  42833. XMEMCPY(fields_copy, fields, sizeof(fields));
  42834. }
  42835. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  42836. if (EXPECT_FAIL()) {
  42837. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  42838. }
  42839. BIO_free(bio);
  42840. bio = NULL;
  42841. /* Test write */
  42842. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42843. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  42844. BIO_free(bio);
  42845. /* Test index */
  42846. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  42847. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  42848. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42849. fields[3] = "12DA";
  42850. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42851. fields[3] = "FFFF";
  42852. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42853. fields[3] = "";
  42854. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42855. TXT_DB_free(db);
  42856. #endif
  42857. return EXPECT_RESULT();
  42858. }
  42859. static int test_wolfSSL_NCONF(void)
  42860. {
  42861. EXPECT_DECLS;
  42862. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42863. const char* confFile = "./tests/NCONF_test.cnf";
  42864. CONF* conf = NULL;
  42865. long eline = 0;
  42866. long num = 0;
  42867. ExpectNotNull(conf = NCONF_new(NULL));
  42868. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  42869. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  42870. ExpectIntEQ(num, 1234);
  42871. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  42872. ExpectIntEQ(num, 4321);
  42873. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  42874. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  42875. "./test-dir/file1");
  42876. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  42877. "./test-dir/file1:./test-dir/file2:./section1:file2");
  42878. NCONF_free(conf);
  42879. #endif
  42880. return EXPECT_RESULT();
  42881. }
  42882. #endif /* OPENSSL_ALL */
  42883. static int test_wolfSSL_X509V3_EXT_get(void) {
  42884. EXPECT_DECLS;
  42885. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42886. XFILE f = XBADFILE;
  42887. int numOfExt =0;
  42888. int extNid = 0;
  42889. int i = 0;
  42890. WOLFSSL_X509* x509 = NULL;
  42891. WOLFSSL_X509_EXTENSION* ext = NULL;
  42892. const WOLFSSL_v3_ext_method* method = NULL;
  42893. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42894. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42895. if (f != XBADFILE)
  42896. XFCLOSE(f);
  42897. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  42898. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  42899. for (i = 0; i < numOfExt; i++) {
  42900. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42901. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  42902. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  42903. ExpectIntEQ(method->ext_nid, extNid);
  42904. }
  42905. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  42906. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  42907. wolfSSL_X509_free(x509);
  42908. #endif
  42909. return EXPECT_RESULT();
  42910. }
  42911. static int test_wolfSSL_X509V3_EXT_nconf(void)
  42912. {
  42913. EXPECT_DECLS;
  42914. #ifdef OPENSSL_ALL
  42915. const char *ext_names[] = {
  42916. "subjectKeyIdentifier",
  42917. "authorityKeyIdentifier",
  42918. "subjectAltName",
  42919. "keyUsage",
  42920. "extendedKeyUsage",
  42921. };
  42922. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  42923. int ext_nids[] = {
  42924. NID_subject_key_identifier,
  42925. NID_authority_key_identifier,
  42926. NID_subject_alt_name,
  42927. NID_key_usage,
  42928. NID_ext_key_usage,
  42929. };
  42930. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  42931. const char *ext_values[] = {
  42932. "hash",
  42933. "hash",
  42934. "DNS:example.com, IP:127.0.0.1",
  42935. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  42936. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  42937. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  42938. "OCSPSigning",
  42939. };
  42940. size_t i;
  42941. X509_EXTENSION* ext = NULL;
  42942. X509* x509 = NULL;
  42943. unsigned int keyUsageFlags;
  42944. unsigned int extKeyUsageFlags;
  42945. ExpectNotNull(x509 = X509_new());
  42946. /* keyUsage / extKeyUsage should match string above */
  42947. keyUsageFlags = KU_DIGITAL_SIGNATURE
  42948. | KU_NON_REPUDIATION
  42949. | KU_KEY_ENCIPHERMENT
  42950. | KU_DATA_ENCIPHERMENT
  42951. | KU_KEY_AGREEMENT
  42952. | KU_KEY_CERT_SIGN
  42953. | KU_CRL_SIGN
  42954. | KU_ENCIPHER_ONLY
  42955. | KU_DECIPHER_ONLY;
  42956. extKeyUsageFlags = XKU_SSL_CLIENT
  42957. | XKU_SSL_SERVER
  42958. | XKU_CODE_SIGN
  42959. | XKU_SMIME
  42960. | XKU_TIMESTAMP
  42961. | XKU_OCSP_SIGN;
  42962. for (i = 0; i < ext_names_count; i++) {
  42963. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42964. ext_values[i]));
  42965. X509_EXTENSION_free(ext);
  42966. ext = NULL;
  42967. }
  42968. for (i = 0; i < ext_nids_count; i++) {
  42969. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  42970. ext_values[i]));
  42971. X509_EXTENSION_free(ext);
  42972. ext = NULL;
  42973. }
  42974. /* Test adding extension to X509 */
  42975. for (i = 0; i < ext_nids_count; i++) {
  42976. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42977. ext_values[i]));
  42978. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  42979. if (ext_nids[i] == NID_key_usage) {
  42980. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  42981. }
  42982. else if (ext_nids[i] == NID_ext_key_usage) {
  42983. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  42984. }
  42985. X509_EXTENSION_free(ext);
  42986. ext = NULL;
  42987. }
  42988. X509_free(x509);
  42989. #endif
  42990. return EXPECT_RESULT();
  42991. }
  42992. static int test_wolfSSL_X509V3_EXT(void) {
  42993. EXPECT_DECLS;
  42994. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42995. XFILE f = XBADFILE;
  42996. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  42997. char* str = NULL;
  42998. unsigned char* data = NULL;
  42999. const WOLFSSL_v3_ext_method* method = NULL;
  43000. WOLFSSL_X509* x509 = NULL;
  43001. WOLFSSL_X509_EXTENSION* ext = NULL;
  43002. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  43003. WOLFSSL_ASN1_OBJECT *obj = NULL;
  43004. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  43005. WOLFSSL_ASN1_STRING* asn1str = NULL;
  43006. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  43007. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  43008. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  43009. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  43010. WOLFSSL_GENERAL_NAME* gn = NULL;
  43011. /* Check NULL argument */
  43012. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  43013. /* Using OCSP cert with X509V3 extensions */
  43014. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  43015. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43016. if (f != XBADFILE)
  43017. XFCLOSE(f);
  43018. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43019. /* Basic Constraints */
  43020. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43021. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43022. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  43023. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  43024. ExpectIntEQ(bc->ca, 1);
  43025. ExpectNull(bc->pathlen);
  43026. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  43027. bc = NULL;
  43028. i++;
  43029. /* Subject Key Identifier */
  43030. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43031. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43032. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  43033. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43034. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  43035. asn1str));
  43036. X509_EXTENSION_free(ext2);
  43037. ext2 = NULL;
  43038. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43039. ExpectNotNull(method->i2s);
  43040. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43041. wolfSSL_ASN1_STRING_free(asn1str);
  43042. asn1str = NULL;
  43043. if (str != NULL) {
  43044. actual = strcmp(str,
  43045. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43046. }
  43047. ExpectIntEQ(actual, 0);
  43048. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43049. str = NULL;
  43050. i++;
  43051. /* Authority Key Identifier */
  43052. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43053. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43054. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43055. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  43056. ext));
  43057. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43058. ExpectNotNull(asn1str = aKeyId->keyid);
  43059. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  43060. asn1str));
  43061. asn1str = NULL;
  43062. if (str != NULL) {
  43063. actual = strcmp(str,
  43064. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43065. }
  43066. ExpectIntEQ(actual, 0);
  43067. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43068. str = NULL;
  43069. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43070. aKeyId = NULL;
  43071. i++;
  43072. /* Key Usage */
  43073. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43074. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43075. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43076. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43077. #if defined(WOLFSSL_QT)
  43078. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43079. #else
  43080. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43081. #endif
  43082. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43083. if (data != NULL) {
  43084. #ifdef BIG_ENDIAN_ORDER
  43085. actual = data[1];
  43086. #else
  43087. actual = data[0];
  43088. #endif
  43089. }
  43090. ExpectIntEQ(actual, expected);
  43091. wolfSSL_ASN1_STRING_free(asn1str);
  43092. asn1str = NULL;
  43093. #if 1
  43094. i++;
  43095. /* Authority Info Access */
  43096. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43097. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43098. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43099. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  43100. ext));
  43101. #if defined(WOLFSSL_QT)
  43102. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43103. #else
  43104. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43105. #endif
  43106. /* URI entry is an ACCESS_DESCRIPTION type */
  43107. #if defined(WOLFSSL_QT)
  43108. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43109. #else
  43110. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43111. #endif
  43112. ExpectNotNull(adObj = ad->method);
  43113. /* Make sure nid is OCSP */
  43114. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43115. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43116. ExpectNotNull(gn = ad->location);
  43117. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43118. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43119. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43120. #if defined(WOLFSSL_QT)
  43121. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  43122. #else
  43123. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  43124. #endif
  43125. if (str != NULL) {
  43126. actual = strcmp(str, "http://127.0.0.1:22220");
  43127. }
  43128. ExpectIntEQ(actual, 0);
  43129. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43130. aia = NULL;
  43131. #else
  43132. (void) aia; (void) ad; (void) adObj; (void) gn;
  43133. #endif
  43134. wolfSSL_X509_free(x509);
  43135. #endif
  43136. return EXPECT_RESULT();
  43137. }
  43138. static int test_wolfSSL_X509_get_extension_flags(void)
  43139. {
  43140. EXPECT_DECLS;
  43141. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43142. XFILE f = XBADFILE;
  43143. X509* x509 = NULL;
  43144. unsigned int extFlags;
  43145. unsigned int keyUsageFlags;
  43146. unsigned int extKeyUsageFlags;
  43147. /* client-int-cert.pem has the following extension flags. */
  43148. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43149. /* and the following key usage flags. */
  43150. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43151. | KU_NON_REPUDIATION
  43152. | KU_KEY_ENCIPHERMENT;
  43153. /* and the following extended key usage flags. */
  43154. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43155. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  43156. XBADFILE);
  43157. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43158. if (f != XBADFILE) {
  43159. XFCLOSE(f);
  43160. f = XBADFILE;
  43161. }
  43162. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43163. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43164. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43165. X509_free(x509);
  43166. x509 = NULL;
  43167. /* client-cert-ext.pem has the following extension flags. */
  43168. extFlags = EXFLAG_KUSAGE;
  43169. /* and the following key usage flags. */
  43170. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43171. | KU_KEY_CERT_SIGN
  43172. | KU_CRL_SIGN;
  43173. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  43174. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43175. if (f != XBADFILE)
  43176. XFCLOSE(f);
  43177. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43178. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43179. X509_free(x509);
  43180. #endif /* OPENSSL_ALL */
  43181. return EXPECT_RESULT();
  43182. }
  43183. static int test_wolfSSL_X509_get_ext(void)
  43184. {
  43185. EXPECT_DECLS;
  43186. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43187. int ret = 0;
  43188. XFILE f = XBADFILE;
  43189. WOLFSSL_X509* x509 = NULL;
  43190. WOLFSSL_X509_EXTENSION* foundExtension;
  43191. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43192. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43193. if (f != XBADFILE)
  43194. XFCLOSE(f);
  43195. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43196. /* wolfSSL_X509_get_ext() valid input */
  43197. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43198. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43199. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43200. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43201. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43202. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43203. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43204. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43205. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43206. wolfSSL_X509_free(x509);
  43207. #endif
  43208. return EXPECT_RESULT();
  43209. }
  43210. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43211. {
  43212. EXPECT_DECLS;
  43213. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43214. int rc;
  43215. XFILE f = XBADFILE;
  43216. WOLFSSL_X509* x509 = NULL;
  43217. ASN1_OBJECT* obj = NULL;
  43218. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43219. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43220. if (f != XBADFILE)
  43221. XFCLOSE(f);
  43222. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43223. -1), 0);
  43224. /* Start search from last location (should fail) */
  43225. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43226. rc), -1);
  43227. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43228. -2), -1);
  43229. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  43230. -1), -1);
  43231. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  43232. /* NID_ext_key_usage, check also its nid and oid */
  43233. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  43234. -1);
  43235. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  43236. x509, rc)));
  43237. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  43238. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  43239. wolfSSL_X509_free(x509);
  43240. #endif
  43241. return EXPECT_RESULT();
  43242. }
  43243. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  43244. {
  43245. EXPECT_DECLS;
  43246. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43247. int rc;
  43248. XFILE f = XBADFILE;
  43249. WOLFSSL_X509* x509 = NULL;
  43250. WOLFSSL_X509_EXTENSION* ext;
  43251. WOLFSSL_ASN1_STRING* sanString;
  43252. byte* sanDer;
  43253. const byte expectedDer[] = {
  43254. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  43255. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  43256. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43257. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43258. if (f != XBADFILE)
  43259. XFCLOSE(f);
  43260. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  43261. ExpectNotNull(ext = X509_get_ext(x509, rc));
  43262. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  43263. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  43264. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  43265. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  43266. X509_free(x509);
  43267. #endif
  43268. return EXPECT_RESULT();
  43269. }
  43270. static int test_wolfSSL_X509_EXTENSION_new(void)
  43271. {
  43272. EXPECT_DECLS;
  43273. #if defined (OPENSSL_ALL)
  43274. WOLFSSL_X509_EXTENSION* ext = NULL;
  43275. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  43276. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  43277. wolfSSL_X509_EXTENSION_free(ext);
  43278. #endif
  43279. return EXPECT_RESULT();
  43280. }
  43281. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  43282. {
  43283. EXPECT_DECLS;
  43284. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43285. WOLFSSL_X509* x509 = NULL;
  43286. WOLFSSL_X509_EXTENSION* ext = NULL;
  43287. WOLFSSL_ASN1_OBJECT* o = NULL;
  43288. XFILE file = XBADFILE;
  43289. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43290. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43291. if (file != XBADFILE)
  43292. XFCLOSE(file);
  43293. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  43294. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43295. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  43296. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  43297. ExpectIntEQ(o->nid, 128);
  43298. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  43299. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  43300. wolfSSL_X509_free(x509);
  43301. #endif
  43302. return EXPECT_RESULT();
  43303. }
  43304. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  43305. {
  43306. EXPECT_DECLS;
  43307. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43308. WOLFSSL_X509* x509 = NULL;
  43309. WOLFSSL_X509_EXTENSION* ext;
  43310. WOLFSSL_ASN1_STRING* str;
  43311. XFILE file = XBADFILE;
  43312. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43313. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43314. if (file != XBADFILE)
  43315. XFCLOSE(file);
  43316. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43317. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  43318. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  43319. wolfSSL_X509_free(x509);
  43320. #endif
  43321. return EXPECT_RESULT();
  43322. }
  43323. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  43324. {
  43325. EXPECT_DECLS;
  43326. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43327. WOLFSSL_X509* x509 = NULL;
  43328. WOLFSSL_X509_EXTENSION* ext;
  43329. XFILE file = XBADFILE;
  43330. int crit;
  43331. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43332. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43333. if (file != XBADFILE)
  43334. XFCLOSE(file);
  43335. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43336. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  43337. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  43338. wolfSSL_X509_free(x509);
  43339. #endif
  43340. return EXPECT_RESULT();
  43341. }
  43342. static int test_wolfSSL_X509V3_EXT_print(void)
  43343. {
  43344. EXPECT_DECLS;
  43345. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  43346. !defined(NO_RSA)
  43347. {
  43348. XFILE f = XBADFILE;
  43349. WOLFSSL_X509* x509 = NULL;
  43350. X509_EXTENSION * ext = NULL;
  43351. int loc;
  43352. BIO *bio = NULL;
  43353. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  43354. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43355. if (f != XBADFILE)
  43356. fclose(f);
  43357. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  43358. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43359. NID_basic_constraints, -1), -1);
  43360. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43361. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43362. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43363. NID_subject_key_identifier, -1), -1);
  43364. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43365. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43366. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43367. NID_authority_key_identifier, -1), -1);
  43368. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43369. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43370. wolfSSL_BIO_free(bio);
  43371. wolfSSL_X509_free(x509);
  43372. }
  43373. {
  43374. X509 *x509 = NULL;
  43375. BIO *bio = NULL;
  43376. X509_EXTENSION *ext;
  43377. unsigned int i;
  43378. unsigned int idx;
  43379. /* Some NIDs to test with */
  43380. int nids[] = {
  43381. /* NID_key_usage, currently X509_get_ext returns this as a bit
  43382. * string, which messes up X509V3_EXT_print */
  43383. /* NID_ext_key_usage, */
  43384. NID_subject_alt_name,
  43385. };
  43386. int* n;
  43387. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  43388. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  43389. WOLFSSL_FILETYPE_PEM));
  43390. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  43391. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  43392. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  43393. * update the index */
  43394. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  43395. ExpectNotNull(ext = X509_get_ext(x509, idx));
  43396. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  43397. ExpectIntGT(fprintf(stderr, "\n"), 0);
  43398. }
  43399. BIO_free(bio);
  43400. X509_free(x509);
  43401. }
  43402. #endif
  43403. return EXPECT_RESULT();
  43404. }
  43405. static int test_wolfSSL_X509_cmp(void)
  43406. {
  43407. EXPECT_DECLS;
  43408. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43409. XFILE file1 = XBADFILE;
  43410. XFILE file2 = XBADFILE;
  43411. WOLFSSL_X509* cert1 = NULL;
  43412. WOLFSSL_X509* cert2 = NULL;
  43413. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43414. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  43415. XBADFILE);
  43416. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  43417. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  43418. if (file1 != XBADFILE)
  43419. fclose(file1);
  43420. if (file2 != XBADFILE)
  43421. fclose(file2);
  43422. /* wolfSSL_X509_cmp() testing matching certs */
  43423. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  43424. /* wolfSSL_X509_cmp() testing mismatched certs */
  43425. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  43426. /* wolfSSL_X509_cmp() testing NULL, valid args */
  43427. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  43428. /* wolfSSL_X509_cmp() testing valid, NULL args */
  43429. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  43430. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  43431. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  43432. wolfSSL_X509_free(cert1);
  43433. wolfSSL_X509_free(cert2);
  43434. #endif
  43435. return EXPECT_RESULT();
  43436. }
  43437. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  43438. {
  43439. EXPECT_DECLS;
  43440. #if defined(OPENSSL_ALL)
  43441. EVP_PKEY* pkey;
  43442. pkey = EVP_PKEY_new();
  43443. ExpectNotNull(pkey);
  43444. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  43445. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43446. EVP_PKEY_free(pkey);
  43447. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43448. EVP_PKEY_free(pkey);
  43449. EVP_PKEY_free(pkey);
  43450. #endif
  43451. return EXPECT_RESULT();
  43452. }
  43453. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  43454. {
  43455. EXPECT_DECLS;
  43456. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  43457. EVP_PKEY* pkey = NULL;
  43458. const unsigned char* p;
  43459. unsigned char *der = NULL;
  43460. unsigned char *tmp = NULL;
  43461. int derLen;
  43462. p = client_keypub_der_2048;
  43463. /* Check that key can be successfully decoded. */
  43464. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  43465. sizeof_client_keypub_der_2048));
  43466. /* Check that key can be successfully encoded. */
  43467. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43468. /* Ensure that the encoded version matches the original. */
  43469. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43470. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43471. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43472. * is advanced. */
  43473. tmp = der;
  43474. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43475. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43476. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43477. ExpectTrue(der + derLen == tmp);
  43478. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43479. EVP_PKEY_free(pkey);
  43480. #endif
  43481. return EXPECT_RESULT();
  43482. }
  43483. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  43484. {
  43485. EXPECT_DECLS;
  43486. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  43487. !defined(NO_ASN) && !defined(NO_PWDBASED)
  43488. EVP_PKEY* pkey = NULL;
  43489. const unsigned char* p;
  43490. unsigned char *der = NULL;
  43491. unsigned char *tmp = NULL;
  43492. int derLen;
  43493. unsigned char pub_buf[65];
  43494. const int pub_len = 65;
  43495. BN_CTX* ctx;
  43496. EC_GROUP* curve = NULL;
  43497. EC_KEY* ephemeral_key = NULL;
  43498. const EC_POINT* h;
  43499. /* Generate an x963 key pair and get public part into pub_buf */
  43500. ExpectNotNull(ctx = BN_CTX_new());
  43501. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  43502. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  43503. NID_X9_62_prime256v1));
  43504. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  43505. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  43506. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  43507. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  43508. /* Prepare the EVP_PKEY */
  43509. ExpectNotNull(pkey = EVP_PKEY_new());
  43510. p = pub_buf;
  43511. /* Check that key can be successfully decoded. */
  43512. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  43513. pub_len));
  43514. /* Check that key can be successfully encoded. */
  43515. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43516. /* Ensure that the encoded version matches the original. */
  43517. ExpectIntEQ(derLen, pub_len);
  43518. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43519. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43520. * is advanced. */
  43521. tmp = der;
  43522. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43523. ExpectIntEQ(derLen, pub_len);
  43524. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43525. ExpectTrue(der + derLen == tmp);
  43526. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43527. EVP_PKEY_free(pkey);
  43528. EC_KEY_free(ephemeral_key);
  43529. EC_GROUP_free(curve);
  43530. #endif
  43531. return EXPECT_RESULT();
  43532. }
  43533. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  43534. {
  43535. EXPECT_DECLS;
  43536. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  43537. DSA* dsa = NULL;
  43538. byte derIn[] = {
  43539. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  43540. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  43541. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  43542. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  43543. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  43544. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  43545. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  43546. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  43547. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  43548. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  43549. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  43550. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  43551. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  43552. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  43553. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  43554. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  43555. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  43556. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  43557. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  43558. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  43559. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  43560. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  43561. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  43562. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  43563. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  43564. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  43565. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  43566. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  43567. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  43568. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  43569. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  43570. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  43571. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  43572. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  43573. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  43574. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  43575. 0x1e, 0xde, 0xc4
  43576. };
  43577. int derInLen = sizeof(derIn);
  43578. byte* derOut = NULL;
  43579. int derOutLen;
  43580. byte* p = derIn;
  43581. /* Check that params can be successfully decoded. */
  43582. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  43583. /* Check that params can be successfully encoded. */
  43584. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  43585. /* Ensure that the encoded version matches the original. */
  43586. ExpectIntEQ(derInLen, derOutLen);
  43587. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  43588. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43589. DSA_free(dsa);
  43590. #endif
  43591. return EXPECT_RESULT();
  43592. }
  43593. static int test_wolfSSL_i2d_PrivateKey(void)
  43594. {
  43595. EXPECT_DECLS;
  43596. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  43597. !defined(NO_ASN) && !defined(NO_PWDBASED)
  43598. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  43599. {
  43600. EVP_PKEY* pkey = NULL;
  43601. const unsigned char* server_key =
  43602. (const unsigned char*)server_key_der_2048;
  43603. unsigned char buf[FOURK_BUF];
  43604. unsigned char* pt = NULL;
  43605. int bufSz;
  43606. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  43607. (long)sizeof_server_key_der_2048));
  43608. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  43609. pt = buf;
  43610. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  43611. ExpectIntNE((pt - buf), 0);
  43612. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  43613. EVP_PKEY_free(pkey);
  43614. }
  43615. #endif
  43616. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43617. {
  43618. EVP_PKEY* pkey = NULL;
  43619. const unsigned char* client_key =
  43620. (const unsigned char*)ecc_clikey_der_256;
  43621. unsigned char buf[FOURK_BUF];
  43622. unsigned char* pt = NULL;
  43623. int bufSz;
  43624. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  43625. (long)sizeof_ecc_clikey_der_256)));
  43626. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  43627. pt = buf;
  43628. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  43629. ExpectIntNE((pt - buf), 0);
  43630. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  43631. EVP_PKEY_free(pkey);
  43632. }
  43633. #endif
  43634. #endif
  43635. return EXPECT_RESULT();
  43636. }
  43637. static int test_wolfSSL_OCSP_id_get0_info(void)
  43638. {
  43639. EXPECT_DECLS;
  43640. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  43641. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43642. X509* cert = NULL;
  43643. X509* issuer = NULL;
  43644. OCSP_CERTID* id = NULL;
  43645. OCSP_CERTID* id2 = NULL;
  43646. ASN1_STRING* name = NULL;
  43647. ASN1_OBJECT* pmd = NULL;
  43648. ASN1_STRING* keyHash = NULL;
  43649. ASN1_INTEGER* serial = NULL;
  43650. ASN1_INTEGER* x509Int;
  43651. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  43652. SSL_FILETYPE_PEM));
  43653. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  43654. SSL_FILETYPE_PEM));
  43655. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  43656. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  43657. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  43658. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  43659. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  43660. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  43661. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  43662. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  43663. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  43664. ExpectNotNull(serial);
  43665. /* compare serial number to one in cert, should be equal */
  43666. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  43667. ExpectIntEQ(x509Int->length, serial->length);
  43668. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  43669. /* test OCSP_id_cmp */
  43670. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  43671. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  43672. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  43673. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  43674. if (id != NULL) {
  43675. id->issuerHash[0] = ~id->issuerHash[0];
  43676. }
  43677. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  43678. OCSP_CERTID_free(id);
  43679. OCSP_CERTID_free(id2);
  43680. X509_free(cert); /* free's x509Int */
  43681. X509_free(issuer);
  43682. #endif
  43683. return EXPECT_RESULT();
  43684. }
  43685. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  43686. {
  43687. EXPECT_DECLS;
  43688. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  43689. WOLFSSL_OCSP_CERTID certId;
  43690. byte* targetBuffer = NULL;
  43691. byte* p;
  43692. /* OCSP CertID bytes taken from PCAP */
  43693. byte rawCertId[] = {
  43694. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  43695. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  43696. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  43697. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  43698. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  43699. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  43700. 0xcf, 0xbc, 0x91
  43701. };
  43702. int ret = 0;
  43703. int i;
  43704. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  43705. certId.rawCertId = rawCertId;
  43706. certId.rawCertIdSize = sizeof(rawCertId);
  43707. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  43708. DYNAMIC_TYPE_TMP_BUFFER));
  43709. p = targetBuffer;
  43710. /* Function returns the size of the encoded data. */
  43711. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  43712. /* If target buffer is not null, function increments targetBuffer to point
  43713. * just past the end of the encoded data. */
  43714. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  43715. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  43716. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  43717. }
  43718. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43719. targetBuffer = NULL;
  43720. /* If target buffer is null, function allocates memory for a buffer and
  43721. * copies the encoded data into it. targetBuffer then points to the start of
  43722. * this newly allocate buffer. */
  43723. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  43724. sizeof(rawCertId));
  43725. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  43726. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  43727. }
  43728. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  43729. #endif
  43730. return EXPECT_RESULT();
  43731. }
  43732. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  43733. {
  43734. EXPECT_DECLS;
  43735. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  43736. WOLFSSL_OCSP_CERTID* certId;
  43737. WOLFSSL_OCSP_CERTID* certIdGood;
  43738. WOLFSSL_OCSP_CERTID* certIdBad;
  43739. const unsigned char* rawCertIdPtr;
  43740. const unsigned char rawCertId[] = {
  43741. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  43742. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  43743. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  43744. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  43745. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  43746. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  43747. 0xcf, 0xbc, 0x91
  43748. };
  43749. rawCertIdPtr = &rawCertId[0];
  43750. /* If the cert ID is NULL the function should allocate it and copy the
  43751. * data to it. */
  43752. certId = NULL;
  43753. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43754. sizeof(rawCertId)));
  43755. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43756. if (certId != NULL) {
  43757. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43758. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  43759. }
  43760. /* If the cert ID is not NULL the function will just copy the data to it. */
  43761. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  43762. DYNAMIC_TYPE_TMP_BUFFER));
  43763. ExpectNotNull(certId);
  43764. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  43765. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  43766. rawCertIdPtr = &rawCertId[0];
  43767. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43768. sizeof(rawCertId)));
  43769. ExpectPtrEq(certIdGood, certId);
  43770. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43771. if (certId != NULL) {
  43772. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43773. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43774. certId = NULL;
  43775. }
  43776. /* The below tests should fail when passed bad parameters. NULL should
  43777. * always be returned. */
  43778. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  43779. sizeof(rawCertId)));
  43780. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  43781. sizeof(rawCertId)));
  43782. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  43783. #endif
  43784. return EXPECT_RESULT();
  43785. }
  43786. static int test_wolfSSL_OCSP_id_cmp(void)
  43787. {
  43788. EXPECT_DECLS;
  43789. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43790. OCSP_CERTID id1;
  43791. OCSP_CERTID id2;
  43792. XMEMSET(&id1, 0, sizeof(id1));
  43793. XMEMSET(&id2, 0, sizeof(id2));
  43794. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  43795. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  43796. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  43797. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  43798. #endif
  43799. return EXPECT_RESULT();
  43800. }
  43801. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  43802. {
  43803. EXPECT_DECLS;
  43804. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43805. WOLFSSL_OCSP_SINGLERESP single;
  43806. const WOLFSSL_OCSP_CERTID* certId;
  43807. XMEMSET(&single, 0, sizeof(single));
  43808. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  43809. ExpectPtrEq(&single, certId);
  43810. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  43811. #endif
  43812. return EXPECT_RESULT();
  43813. }
  43814. static int test_wolfSSL_OCSP_single_get0_status(void)
  43815. {
  43816. EXPECT_DECLS;
  43817. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43818. WOLFSSL_OCSP_SINGLERESP single;
  43819. CertStatus certStatus;
  43820. WOLFSSL_ASN1_TIME* thisDate;
  43821. WOLFSSL_ASN1_TIME* nextDate;
  43822. int ret, i;
  43823. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43824. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  43825. /* Fill the date fields with some dummy data. */
  43826. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  43827. certStatus.thisDateParsed.data[i] = i;
  43828. certStatus.nextDateParsed.data[i] = i;
  43829. }
  43830. certStatus.status = CERT_GOOD;
  43831. single.status = &certStatus;
  43832. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  43833. &nextDate);
  43834. ExpectIntEQ(ret, CERT_GOOD);
  43835. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  43836. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  43837. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  43838. CERT_GOOD);
  43839. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  43840. NULL), CERT_GOOD);
  43841. #endif
  43842. return EXPECT_RESULT();
  43843. }
  43844. static int test_wolfSSL_OCSP_resp_count(void)
  43845. {
  43846. EXPECT_DECLS;
  43847. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43848. WOLFSSL_OCSP_BASICRESP basicResp;
  43849. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43850. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43851. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43852. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43853. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43854. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  43855. basicResp.single = &singleRespOne;
  43856. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  43857. singleRespOne.next = &singleRespTwo;
  43858. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  43859. #endif
  43860. return EXPECT_RESULT();
  43861. }
  43862. static int test_wolfSSL_OCSP_resp_get0(void)
  43863. {
  43864. EXPECT_DECLS;
  43865. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43866. WOLFSSL_OCSP_BASICRESP basicResp;
  43867. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43868. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43869. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43870. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43871. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43872. basicResp.single = &singleRespOne;
  43873. singleRespOne.next = &singleRespTwo;
  43874. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  43875. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  43876. #endif
  43877. return EXPECT_RESULT();
  43878. }
  43879. static int test_wolfSSL_EVP_PKEY_derive(void)
  43880. {
  43881. EXPECT_DECLS;
  43882. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43883. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  43884. EVP_PKEY_CTX *ctx = NULL;
  43885. unsigned char *skey = NULL;
  43886. size_t skeylen;
  43887. EVP_PKEY *pkey = NULL;
  43888. EVP_PKEY *peerkey = NULL;
  43889. const unsigned char* key;
  43890. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  43891. /* DH */
  43892. key = dh_key_der_2048;
  43893. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43894. sizeof_dh_key_der_2048)));
  43895. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  43896. key = dh_key_der_2048;
  43897. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43898. sizeof_dh_key_der_2048)));
  43899. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  43900. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43901. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43902. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43903. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43904. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43905. DYNAMIC_TYPE_OPENSSL));
  43906. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43907. EVP_PKEY_CTX_free(ctx);
  43908. ctx = NULL;
  43909. EVP_PKEY_free(peerkey);
  43910. peerkey = NULL;
  43911. EVP_PKEY_free(pkey);
  43912. pkey = NULL;
  43913. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43914. skey = NULL;
  43915. #endif
  43916. #ifdef HAVE_ECC
  43917. /* ECDH */
  43918. key = ecc_clikey_der_256;
  43919. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  43920. sizeof_ecc_clikey_der_256)));
  43921. key = ecc_clikeypub_der_256;
  43922. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  43923. sizeof_ecc_clikeypub_der_256)));
  43924. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43925. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43926. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43927. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43928. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43929. DYNAMIC_TYPE_OPENSSL));
  43930. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43931. EVP_PKEY_CTX_free(ctx);
  43932. EVP_PKEY_free(peerkey);
  43933. EVP_PKEY_free(pkey);
  43934. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43935. #endif /* HAVE_ECC */
  43936. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  43937. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43938. return EXPECT_RESULT();
  43939. }
  43940. static int test_wolfSSL_EVP_PBE_scrypt(void)
  43941. {
  43942. EXPECT_DECLS;
  43943. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  43944. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  43945. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  43946. int ret;
  43947. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  43948. int pwdlen = sizeof(pwd);
  43949. const byte salt[] = {'N','a','C','l'};
  43950. int saltlen = sizeof(salt);
  43951. byte key[80];
  43952. word64 numOvr32 = (word64)INT32_MAX + 1;
  43953. /* expected derived key for N:16, r:1, p:1 */
  43954. const byte expectedKey[] = {
  43955. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  43956. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  43957. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  43958. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  43959. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  43960. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  43961. 0xE7, 0xE9, 0xC0, 0x9A};
  43962. /* N r p mx key keylen */
  43963. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  43964. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  43965. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  43966. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  43967. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  43968. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  43969. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  43970. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  43971. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  43972. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  43973. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  43974. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  43975. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  43976. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  43977. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  43978. ExpectIntEQ(ret, 1); /* should succeed */
  43979. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  43980. key, 64);
  43981. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  43982. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  43983. key, 64);
  43984. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  43985. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  43986. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  43987. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  43988. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  43989. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  43990. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  43991. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  43992. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  43993. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  43994. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  43995. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  43996. ExpectIntEQ(ret, 1);
  43997. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  43998. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  43999. #endif /* !NO_PWDBASED && !NO_SHA256 */
  44000. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  44001. return EXPECT_RESULT();
  44002. }
  44003. static int test_no_op_functions(void)
  44004. {
  44005. EXPECT_DECLS;
  44006. #if defined(OPENSSL_EXTRA)
  44007. /* this makes sure wolfSSL can compile and run these no-op functions */
  44008. SSL_load_error_strings();
  44009. ENGINE_load_builtin_engines();
  44010. OpenSSL_add_all_ciphers();
  44011. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  44012. #endif
  44013. return EXPECT_RESULT();
  44014. }
  44015. static int test_wolfSSL_CRYPTO_memcmp(void)
  44016. {
  44017. EXPECT_DECLS;
  44018. #ifdef OPENSSL_EXTRA
  44019. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44020. "implementation of TLS/SSL for embedded devices to the cloud.";
  44021. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44022. "implementation of TLS/SSL for embedded devices to the cloud.";
  44023. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44024. "implementation of TLS/SSL for embedded devices to the cloud!";
  44025. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  44026. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  44027. #endif
  44028. return EXPECT_RESULT();
  44029. }
  44030. /*----------------------------------------------------------------------------*
  44031. | wolfCrypt ASN
  44032. *----------------------------------------------------------------------------*/
  44033. static int test_wc_CreateEncryptedPKCS8Key(void)
  44034. {
  44035. EXPECT_DECLS;
  44036. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44037. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44038. WC_RNG rng;
  44039. byte* encKey = NULL;
  44040. word32 encKeySz = 0;
  44041. word32 decKeySz = 0;
  44042. const char password[] = "Lorem ipsum dolor sit amet";
  44043. word32 passwordSz = (word32)XSTRLEN(password);
  44044. word32 tradIdx = 0;
  44045. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44046. ExpectIntEQ(wc_InitRng(&rng), 0);
  44047. /* Call with NULL for out buffer to get necessary length. */
  44048. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44049. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44050. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44051. LENGTH_ONLY_E);
  44052. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44053. DYNAMIC_TYPE_TMP_BUFFER));
  44054. /* Call with the allocated out buffer. */
  44055. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44056. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44057. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44058. 0);
  44059. /* Decrypt the encrypted PKCS8 key we just made. */
  44060. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44061. passwordSz)), 0);
  44062. /* encKey now holds the decrypted key (decrypted in place). */
  44063. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44064. /* Check that the decrypted key matches the key prior to encryption. */
  44065. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44066. sizeof_server_key_der_2048), 0);
  44067. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44068. wc_FreeRng(&rng);
  44069. #endif
  44070. return EXPECT_RESULT();
  44071. }
  44072. static int test_wc_GetPkcs8TraditionalOffset(void)
  44073. {
  44074. EXPECT_DECLS;
  44075. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44076. int length;
  44077. int derSz = 0;
  44078. word32 inOutIdx;
  44079. const char* path = "./certs/server-keyPkcs8.der";
  44080. XFILE file = XBADFILE;
  44081. byte der[2048];
  44082. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  44083. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  44084. if (file != XBADFILE)
  44085. XFCLOSE(file);
  44086. /* valid case */
  44087. inOutIdx = 0;
  44088. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44089. 0);
  44090. /* inOutIdx > sz */
  44091. inOutIdx = 4000;
  44092. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44093. BAD_FUNC_ARG);
  44094. /* null input */
  44095. inOutIdx = 0;
  44096. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  44097. BAD_FUNC_ARG);
  44098. /* invalid input, fill buffer with 1's */
  44099. XMEMSET(der, 1, sizeof(der));
  44100. inOutIdx = 0;
  44101. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44102. ASN_PARSE_E);
  44103. #endif /* NO_ASN */
  44104. return EXPECT_RESULT();
  44105. }
  44106. static int test_wc_SetSubjectRaw(void)
  44107. {
  44108. EXPECT_DECLS;
  44109. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44110. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44111. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44112. WOLFSSL_X509* x509 = NULL;
  44113. int peerCertSz;
  44114. const byte* peerCertBuf = NULL;
  44115. Cert forgedCert;
  44116. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44117. WOLFSSL_FILETYPE_ASN1));
  44118. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44119. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44120. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44121. wolfSSL_FreeX509(x509);
  44122. #endif
  44123. return EXPECT_RESULT();
  44124. }
  44125. static int test_wc_GetSubjectRaw(void)
  44126. {
  44127. EXPECT_DECLS;
  44128. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44129. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44130. Cert cert;
  44131. byte *subjectRaw;
  44132. ExpectIntEQ(0, wc_InitCert(&cert));
  44133. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44134. #endif
  44135. return EXPECT_RESULT();
  44136. }
  44137. static int test_wc_SetIssuerRaw(void)
  44138. {
  44139. EXPECT_DECLS;
  44140. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44141. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44142. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44143. WOLFSSL_X509* x509 = NULL;
  44144. int peerCertSz;
  44145. const byte* peerCertBuf;
  44146. Cert forgedCert;
  44147. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44148. WOLFSSL_FILETYPE_ASN1));
  44149. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44150. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44151. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44152. wolfSSL_FreeX509(x509);
  44153. #endif
  44154. return EXPECT_RESULT();
  44155. }
  44156. static int test_wc_SetIssueBuffer(void)
  44157. {
  44158. EXPECT_DECLS;
  44159. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44160. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44161. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44162. WOLFSSL_X509* x509 = NULL;
  44163. int peerCertSz;
  44164. const byte* peerCertBuf;
  44165. Cert forgedCert;
  44166. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44167. WOLFSSL_FILETYPE_ASN1));
  44168. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44169. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44170. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44171. wolfSSL_FreeX509(x509);
  44172. #endif
  44173. return EXPECT_RESULT();
  44174. }
  44175. /*
  44176. * Testing wc_SetSubjectKeyId
  44177. */
  44178. static int test_wc_SetSubjectKeyId(void)
  44179. {
  44180. EXPECT_DECLS;
  44181. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44182. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44183. Cert cert;
  44184. const char* file = "certs/ecc-client-keyPub.pem";
  44185. ExpectIntEQ(0, wc_InitCert(&cert));
  44186. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44187. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44188. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44189. #endif
  44190. return EXPECT_RESULT();
  44191. } /* END test_wc_SetSubjectKeyId */
  44192. /*
  44193. * Testing wc_SetSubject
  44194. */
  44195. static int test_wc_SetSubject(void)
  44196. {
  44197. EXPECT_DECLS;
  44198. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44199. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44200. Cert cert;
  44201. const char* file = "./certs/ca-ecc-cert.pem";
  44202. ExpectIntEQ(0, wc_InitCert(&cert));
  44203. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  44204. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44205. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44206. #endif
  44207. return EXPECT_RESULT();
  44208. } /* END test_wc_SetSubject */
  44209. static int test_CheckCertSignature(void)
  44210. {
  44211. EXPECT_DECLS;
  44212. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44213. WOLFSSL_CERT_MANAGER* cm = NULL;
  44214. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44215. XFILE fp = XBADFILE;
  44216. byte cert[4096];
  44217. int certSz;
  44218. #endif
  44219. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44220. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44221. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44222. #ifndef NO_RSA
  44223. #ifdef USE_CERT_BUFFERS_1024
  44224. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44225. sizeof_server_cert_der_1024, NULL, cm));
  44226. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44227. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44228. WOLFSSL_FILETYPE_ASN1));
  44229. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44230. sizeof_server_cert_der_1024, NULL, cm));
  44231. #elif defined(USE_CERT_BUFFERS_2048)
  44232. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  44233. sizeof_server_cert_der_2048, NULL, cm));
  44234. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44235. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  44236. WOLFSSL_FILETYPE_ASN1));
  44237. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  44238. sizeof_server_cert_der_2048, NULL, cm));
  44239. #endif
  44240. #endif
  44241. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44242. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  44243. sizeof_serv_ecc_der_256, NULL, cm));
  44244. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44245. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  44246. WOLFSSL_FILETYPE_ASN1));
  44247. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  44248. NULL, cm));
  44249. #endif
  44250. #if !defined(NO_FILESYSTEM)
  44251. wolfSSL_CertManagerFree(cm);
  44252. cm = NULL;
  44253. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44254. #ifndef NO_RSA
  44255. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  44256. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44257. if (fp != XBADFILE) {
  44258. XFCLOSE(fp);
  44259. fp = XBADFILE;
  44260. }
  44261. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44262. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44263. "./certs/ca-cert.pem", NULL));
  44264. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44265. #endif
  44266. #ifdef HAVE_ECC
  44267. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  44268. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44269. if (fp != XBADFILE) {
  44270. XFCLOSE(fp);
  44271. fp = XBADFILE;
  44272. }
  44273. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44274. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44275. "./certs/ca-ecc-cert.pem", NULL));
  44276. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44277. #endif
  44278. #endif
  44279. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44280. (void)fp;
  44281. (void)cert;
  44282. (void)certSz;
  44283. #endif
  44284. wolfSSL_CertManagerFree(cm);
  44285. #endif
  44286. return EXPECT_RESULT();
  44287. }
  44288. static int test_wc_ParseCert(void)
  44289. {
  44290. EXPECT_DECLS;
  44291. #if !defined(NO_CERTS) && !defined(NO_RSA)
  44292. DecodedCert decodedCert;
  44293. const byte* rawCert = client_cert_der_2048;
  44294. const int rawCertSize = sizeof_client_cert_der_2048;
  44295. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  44296. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44297. #ifndef IGNORE_NAME_CONSTRAINTS
  44298. /* check that the subjects emailAddress was not put in the alt name list */
  44299. ExpectNotNull(decodedCert.subjectEmail);
  44300. ExpectNull(decodedCert.altEmailNames);
  44301. #endif
  44302. wc_FreeDecodedCert(&decodedCert);
  44303. #endif
  44304. return EXPECT_RESULT();
  44305. }
  44306. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  44307. * the API safely errors out on badly-formed ASN input.
  44308. * NOTE: Test not compatible with released FIPS implementations!
  44309. */
  44310. static int test_wc_ParseCert_Error(void)
  44311. {
  44312. EXPECT_DECLS;
  44313. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  44314. (!defined(HAVE_FIPS) || \
  44315. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  44316. DecodedCert decodedCert;
  44317. int i;
  44318. /* Certificate data */
  44319. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  44320. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44321. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44322. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  44323. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  44324. /* Test data */
  44325. const struct testStruct {
  44326. const byte* c;
  44327. const int cSz;
  44328. const int expRet;
  44329. } t[] = {
  44330. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  44331. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  44332. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  44333. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  44334. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  44335. };
  44336. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  44337. for (i = 0; i < tSz; i++) {
  44338. WOLFSSL_MSG_EX("i == %d", i);
  44339. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  44340. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  44341. wc_FreeDecodedCert(&decodedCert);
  44342. }
  44343. #endif
  44344. return EXPECT_RESULT();
  44345. }
  44346. static int test_MakeCertWithPathLen(void)
  44347. {
  44348. EXPECT_DECLS;
  44349. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  44350. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44351. const byte expectedPathLen = 7;
  44352. Cert cert;
  44353. DecodedCert decodedCert;
  44354. byte der[FOURK_BUF];
  44355. int derSize = 0;
  44356. WC_RNG rng;
  44357. ecc_key key;
  44358. int ret;
  44359. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44360. XMEMSET(&key, 0, sizeof(ecc_key));
  44361. XMEMSET(&cert, 0, sizeof(Cert));
  44362. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44363. ExpectIntEQ(wc_InitRng(&rng), 0);
  44364. ExpectIntEQ(wc_ecc_init(&key), 0);
  44365. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44366. ExpectIntEQ(wc_InitCert(&cert), 0);
  44367. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44368. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44369. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44370. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44371. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44372. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44373. CTC_NAME_SIZE);
  44374. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44375. CTC_NAME_SIZE);
  44376. cert.selfSigned = 1;
  44377. cert.isCA = 1;
  44378. cert.pathLen = expectedPathLen;
  44379. cert.pathLenSet = 1;
  44380. cert.sigType = CTC_SHA256wECDSA;
  44381. #ifdef WOLFSSL_CERT_EXT
  44382. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  44383. #endif
  44384. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44385. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44386. FOURK_BUF, NULL, &key, &rng), 0);
  44387. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44388. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44389. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  44390. wc_FreeDecodedCert(&decodedCert);
  44391. ret = wc_ecc_free(&key);
  44392. ExpectIntEQ(ret, 0);
  44393. ret = wc_FreeRng(&rng);
  44394. ExpectIntEQ(ret, 0);
  44395. #endif
  44396. return EXPECT_RESULT();
  44397. }
  44398. static int test_MakeCertWithCaFalse(void)
  44399. {
  44400. EXPECT_DECLS;
  44401. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  44402. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44403. const byte expectedIsCa = 0;
  44404. Cert cert;
  44405. DecodedCert decodedCert;
  44406. byte der[FOURK_BUF];
  44407. int derSize = 0;
  44408. WC_RNG rng;
  44409. ecc_key key;
  44410. int ret;
  44411. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44412. XMEMSET(&key, 0, sizeof(ecc_key));
  44413. XMEMSET(&cert, 0, sizeof(Cert));
  44414. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44415. ExpectIntEQ(wc_InitRng(&rng), 0);
  44416. ExpectIntEQ(wc_ecc_init(&key), 0);
  44417. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44418. ExpectIntEQ(wc_InitCert(&cert), 0);
  44419. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44420. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44421. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44422. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44423. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44424. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44425. CTC_NAME_SIZE);
  44426. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44427. CTC_NAME_SIZE);
  44428. cert.selfSigned = 1;
  44429. cert.isCA = expectedIsCa;
  44430. cert.isCaSet = 1;
  44431. cert.sigType = CTC_SHA256wECDSA;
  44432. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44433. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44434. FOURK_BUF, NULL, &key, &rng), 0);
  44435. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44436. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44437. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  44438. wc_FreeDecodedCert(&decodedCert);
  44439. ret = wc_ecc_free(&key);
  44440. ExpectIntEQ(ret, 0);
  44441. ret = wc_FreeRng(&rng);
  44442. ExpectIntEQ(ret, 0);
  44443. #endif
  44444. return EXPECT_RESULT();
  44445. }
  44446. /*----------------------------------------------------------------------------*
  44447. | wolfCrypt ECC
  44448. *----------------------------------------------------------------------------*/
  44449. static int test_wc_ecc_get_curve_size_from_name(void)
  44450. {
  44451. EXPECT_DECLS;
  44452. #ifdef HAVE_ECC
  44453. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44454. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  44455. #endif
  44456. /* invalid case */
  44457. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  44458. /* NULL input */
  44459. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  44460. #endif /* HAVE_ECC */
  44461. return EXPECT_RESULT();
  44462. }
  44463. static int test_wc_ecc_get_curve_id_from_name(void)
  44464. {
  44465. EXPECT_DECLS;
  44466. #ifdef HAVE_ECC
  44467. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44468. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  44469. ECC_SECP256R1);
  44470. #endif
  44471. /* invalid case */
  44472. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  44473. /* NULL input */
  44474. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  44475. #endif /* HAVE_ECC */
  44476. return EXPECT_RESULT();
  44477. }
  44478. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44479. !defined(HAVE_SELFTEST) && \
  44480. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44481. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  44482. {
  44483. EXPECT_DECLS;
  44484. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44485. ecc_key* key;
  44486. const ecc_set_type* params = NULL;
  44487. int ret;
  44488. #endif
  44489. WOLFSSL_EC_KEY *ecKey = NULL;
  44490. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44491. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  44492. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  44493. if (EXPECT_SUCCESS()) {
  44494. ret = EC_KEY_generate_key(ecKey);
  44495. } else
  44496. ret = 0;
  44497. if (ret == 1) {
  44498. /* normal test */
  44499. key = (ecc_key*)ecKey->internal;
  44500. if (key != NULL) {
  44501. params = key->dp;
  44502. }
  44503. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  44504. ECC_SECP256R1);
  44505. }
  44506. #endif
  44507. /* invalid case, NULL input*/
  44508. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  44509. wolfSSL_EC_KEY_free(ecKey);
  44510. return EXPECT_RESULT();
  44511. }
  44512. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  44513. static int test_wc_ecc_get_curve_id_from_params(void)
  44514. {
  44515. EXPECT_DECLS;
  44516. #ifdef HAVE_ECC
  44517. const byte prime[] =
  44518. {
  44519. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44520. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44521. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44522. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  44523. };
  44524. const byte primeInvalid[] =
  44525. {
  44526. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44527. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44528. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44529. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  44530. };
  44531. const byte Af[] =
  44532. {
  44533. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44534. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44535. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44536. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  44537. };
  44538. const byte Bf[] =
  44539. {
  44540. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  44541. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  44542. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  44543. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  44544. };
  44545. const byte order[] =
  44546. {
  44547. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  44548. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  44549. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  44550. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  44551. };
  44552. const byte Gx[] =
  44553. {
  44554. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  44555. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  44556. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  44557. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  44558. };
  44559. const byte Gy[] =
  44560. {
  44561. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  44562. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  44563. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  44564. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  44565. };
  44566. int cofactor = 1;
  44567. int fieldSize = 256;
  44568. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44569. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44570. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  44571. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  44572. ECC_SECP256R1);
  44573. #endif
  44574. /* invalid case, fieldSize = 0 */
  44575. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  44576. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44577. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44578. /* invalid case, NULL prime */
  44579. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  44580. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44581. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  44582. /* invalid case, invalid prime */
  44583. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44584. primeInvalid, sizeof(primeInvalid),
  44585. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44586. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44587. #endif
  44588. return EXPECT_RESULT();
  44589. }
  44590. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  44591. {
  44592. EXPECT_DECLS;
  44593. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44594. !defined(HAVE_FAST_RSA)
  44595. WOLFSSL_RSA* rsa = NULL;
  44596. WOLFSSL_EVP_PKEY* pkey = NULL;
  44597. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  44598. const char* in = "What is easy to do is easy not to do.";
  44599. size_t inlen = XSTRLEN(in);
  44600. size_t outEncLen = 0;
  44601. byte* outEnc = NULL;
  44602. byte* outDec = NULL;
  44603. size_t outDecLen = 0;
  44604. size_t rsaKeySz = 2048/8; /* Bytes */
  44605. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44606. byte* inTmp = NULL;
  44607. byte* outEncTmp = NULL;
  44608. byte* outDecTmp = NULL;
  44609. #endif
  44610. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44611. DYNAMIC_TYPE_TMP_BUFFER));
  44612. if (outEnc != NULL) {
  44613. XMEMSET(outEnc, 0, rsaKeySz);
  44614. }
  44615. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44616. DYNAMIC_TYPE_TMP_BUFFER));
  44617. if (outDec != NULL) {
  44618. XMEMSET(outDec, 0, rsaKeySz);
  44619. }
  44620. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44621. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44622. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44623. if (EXPECT_FAIL()) {
  44624. RSA_free(rsa);
  44625. }
  44626. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44627. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  44628. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44629. WOLFSSL_SUCCESS);
  44630. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  44631. since ctx uses it.*/
  44632. ExpectIntEQ(pkey->ref.count, 2);
  44633. EVP_PKEY_free(pkey);
  44634. ExpectIntEQ(pkey->ref.count, 1);
  44635. /* Encrypt data */
  44636. /* Check that we can get the required output buffer length by passing in a
  44637. * NULL output buffer. */
  44638. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  44639. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  44640. ExpectIntEQ(rsaKeySz, outEncLen);
  44641. /* Now do the actual encryption. */
  44642. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  44643. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  44644. /* Decrypt data */
  44645. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  44646. /* Check that we can get the required output buffer length by passing in a
  44647. * NULL output buffer. */
  44648. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  44649. WOLFSSL_SUCCESS);
  44650. ExpectIntEQ(rsaKeySz, outDecLen);
  44651. /* Now do the actual decryption. */
  44652. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  44653. WOLFSSL_SUCCESS);
  44654. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  44655. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44656. /* The input length must be the same size as the RSA key.*/
  44657. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44658. DYNAMIC_TYPE_TMP_BUFFER));
  44659. if (inTmp != NULL) {
  44660. XMEMSET(inTmp, 9, rsaKeySz);
  44661. }
  44662. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44663. DYNAMIC_TYPE_TMP_BUFFER));
  44664. if (outEncTmp != NULL) {
  44665. XMEMSET(outEncTmp, 0, rsaKeySz);
  44666. }
  44667. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  44668. DYNAMIC_TYPE_TMP_BUFFER));
  44669. if (outDecTmp != NULL) {
  44670. XMEMSET(outDecTmp, 0, rsaKeySz);
  44671. }
  44672. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  44673. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  44674. WOLFSSL_SUCCESS);
  44675. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  44676. WOLFSSL_SUCCESS);
  44677. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  44678. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  44679. outEncLen), WOLFSSL_SUCCESS);
  44680. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  44681. #endif
  44682. EVP_PKEY_CTX_free(ctx);
  44683. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44684. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44685. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  44686. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44687. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44688. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44689. #endif
  44690. #endif
  44691. return EXPECT_RESULT();
  44692. }
  44693. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44694. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44695. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44696. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44697. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44698. #endif
  44699. #endif
  44700. #endif
  44701. #if defined(OPENSSL_EXTRA)
  44702. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44703. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44704. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44705. #endif
  44706. #endif
  44707. #endif
  44708. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44709. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44710. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44711. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44712. #endif
  44713. #endif
  44714. #endif
  44715. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  44716. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  44717. {
  44718. EXPECT_DECLS;
  44719. #if defined(OPENSSL_EXTRA)
  44720. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44721. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44722. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44723. WOLFSSL_RSA* rsa = NULL;
  44724. #endif
  44725. #endif
  44726. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44727. WOLFSSL_DSA* dsa = NULL;
  44728. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44729. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44730. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44731. WOLFSSL_EC_KEY* ecKey = NULL;
  44732. #endif
  44733. #endif
  44734. WOLFSSL_EVP_PKEY* pkey = NULL;
  44735. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  44736. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  44737. const char* in = "What is easy to do is easy not to do.";
  44738. size_t inlen = XSTRLEN(in);
  44739. byte hash[SHA256_DIGEST_LENGTH] = {0};
  44740. byte zero[SHA256_DIGEST_LENGTH] = {0};
  44741. SHA256_CTX c;
  44742. byte* sig = NULL;
  44743. byte* sigVerify = NULL;
  44744. size_t siglen;
  44745. size_t siglenOnlyLen;
  44746. size_t keySz = 2048/8; /* Bytes */
  44747. ExpectNotNull(sig =
  44748. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  44749. ExpectNotNull(sigVerify =
  44750. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  44751. siglen = keySz;
  44752. ExpectNotNull(XMEMSET(sig, 0, keySz));
  44753. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  44754. /* Generate hash */
  44755. SHA256_Init(&c);
  44756. SHA256_Update(&c, in, inlen);
  44757. SHA256_Final(hash, &c);
  44758. #ifdef WOLFSSL_SMALL_STACK_CACHE
  44759. /* workaround for small stack cache case */
  44760. wc_Sha256Free((wc_Sha256*)&c);
  44761. #endif
  44762. /* Generate key */
  44763. ExpectNotNull(pkey = EVP_PKEY_new());
  44764. switch (keyType) {
  44765. case EVP_PKEY_RSA:
  44766. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44767. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44768. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44769. {
  44770. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44771. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44772. }
  44773. #endif
  44774. #endif
  44775. break;
  44776. case EVP_PKEY_DSA:
  44777. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44778. ExpectNotNull(dsa = DSA_new());
  44779. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  44780. NULL, 0, NULL, NULL, NULL), 1);
  44781. ExpectIntEQ(DSA_generate_key(dsa), 1);
  44782. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  44783. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44784. break;
  44785. case EVP_PKEY_EC:
  44786. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44787. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44788. {
  44789. ExpectNotNull(ecKey = EC_KEY_new());
  44790. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  44791. ExpectIntEQ(
  44792. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44793. if (EXPECT_FAIL()) {
  44794. EC_KEY_free(ecKey);
  44795. }
  44796. }
  44797. #endif
  44798. #endif
  44799. break;
  44800. }
  44801. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44802. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44803. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44804. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44805. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44806. if (keyType == EVP_PKEY_RSA)
  44807. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44808. WOLFSSL_SUCCESS);
  44809. #endif
  44810. #endif
  44811. /* Check returning only length */
  44812. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  44813. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44814. ExpectIntGT(siglenOnlyLen, 0);
  44815. /* Sign data */
  44816. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  44817. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44818. ExpectIntGE(siglenOnlyLen, siglen);
  44819. /* Verify signature */
  44820. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  44821. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44822. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44823. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44824. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44825. if (keyType == EVP_PKEY_RSA)
  44826. ExpectIntEQ(
  44827. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  44828. WOLFSSL_SUCCESS);
  44829. #endif
  44830. #endif
  44831. ExpectIntEQ(EVP_PKEY_verify(
  44832. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  44833. WOLFSSL_SUCCESS);
  44834. ExpectIntEQ(EVP_PKEY_verify(
  44835. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  44836. WOLFSSL_FAILURE);
  44837. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44838. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44839. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44840. if (keyType == EVP_PKEY_RSA) {
  44841. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  44842. /* Try RSA sign/verify with no padding. */
  44843. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44844. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  44845. WOLFSSL_SUCCESS);
  44846. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44847. siglen), WOLFSSL_SUCCESS);
  44848. ExpectIntGE(siglenOnlyLen, siglen);
  44849. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44850. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44851. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  44852. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44853. siglen), WOLFSSL_SUCCESS);
  44854. #endif
  44855. /* Wrong padding schemes. */
  44856. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44857. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  44858. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44859. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44860. siglen), WOLFSSL_SUCCESS);
  44861. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44862. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44863. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44864. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44865. siglen), WOLFSSL_SUCCESS);
  44866. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44867. WOLFSSL_SUCCESS);
  44868. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44869. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  44870. }
  44871. #endif
  44872. #endif
  44873. /* error cases */
  44874. siglen = keySz; /* Reset because sig size may vary slightly */
  44875. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  44876. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44877. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  44878. WOLFSSL_SUCCESS);
  44879. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  44880. WOLFSSL_SUCCESS);
  44881. EVP_PKEY_free(pkey);
  44882. pkey = NULL;
  44883. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44884. DSA_free(dsa);
  44885. dsa = NULL;
  44886. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44887. EVP_PKEY_CTX_free(ctx_verify);
  44888. ctx_verify = NULL;
  44889. EVP_PKEY_CTX_free(ctx);
  44890. ctx = NULL;
  44891. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44892. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44893. #endif /* OPENSSL_EXTRA */
  44894. return EXPECT_RESULT();
  44895. }
  44896. #endif
  44897. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  44898. {
  44899. EXPECT_DECLS;
  44900. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44901. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44902. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44903. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  44904. #endif
  44905. #endif
  44906. return EXPECT_RESULT();
  44907. }
  44908. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  44909. {
  44910. EXPECT_DECLS;
  44911. #if defined(OPENSSL_EXTRA)
  44912. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44913. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  44914. #endif
  44915. #endif
  44916. return EXPECT_RESULT();
  44917. }
  44918. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  44919. {
  44920. EXPECT_DECLS;
  44921. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44922. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44923. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  44924. #endif
  44925. #endif
  44926. return EXPECT_RESULT();
  44927. }
  44928. static int test_EVP_PKEY_rsa(void)
  44929. {
  44930. EXPECT_DECLS;
  44931. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44932. WOLFSSL_RSA* rsa = NULL;
  44933. WOLFSSL_EVP_PKEY* pkey = NULL;
  44934. ExpectNotNull(rsa = wolfSSL_RSA_new());
  44935. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44936. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  44937. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  44938. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44939. if (EXPECT_FAIL()) {
  44940. wolfSSL_RSA_free(rsa);
  44941. }
  44942. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  44943. wolfSSL_EVP_PKEY_free(pkey);
  44944. #endif
  44945. return EXPECT_RESULT();
  44946. }
  44947. static int test_EVP_PKEY_ec(void)
  44948. {
  44949. EXPECT_DECLS;
  44950. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44951. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44952. WOLFSSL_EC_KEY* ecKey = NULL;
  44953. WOLFSSL_EVP_PKEY* pkey = NULL;
  44954. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44955. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44956. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  44957. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  44958. /* Should fail since ecKey is empty */
  44959. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  44960. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44961. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44962. if (EXPECT_FAIL()) {
  44963. wolfSSL_EC_KEY_free(ecKey);
  44964. }
  44965. wolfSSL_EVP_PKEY_free(pkey);
  44966. #endif
  44967. #endif
  44968. return EXPECT_RESULT();
  44969. }
  44970. static int test_EVP_PKEY_cmp(void)
  44971. {
  44972. EXPECT_DECLS;
  44973. #if defined(OPENSSL_EXTRA)
  44974. EVP_PKEY *a = NULL;
  44975. EVP_PKEY *b = NULL;
  44976. const unsigned char *in;
  44977. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44978. in = client_key_der_2048;
  44979. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44980. &in, (long)sizeof_client_key_der_2048));
  44981. in = client_key_der_2048;
  44982. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44983. &in, (long)sizeof_client_key_der_2048));
  44984. /* Test success case RSA */
  44985. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44986. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  44987. #else
  44988. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44989. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44990. EVP_PKEY_free(b);
  44991. b = NULL;
  44992. EVP_PKEY_free(a);
  44993. a = NULL;
  44994. #endif
  44995. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44996. in = ecc_clikey_der_256;
  44997. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44998. &in, (long)sizeof_ecc_clikey_der_256));
  44999. in = ecc_clikey_der_256;
  45000. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45001. &in, (long)sizeof_ecc_clikey_der_256));
  45002. /* Test success case ECC */
  45003. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45004. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  45005. #else
  45006. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45007. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45008. EVP_PKEY_free(b);
  45009. b = NULL;
  45010. EVP_PKEY_free(a);
  45011. a = NULL;
  45012. #endif
  45013. /* Test failure cases */
  45014. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  45015. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45016. in = client_key_der_2048;
  45017. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45018. &in, (long)sizeof_client_key_der_2048));
  45019. in = ecc_clikey_der_256;
  45020. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45021. &in, (long)sizeof_ecc_clikey_der_256));
  45022. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45023. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  45024. #else
  45025. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45026. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45027. EVP_PKEY_free(b);
  45028. b = NULL;
  45029. EVP_PKEY_free(a);
  45030. a = NULL;
  45031. #endif
  45032. /* invalid or empty failure cases */
  45033. a = EVP_PKEY_new();
  45034. b = EVP_PKEY_new();
  45035. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45036. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45037. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45038. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45039. #ifdef NO_RSA
  45040. /* Type check will fail since RSA is the default EVP key type */
  45041. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  45042. #else
  45043. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45044. #endif
  45045. #else
  45046. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45047. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45048. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45049. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45050. #endif
  45051. EVP_PKEY_free(b);
  45052. EVP_PKEY_free(a);
  45053. (void)in;
  45054. #endif
  45055. return EXPECT_RESULT();
  45056. }
  45057. static int test_ERR_load_crypto_strings(void)
  45058. {
  45059. #if defined(OPENSSL_ALL)
  45060. ERR_load_crypto_strings();
  45061. return TEST_SUCCESS;
  45062. #else
  45063. return TEST_SKIPPED;
  45064. #endif
  45065. }
  45066. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45067. static void free_x509(X509* x)
  45068. {
  45069. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45070. }
  45071. #endif
  45072. static int test_sk_X509(void)
  45073. {
  45074. EXPECT_DECLS;
  45075. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45076. {
  45077. STACK_OF(X509)* s = NULL;
  45078. ExpectNotNull(s = sk_X509_new_null());
  45079. ExpectIntEQ(sk_X509_num(s), 0);
  45080. sk_X509_pop_free(s, NULL);
  45081. ExpectNotNull(s = sk_X509_new_null());
  45082. ExpectIntEQ(sk_X509_num(s), 0);
  45083. sk_X509_pop_free(s, NULL);
  45084. ExpectNotNull(s = sk_X509_new_null());
  45085. sk_X509_push(s, (X509*)1);
  45086. ExpectIntEQ(sk_X509_num(s), 1);
  45087. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45088. sk_X509_push(s, (X509*)2);
  45089. ExpectIntEQ(sk_X509_num(s), 2);
  45090. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45091. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45092. sk_X509_push(s, (X509*)2);
  45093. sk_X509_pop_free(s, free_x509);
  45094. }
  45095. {
  45096. /* Push a list of 10 X509s onto stack, then verify that
  45097. * value(), push(), shift(), and pop() behave as expected. */
  45098. STACK_OF(X509)* s = NULL;
  45099. X509* xList[10];
  45100. int i = 0;
  45101. const int len = (sizeof(xList) / sizeof(xList[0]));
  45102. for (i = 0; i < len; ++i) {
  45103. xList[i] = NULL;
  45104. ExpectNotNull(xList[i] = X509_new());
  45105. }
  45106. /* test push, pop, and free */
  45107. ExpectNotNull(s = sk_X509_new_null());
  45108. for (i = 0; i < len; ++i) {
  45109. sk_X509_push(s, xList[i]);
  45110. ExpectIntEQ(sk_X509_num(s), i + 1);
  45111. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45112. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45113. }
  45114. /* pop returns and removes last pushed on stack, which is index 0
  45115. * in sk_x509_value */
  45116. for (i = 0; i < len; ++i) {
  45117. X509 * x = sk_X509_value(s, 0);
  45118. X509 * y = sk_X509_pop(s);
  45119. X509 * z = xList[len - 1 - i];
  45120. ExpectIntEQ((x == y), 1);
  45121. ExpectIntEQ((x == z), 1);
  45122. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45123. }
  45124. sk_free(s);
  45125. s = NULL;
  45126. /* test push, shift, and free */
  45127. ExpectNotNull(s = sk_X509_new_null());
  45128. for (i = 0; i < len; ++i) {
  45129. sk_X509_push(s, xList[i]);
  45130. ExpectIntEQ(sk_X509_num(s), i + 1);
  45131. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45132. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45133. }
  45134. /* shift returns and removes first pushed on stack, which is index i
  45135. * in sk_x509_value() */
  45136. for (i = 0; i < len; ++i) {
  45137. X509 * x = sk_X509_value(s, len - 1 - i);
  45138. X509 * y = sk_X509_shift(s);
  45139. X509 * z = xList[i];
  45140. ExpectIntEQ((x == y), 1);
  45141. ExpectIntEQ((x == z), 1);
  45142. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45143. }
  45144. sk_free(s);
  45145. for (i = 0; i < len; ++i)
  45146. X509_free(xList[i]);
  45147. }
  45148. #endif
  45149. return EXPECT_RESULT();
  45150. }
  45151. static int test_sk_X509_CRL(void)
  45152. {
  45153. EXPECT_DECLS;
  45154. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45155. X509_CRL* crl = NULL;
  45156. XFILE fp = XBADFILE;
  45157. STACK_OF(X509_CRL)* s = NULL;
  45158. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  45159. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45160. NULL, NULL));
  45161. if (fp != XBADFILE)
  45162. XFCLOSE(fp);
  45163. ExpectNotNull(s = sk_X509_CRL_new());
  45164. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  45165. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  45166. if (EXPECT_FAIL()) {
  45167. X509_CRL_free(crl);
  45168. }
  45169. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  45170. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  45171. sk_X509_CRL_free(s);
  45172. #endif
  45173. return EXPECT_RESULT();
  45174. }
  45175. static int test_X509_get_signature_nid(void)
  45176. {
  45177. EXPECT_DECLS;
  45178. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45179. X509* x509 = NULL;
  45180. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  45181. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45182. SSL_FILETYPE_PEM));
  45183. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45184. X509_free(x509);
  45185. #endif
  45186. return EXPECT_RESULT();
  45187. }
  45188. static int test_X509_REQ(void)
  45189. {
  45190. EXPECT_DECLS;
  45191. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45192. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45193. X509_NAME* name = NULL;
  45194. #ifndef NO_RSA
  45195. X509_NAME* subject = NULL;
  45196. #endif
  45197. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45198. X509_REQ* req = NULL;
  45199. EVP_PKEY* priv = NULL;
  45200. EVP_PKEY* pub = NULL;
  45201. unsigned char* der = NULL;
  45202. int len;
  45203. #endif
  45204. #ifndef NO_RSA
  45205. EVP_MD_CTX *mctx = NULL;
  45206. EVP_PKEY_CTX *pkctx = NULL;
  45207. #ifdef USE_CERT_BUFFERS_1024
  45208. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45209. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45210. #elif defined(USE_CERT_BUFFERS_2048)
  45211. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45212. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45213. #endif
  45214. #endif
  45215. #ifdef HAVE_ECC
  45216. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45217. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45218. #endif
  45219. ExpectNotNull(name = X509_NAME_new());
  45220. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45221. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  45222. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45223. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  45224. #ifndef NO_RSA
  45225. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45226. (long)sizeof_client_key_der_2048));
  45227. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45228. (long)sizeof_client_keypub_der_2048));
  45229. ExpectNotNull(req = X509_REQ_new());
  45230. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45231. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  45232. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45233. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  45234. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  45235. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45236. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  45237. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45238. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  45239. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45240. len = i2d_X509_REQ(req, &der);
  45241. DEBUG_WRITE_DER(der, len, "req.der");
  45242. #ifdef USE_CERT_BUFFERS_1024
  45243. ExpectIntEQ(len, 381);
  45244. #else
  45245. ExpectIntEQ(len, 643);
  45246. #endif
  45247. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45248. der = NULL;
  45249. mctx = EVP_MD_CTX_new();
  45250. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  45251. WOLFSSL_SUCCESS);
  45252. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  45253. EVP_MD_CTX_free(mctx);
  45254. mctx = NULL;
  45255. X509_REQ_free(NULL);
  45256. X509_REQ_free(req);
  45257. req = NULL;
  45258. /* Test getting the subject from a newly created X509_REQ */
  45259. ExpectNotNull(req = X509_REQ_new());
  45260. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  45261. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  45262. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  45263. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  45264. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  45265. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  45266. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  45267. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  45268. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  45269. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  45270. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  45271. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  45272. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  45273. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45274. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45275. len = i2d_X509_REQ(req, &der);
  45276. DEBUG_WRITE_DER(der, len, "req2.der");
  45277. #ifdef USE_CERT_BUFFERS_1024
  45278. ExpectIntEQ(len, 435);
  45279. #else
  45280. ExpectIntEQ(len, 696);
  45281. #endif
  45282. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45283. der = NULL;
  45284. EVP_PKEY_free(pub);
  45285. pub = NULL;
  45286. EVP_PKEY_free(priv);
  45287. priv = NULL;
  45288. X509_REQ_free(req);
  45289. req = NULL;
  45290. #endif
  45291. #ifdef HAVE_ECC
  45292. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  45293. sizeof_ecc_clikey_der_256));
  45294. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  45295. sizeof_ecc_clikeypub_der_256));
  45296. ExpectNotNull(req = X509_REQ_new());
  45297. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45298. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45299. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45300. /* Signature is random and may be shorter or longer. */
  45301. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  45302. ExpectIntLE(len, 253);
  45303. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45304. X509_REQ_free(req);
  45305. EVP_PKEY_free(pub);
  45306. EVP_PKEY_free(priv);
  45307. #ifdef FP_ECC
  45308. wc_ecc_fp_free();
  45309. #endif
  45310. #endif /* HAVE_ECC */
  45311. X509_NAME_free(name);
  45312. #endif
  45313. return EXPECT_RESULT();
  45314. }
  45315. static int test_wolfssl_PKCS7(void)
  45316. {
  45317. EXPECT_DECLS;
  45318. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45319. !defined(NO_RSA)
  45320. PKCS7* pkcs7 = NULL;
  45321. byte data[FOURK_BUF];
  45322. word32 len = sizeof(data);
  45323. const byte* p = data;
  45324. byte content[] = "Test data to encode.";
  45325. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45326. BIO* bio = NULL;
  45327. byte key[sizeof(client_key_der_2048)];
  45328. word32 keySz = (word32)sizeof(key);
  45329. byte* out = NULL;
  45330. #endif
  45331. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  45332. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  45333. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  45334. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  45335. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45336. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  45337. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  45338. PKCS7_free(pkcs7);
  45339. pkcs7 = NULL;
  45340. /* fail case, without PKCS7_NOVERIFY */
  45341. p = data;
  45342. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45343. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45344. 0), WOLFSSL_FAILURE);
  45345. PKCS7_free(pkcs7);
  45346. pkcs7 = NULL;
  45347. /* success case, with PKCS7_NOVERIFY */
  45348. p = data;
  45349. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45350. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45351. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  45352. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45353. /* test i2d */
  45354. XMEMCPY(key, client_key_der_2048, keySz);
  45355. if (pkcs7 != NULL) {
  45356. pkcs7->privateKey = key;
  45357. pkcs7->privateKeySz = (word32)sizeof(key);
  45358. pkcs7->encryptOID = RSAk;
  45359. #ifdef NO_SHA
  45360. pkcs7->hashOID = SHA256h;
  45361. #else
  45362. pkcs7->hashOID = SHAh;
  45363. #endif
  45364. }
  45365. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45366. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  45367. #ifndef NO_ASN_TIME
  45368. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  45369. #else
  45370. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  45371. #endif
  45372. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45373. BIO_free(bio);
  45374. #endif
  45375. PKCS7_free(NULL);
  45376. PKCS7_free(pkcs7);
  45377. #endif
  45378. return EXPECT_RESULT();
  45379. }
  45380. static int test_wolfSSL_PKCS7_sign(void)
  45381. {
  45382. EXPECT_DECLS;
  45383. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45384. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45385. PKCS7* p7 = NULL;
  45386. PKCS7* p7Ver = NULL;
  45387. byte* out = NULL;
  45388. byte* tmpPtr = NULL;
  45389. int outLen = 0;
  45390. int flags = 0;
  45391. byte data[] = "Test data to encode.";
  45392. const char* cert = "./certs/server-cert.pem";
  45393. const char* key = "./certs/server-key.pem";
  45394. const char* ca = "./certs/ca-cert.pem";
  45395. WOLFSSL_BIO* certBio = NULL;
  45396. WOLFSSL_BIO* keyBio = NULL;
  45397. WOLFSSL_BIO* caBio = NULL;
  45398. WOLFSSL_BIO* inBio = NULL;
  45399. X509* signCert = NULL;
  45400. EVP_PKEY* signKey = NULL;
  45401. X509* caCert = NULL;
  45402. X509_STORE* store = NULL;
  45403. /* read signer cert/key into BIO */
  45404. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45405. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45406. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45407. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45408. /* read CA cert into store (for verify) */
  45409. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45410. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45411. ExpectNotNull(store = X509_STORE_new());
  45412. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45413. /* data to be signed into BIO */
  45414. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45415. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45416. /* PKCS7_sign, bad args: signer NULL */
  45417. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  45418. /* PKCS7_sign, bad args: signer key NULL */
  45419. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  45420. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  45421. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  45422. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  45423. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  45424. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  45425. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  45426. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  45427. {
  45428. flags = PKCS7_BINARY;
  45429. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45430. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45431. /* verify with d2i_PKCS7 */
  45432. tmpPtr = out;
  45433. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45434. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45435. PKCS7_free(p7Ver);
  45436. p7Ver = NULL;
  45437. /* verify with wc_PKCS7_VerifySignedData */
  45438. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45439. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  45440. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45441. /* compare the signer found to expected signer */
  45442. ExpectIntNE(p7Ver->verifyCertSz, 0);
  45443. tmpPtr = NULL;
  45444. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  45445. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  45446. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  45447. tmpPtr = NULL;
  45448. wc_PKCS7_Free(p7Ver);
  45449. p7Ver = NULL;
  45450. ExpectNotNull(out);
  45451. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45452. out = NULL;
  45453. PKCS7_free(p7);
  45454. p7 = NULL;
  45455. }
  45456. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  45457. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  45458. {
  45459. /* re-populate input BIO, may have been consumed */
  45460. BIO_free(inBio);
  45461. inBio = NULL;
  45462. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45463. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45464. flags = PKCS7_BINARY | PKCS7_STREAM;
  45465. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45466. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45467. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45468. /* PKCS7_final, bad args: PKCS7 null */
  45469. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  45470. /* PKCS7_final, bad args: PKCS7 null */
  45471. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  45472. tmpPtr = out;
  45473. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45474. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45475. PKCS7_free(p7Ver);
  45476. p7Ver = NULL;
  45477. ExpectNotNull(out);
  45478. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45479. out = NULL;
  45480. PKCS7_free(p7);
  45481. p7 = NULL;
  45482. }
  45483. /* TEST SUCCESS: Detached, not streaming, not MIME */
  45484. {
  45485. /* re-populate input BIO, may have been consumed */
  45486. BIO_free(inBio);
  45487. inBio = NULL;
  45488. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45489. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45490. flags = PKCS7_BINARY | PKCS7_DETACHED;
  45491. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45492. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45493. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45494. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45495. if (p7Ver != NULL) {
  45496. p7Ver->content = data;
  45497. p7Ver->contentSz = sizeof(data);
  45498. }
  45499. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45500. wc_PKCS7_Free(p7Ver);
  45501. p7Ver = NULL;
  45502. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  45503. * yet support detached content */
  45504. tmpPtr = out;
  45505. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45506. PKCS7_free(p7Ver);
  45507. p7Ver = NULL;
  45508. ExpectNotNull(out);
  45509. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45510. out = NULL;
  45511. PKCS7_free(p7);
  45512. p7 = NULL;
  45513. }
  45514. /* TEST SUCCESS: Detached, streaming, not MIME */
  45515. {
  45516. /* re-populate input BIO, may have been consumed */
  45517. BIO_free(inBio);
  45518. inBio = NULL;
  45519. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45520. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45521. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  45522. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45523. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45524. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45525. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45526. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45527. if (p7Ver != NULL) {
  45528. p7Ver->content = data;
  45529. p7Ver->contentSz = sizeof(data);
  45530. }
  45531. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45532. wc_PKCS7_Free(p7Ver);
  45533. p7Ver = NULL;
  45534. ExpectNotNull(out);
  45535. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45536. PKCS7_free(p7);
  45537. p7 = NULL;
  45538. }
  45539. X509_STORE_free(store);
  45540. X509_free(caCert);
  45541. X509_free(signCert);
  45542. EVP_PKEY_free(signKey);
  45543. BIO_free(inBio);
  45544. BIO_free(keyBio);
  45545. BIO_free(certBio);
  45546. BIO_free(caBio);
  45547. #endif
  45548. return EXPECT_RESULT();
  45549. }
  45550. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  45551. {
  45552. EXPECT_DECLS;
  45553. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  45554. PKCS7_SIGNED* pkcs7 = NULL;
  45555. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  45556. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  45557. PKCS7_SIGNED_free(pkcs7);
  45558. #endif
  45559. return EXPECT_RESULT();
  45560. }
  45561. #ifndef NO_BIO
  45562. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  45563. {
  45564. EXPECT_DECLS;
  45565. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  45566. PKCS7* pkcs7 = NULL;
  45567. BIO* bio = NULL;
  45568. const byte* cert_buf = NULL;
  45569. int ret = 0;
  45570. WC_RNG rng;
  45571. const byte data[] = { /* Hello World */
  45572. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45573. 0x72,0x6c,0x64
  45574. };
  45575. #ifndef NO_RSA
  45576. #if defined(USE_CERT_BUFFERS_2048)
  45577. byte key[sizeof(client_key_der_2048)];
  45578. byte cert[sizeof(client_cert_der_2048)];
  45579. word32 keySz = (word32)sizeof(key);
  45580. word32 certSz = (word32)sizeof(cert);
  45581. XMEMSET(key, 0, keySz);
  45582. XMEMSET(cert, 0, certSz);
  45583. XMEMCPY(key, client_key_der_2048, keySz);
  45584. XMEMCPY(cert, client_cert_der_2048, certSz);
  45585. #elif defined(USE_CERT_BUFFERS_1024)
  45586. byte key[sizeof_client_key_der_1024];
  45587. byte cert[sizeof(sizeof_client_cert_der_1024)];
  45588. word32 keySz = (word32)sizeof(key);
  45589. word32 certSz = (word32)sizeof(cert);
  45590. XMEMSET(key, 0, keySz);
  45591. XMEMSET(cert, 0, certSz);
  45592. XMEMCPY(key, client_key_der_1024, keySz);
  45593. XMEMCPY(cert, client_cert_der_1024, certSz);
  45594. #else
  45595. unsigned char cert[ONEK_BUF];
  45596. unsigned char key[ONEK_BUF];
  45597. XFILE fp = XBADFILE;
  45598. int certSz;
  45599. int keySz;
  45600. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  45601. XBADFILE);
  45602. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  45603. fp), 0);
  45604. if (fp != XBADFILE) {
  45605. XFCLOSE(fp);
  45606. fp = XBADFILE;
  45607. }
  45608. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  45609. XBADFILE);
  45610. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  45611. 0);
  45612. if (fp != XBADFILE) {
  45613. XFCLOSE(fp);
  45614. fp = XBADFILE;
  45615. }
  45616. #endif
  45617. #elif defined(HAVE_ECC)
  45618. #if defined(USE_CERT_BUFFERS_256)
  45619. unsigned char cert[sizeof(cliecc_cert_der_256)];
  45620. unsigned char key[sizeof(ecc_clikey_der_256)];
  45621. int certSz = (int)sizeof(cert);
  45622. int keySz = (int)sizeof(key);
  45623. XMEMSET(cert, 0, certSz);
  45624. XMEMSET(key, 0, keySz);
  45625. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  45626. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  45627. #else
  45628. unsigned char cert[ONEK_BUF];
  45629. unsigned char key[ONEK_BUF];
  45630. XFILE fp = XBADFILE;
  45631. int certSz, keySz;
  45632. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  45633. XBADFILE);
  45634. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  45635. fp), 0);
  45636. if (fp != XBADFILE) {
  45637. XFCLOSE(fp);
  45638. fp = XBADFILE;
  45639. }
  45640. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  45641. XBADFILE);
  45642. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  45643. 0);
  45644. if (fp != XBADFILE) {
  45645. XFCLOSE(fp);
  45646. fp = XBADFILE;
  45647. }
  45648. #endif
  45649. #else
  45650. #error PKCS7 requires ECC or RSA
  45651. #endif
  45652. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  45653. /* initialize with DER encoded cert */
  45654. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  45655. /* init rng */
  45656. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45657. ExpectIntEQ(wc_InitRng(&rng), 0);
  45658. if (pkcs7 != NULL) {
  45659. pkcs7->rng = &rng;
  45660. pkcs7->content = (byte*)data; /* not used for ex */
  45661. pkcs7->contentSz = (word32)sizeof(data);
  45662. pkcs7->contentOID = SIGNED_DATA;
  45663. pkcs7->privateKey = key;
  45664. pkcs7->privateKeySz = (word32)sizeof(key);
  45665. pkcs7->encryptOID = RSAk;
  45666. #ifdef NO_SHA
  45667. pkcs7->hashOID = SHA256h;
  45668. #else
  45669. pkcs7->hashOID = SHAh;
  45670. #endif
  45671. pkcs7->signedAttribs = NULL;
  45672. pkcs7->signedAttribsSz = 0;
  45673. }
  45674. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45675. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  45676. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  45677. /* Read PKCS#7 PEM from BIO */
  45678. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  45679. ExpectIntGE(ret, 0);
  45680. BIO_free(bio);
  45681. wc_PKCS7_Free(pkcs7);
  45682. wc_FreeRng(&rng);
  45683. #endif
  45684. return EXPECT_RESULT();
  45685. }
  45686. #ifdef HAVE_SMIME
  45687. static int test_wolfSSL_SMIME_read_PKCS7(void)
  45688. {
  45689. EXPECT_DECLS;
  45690. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  45691. !defined(NO_RSA)
  45692. PKCS7* pkcs7 = NULL;
  45693. BIO* bio = NULL;
  45694. BIO* bcont = NULL;
  45695. BIO* out = NULL;
  45696. const byte* outBuf = NULL;
  45697. int outBufLen = 0;
  45698. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  45699. XFILE smimeTestFile = XBADFILE;
  45700. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  45701. XBADFILE);
  45702. /* smime-test.p7s */
  45703. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  45704. ExpectNotNull(bio);
  45705. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45706. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45707. ExpectNotNull(pkcs7);
  45708. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45709. PKCS7_NOVERIFY), SSL_SUCCESS);
  45710. XFCLOSE(smimeTestFile);
  45711. if (bcont) BIO_free(bcont);
  45712. bcont = NULL;
  45713. wolfSSL_PKCS7_free(pkcs7);
  45714. pkcs7 = NULL;
  45715. /* smime-test-multipart.p7s */
  45716. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  45717. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45718. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45719. ExpectNotNull(pkcs7);
  45720. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45721. PKCS7_NOVERIFY), SSL_SUCCESS);
  45722. XFCLOSE(smimeTestFile);
  45723. if (bcont) BIO_free(bcont);
  45724. bcont = NULL;
  45725. wolfSSL_PKCS7_free(pkcs7);
  45726. pkcs7 = NULL;
  45727. /* smime-test-multipart-badsig.p7s */
  45728. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  45729. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45730. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45731. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  45732. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45733. PKCS7_NOVERIFY), SSL_FAILURE);
  45734. XFCLOSE(smimeTestFile);
  45735. if (bcont) BIO_free(bcont);
  45736. bcont = NULL;
  45737. wolfSSL_PKCS7_free(pkcs7);
  45738. pkcs7 = NULL;
  45739. /* smime-test-canon.p7s */
  45740. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  45741. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45742. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45743. ExpectNotNull(pkcs7);
  45744. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  45745. PKCS7_NOVERIFY), SSL_SUCCESS);
  45746. XFCLOSE(smimeTestFile);
  45747. if (bcont) BIO_free(bcont);
  45748. bcont = NULL;
  45749. wolfSSL_PKCS7_free(pkcs7);
  45750. pkcs7 = NULL;
  45751. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  45752. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  45753. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  45754. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  45755. ExpectNotNull(pkcs7);
  45756. out = wolfSSL_BIO_new(BIO_s_mem());
  45757. ExpectNotNull(out);
  45758. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  45759. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  45760. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  45761. /* Content-Type should not show up at beginning of output buffer */
  45762. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  45763. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  45764. BIO_free(out);
  45765. BIO_free(bio);
  45766. if (bcont) BIO_free(bcont);
  45767. wolfSSL_PKCS7_free(pkcs7);
  45768. #endif
  45769. return EXPECT_RESULT();
  45770. }
  45771. static int test_wolfSSL_SMIME_write_PKCS7(void)
  45772. {
  45773. EXPECT_DECLS;
  45774. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  45775. PKCS7* p7 = NULL;
  45776. PKCS7* p7Ver = NULL;
  45777. int flags = 0;
  45778. byte data[] = "Test data to encode.";
  45779. const char* cert = "./certs/server-cert.pem";
  45780. const char* key = "./certs/server-key.pem";
  45781. const char* ca = "./certs/ca-cert.pem";
  45782. WOLFSSL_BIO* certBio = NULL;
  45783. WOLFSSL_BIO* keyBio = NULL;
  45784. WOLFSSL_BIO* caBio = NULL;
  45785. WOLFSSL_BIO* inBio = NULL;
  45786. WOLFSSL_BIO* outBio = NULL;
  45787. WOLFSSL_BIO* content = NULL;
  45788. X509* signCert = NULL;
  45789. EVP_PKEY* signKey = NULL;
  45790. X509* caCert = NULL;
  45791. X509_STORE* store = NULL;
  45792. /* read signer cert/key into BIO */
  45793. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45794. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45795. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45796. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45797. /* read CA cert into store (for verify) */
  45798. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45799. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45800. ExpectNotNull(store = X509_STORE_new());
  45801. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45802. /* generate and verify SMIME: not detached */
  45803. {
  45804. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45805. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45806. flags = PKCS7_STREAM;
  45807. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45808. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45809. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45810. /* bad arg: out NULL */
  45811. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  45812. /* bad arg: pkcs7 NULL */
  45813. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  45814. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45815. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45816. BIO_free(content);
  45817. content = NULL;
  45818. BIO_free(inBio);
  45819. inBio = NULL;
  45820. BIO_free(outBio);
  45821. outBio = NULL;
  45822. PKCS7_free(p7Ver);
  45823. p7Ver = NULL;
  45824. PKCS7_free(p7);
  45825. p7 = NULL;
  45826. }
  45827. /* generate and verify SMIME: not detached, add Content-Type */
  45828. {
  45829. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45830. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45831. flags = PKCS7_STREAM | PKCS7_TEXT;
  45832. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45833. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45834. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45835. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45836. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45837. BIO_free(content);
  45838. content = NULL;
  45839. BIO_free(inBio);
  45840. inBio = NULL;
  45841. BIO_free(outBio);
  45842. outBio = NULL;
  45843. PKCS7_free(p7Ver);
  45844. p7Ver = NULL;
  45845. PKCS7_free(p7);
  45846. p7 = NULL;
  45847. }
  45848. /* generate and verify SMIME: detached */
  45849. {
  45850. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45851. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45852. flags = PKCS7_DETACHED | PKCS7_STREAM;
  45853. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45854. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45855. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45856. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45857. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45858. BIO_free(content);
  45859. content = NULL;
  45860. BIO_free(inBio);
  45861. inBio = NULL;
  45862. BIO_free(outBio);
  45863. outBio = NULL;
  45864. PKCS7_free(p7Ver);
  45865. p7Ver = NULL;
  45866. PKCS7_free(p7);
  45867. p7 = NULL;
  45868. }
  45869. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  45870. {
  45871. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45872. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45873. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  45874. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45875. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45876. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45877. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45878. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45879. BIO_free(content);
  45880. content = NULL;
  45881. BIO_free(inBio);
  45882. inBio = NULL;
  45883. BIO_free(outBio);
  45884. outBio = NULL;
  45885. PKCS7_free(p7Ver);
  45886. p7Ver = NULL;
  45887. PKCS7_free(p7);
  45888. p7 = NULL;
  45889. }
  45890. X509_STORE_free(store);
  45891. X509_free(caCert);
  45892. X509_free(signCert);
  45893. EVP_PKEY_free(signKey);
  45894. BIO_free(keyBio);
  45895. BIO_free(certBio);
  45896. BIO_free(caBio);
  45897. #endif
  45898. return EXPECT_RESULT();
  45899. }
  45900. #endif /* HAVE_SMIME */
  45901. #endif /* !NO_BIO */
  45902. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  45903. * returns 0) */
  45904. static int test_X509_STORE_No_SSL_CTX(void)
  45905. {
  45906. EXPECT_DECLS;
  45907. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45908. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45909. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45910. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45911. defined(HAVE_CRL) && !defined(NO_RSA)
  45912. X509_STORE * store = NULL;
  45913. X509_STORE_CTX * storeCtx = NULL;
  45914. X509_CRL * crl = NULL;
  45915. X509 * ca = NULL;
  45916. X509 * cert = NULL;
  45917. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45918. const char srvCert[] = "./certs/server-cert.pem";
  45919. const char caCert[] = "./certs/ca-cert.pem";
  45920. const char caDir[] = "./certs/crl/hash_pem";
  45921. XFILE fp = XBADFILE;
  45922. X509_LOOKUP * lookup = NULL;
  45923. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45924. /* Set up store with CA */
  45925. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45926. SSL_FILETYPE_PEM)));
  45927. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45928. /* Add CRL lookup directory to store
  45929. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  45930. * of crl.pem */
  45931. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45932. X509_LOOKUP_hash_dir())));
  45933. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  45934. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  45935. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45936. SSL_SUCCESS);
  45937. /* Add CRL to store NOT containing the verified certificate, which
  45938. * forces use of the CRL lookup directory */
  45939. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45940. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45941. NULL, NULL));
  45942. if (fp != XBADFILE)
  45943. XFCLOSE(fp);
  45944. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45945. /* Create verification context outside of an SSL session */
  45946. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45947. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45948. SSL_FILETYPE_PEM)));
  45949. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45950. /* Perform verification, which should NOT indicate CRL missing due to the
  45951. * store CM's X509 store pointer being NULL */
  45952. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  45953. X509_CRL_free(crl);
  45954. X509_STORE_free(store);
  45955. X509_STORE_CTX_free(storeCtx);
  45956. X509_free(cert);
  45957. X509_free(ca);
  45958. #endif
  45959. return EXPECT_RESULT();
  45960. }
  45961. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  45962. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  45963. static int test_X509_LOOKUP_add_dir(void)
  45964. {
  45965. EXPECT_DECLS;
  45966. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45967. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45968. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45969. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45970. defined(HAVE_CRL) && !defined(NO_RSA)
  45971. X509_STORE * store = NULL;
  45972. X509_STORE_CTX * storeCtx = NULL;
  45973. X509_CRL * crl = NULL;
  45974. X509 * ca = NULL;
  45975. X509 * cert = NULL;
  45976. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45977. const char srvCert[] = "./certs/server-cert.pem";
  45978. const char caCert[] = "./certs/ca-cert.pem";
  45979. const char caDir[] = "./certs/crl/hash_der";
  45980. XFILE fp = XBADFILE;
  45981. X509_LOOKUP * lookup = NULL;
  45982. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45983. /* Set up store with CA */
  45984. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45985. SSL_FILETYPE_PEM)));
  45986. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45987. /* Add CRL lookup directory to store.
  45988. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  45989. * of crl.der */
  45990. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45991. X509_LOOKUP_hash_dir())));
  45992. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  45993. SSL_SUCCESS);
  45994. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45995. SSL_SUCCESS);
  45996. /* Add CRL to store NOT containing the verified certificate, which
  45997. * forces use of the CRL lookup directory */
  45998. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45999. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46000. NULL, NULL));
  46001. if (fp != XBADFILE) {
  46002. XFCLOSE(fp);
  46003. fp = XBADFILE;
  46004. }
  46005. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46006. /* Create verification context outside of an SSL session */
  46007. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46008. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46009. SSL_FILETYPE_PEM)));
  46010. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46011. /* Perform verification, which should NOT return CRL missing */
  46012. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46013. X509_CRL_free(crl);
  46014. crl = NULL;
  46015. X509_STORE_free(store);
  46016. store = NULL;
  46017. X509_STORE_CTX_free(storeCtx);
  46018. storeCtx = NULL;
  46019. X509_free(cert);
  46020. cert = NULL;
  46021. X509_free(ca);
  46022. ca = NULL;
  46023. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  46024. * We should get CRL_MISSING at the end, because the lookup
  46025. * dir has only ASN1 CRLs. */
  46026. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46027. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46028. SSL_FILETYPE_PEM)));
  46029. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46030. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46031. X509_LOOKUP_hash_dir())));
  46032. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  46033. SSL_SUCCESS);
  46034. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46035. SSL_SUCCESS);
  46036. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46037. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46038. NULL, NULL));
  46039. if (fp != XBADFILE) {
  46040. XFCLOSE(fp);
  46041. fp = XBADFILE;
  46042. }
  46043. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46044. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46045. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46046. SSL_FILETYPE_PEM)));
  46047. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46048. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46049. * in dir containing only ASN1/DER. */
  46050. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  46051. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  46052. X509_CRL_free(crl);
  46053. X509_STORE_free(store);
  46054. X509_STORE_CTX_free(storeCtx);
  46055. X509_free(cert);
  46056. X509_free(ca);
  46057. #endif
  46058. return EXPECT_RESULT();
  46059. }
  46060. /*----------------------------------------------------------------------------*
  46061. | Certificate Failure Checks
  46062. *----------------------------------------------------------------------------*/
  46063. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46064. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46065. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46066. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46067. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46068. int type)
  46069. {
  46070. int ret;
  46071. WOLFSSL_CERT_MANAGER* cm = NULL;
  46072. switch (type) {
  46073. case TESTING_RSA:
  46074. #ifdef NO_RSA
  46075. fprintf(stderr, "RSA disabled, skipping test\n");
  46076. return ASN_SIG_CONFIRM_E;
  46077. #else
  46078. break;
  46079. #endif
  46080. case TESTING_ECC:
  46081. #ifndef HAVE_ECC
  46082. fprintf(stderr, "ECC disabled, skipping test\n");
  46083. return ASN_SIG_CONFIRM_E;
  46084. #else
  46085. break;
  46086. #endif
  46087. default:
  46088. fprintf(stderr, "Bad function argument\n");
  46089. return BAD_FUNC_ARG;
  46090. }
  46091. cm = wolfSSL_CertManagerNew();
  46092. if (cm == NULL) {
  46093. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46094. return -1;
  46095. }
  46096. #ifndef NO_FILESYSTEM
  46097. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46098. if (ret != WOLFSSL_SUCCESS) {
  46099. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46100. wolfSSL_CertManagerFree(cm);
  46101. return ret;
  46102. }
  46103. #else
  46104. (void)ca;
  46105. #endif
  46106. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  46107. WOLFSSL_FILETYPE_ASN1);
  46108. /* Let ExpectIntEQ handle return code */
  46109. wolfSSL_CertManagerFree(cm);
  46110. return ret;
  46111. }
  46112. #endif
  46113. #if !defined(NO_FILESYSTEM)
  46114. static int test_RsaSigFailure_cm(void)
  46115. {
  46116. EXPECT_DECLS;
  46117. #ifndef NO_RSA
  46118. const char* ca_cert = "./certs/ca-cert.pem";
  46119. const char* server_cert = "./certs/server-cert.der";
  46120. byte* cert_buf = NULL;
  46121. size_t cert_sz = 0;
  46122. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46123. if (cert_buf != NULL) {
  46124. /* corrupt DER - invert last byte, which is signature */
  46125. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46126. /* test bad cert */
  46127. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46128. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46129. WOLFSSL_FATAL_ERROR);
  46130. #else
  46131. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46132. ASN_SIG_CONFIRM_E);
  46133. #endif
  46134. }
  46135. /* load_file() uses malloc. */
  46136. if (cert_buf != NULL) {
  46137. free(cert_buf);
  46138. }
  46139. #endif /* !NO_RSA */
  46140. return EXPECT_RESULT();
  46141. }
  46142. static int test_EccSigFailure_cm(void)
  46143. {
  46144. EXPECT_DECLS;
  46145. #ifdef HAVE_ECC
  46146. /* self-signed ECC cert, so use server cert as CA */
  46147. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46148. const char* server_cert = "./certs/server-ecc.der";
  46149. byte* cert_buf = NULL;
  46150. size_t cert_sz = 0;
  46151. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46152. if (cert_buf != NULL) {
  46153. /* corrupt DER - invert last byte, which is signature */
  46154. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46155. /* test bad cert */
  46156. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46157. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46158. WOLFSSL_FATAL_ERROR);
  46159. #else
  46160. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46161. ASN_SIG_CONFIRM_E);
  46162. #endif
  46163. }
  46164. /* load_file() uses malloc. */
  46165. if (cert_buf != NULL) {
  46166. free(cert_buf);
  46167. }
  46168. #ifdef FP_ECC
  46169. wc_ecc_fp_free();
  46170. #endif
  46171. #endif /* HAVE_ECC */
  46172. return EXPECT_RESULT();
  46173. }
  46174. #endif /* !NO_FILESYSTEM */
  46175. #endif /* NO_CERTS */
  46176. #ifdef WOLFSSL_TLS13
  46177. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46178. #ifdef WC_SHA384_DIGEST_SIZE
  46179. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46180. #else
  46181. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46182. #endif
  46183. #endif
  46184. #ifdef WOLFSSL_EARLY_DATA
  46185. static const char earlyData[] = "Early Data";
  46186. static char earlyDataBuffer[1];
  46187. #endif
  46188. static int test_tls13_apis(void)
  46189. {
  46190. EXPECT_DECLS;
  46191. int ret;
  46192. #ifndef WOLFSSL_NO_TLS12
  46193. #ifndef NO_WOLFSSL_CLIENT
  46194. WOLFSSL_CTX* clientTls12Ctx = NULL;
  46195. WOLFSSL* clientTls12Ssl = NULL;
  46196. #endif
  46197. #ifndef NO_WOLFSSL_SERVER
  46198. WOLFSSL_CTX* serverTls12Ctx = NULL;
  46199. WOLFSSL* serverTls12Ssl = NULL;
  46200. #endif
  46201. #endif
  46202. #ifndef NO_WOLFSSL_CLIENT
  46203. WOLFSSL_CTX* clientCtx = NULL;
  46204. WOLFSSL* clientSsl = NULL;
  46205. #endif
  46206. #ifndef NO_WOLFSSL_SERVER
  46207. WOLFSSL_CTX* serverCtx = NULL;
  46208. WOLFSSL* serverSsl = NULL;
  46209. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46210. const char* ourCert = svrCertFile;
  46211. const char* ourKey = svrKeyFile;
  46212. #endif
  46213. #endif
  46214. int required;
  46215. #ifdef WOLFSSL_EARLY_DATA
  46216. int outSz;
  46217. #endif
  46218. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46219. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46220. #ifdef HAVE_PQC
  46221. WOLFSSL_KYBER_LEVEL1
  46222. #else
  46223. WOLFSSL_ECC_SECP256R1
  46224. #endif
  46225. };
  46226. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46227. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46228. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46229. int numGroups = 2;
  46230. #endif
  46231. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46232. char groupList[] =
  46233. #ifndef NO_ECC_SECP
  46234. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  46235. "P-521:"
  46236. #endif
  46237. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  46238. "P-384:"
  46239. #endif
  46240. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  46241. "P-256"
  46242. #ifdef HAVE_PQC
  46243. ":P256_KYBER_LEVEL1"
  46244. #endif
  46245. #endif
  46246. #endif /* !defined(NO_ECC_SECP) */
  46247. #ifdef HAVE_PQC
  46248. ":KYBER_LEVEL1"
  46249. #endif
  46250. "";
  46251. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46252. (void)ret;
  46253. #ifndef WOLFSSL_NO_TLS12
  46254. #ifndef NO_WOLFSSL_CLIENT
  46255. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  46256. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  46257. #endif
  46258. #ifndef NO_WOLFSSL_SERVER
  46259. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  46260. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46261. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  46262. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  46263. WOLFSSL_FILETYPE_PEM);
  46264. #endif
  46265. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  46266. #endif
  46267. #endif
  46268. #ifndef NO_WOLFSSL_CLIENT
  46269. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  46270. clientSsl = wolfSSL_new(clientCtx);
  46271. #endif
  46272. #ifndef NO_WOLFSSL_SERVER
  46273. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  46274. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46275. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  46276. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  46277. #endif
  46278. serverSsl = wolfSSL_new(serverCtx);
  46279. ExpectNotNull(serverSsl);
  46280. #endif
  46281. #ifdef WOLFSSL_SEND_HRR_COOKIE
  46282. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  46283. #ifndef NO_WOLFSSL_CLIENT
  46284. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  46285. #endif
  46286. #ifndef NO_WOLFSSL_SERVER
  46287. #ifndef WOLFSSL_NO_TLS12
  46288. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  46289. BAD_FUNC_ARG);
  46290. #endif
  46291. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  46292. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  46293. WOLFSSL_SUCCESS);
  46294. #endif
  46295. #endif
  46296. #ifdef HAVE_SUPPORTED_CURVES
  46297. #ifdef HAVE_ECC
  46298. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46299. BAD_FUNC_ARG);
  46300. #ifndef NO_WOLFSSL_SERVER
  46301. do {
  46302. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  46303. #ifdef WOLFSSL_ASYNC_CRYPT
  46304. if (ret == WC_PENDING_E)
  46305. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  46306. #endif
  46307. }
  46308. while (ret == WC_PENDING_E);
  46309. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46310. #endif
  46311. #ifndef NO_WOLFSSL_CLIENT
  46312. #ifndef WOLFSSL_NO_TLS12
  46313. do {
  46314. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  46315. #ifdef WOLFSSL_ASYNC_CRYPT
  46316. if (ret == WC_PENDING_E)
  46317. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  46318. #endif
  46319. }
  46320. while (ret == WC_PENDING_E);
  46321. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46322. #endif
  46323. do {
  46324. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  46325. #ifdef WOLFSSL_ASYNC_CRYPT
  46326. if (ret == WC_PENDING_E)
  46327. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  46328. #endif
  46329. }
  46330. while (ret == WC_PENDING_E);
  46331. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46332. #endif
  46333. #elif defined(HAVE_CURVE25519)
  46334. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  46335. #ifndef NO_WOLFSSL_SERVER
  46336. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  46337. WOLFSSL_SUCCESS);
  46338. #endif
  46339. #ifndef NO_WOLFSSL_CLIENT
  46340. #ifndef WOLFSSL_NO_TLS12
  46341. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  46342. WOLFSSL_SUCCESS);
  46343. #endif
  46344. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  46345. WOLFSSL_SUCCESS);
  46346. #endif
  46347. #elif defined(HAVE_CURVE448)
  46348. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  46349. #ifndef NO_WOLFSSL_SERVER
  46350. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  46351. WOLFSSL_SUCCESS);
  46352. #endif
  46353. #ifndef NO_WOLFSSL_CLIENT
  46354. #ifndef WOLFSSL_NO_TLS12
  46355. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  46356. WOLFSSL_SUCCESS);
  46357. #endif
  46358. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  46359. WOLFSSL_SUCCESS);
  46360. #endif
  46361. #else
  46362. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46363. BAD_FUNC_ARG);
  46364. #ifndef NO_WOLFSSL_CLIENT
  46365. #ifndef WOLFSSL_NO_TLS12
  46366. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  46367. NOT_COMPILED_IN);
  46368. #endif
  46369. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  46370. NOT_COMPILED_IN);
  46371. #endif
  46372. #endif
  46373. #if defined(HAVE_PQC)
  46374. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  46375. #ifndef NO_WOLFSSL_SERVER
  46376. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  46377. WOLFSSL_SUCCESS);
  46378. #endif
  46379. #ifndef NO_WOLFSSL_CLIENT
  46380. #ifndef WOLFSSL_NO_TLS12
  46381. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  46382. BAD_FUNC_ARG);
  46383. #endif
  46384. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  46385. WOLFSSL_SUCCESS);
  46386. #endif
  46387. #endif
  46388. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  46389. #ifndef NO_WOLFSSL_SERVER
  46390. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  46391. #endif
  46392. #ifndef NO_WOLFSSL_CLIENT
  46393. #ifndef WOLFSSL_NO_TLS12
  46394. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  46395. #endif
  46396. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  46397. #endif
  46398. #endif /* HAVE_SUPPORTED_CURVES */
  46399. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46400. #ifndef NO_WOLFSSL_CLIENT
  46401. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  46402. #endif
  46403. #ifndef NO_WOLFSSL_SERVER
  46404. #ifndef WOLFSSL_NO_TLS12
  46405. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  46406. #endif
  46407. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  46408. #endif
  46409. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46410. #ifndef NO_WOLFSSL_CLIENT
  46411. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  46412. #endif
  46413. #ifndef NO_WOLFSSL_SERVER
  46414. #ifndef WOLFSSL_NO_TLS12
  46415. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  46416. #endif
  46417. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  46418. #endif
  46419. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46420. #ifndef NO_WOLFSSL_CLIENT
  46421. #ifndef WOLFSSL_NO_TLS12
  46422. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  46423. #endif
  46424. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  46425. #endif
  46426. #ifndef NO_WOLFSSL_SERVER
  46427. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  46428. #endif
  46429. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46430. #ifndef NO_WOLFSSL_CLIENT
  46431. #ifndef WOLFSSL_NO_TLS12
  46432. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  46433. #endif
  46434. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  46435. #endif
  46436. #ifndef NO_WOLFSSL_SERVER
  46437. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  46438. #endif
  46439. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  46440. #ifndef NO_WOLFSSL_CLIENT
  46441. #ifndef WOLFSSL_NO_TLS12
  46442. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  46443. #endif
  46444. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  46445. #endif
  46446. #ifndef NO_WOLFSSL_SERVER
  46447. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  46448. #endif
  46449. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  46450. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  46451. #ifndef NO_WOLFSSL_CLIENT
  46452. #ifndef WOLFSSL_NO_TLS12
  46453. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  46454. BAD_FUNC_ARG);
  46455. #endif
  46456. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  46457. #endif
  46458. #ifndef NO_WOLFSSL_SERVER
  46459. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  46460. #endif
  46461. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  46462. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46463. #ifndef NO_WOLFSSL_SERVER
  46464. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  46465. #endif
  46466. #ifndef NO_WOLFSSL_CLIENT
  46467. #ifndef WOLFSSL_NO_TLS12
  46468. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  46469. BAD_FUNC_ARG);
  46470. #endif
  46471. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  46472. #endif
  46473. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46474. #ifndef NO_WOLFSSL_SERVER
  46475. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  46476. #endif
  46477. #ifndef NO_WOLFSSL_CLIENT
  46478. #ifndef WOLFSSL_NO_TLS12
  46479. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  46480. BAD_FUNC_ARG);
  46481. #endif
  46482. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  46483. #endif
  46484. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  46485. #ifndef NO_WOLFSSL_CLIENT
  46486. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  46487. #endif
  46488. #ifndef NO_WOLFSSL_SERVER
  46489. #ifndef WOLFSSL_NO_TLS12
  46490. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  46491. BAD_FUNC_ARG);
  46492. #endif
  46493. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  46494. #endif
  46495. #endif
  46496. #ifdef HAVE_ECC
  46497. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  46498. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  46499. #ifndef NO_WOLFSSL_SERVER
  46500. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  46501. #endif
  46502. #ifndef NO_WOLFSSL_CLIENT
  46503. #ifndef WOLFSSL_NO_TLS12
  46504. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  46505. #endif
  46506. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  46507. #endif
  46508. #endif
  46509. #ifdef HAVE_SUPPORTED_CURVES
  46510. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46511. #ifndef NO_WOLFSSL_CLIENT
  46512. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  46513. #endif
  46514. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46515. #ifndef NO_WOLFSSL_CLIENT
  46516. #ifndef WOLFSSL_NO_TLS12
  46517. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  46518. BAD_FUNC_ARG);
  46519. #endif
  46520. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  46521. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46522. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  46523. WOLFSSL_SUCCESS);
  46524. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  46525. BAD_FUNC_ARG);
  46526. #endif
  46527. #ifndef NO_WOLFSSL_SERVER
  46528. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  46529. WOLFSSL_SUCCESS);
  46530. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  46531. BAD_FUNC_ARG);
  46532. #endif
  46533. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46534. #ifndef NO_WOLFSSL_CLIENT
  46535. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  46536. #endif
  46537. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46538. #ifndef NO_WOLFSSL_CLIENT
  46539. #ifndef WOLFSSL_NO_TLS12
  46540. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  46541. BAD_FUNC_ARG);
  46542. #endif
  46543. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  46544. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46545. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  46546. WOLFSSL_SUCCESS);
  46547. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  46548. BAD_FUNC_ARG);
  46549. #endif
  46550. #ifndef NO_WOLFSSL_SERVER
  46551. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  46552. WOLFSSL_SUCCESS);
  46553. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  46554. BAD_FUNC_ARG);
  46555. #endif
  46556. #ifdef OPENSSL_EXTRA
  46557. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46558. #ifndef NO_WOLFSSL_CLIENT
  46559. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  46560. WOLFSSL_FAILURE);
  46561. #endif
  46562. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  46563. WOLFSSL_FAILURE);
  46564. #ifndef NO_WOLFSSL_CLIENT
  46565. #ifndef WOLFSSL_NO_TLS12
  46566. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  46567. WOLFSSL_FAILURE);
  46568. #endif
  46569. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  46570. WOLFSSL_SUCCESS);
  46571. #endif
  46572. #ifndef NO_WOLFSSL_SERVER
  46573. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  46574. WOLFSSL_SUCCESS);
  46575. #endif
  46576. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46577. #ifndef NO_WOLFSSL_CLIENT
  46578. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  46579. #endif
  46580. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46581. #ifndef NO_WOLFSSL_CLIENT
  46582. #ifndef WOLFSSL_NO_TLS12
  46583. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  46584. WOLFSSL_FAILURE);
  46585. #endif
  46586. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  46587. WOLFSSL_SUCCESS);
  46588. #endif
  46589. #ifndef NO_WOLFSSL_SERVER
  46590. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  46591. WOLFSSL_SUCCESS);
  46592. #endif
  46593. #endif /* OPENSSL_EXTRA */
  46594. #endif /* HAVE_SUPPORTED_CURVES */
  46595. #endif /* HAVE_ECC */
  46596. #ifdef WOLFSSL_EARLY_DATA
  46597. #ifndef OPENSSL_EXTRA
  46598. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46599. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46600. #else
  46601. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46602. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46603. #endif
  46604. #ifndef NO_WOLFSSL_CLIENT
  46605. #ifndef OPENSSL_EXTRA
  46606. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46607. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46608. #else
  46609. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46610. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46611. #endif
  46612. #endif
  46613. #ifndef NO_WOLFSSL_SERVER
  46614. #ifndef WOLFSSL_NO_TLS12
  46615. #ifndef OPENSSL_EXTRA
  46616. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46617. BAD_FUNC_ARG);
  46618. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46619. #else
  46620. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46621. BAD_FUNC_ARG);
  46622. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46623. #endif
  46624. #endif
  46625. #ifndef OPENSSL_EXTRA
  46626. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46627. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  46628. WOLFSSL_SUCCESS);
  46629. #else
  46630. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  46631. #endif
  46632. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  46633. #else
  46634. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  46635. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  46636. #endif
  46637. #endif
  46638. #ifndef OPENSSL_EXTRA
  46639. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46640. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46641. #else
  46642. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46643. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46644. #endif
  46645. #ifndef NO_WOLFSSL_CLIENT
  46646. #ifndef OPENSSL_EXTRA
  46647. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46648. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46649. #else
  46650. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  46651. #endif
  46652. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  46653. #else
  46654. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46655. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  46656. #endif
  46657. #endif
  46658. #ifndef NO_WOLFSSL_SERVER
  46659. #ifndef WOLFSSL_NO_TLS12
  46660. #ifndef OPENSSL_EXTRA
  46661. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  46662. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  46663. #else
  46664. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  46665. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  46666. #endif
  46667. #endif
  46668. #ifndef OPENSSL_EXTRA
  46669. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46670. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  46671. #else
  46672. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  46673. #endif
  46674. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  46675. #else
  46676. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  46677. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  46678. #endif
  46679. #endif
  46680. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  46681. &outSz), BAD_FUNC_ARG);
  46682. #ifndef NO_WOLFSSL_CLIENT
  46683. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  46684. &outSz), BAD_FUNC_ARG);
  46685. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  46686. BAD_FUNC_ARG);
  46687. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  46688. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  46689. #endif
  46690. #ifndef NO_WOLFSSL_SERVER
  46691. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  46692. sizeof(earlyData), &outSz), SIDE_ERROR);
  46693. #endif
  46694. #ifndef NO_WOLFSSL_CLIENT
  46695. #ifndef WOLFSSL_NO_TLS12
  46696. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  46697. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  46698. #endif
  46699. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  46700. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  46701. #endif
  46702. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  46703. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46704. #ifndef NO_WOLFSSL_SERVER
  46705. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  46706. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46707. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  46708. &outSz), BAD_FUNC_ARG);
  46709. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  46710. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  46711. #endif
  46712. #ifndef NO_WOLFSSL_CLIENT
  46713. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  46714. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  46715. #endif
  46716. #ifndef NO_WOLFSSL_SERVER
  46717. #ifndef WOLFSSL_NO_TLS12
  46718. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  46719. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  46720. #endif
  46721. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  46722. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  46723. #endif
  46724. #endif
  46725. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  46726. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  46727. #endif
  46728. #ifndef NO_WOLFSSL_SERVER
  46729. wolfSSL_free(serverSsl);
  46730. wolfSSL_CTX_free(serverCtx);
  46731. #endif
  46732. #ifndef NO_WOLFSSL_CLIENT
  46733. wolfSSL_free(clientSsl);
  46734. wolfSSL_CTX_free(clientCtx);
  46735. #endif
  46736. #ifndef WOLFSSL_NO_TLS12
  46737. #ifndef NO_WOLFSSL_SERVER
  46738. wolfSSL_free(serverTls12Ssl);
  46739. wolfSSL_CTX_free(serverTls12Ctx);
  46740. #endif
  46741. #ifndef NO_WOLFSSL_CLIENT
  46742. wolfSSL_free(clientTls12Ssl);
  46743. wolfSSL_CTX_free(clientTls12Ctx);
  46744. #endif
  46745. #endif
  46746. return EXPECT_RESULT();
  46747. }
  46748. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  46749. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  46750. defined(BUILD_TLS_AES_256_GCM_SHA384)
  46751. /* Called when writing. */
  46752. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  46753. {
  46754. (void)ssl;
  46755. (void)buf;
  46756. (void)sz;
  46757. (void)ctx;
  46758. /* Force error return from wolfSSL_accept_TLSv13(). */
  46759. return WANT_WRITE;
  46760. }
  46761. /* Called when reading. */
  46762. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  46763. {
  46764. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  46765. int len = (int)msg->length;
  46766. (void)ssl;
  46767. (void)sz;
  46768. /* Pass back as much of message as will fit in buffer. */
  46769. if (len > sz)
  46770. len = sz;
  46771. XMEMCPY(buf, msg->buffer, len);
  46772. /* Move over returned data. */
  46773. msg->buffer += len;
  46774. msg->length -= len;
  46775. /* Amount actually copied. */
  46776. return len;
  46777. }
  46778. #endif
  46779. static int test_tls13_cipher_suites(void)
  46780. {
  46781. EXPECT_DECLS;
  46782. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  46783. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  46784. defined(BUILD_TLS_AES_256_GCM_SHA384)
  46785. WOLFSSL_CTX* ctx = NULL;
  46786. WOLFSSL *ssl = NULL;
  46787. int i;
  46788. byte clientHello[] = {
  46789. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  46790. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  46791. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  46792. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  46793. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  46794. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  46795. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  46796. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  46797. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46798. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  46799. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  46800. 0x13, 0x01,
  46801. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  46802. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  46803. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  46804. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  46805. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  46806. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  46807. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  46808. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  46809. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  46810. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  46811. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  46812. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  46813. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  46814. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  46815. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  46816. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  46817. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  46818. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  46819. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  46820. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  46821. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  46822. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  46823. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  46824. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  46825. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  46826. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  46827. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  46828. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  46829. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  46830. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  46831. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  46832. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  46833. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  46834. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  46835. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  46836. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46837. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46838. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  46839. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  46840. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  46841. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  46842. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  46843. };
  46844. WOLFSSL_BUFFER_INFO msg;
  46845. /* Offset into ClientHello message data of first cipher suite. */
  46846. const int csOff = 78;
  46847. /* Server cipher list. */
  46848. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  46849. /* Suite list with duplicates. */
  46850. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  46851. "TLS13-AES128-GCM-SHA256:"
  46852. "TLS13-AES256-GCM-SHA384:"
  46853. "TLS13-AES256-GCM-SHA384:"
  46854. "TLS13-AES128-GCM-SHA256";
  46855. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46856. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46857. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46858. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46859. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46860. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  46861. #endif
  46862. /* Set up wolfSSL context. */
  46863. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46864. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  46865. WOLFSSL_FILETYPE_PEM));
  46866. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  46867. WOLFSSL_FILETYPE_PEM));
  46868. /* Read from 'msg'. */
  46869. wolfSSL_SetIORecv(ctx, CsRecv);
  46870. /* No where to send to - dummy sender. */
  46871. wolfSSL_SetIOSend(ctx, CsSend);
  46872. /* Test cipher suite list with many copies of a cipher suite. */
  46873. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46874. msg.buffer = clientHello;
  46875. msg.length = (unsigned int)sizeof(clientHello);
  46876. wolfSSL_SetIOReadCtx(ssl, &msg);
  46877. /* Force server to have as many occurrences of same cipher suite as
  46878. * possible. */
  46879. if (ssl != NULL) {
  46880. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  46881. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  46882. for (i = 0; i < suites->suiteSz; i += 2) {
  46883. suites->suites[i + 0] = TLS13_BYTE;
  46884. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  46885. }
  46886. }
  46887. /* Test multiple occurrences of same cipher suite. */
  46888. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46889. wolfSSL_free(ssl);
  46890. ssl = NULL;
  46891. /* Set client order opposite to server order:
  46892. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  46893. clientHello[csOff + 0] = TLS13_BYTE;
  46894. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  46895. clientHello[csOff + 2] = TLS13_BYTE;
  46896. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  46897. /* Test server order negotiation. */
  46898. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46899. msg.buffer = clientHello;
  46900. msg.length = (unsigned int)sizeof(clientHello);
  46901. wolfSSL_SetIOReadCtx(ssl, &msg);
  46902. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46903. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46904. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  46905. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46906. /* Check refined order - server order. */
  46907. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46908. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46909. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46910. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46911. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46912. wolfSSL_free(ssl);
  46913. ssl = NULL;
  46914. /* Test client order negotiation. */
  46915. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46916. msg.buffer = clientHello;
  46917. msg.length = (unsigned int)sizeof(clientHello);
  46918. wolfSSL_SetIOReadCtx(ssl, &msg);
  46919. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46920. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46921. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  46922. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  46923. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46924. /* Check refined order - client order. */
  46925. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46926. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46927. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46928. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46929. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46930. wolfSSL_free(ssl);
  46931. ssl = NULL;
  46932. /* Check duplicate detection is working. */
  46933. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  46934. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46935. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46936. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46937. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46938. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46939. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46940. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  46941. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  46942. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46943. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46944. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46945. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46946. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46947. #endif
  46948. wolfSSL_CTX_free(ctx);
  46949. #endif
  46950. return EXPECT_RESULT();
  46951. }
  46952. #endif
  46953. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  46954. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  46955. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  46956. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46957. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  46958. const unsigned char* priv, unsigned int privSz,
  46959. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  46960. unsigned char* out, unsigned int* outlen,
  46961. void* ctx)
  46962. {
  46963. int result;
  46964. /* Test fail when context associated with WOLFSSL is NULL */
  46965. if (ctx == NULL) {
  46966. return -1;
  46967. }
  46968. (void)ssl;
  46969. /* return 0 on success */
  46970. PRIVATE_KEY_UNLOCK();
  46971. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  46972. PRIVATE_KEY_LOCK();
  46973. return result;
  46974. }
  46975. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  46976. EXPECT_DECLS;
  46977. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  46978. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  46979. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  46980. WOLFSSL_SUCCESS);
  46981. #endif
  46982. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  46983. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  46984. WOLFSSL_SUCCESS);
  46985. #endif
  46986. return EXPECT_RESULT();
  46987. }
  46988. static int test_dh_ssl_setup(WOLFSSL* ssl)
  46989. {
  46990. EXPECT_DECLS;
  46991. static int dh_test_ctx = 1;
  46992. int ret;
  46993. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  46994. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  46995. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  46996. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  46997. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46998. }
  46999. return EXPECT_RESULT();
  47000. }
  47001. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  47002. {
  47003. EXPECT_DECLS;
  47004. int ret;
  47005. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  47006. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  47007. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47008. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47009. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47010. }
  47011. return EXPECT_RESULT();
  47012. }
  47013. #endif
  47014. static int test_DhCallbacks(void)
  47015. {
  47016. EXPECT_DECLS;
  47017. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  47018. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47019. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  47020. WOLFSSL_CTX *ctx = NULL;
  47021. WOLFSSL *ssl = NULL;
  47022. int test;
  47023. test_ssl_cbf func_cb_client;
  47024. test_ssl_cbf func_cb_server;
  47025. /* Test that DH callback APIs work. */
  47026. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47027. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  47028. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  47029. /* load client ca cert */
  47030. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  47031. WOLFSSL_SUCCESS);
  47032. /* test with NULL arguments */
  47033. wolfSSL_SetDhAgreeCtx(NULL, &test);
  47034. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  47035. /* test success case */
  47036. test = 1;
  47037. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47038. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47039. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47040. wolfSSL_free(ssl);
  47041. wolfSSL_CTX_free(ctx);
  47042. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47043. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47044. /* set callbacks to use DH functions */
  47045. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47046. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47047. func_cb_client.method = wolfTLSv1_2_client_method;
  47048. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47049. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47050. func_cb_server.method = wolfTLSv1_2_server_method;
  47051. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47052. &func_cb_server, NULL), TEST_SUCCESS);
  47053. /* Test fail */
  47054. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47055. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47056. /* set callbacks to use DH functions */
  47057. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47058. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47059. func_cb_client.method = wolfTLSv1_2_client_method;
  47060. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47061. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47062. func_cb_server.method = wolfTLSv1_2_server_method;
  47063. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47064. &func_cb_server, NULL), TEST_FAIL);
  47065. #endif
  47066. return EXPECT_RESULT();
  47067. }
  47068. #endif /* HAVE_PK_CALLBACKS */
  47069. #ifdef HAVE_HASHDRBG
  47070. #ifdef TEST_RESEED_INTERVAL
  47071. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47072. {
  47073. EXPECT_DECLS;
  47074. int i;
  47075. WC_RNG rng;
  47076. byte key[32];
  47077. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47078. ExpectIntEQ(wc_InitRng(&rng), 0);
  47079. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47080. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47081. }
  47082. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47083. return EXPECT_RESULT();
  47084. }
  47085. #endif /* TEST_RESEED_INTERVAL */
  47086. static int test_wc_RNG_GenerateBlock(void)
  47087. {
  47088. EXPECT_DECLS;
  47089. int i;
  47090. WC_RNG rng;
  47091. byte key[32];
  47092. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47093. ExpectIntEQ(wc_InitRng(&rng), 0);
  47094. for (i = 0; i < 10; i++) {
  47095. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47096. }
  47097. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47098. return EXPECT_RESULT();
  47099. }
  47100. #endif /* HAVE_HASHDRBG */
  47101. /*
  47102. * Testing get_rand_digit
  47103. */
  47104. static int test_get_rand_digit(void)
  47105. {
  47106. EXPECT_DECLS;
  47107. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47108. WC_RNG rng;
  47109. mp_digit d;
  47110. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47111. ExpectIntEQ(wc_InitRng(&rng), 0);
  47112. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  47113. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  47114. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  47115. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  47116. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47117. #endif
  47118. return EXPECT_RESULT();
  47119. } /* End test_get_rand_digit*/
  47120. /*
  47121. * Testing get_digit_count
  47122. */
  47123. static int test_get_digit_count(void)
  47124. {
  47125. EXPECT_DECLS;
  47126. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47127. mp_int a;
  47128. XMEMSET(&a, 0, sizeof(mp_int));
  47129. ExpectIntEQ(mp_init(&a), 0);
  47130. ExpectIntEQ(get_digit_count(NULL), 0);
  47131. ExpectIntEQ(get_digit_count(&a), 0);
  47132. mp_clear(&a);
  47133. #endif
  47134. return EXPECT_RESULT();
  47135. } /* End test_get_digit_count*/
  47136. /*
  47137. * Testing mp_cond_copy
  47138. */
  47139. static int test_mp_cond_copy(void)
  47140. {
  47141. EXPECT_DECLS;
  47142. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47143. defined(WOLFSSL_PUBLIC_MP)
  47144. mp_int a;
  47145. mp_int b;
  47146. int copy = 0;
  47147. XMEMSET(&a, 0, sizeof(mp_int));
  47148. XMEMSET(&b, 0, sizeof(mp_int));
  47149. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47150. ExpectIntEQ(mp_init(&b), MP_OKAY);
  47151. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  47152. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  47153. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  47154. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  47155. mp_clear(&a);
  47156. mp_clear(&b);
  47157. #endif
  47158. return EXPECT_RESULT();
  47159. } /* End test_mp_cond_copy*/
  47160. /*
  47161. * Testing mp_rand
  47162. */
  47163. static int test_mp_rand(void)
  47164. {
  47165. EXPECT_DECLS;
  47166. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47167. mp_int a;
  47168. WC_RNG rng;
  47169. int digits = 1;
  47170. XMEMSET(&a, 0, sizeof(mp_int));
  47171. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47172. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47173. ExpectIntEQ(wc_InitRng(&rng), 0);
  47174. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  47175. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  47176. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  47177. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  47178. mp_clear(&a);
  47179. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47180. #endif
  47181. return EXPECT_RESULT();
  47182. } /* End test_mp_rand*/
  47183. /*
  47184. * Testing get_digit
  47185. */
  47186. static int test_get_digit(void)
  47187. {
  47188. EXPECT_DECLS;
  47189. #if defined(WOLFSSL_PUBLIC_MP)
  47190. mp_int a;
  47191. int n = 0;
  47192. XMEMSET(&a, 0, sizeof(mp_int));
  47193. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47194. ExpectIntEQ(get_digit(NULL, n), 0);
  47195. ExpectIntEQ(get_digit(&a, n), 0);
  47196. mp_clear(&a);
  47197. #endif
  47198. return EXPECT_RESULT();
  47199. } /* End test_get_digit*/
  47200. /*
  47201. * Testing wc_export_int
  47202. */
  47203. static int test_wc_export_int(void)
  47204. {
  47205. EXPECT_DECLS;
  47206. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47207. defined(WOLFSSL_PUBLIC_MP)
  47208. mp_int mp;
  47209. byte buf[32];
  47210. word32 keySz = (word32)sizeof(buf);
  47211. word32 len = (word32)sizeof(buf);
  47212. XMEMSET(&mp, 0, sizeof(mp_int));
  47213. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  47214. ExpectIntEQ(mp_set(&mp, 1234), 0);
  47215. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47216. BAD_FUNC_ARG);
  47217. len = sizeof(buf)-1;
  47218. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47219. BUFFER_E);
  47220. len = sizeof(buf);
  47221. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  47222. len = 4; /* test input too small */
  47223. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  47224. len = sizeof(buf);
  47225. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  47226. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47227. ExpectIntEQ(len, 5);
  47228. mp_clear(&mp);
  47229. #endif
  47230. return EXPECT_RESULT();
  47231. } /* End test_wc_export_int*/
  47232. static int test_wc_InitRngNonce(void)
  47233. {
  47234. EXPECT_DECLS;
  47235. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47236. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47237. HAVE_FIPS_VERSION >= 2))
  47238. WC_RNG rng;
  47239. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47240. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47241. word32 nonceSz = sizeof(nonce);
  47242. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  47243. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47244. #endif
  47245. return EXPECT_RESULT();
  47246. } /* End test_wc_InitRngNonce*/
  47247. /*
  47248. * Testing wc_InitRngNonce_ex
  47249. */
  47250. static int test_wc_InitRngNonce_ex(void)
  47251. {
  47252. EXPECT_DECLS;
  47253. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47254. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47255. HAVE_FIPS_VERSION >= 2))
  47256. WC_RNG rng;
  47257. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47258. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47259. word32 nonceSz = sizeof(nonce);
  47260. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  47261. 0);
  47262. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47263. #endif
  47264. return EXPECT_RESULT();
  47265. } /* End test_wc_InitRngNonce_ex */
  47266. static int test_wolfSSL_X509_CRL(void)
  47267. {
  47268. EXPECT_DECLS;
  47269. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  47270. X509_CRL *crl = NULL;
  47271. char pem[][100] = {
  47272. "./certs/crl/crl.pem",
  47273. "./certs/crl/crl2.pem",
  47274. "./certs/crl/caEccCrl.pem",
  47275. "./certs/crl/eccCliCRL.pem",
  47276. "./certs/crl/eccSrvCRL.pem",
  47277. ""
  47278. };
  47279. #ifndef NO_BIO
  47280. BIO *bio = NULL;
  47281. #endif
  47282. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47283. char der[][100] = {
  47284. "./certs/crl/crl.der",
  47285. "./certs/crl/crl2.der",
  47286. ""};
  47287. #endif
  47288. XFILE fp = XBADFILE;
  47289. int i;
  47290. for (i = 0; pem[i][0] != '\0'; i++)
  47291. {
  47292. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47293. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47294. NULL, NULL));
  47295. ExpectNotNull(crl);
  47296. X509_CRL_free(crl);
  47297. if (fp != XBADFILE) {
  47298. XFCLOSE(fp);
  47299. fp = XBADFILE;
  47300. }
  47301. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47302. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  47303. NULL));
  47304. if (EXPECT_FAIL()) {
  47305. crl = NULL;
  47306. }
  47307. ExpectNotNull(crl);
  47308. X509_CRL_free(crl);
  47309. crl = NULL;
  47310. if (fp != XBADFILE) {
  47311. XFCLOSE(fp);
  47312. fp = XBADFILE;
  47313. }
  47314. }
  47315. #ifndef NO_BIO
  47316. for (i = 0; pem[i][0] != '\0'; i++)
  47317. {
  47318. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  47319. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  47320. X509_CRL_free(crl);
  47321. crl = NULL;
  47322. BIO_free(bio);
  47323. bio = NULL;
  47324. }
  47325. #endif
  47326. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47327. for (i = 0; der[i][0] != '\0'; i++) {
  47328. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  47329. ExpectTrue((fp != XBADFILE));
  47330. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  47331. ExpectNotNull(crl);
  47332. X509_CRL_free(crl);
  47333. if (fp != XBADFILE) {
  47334. XFCLOSE(fp);
  47335. fp = XBADFILE;
  47336. }
  47337. fp = XFOPEN(der[i], "rb");
  47338. ExpectTrue((fp != XBADFILE));
  47339. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  47340. if (EXPECT_FAIL()) {
  47341. crl = NULL;
  47342. }
  47343. ExpectNotNull(crl);
  47344. X509_CRL_free(crl);
  47345. crl = NULL;
  47346. if (fp != XBADFILE) {
  47347. XFCLOSE(fp);
  47348. fp = XBADFILE;
  47349. }
  47350. }
  47351. #endif
  47352. #endif
  47353. return EXPECT_RESULT();
  47354. }
  47355. static int test_wolfSSL_X509_load_crl_file(void)
  47356. {
  47357. EXPECT_DECLS;
  47358. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47359. !defined(NO_RSA) && !defined(NO_BIO)
  47360. int i;
  47361. char pem[][100] = {
  47362. "./certs/crl/crl.pem",
  47363. "./certs/crl/crl2.pem",
  47364. "./certs/crl/caEccCrl.pem",
  47365. "./certs/crl/eccCliCRL.pem",
  47366. "./certs/crl/eccSrvCRL.pem",
  47367. ""
  47368. };
  47369. char der[][100] = {
  47370. "./certs/crl/crl.der",
  47371. "./certs/crl/crl2.der",
  47372. ""
  47373. };
  47374. WOLFSSL_X509_STORE* store = NULL;
  47375. WOLFSSL_X509_LOOKUP* lookup = NULL;
  47376. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47377. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47378. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47379. X509_FILETYPE_PEM), 1);
  47380. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47381. X509_FILETYPE_PEM), 1);
  47382. if (store) {
  47383. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47384. WOLFSSL_FILETYPE_PEM), 1);
  47385. /* since store hasn't yet known the revoked cert*/
  47386. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47387. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47388. }
  47389. for (i = 0; pem[i][0] != '\0'; i++) {
  47390. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  47391. 1);
  47392. }
  47393. if (store) {
  47394. /* since store knows crl list */
  47395. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47396. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47397. CRL_CERT_REVOKED);
  47398. }
  47399. /* once feeing store */
  47400. X509_STORE_free(store);
  47401. store = NULL;
  47402. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47403. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47404. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47405. X509_FILETYPE_PEM), 1);
  47406. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47407. X509_FILETYPE_PEM), 1);
  47408. if (store) {
  47409. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47410. WOLFSSL_FILETYPE_PEM), 1);
  47411. /* since store hasn't yet known the revoked cert*/
  47412. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47413. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47414. }
  47415. for (i = 0; der[i][0] != '\0'; i++) {
  47416. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  47417. 1);
  47418. }
  47419. if (store) {
  47420. /* since store knows crl list */
  47421. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47422. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47423. CRL_CERT_REVOKED);
  47424. }
  47425. /* test for incorrect parameter */
  47426. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  47427. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  47428. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  47429. X509_STORE_free(store);
  47430. store = NULL;
  47431. #endif
  47432. return EXPECT_RESULT();
  47433. }
  47434. static int test_wolfSSL_i2d_X509(void)
  47435. {
  47436. EXPECT_DECLS;
  47437. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  47438. const unsigned char* cert_buf = server_cert_der_2048;
  47439. unsigned char* out = NULL;
  47440. unsigned char* tmp = NULL;
  47441. X509* cert = NULL;
  47442. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  47443. /* Pointer should be advanced */
  47444. ExpectPtrGT(cert_buf, server_cert_der_2048);
  47445. ExpectIntGT(i2d_X509(cert, &out), 0);
  47446. ExpectNotNull(out);
  47447. tmp = out;
  47448. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  47449. ExpectPtrGT(tmp, out);
  47450. if (out != NULL)
  47451. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  47452. X509_free(cert);
  47453. #endif
  47454. return EXPECT_RESULT();
  47455. }
  47456. static int test_wolfSSL_d2i_X509_REQ(void)
  47457. {
  47458. EXPECT_DECLS;
  47459. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  47460. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  47461. !defined(WOLFSSL_SP_MATH)
  47462. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  47463. * were generated by libest
  47464. * ./certs/csr.attr.der contains sample attributes
  47465. * ./certs/csr.ext.der contains sample extensions */
  47466. const char* csrFile = "./certs/csr.signed.der";
  47467. const char* csrPopFile = "./certs/csr.attr.der";
  47468. const char* csrExtFile = "./certs/csr.ext.der";
  47469. /* ./certs/csr.dsa.pem is generated using
  47470. * openssl req -newkey dsa:certs/dsaparams.pem \
  47471. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  47472. * -outform PEM
  47473. * with the passphrase "wolfSSL"
  47474. */
  47475. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47476. const char* csrDsaFile = "./certs/csr.dsa.pem";
  47477. XFILE f = XBADFILE;
  47478. #endif
  47479. BIO* bio = NULL;
  47480. X509* req = NULL;
  47481. EVP_PKEY *pub_key = NULL;
  47482. {
  47483. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  47484. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47485. /*
  47486. * Extract the public key from the CSR
  47487. */
  47488. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47489. /*
  47490. * Verify the signature in the CSR
  47491. */
  47492. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47493. X509_free(req);
  47494. req = NULL;
  47495. BIO_free(bio);
  47496. bio = NULL;
  47497. EVP_PKEY_free(pub_key);
  47498. pub_key = NULL;
  47499. }
  47500. {
  47501. #ifdef OPENSSL_ALL
  47502. X509_ATTRIBUTE* attr;
  47503. ASN1_TYPE *at;
  47504. #endif
  47505. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  47506. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47507. /*
  47508. * Extract the public key from the CSR
  47509. */
  47510. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47511. /*
  47512. * Verify the signature in the CSR
  47513. */
  47514. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47515. #ifdef OPENSSL_ALL
  47516. /*
  47517. * Obtain the challenge password from the CSR
  47518. */
  47519. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47520. -1), 1);
  47521. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  47522. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47523. ExpectNotNull(at->value.asn1_string);
  47524. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  47525. "2xIE+qqp/rhyTXP+");
  47526. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  47527. #endif
  47528. X509_free(req);
  47529. req = NULL;
  47530. BIO_free(bio);
  47531. bio = NULL;
  47532. EVP_PKEY_free(pub_key);
  47533. pub_key = NULL;
  47534. }
  47535. {
  47536. #ifdef OPENSSL_ALL
  47537. X509_ATTRIBUTE* attr;
  47538. ASN1_TYPE *at;
  47539. STACK_OF(X509_EXTENSION) *exts = NULL;
  47540. #endif
  47541. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  47542. /* This CSR contains an Extension Request attribute so
  47543. * we test extension parsing in a CSR attribute here. */
  47544. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47545. /*
  47546. * Extract the public key from the CSR
  47547. */
  47548. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47549. /*
  47550. * Verify the signature in the CSR
  47551. */
  47552. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47553. #ifdef OPENSSL_ALL
  47554. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  47555. req));
  47556. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  47557. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  47558. /*
  47559. * Obtain the challenge password from the CSR
  47560. */
  47561. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47562. -1), 0);
  47563. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  47564. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47565. ExpectNotNull(at->value.asn1_string);
  47566. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  47567. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  47568. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  47569. #endif
  47570. X509_free(req);
  47571. req = NULL;
  47572. BIO_free(bio);
  47573. bio = NULL;
  47574. EVP_PKEY_free(pub_key);
  47575. pub_key = NULL;
  47576. }
  47577. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47578. {
  47579. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  47580. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  47581. /*
  47582. * Extract the public key from the CSR
  47583. */
  47584. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47585. /*
  47586. * Verify the signature in the CSR
  47587. */
  47588. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47589. X509_free(req);
  47590. req = NULL;
  47591. BIO_free(bio);
  47592. /* Run the same test, but with a file pointer instead of a BIO.
  47593. * (PEM_read_X509_REQ)*/
  47594. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  47595. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  47596. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47597. X509_free(req);
  47598. EVP_PKEY_free(pub_key);
  47599. }
  47600. #endif /* !NO_DSA && !HAVE_SELFTEST */
  47601. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  47602. return EXPECT_RESULT();
  47603. }
  47604. static int test_wolfSSL_PEM_read_X509(void)
  47605. {
  47606. EXPECT_DECLS;
  47607. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47608. !defined(NO_RSA)
  47609. X509 *x509 = NULL;
  47610. XFILE fp = XBADFILE;
  47611. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  47612. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  47613. X509_free(x509);
  47614. if (fp != XBADFILE)
  47615. XFCLOSE(fp);
  47616. #endif
  47617. return EXPECT_RESULT();
  47618. }
  47619. static int test_wolfSSL_PEM_read(void)
  47620. {
  47621. EXPECT_DECLS;
  47622. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  47623. const char* filename = "./certs/server-keyEnc.pem";
  47624. XFILE fp = XBADFILE;
  47625. char* name = NULL;
  47626. char* header = NULL;
  47627. byte* data = NULL;
  47628. long len;
  47629. EVP_CIPHER_INFO cipher;
  47630. WOLFSSL_BIO* bio = NULL;
  47631. byte* fileData = NULL;
  47632. size_t fileDataSz = 0;
  47633. byte* out;
  47634. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  47635. /* Fail cases. */
  47636. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  47637. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  47638. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  47639. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  47640. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  47641. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  47642. ExpectIntGT(XSTRLEN(header), 0);
  47643. ExpectIntGT(len, 0);
  47644. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  47645. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  47646. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  47647. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  47648. DYNAMIC_TYPE_TMP_BUFFER));
  47649. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  47650. if (fp != XBADFILE) {
  47651. XFCLOSE(fp);
  47652. fp = XBADFILE;
  47653. }
  47654. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  47655. /* Fail cases. */
  47656. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  47657. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  47658. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  47659. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  47660. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  47661. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  47662. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  47663. /* Fail cases. */
  47664. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  47665. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  47666. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  47667. #ifndef NO_DES3
  47668. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  47669. #endif
  47670. /* Fail cases. */
  47671. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  47672. (void*)"yassl123"), WOLFSSL_FAILURE);
  47673. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  47674. (void*)"yassl123"), WOLFSSL_FAILURE);
  47675. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  47676. (void*)"yassl123"), WOLFSSL_FAILURE);
  47677. #if !defined(NO_DES3) && !defined(NO_MD5)
  47678. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  47679. (void*)"yassl123"), WOLFSSL_SUCCESS);
  47680. #endif
  47681. BIO_free(bio);
  47682. bio = NULL;
  47683. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47684. fileData = NULL;
  47685. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47686. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47687. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47688. name = NULL;
  47689. header = NULL;
  47690. data = NULL;
  47691. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  47692. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  47693. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  47694. ExpectIntEQ(XSTRLEN(header), 0);
  47695. ExpectIntGT(len, 0);
  47696. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  47697. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  47698. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  47699. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  47700. DYNAMIC_TYPE_TMP_BUFFER));
  47701. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  47702. if (fp != XBADFILE)
  47703. XFCLOSE(fp);
  47704. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  47705. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  47706. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  47707. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  47708. BIO_free(bio);
  47709. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47710. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47711. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47712. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47713. #endif
  47714. return EXPECT_RESULT();
  47715. }
  47716. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  47717. {
  47718. EXPECT_DECLS;
  47719. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47720. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47721. const byte iv[12] = { 0 };
  47722. const byte key[16] = { 0 };
  47723. const byte cleartext[16] = { 0 };
  47724. const byte aad[] = {
  47725. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  47726. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  47727. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  47728. 0x4f
  47729. };
  47730. byte out1Part[16];
  47731. byte outTag1Part[16];
  47732. byte out2Part[16];
  47733. byte outTag2Part[16];
  47734. byte decryptBuf[16];
  47735. int len = 0;
  47736. int tlen;
  47737. EVP_CIPHER_CTX* ctx = NULL;
  47738. /* ENCRYPT */
  47739. /* Send AAD and data in 1 part */
  47740. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47741. tlen = 0;
  47742. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47743. 1);
  47744. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47745. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  47746. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  47747. sizeof(cleartext)), 1);
  47748. tlen += len;
  47749. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  47750. tlen += len;
  47751. ExpectIntEQ(tlen, sizeof(cleartext));
  47752. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  47753. outTag1Part), 1);
  47754. EVP_CIPHER_CTX_free(ctx);
  47755. ctx = NULL;
  47756. /* DECRYPT */
  47757. /* Send AAD and data in 1 part */
  47758. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47759. tlen = 0;
  47760. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47761. 1);
  47762. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47763. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  47764. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  47765. sizeof(cleartext)), 1);
  47766. tlen += len;
  47767. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  47768. outTag1Part), 1);
  47769. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  47770. tlen += len;
  47771. ExpectIntEQ(tlen, sizeof(cleartext));
  47772. EVP_CIPHER_CTX_free(ctx);
  47773. ctx = NULL;
  47774. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  47775. /* ENCRYPT */
  47776. /* Send AAD and data in 2 parts */
  47777. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47778. tlen = 0;
  47779. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47780. 1);
  47781. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47782. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  47783. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  47784. 1);
  47785. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  47786. tlen += len;
  47787. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  47788. sizeof(cleartext) - 1), 1);
  47789. tlen += len;
  47790. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  47791. tlen += len;
  47792. ExpectIntEQ(tlen, sizeof(cleartext));
  47793. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  47794. outTag2Part), 1);
  47795. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  47796. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  47797. EVP_CIPHER_CTX_free(ctx);
  47798. ctx = NULL;
  47799. /* DECRYPT */
  47800. /* Send AAD and data in 2 parts */
  47801. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47802. tlen = 0;
  47803. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47804. 1);
  47805. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47806. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  47807. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  47808. 1);
  47809. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  47810. tlen += len;
  47811. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  47812. sizeof(cleartext) - 1), 1);
  47813. tlen += len;
  47814. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  47815. outTag1Part), 1);
  47816. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  47817. tlen += len;
  47818. ExpectIntEQ(tlen, sizeof(cleartext));
  47819. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  47820. /* Test AAD reuse */
  47821. EVP_CIPHER_CTX_free(ctx);
  47822. #endif
  47823. return EXPECT_RESULT();
  47824. }
  47825. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  47826. {
  47827. EXPECT_DECLS;
  47828. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47829. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47830. /* Zero length plain text */
  47831. byte key[] = {
  47832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47833. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47834. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47835. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47836. }; /* align */
  47837. byte iv[] = {
  47838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47839. }; /* align */
  47840. byte plaintxt[1];
  47841. int ivSz = 12;
  47842. int plaintxtSz = 0;
  47843. unsigned char tag[16];
  47844. unsigned char tag_kat[] = {
  47845. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  47846. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  47847. };
  47848. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47849. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47850. int ciphertxtSz = 0;
  47851. int decryptedtxtSz = 0;
  47852. int len = 0;
  47853. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47854. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  47855. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  47856. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47857. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  47858. plaintxtSz));
  47859. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  47860. ciphertxtSz += len;
  47861. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  47862. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  47863. ExpectIntEQ(0, ciphertxtSz);
  47864. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  47865. EVP_CIPHER_CTX_init(de);
  47866. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  47867. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47868. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  47869. decryptedtxtSz = len;
  47870. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  47871. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  47872. decryptedtxtSz += len;
  47873. ExpectIntEQ(0, decryptedtxtSz);
  47874. EVP_CIPHER_CTX_free(en);
  47875. EVP_CIPHER_CTX_free(de);
  47876. #endif
  47877. return EXPECT_RESULT();
  47878. }
  47879. static int test_wolfssl_EVP_aes_gcm(void)
  47880. {
  47881. EXPECT_DECLS;
  47882. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47883. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47884. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47885. byte *key = (byte*)"01234567890123456789012345678901";
  47886. /* A 128 bit IV */
  47887. byte *iv = (byte*)"0123456789012345";
  47888. int ivSz = AES_BLOCK_SIZE;
  47889. /* Message to be encrypted */
  47890. byte *plaintxt = (byte*)"for things to change you have to change";
  47891. /* Additional non-confidential data */
  47892. byte *aad = (byte*)"Don't spend major time on minor things.";
  47893. unsigned char tag[AES_BLOCK_SIZE] = {0};
  47894. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  47895. int aadSz = (int)XSTRLEN((char*)aad);
  47896. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47897. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47898. int ciphertxtSz = 0;
  47899. int decryptedtxtSz = 0;
  47900. int len = 0;
  47901. int i = 0;
  47902. EVP_CIPHER_CTX en[2];
  47903. EVP_CIPHER_CTX de[2];
  47904. for (i = 0; i < 2; i++) {
  47905. EVP_CIPHER_CTX_init(&en[i]);
  47906. if (i == 0) {
  47907. /* Default uses 96-bits IV length */
  47908. #ifdef WOLFSSL_AES_128
  47909. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47910. key, iv));
  47911. #elif defined(WOLFSSL_AES_192)
  47912. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47913. key, iv));
  47914. #elif defined(WOLFSSL_AES_256)
  47915. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47916. key, iv));
  47917. #endif
  47918. }
  47919. else {
  47920. #ifdef WOLFSSL_AES_128
  47921. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47922. NULL, NULL));
  47923. #elif defined(WOLFSSL_AES_192)
  47924. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47925. NULL, NULL));
  47926. #elif defined(WOLFSSL_AES_256)
  47927. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47928. NULL, NULL));
  47929. #endif
  47930. /* non-default must to set the IV length first */
  47931. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  47932. ivSz, NULL));
  47933. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47934. }
  47935. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47936. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  47937. plaintxtSz));
  47938. ciphertxtSz = len;
  47939. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47940. ciphertxtSz += len;
  47941. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  47942. AES_BLOCK_SIZE, tag));
  47943. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  47944. EVP_CIPHER_CTX_init(&de[i]);
  47945. if (i == 0) {
  47946. /* Default uses 96-bits IV length */
  47947. #ifdef WOLFSSL_AES_128
  47948. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47949. key, iv));
  47950. #elif defined(WOLFSSL_AES_192)
  47951. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47952. key, iv));
  47953. #elif defined(WOLFSSL_AES_256)
  47954. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47955. key, iv));
  47956. #endif
  47957. }
  47958. else {
  47959. #ifdef WOLFSSL_AES_128
  47960. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47961. NULL, NULL));
  47962. #elif defined(WOLFSSL_AES_192)
  47963. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47964. NULL, NULL));
  47965. #elif defined(WOLFSSL_AES_256)
  47966. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47967. NULL, NULL));
  47968. #endif
  47969. /* non-default must to set the IV length first */
  47970. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  47971. ivSz, NULL));
  47972. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47973. }
  47974. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47975. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47976. ciphertxtSz));
  47977. decryptedtxtSz = len;
  47978. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47979. AES_BLOCK_SIZE, tag));
  47980. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47981. decryptedtxtSz += len;
  47982. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  47983. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47984. /* modify tag*/
  47985. if (i == 0) {
  47986. /* Default uses 96-bits IV length */
  47987. #ifdef WOLFSSL_AES_128
  47988. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47989. key, iv));
  47990. #elif defined(WOLFSSL_AES_192)
  47991. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47992. key, iv));
  47993. #elif defined(WOLFSSL_AES_256)
  47994. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47995. key, iv));
  47996. #endif
  47997. }
  47998. else {
  47999. #ifdef WOLFSSL_AES_128
  48000. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48001. NULL, NULL));
  48002. #elif defined(WOLFSSL_AES_192)
  48003. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48004. NULL, NULL));
  48005. #elif defined(WOLFSSL_AES_256)
  48006. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48007. NULL, NULL));
  48008. #endif
  48009. /* non-default must to set the IV length first */
  48010. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48011. ivSz, NULL));
  48012. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48013. }
  48014. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48015. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48016. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48017. AES_BLOCK_SIZE, tag));
  48018. /* fail due to wrong tag */
  48019. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48020. ciphertxtSz));
  48021. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48022. ExpectIntEQ(0, len);
  48023. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48024. }
  48025. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48026. return EXPECT_RESULT();
  48027. }
  48028. static int test_wolfssl_EVP_aria_gcm(void)
  48029. {
  48030. int res = TEST_SKIPPED;
  48031. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  48032. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48033. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48034. byte *key = (byte*)"01234567890123456789012345678901";
  48035. /* A 128 bit IV */
  48036. byte *iv = (byte*)"0123456789012345";
  48037. int ivSz = ARIA_BLOCK_SIZE;
  48038. /* Message to be encrypted */
  48039. const int plaintxtSz = 40;
  48040. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48041. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  48042. /* Additional non-confidential data */
  48043. byte *aad = (byte*)"Don't spend major time on minor things.";
  48044. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  48045. int aadSz = (int)XSTRLEN((char*)aad);
  48046. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48047. byte decryptedtxt[plaintxtSz];
  48048. int ciphertxtSz = 0;
  48049. int decryptedtxtSz = 0;
  48050. int len = 0;
  48051. int i = 0;
  48052. #define TEST_ARIA_GCM_COUNT 6
  48053. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  48054. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  48055. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  48056. EVP_CIPHER_CTX_init(&en[i]);
  48057. switch (i) {
  48058. case 0:
  48059. /* Default uses 96-bits IV length */
  48060. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  48061. break;
  48062. case 1:
  48063. /* Default uses 96-bits IV length */
  48064. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  48065. break;
  48066. case 2:
  48067. /* Default uses 96-bits IV length */
  48068. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  48069. break;
  48070. case 3:
  48071. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48072. /* non-default must to set the IV length first */
  48073. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48074. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48075. break;
  48076. case 4:
  48077. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48078. /* non-default must to set the IV length first */
  48079. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48080. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48081. break;
  48082. case 5:
  48083. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48084. /* non-default must to set the IV length first */
  48085. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48086. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48087. break;
  48088. }
  48089. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  48090. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48091. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48092. ciphertxtSz = len;
  48093. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48094. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  48095. ciphertxtSz += len;
  48096. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  48097. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48098. EVP_CIPHER_CTX_init(&de[i]);
  48099. switch (i) {
  48100. case 0:
  48101. /* Default uses 96-bits IV length */
  48102. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  48103. break;
  48104. case 1:
  48105. /* Default uses 96-bits IV length */
  48106. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  48107. break;
  48108. case 2:
  48109. /* Default uses 96-bits IV length */
  48110. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  48111. break;
  48112. case 3:
  48113. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48114. /* non-default must to set the IV length first */
  48115. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48116. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48117. break;
  48118. case 4:
  48119. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48120. /* non-default must to set the IV length first */
  48121. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48122. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48123. break;
  48124. case 5:
  48125. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48126. /* non-default must to set the IV length first */
  48127. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48128. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48129. break;
  48130. }
  48131. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48132. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48133. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48134. decryptedtxtSz = len;
  48135. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48136. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48137. decryptedtxtSz += len;
  48138. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  48139. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48140. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48141. /* modify tag*/
  48142. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48143. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48144. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48145. /* fail due to wrong tag */
  48146. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48147. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48148. AssertIntEQ(0, len);
  48149. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48150. }
  48151. res = TEST_RES_CHECK(1);
  48152. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48153. return res;
  48154. }
  48155. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48156. {
  48157. EXPECT_DECLS;
  48158. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48159. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48160. /* Zero length plain text */
  48161. byte key[] = {
  48162. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48163. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48164. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48165. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48166. }; /* align */
  48167. byte iv[] = {
  48168. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48169. }; /* align */
  48170. byte plaintxt[1];
  48171. int ivSz = 12;
  48172. int plaintxtSz = 0;
  48173. unsigned char tag[16];
  48174. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48175. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48176. int ciphertxtSz = 0;
  48177. int decryptedtxtSz = 0;
  48178. int len = 0;
  48179. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48180. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48181. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48182. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48183. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48184. plaintxtSz));
  48185. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48186. ciphertxtSz += len;
  48187. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48188. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48189. ExpectIntEQ(0, ciphertxtSz);
  48190. EVP_CIPHER_CTX_init(de);
  48191. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48192. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48193. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48194. decryptedtxtSz = len;
  48195. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48196. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48197. decryptedtxtSz += len;
  48198. ExpectIntEQ(0, decryptedtxtSz);
  48199. EVP_CIPHER_CTX_free(en);
  48200. EVP_CIPHER_CTX_free(de);
  48201. #endif
  48202. return EXPECT_RESULT();
  48203. }
  48204. static int test_wolfssl_EVP_aes_ccm(void)
  48205. {
  48206. EXPECT_DECLS;
  48207. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48208. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48209. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48210. byte *key = (byte*)"01234567890123456789012345678901";
  48211. /* A 128 bit IV */
  48212. byte *iv = (byte*)"0123456789012";
  48213. int ivSz = (int)XSTRLEN((char*)iv);
  48214. /* Message to be encrypted */
  48215. byte *plaintxt = (byte*)"for things to change you have to change";
  48216. /* Additional non-confidential data */
  48217. byte *aad = (byte*)"Don't spend major time on minor things.";
  48218. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48219. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48220. int aadSz = (int)XSTRLEN((char*)aad);
  48221. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48222. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48223. int ciphertxtSz = 0;
  48224. int decryptedtxtSz = 0;
  48225. int len = 0;
  48226. int i = 0;
  48227. int ret;
  48228. EVP_CIPHER_CTX en[2];
  48229. EVP_CIPHER_CTX de[2];
  48230. for (i = 0; i < 2; i++) {
  48231. EVP_CIPHER_CTX_init(&en[i]);
  48232. if (i == 0) {
  48233. /* Default uses 96-bits IV length */
  48234. #ifdef WOLFSSL_AES_128
  48235. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48236. key, iv));
  48237. #elif defined(WOLFSSL_AES_192)
  48238. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48239. key, iv));
  48240. #elif defined(WOLFSSL_AES_256)
  48241. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48242. key, iv));
  48243. #endif
  48244. }
  48245. else {
  48246. #ifdef WOLFSSL_AES_128
  48247. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48248. NULL, NULL));
  48249. #elif defined(WOLFSSL_AES_192)
  48250. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48251. NULL, NULL));
  48252. #elif defined(WOLFSSL_AES_256)
  48253. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48254. NULL, NULL));
  48255. #endif
  48256. /* non-default must to set the IV length first */
  48257. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48258. ivSz, NULL));
  48259. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48260. }
  48261. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48262. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48263. plaintxtSz));
  48264. ciphertxtSz = len;
  48265. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48266. ciphertxtSz += len;
  48267. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48268. AES_BLOCK_SIZE, tag));
  48269. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  48270. ExpectIntEQ(ret, 1);
  48271. EVP_CIPHER_CTX_init(&de[i]);
  48272. if (i == 0) {
  48273. /* Default uses 96-bits IV length */
  48274. #ifdef WOLFSSL_AES_128
  48275. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48276. key, iv));
  48277. #elif defined(WOLFSSL_AES_192)
  48278. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48279. key, iv));
  48280. #elif defined(WOLFSSL_AES_256)
  48281. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48282. key, iv));
  48283. #endif
  48284. }
  48285. else {
  48286. #ifdef WOLFSSL_AES_128
  48287. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48288. NULL, NULL));
  48289. #elif defined(WOLFSSL_AES_192)
  48290. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48291. NULL, NULL));
  48292. #elif defined(WOLFSSL_AES_256)
  48293. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48294. NULL, NULL));
  48295. #endif
  48296. /* non-default must to set the IV length first */
  48297. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48298. ivSz, NULL));
  48299. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48300. }
  48301. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48302. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48303. ciphertxtSz));
  48304. decryptedtxtSz = len;
  48305. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48306. AES_BLOCK_SIZE, tag));
  48307. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48308. decryptedtxtSz += len;
  48309. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48310. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48311. /* modify tag*/
  48312. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48313. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48314. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48315. AES_BLOCK_SIZE, tag));
  48316. /* fail due to wrong tag */
  48317. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48318. ciphertxtSz));
  48319. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48320. ExpectIntEQ(0, len);
  48321. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  48322. ExpectIntEQ(ret, 1);
  48323. }
  48324. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  48325. return EXPECT_RESULT();
  48326. }
  48327. static int test_wolfssl_EVP_chacha20_poly1305(void)
  48328. {
  48329. EXPECT_DECLS;
  48330. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  48331. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  48332. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  48333. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48334. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  48335. byte cipherText[sizeof(plainText)];
  48336. byte decryptedText[sizeof(plainText)];
  48337. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  48338. EVP_CIPHER_CTX* ctx = NULL;
  48339. int outSz;
  48340. /* Encrypt. */
  48341. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48342. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48343. NULL), WOLFSSL_SUCCESS);
  48344. /* Invalid IV length. */
  48345. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48346. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  48347. /* Valid IV length. */
  48348. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48349. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48350. /* Invalid tag length. */
  48351. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48352. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  48353. /* Valid tag length. */
  48354. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48355. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  48356. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48357. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48358. WOLFSSL_SUCCESS);
  48359. ExpectIntEQ(outSz, sizeof(aad));
  48360. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48361. sizeof(plainText)), WOLFSSL_SUCCESS);
  48362. ExpectIntEQ(outSz, sizeof(plainText));
  48363. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48364. ExpectIntEQ(outSz, 0);
  48365. /* Invalid tag length. */
  48366. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48367. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  48368. /* Valid tag length. */
  48369. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48370. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48371. EVP_CIPHER_CTX_free(ctx);
  48372. ctx = NULL;
  48373. /* Decrypt. */
  48374. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48375. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48376. NULL), WOLFSSL_SUCCESS);
  48377. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48378. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48379. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48380. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48381. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48382. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48383. WOLFSSL_SUCCESS);
  48384. ExpectIntEQ(outSz, sizeof(aad));
  48385. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48386. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48387. ExpectIntEQ(outSz, sizeof(cipherText));
  48388. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48389. WOLFSSL_SUCCESS);
  48390. ExpectIntEQ(outSz, 0);
  48391. EVP_CIPHER_CTX_free(ctx);
  48392. ctx = NULL;
  48393. /* Test partial Inits. CipherInit() allow setting of key and iv
  48394. * in separate calls. */
  48395. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48396. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  48397. key, NULL, 1), WOLFSSL_SUCCESS);
  48398. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48399. WOLFSSL_SUCCESS);
  48400. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  48401. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  48402. ExpectIntEQ(outSz, sizeof(aad));
  48403. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48404. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48405. ExpectIntEQ(outSz, sizeof(cipherText));
  48406. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48407. WOLFSSL_SUCCESS);
  48408. ExpectIntEQ(outSz, 0);
  48409. EVP_CIPHER_CTX_free(ctx);
  48410. #endif
  48411. return EXPECT_RESULT();
  48412. }
  48413. static int test_wolfssl_EVP_chacha20(void)
  48414. {
  48415. EXPECT_DECLS;
  48416. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  48417. byte key[CHACHA_MAX_KEY_SZ];
  48418. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  48419. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48420. byte cipherText[sizeof(plainText)];
  48421. byte decryptedText[sizeof(plainText)];
  48422. EVP_CIPHER_CTX* ctx = NULL;
  48423. int outSz;
  48424. XMEMSET(key, 0, sizeof(key));
  48425. XMEMSET(iv, 0, sizeof(iv));
  48426. /* Encrypt. */
  48427. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48428. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48429. NULL), WOLFSSL_SUCCESS);
  48430. /* Any tag length must fail - not an AEAD cipher. */
  48431. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48432. 16, NULL), WOLFSSL_FAILURE);
  48433. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48434. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48435. sizeof(plainText)), WOLFSSL_SUCCESS);
  48436. ExpectIntEQ(outSz, sizeof(plainText));
  48437. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48438. ExpectIntEQ(outSz, 0);
  48439. EVP_CIPHER_CTX_free(ctx);
  48440. ctx = NULL;
  48441. /* Decrypt. */
  48442. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48443. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48444. NULL), WOLFSSL_SUCCESS);
  48445. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48446. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48447. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48448. ExpectIntEQ(outSz, sizeof(cipherText));
  48449. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48450. WOLFSSL_SUCCESS);
  48451. ExpectIntEQ(outSz, 0);
  48452. EVP_CIPHER_CTX_free(ctx);
  48453. ctx = NULL;
  48454. /* Test partial Inits. CipherInit() allow setting of key and iv
  48455. * in separate calls. */
  48456. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48457. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  48458. key, NULL, 1), WOLFSSL_SUCCESS);
  48459. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48460. WOLFSSL_SUCCESS);
  48461. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48462. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48463. ExpectIntEQ(outSz, sizeof(cipherText));
  48464. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48465. WOLFSSL_SUCCESS);
  48466. ExpectIntEQ(outSz, 0);
  48467. EVP_CIPHER_CTX_free(ctx);
  48468. #endif
  48469. return EXPECT_RESULT();
  48470. }
  48471. static int test_wolfssl_EVP_sm4_ecb(void)
  48472. {
  48473. int res = TEST_SKIPPED;
  48474. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  48475. EXPECT_DECLS;
  48476. byte key[SM4_KEY_SIZE];
  48477. byte plainText[SM4_BLOCK_SIZE] = {
  48478. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48479. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48480. };
  48481. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48482. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48483. EVP_CIPHER_CTX* ctx;
  48484. int outSz;
  48485. XMEMSET(key, 0, sizeof(key));
  48486. /* Encrypt. */
  48487. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48488. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48489. WOLFSSL_SUCCESS);
  48490. /* Any tag length must fail - not an AEAD cipher. */
  48491. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48492. WOLFSSL_FAILURE);
  48493. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  48494. WOLFSSL_SUCCESS);
  48495. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48496. sizeof(plainText)), WOLFSSL_SUCCESS);
  48497. ExpectIntEQ(outSz, sizeof(plainText));
  48498. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48499. WOLFSSL_SUCCESS);
  48500. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48501. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48502. EVP_CIPHER_CTX_free(ctx);
  48503. /* Decrypt. */
  48504. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48505. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48506. WOLFSSL_SUCCESS);
  48507. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  48508. WOLFSSL_SUCCESS);
  48509. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48510. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48511. ExpectIntEQ(outSz, sizeof(plainText));
  48512. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48513. WOLFSSL_SUCCESS);
  48514. ExpectIntEQ(outSz, 0);
  48515. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48516. EVP_CIPHER_CTX_free(ctx);
  48517. res = EXPECT_RESULT();
  48518. #endif
  48519. return res;
  48520. }
  48521. static int test_wolfssl_EVP_sm4_cbc(void)
  48522. {
  48523. int res = TEST_SKIPPED;
  48524. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  48525. EXPECT_DECLS;
  48526. byte key[SM4_KEY_SIZE];
  48527. byte iv[SM4_BLOCK_SIZE];
  48528. byte plainText[SM4_BLOCK_SIZE] = {
  48529. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48530. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48531. };
  48532. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48533. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48534. EVP_CIPHER_CTX* ctx;
  48535. int outSz;
  48536. XMEMSET(key, 0, sizeof(key));
  48537. XMEMSET(iv, 0, sizeof(iv));
  48538. /* Encrypt. */
  48539. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48540. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48541. WOLFSSL_SUCCESS);
  48542. /* Any tag length must fail - not an AEAD cipher. */
  48543. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48544. WOLFSSL_FAILURE);
  48545. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48546. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48547. sizeof(plainText)), WOLFSSL_SUCCESS);
  48548. ExpectIntEQ(outSz, sizeof(plainText));
  48549. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48550. WOLFSSL_SUCCESS);
  48551. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48552. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48553. EVP_CIPHER_CTX_free(ctx);
  48554. /* Decrypt. */
  48555. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48556. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48557. WOLFSSL_SUCCESS);
  48558. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48559. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48560. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48561. ExpectIntEQ(outSz, sizeof(plainText));
  48562. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48563. WOLFSSL_SUCCESS);
  48564. ExpectIntEQ(outSz, 0);
  48565. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48566. EVP_CIPHER_CTX_free(ctx);
  48567. /* Test partial Inits. CipherInit() allow setting of key and iv
  48568. * in separate calls. */
  48569. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48570. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  48571. WOLFSSL_SUCCESS);
  48572. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  48573. WOLFSSL_SUCCESS);
  48574. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48575. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48576. ExpectIntEQ(outSz, sizeof(plainText));
  48577. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48578. WOLFSSL_SUCCESS);
  48579. ExpectIntEQ(outSz, 0);
  48580. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48581. EVP_CIPHER_CTX_free(ctx);
  48582. res = EXPECT_RESULT();
  48583. #endif
  48584. return res;
  48585. }
  48586. static int test_wolfssl_EVP_sm4_ctr(void)
  48587. {
  48588. int res = TEST_SKIPPED;
  48589. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  48590. EXPECT_DECLS;
  48591. byte key[SM4_KEY_SIZE];
  48592. byte iv[SM4_BLOCK_SIZE];
  48593. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48594. byte cipherText[sizeof(plainText)];
  48595. byte decryptedText[sizeof(plainText)];
  48596. EVP_CIPHER_CTX* ctx;
  48597. int outSz;
  48598. XMEMSET(key, 0, sizeof(key));
  48599. XMEMSET(iv, 0, sizeof(iv));
  48600. /* Encrypt. */
  48601. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48602. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  48603. WOLFSSL_SUCCESS);
  48604. /* Any tag length must fail - not an AEAD cipher. */
  48605. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48606. WOLFSSL_FAILURE);
  48607. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48608. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48609. sizeof(plainText)), WOLFSSL_SUCCESS);
  48610. ExpectIntEQ(outSz, sizeof(plainText));
  48611. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48612. ExpectIntEQ(outSz, 0);
  48613. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48614. EVP_CIPHER_CTX_free(ctx);
  48615. /* Decrypt. */
  48616. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48617. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  48618. WOLFSSL_SUCCESS);
  48619. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48620. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48621. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48622. ExpectIntEQ(outSz, sizeof(cipherText));
  48623. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48624. WOLFSSL_SUCCESS);
  48625. ExpectIntEQ(outSz, 0);
  48626. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48627. EVP_CIPHER_CTX_free(ctx);
  48628. /* Test partial Inits. CipherInit() allow setting of key and iv
  48629. * in separate calls. */
  48630. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48631. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  48632. WOLFSSL_SUCCESS);
  48633. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48634. WOLFSSL_SUCCESS);
  48635. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48636. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48637. ExpectIntEQ(outSz, sizeof(cipherText));
  48638. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48639. WOLFSSL_SUCCESS);
  48640. ExpectIntEQ(outSz, 0);
  48641. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48642. EVP_CIPHER_CTX_free(ctx);
  48643. res = EXPECT_RESULT();
  48644. #endif
  48645. return res;
  48646. }
  48647. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  48648. {
  48649. int res = TEST_SKIPPED;
  48650. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  48651. /* Zero length plain text */
  48652. EXPECT_DECLS;
  48653. byte key[] = {
  48654. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48655. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48656. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48657. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48658. }; /* align */
  48659. byte iv[] = {
  48660. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48661. }; /* align */
  48662. byte plaintxt[1];
  48663. int ivSz = 12;
  48664. int plaintxtSz = 0;
  48665. unsigned char tag[16];
  48666. unsigned char tag_kat[16] = {
  48667. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  48668. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  48669. };
  48670. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48671. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48672. int ciphertxtSz = 0;
  48673. int decryptedtxtSz = 0;
  48674. int len = 0;
  48675. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48676. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48677. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  48678. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48679. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48680. plaintxtSz));
  48681. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48682. ciphertxtSz += len;
  48683. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48684. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48685. ExpectIntEQ(0, ciphertxtSz);
  48686. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48687. EVP_CIPHER_CTX_init(de);
  48688. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  48689. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48690. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48691. decryptedtxtSz = len;
  48692. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48693. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48694. decryptedtxtSz += len;
  48695. ExpectIntEQ(0, decryptedtxtSz);
  48696. EVP_CIPHER_CTX_free(en);
  48697. EVP_CIPHER_CTX_free(de);
  48698. res = EXPECT_RESULT();
  48699. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  48700. return res;
  48701. }
  48702. static int test_wolfssl_EVP_sm4_gcm(void)
  48703. {
  48704. int res = TEST_SKIPPED;
  48705. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  48706. EXPECT_DECLS;
  48707. byte *key = (byte*)"0123456789012345";
  48708. /* A 128 bit IV */
  48709. byte *iv = (byte*)"0123456789012345";
  48710. int ivSz = SM4_BLOCK_SIZE;
  48711. /* Message to be encrypted */
  48712. byte *plaintxt = (byte*)"for things to change you have to change";
  48713. /* Additional non-confidential data */
  48714. byte *aad = (byte*)"Don't spend major time on minor things.";
  48715. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  48716. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48717. int aadSz = (int)XSTRLEN((char*)aad);
  48718. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48719. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48720. int ciphertxtSz = 0;
  48721. int decryptedtxtSz = 0;
  48722. int len = 0;
  48723. int i = 0;
  48724. EVP_CIPHER_CTX en[2];
  48725. EVP_CIPHER_CTX de[2];
  48726. for (i = 0; i < 2; i++) {
  48727. EVP_CIPHER_CTX_init(&en[i]);
  48728. if (i == 0) {
  48729. /* Default uses 96-bits IV length */
  48730. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  48731. iv));
  48732. }
  48733. else {
  48734. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  48735. NULL));
  48736. /* non-default must to set the IV length first */
  48737. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  48738. ivSz, NULL));
  48739. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48740. }
  48741. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48742. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48743. plaintxtSz));
  48744. ciphertxtSz = len;
  48745. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48746. ciphertxtSz += len;
  48747. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  48748. SM4_BLOCK_SIZE, tag));
  48749. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48750. EVP_CIPHER_CTX_init(&de[i]);
  48751. if (i == 0) {
  48752. /* Default uses 96-bits IV length */
  48753. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  48754. iv));
  48755. }
  48756. else {
  48757. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  48758. NULL));
  48759. /* non-default must to set the IV length first */
  48760. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48761. ivSz, NULL));
  48762. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48763. }
  48764. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48765. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48766. ciphertxtSz));
  48767. decryptedtxtSz = len;
  48768. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48769. SM4_BLOCK_SIZE, tag));
  48770. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48771. decryptedtxtSz += len;
  48772. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48773. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48774. /* modify tag*/
  48775. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  48776. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48777. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48778. SM4_BLOCK_SIZE, tag));
  48779. /* fail due to wrong tag */
  48780. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48781. ciphertxtSz));
  48782. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48783. ExpectIntEQ(0, len);
  48784. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48785. }
  48786. res = EXPECT_RESULT();
  48787. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  48788. return res;
  48789. }
  48790. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  48791. {
  48792. int res = TEST_SKIPPED;
  48793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  48794. /* Zero length plain text */
  48795. EXPECT_DECLS;
  48796. byte key[] = {
  48797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48799. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48800. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48801. }; /* align */
  48802. byte iv[] = {
  48803. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48804. }; /* align */
  48805. byte plaintxt[1];
  48806. int ivSz = 12;
  48807. int plaintxtSz = 0;
  48808. unsigned char tag[16];
  48809. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48810. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48811. int ciphertxtSz = 0;
  48812. int decryptedtxtSz = 0;
  48813. int len = 0;
  48814. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48815. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48816. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  48817. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48818. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48819. plaintxtSz));
  48820. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48821. ciphertxtSz += len;
  48822. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48823. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48824. ExpectIntEQ(0, ciphertxtSz);
  48825. EVP_CIPHER_CTX_init(de);
  48826. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  48827. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48828. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48829. decryptedtxtSz = len;
  48830. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48831. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48832. decryptedtxtSz += len;
  48833. ExpectIntEQ(0, decryptedtxtSz);
  48834. EVP_CIPHER_CTX_free(en);
  48835. EVP_CIPHER_CTX_free(de);
  48836. res = EXPECT_RESULT();
  48837. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48838. return res;
  48839. }
  48840. static int test_wolfssl_EVP_sm4_ccm(void)
  48841. {
  48842. int res = TEST_SKIPPED;
  48843. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  48844. EXPECT_DECLS;
  48845. byte *key = (byte*)"0123456789012345";
  48846. byte *iv = (byte*)"0123456789012";
  48847. int ivSz = (int)XSTRLEN((char*)iv);
  48848. /* Message to be encrypted */
  48849. byte *plaintxt = (byte*)"for things to change you have to change";
  48850. /* Additional non-confidential data */
  48851. byte *aad = (byte*)"Don't spend major time on minor things.";
  48852. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  48853. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48854. int aadSz = (int)XSTRLEN((char*)aad);
  48855. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48856. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48857. int ciphertxtSz = 0;
  48858. int decryptedtxtSz = 0;
  48859. int len = 0;
  48860. int i = 0;
  48861. EVP_CIPHER_CTX en[2];
  48862. EVP_CIPHER_CTX de[2];
  48863. for (i = 0; i < 2; i++) {
  48864. EVP_CIPHER_CTX_init(&en[i]);
  48865. if (i == 0) {
  48866. /* Default uses 96-bits IV length */
  48867. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  48868. iv));
  48869. }
  48870. else {
  48871. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  48872. NULL));
  48873. /* non-default must to set the IV length first */
  48874. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48875. ivSz, NULL));
  48876. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48877. }
  48878. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48879. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48880. plaintxtSz));
  48881. ciphertxtSz = len;
  48882. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48883. ciphertxtSz += len;
  48884. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48885. SM4_BLOCK_SIZE, tag));
  48886. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48887. EVP_CIPHER_CTX_init(&de[i]);
  48888. if (i == 0) {
  48889. /* Default uses 96-bits IV length */
  48890. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  48891. iv));
  48892. }
  48893. else {
  48894. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  48895. NULL));
  48896. /* non-default must to set the IV length first */
  48897. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48898. ivSz, NULL));
  48899. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48900. }
  48901. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48902. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48903. ciphertxtSz));
  48904. decryptedtxtSz = len;
  48905. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48906. SM4_BLOCK_SIZE, tag));
  48907. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48908. decryptedtxtSz += len;
  48909. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48910. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48911. /* modify tag*/
  48912. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  48913. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48914. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48915. SM4_BLOCK_SIZE, tag));
  48916. /* fail due to wrong tag */
  48917. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48918. ciphertxtSz));
  48919. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48920. ExpectIntEQ(0, len);
  48921. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48922. }
  48923. res = EXPECT_RESULT();
  48924. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48925. return res;
  48926. }
  48927. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  48928. {
  48929. EXPECT_DECLS;
  48930. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  48931. EVP_PKEY_CTX* ctx = NULL;
  48932. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48933. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  48934. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  48935. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  48936. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  48937. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  48938. byte outKey[34];
  48939. size_t outKeySz = sizeof(outKey);
  48940. /* These expected outputs were gathered by running the same test below using
  48941. * OpenSSL. */
  48942. const byte extractAndExpand[] = {
  48943. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  48944. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  48945. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  48946. };
  48947. const byte extractOnly[] = {
  48948. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  48949. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  48950. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  48951. };
  48952. const byte expandOnly[] = {
  48953. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  48954. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  48955. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  48956. };
  48957. const byte extractAndExpandAddInfo[] = {
  48958. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  48959. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  48960. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  48961. };
  48962. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  48963. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  48964. /* NULL ctx. */
  48965. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48966. /* NULL md. */
  48967. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  48968. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  48969. /* NULL ctx. */
  48970. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  48971. WOLFSSL_FAILURE);
  48972. /* NULL salt is ok. */
  48973. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  48974. WOLFSSL_SUCCESS);
  48975. /* Salt length <= 0. */
  48976. /* Length 0 salt is ok. */
  48977. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  48978. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  48979. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  48980. WOLFSSL_SUCCESS);
  48981. /* NULL ctx. */
  48982. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  48983. WOLFSSL_FAILURE);
  48984. /* NULL key. */
  48985. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  48986. WOLFSSL_FAILURE);
  48987. /* Key length <= 0 */
  48988. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  48989. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  48990. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  48991. WOLFSSL_SUCCESS);
  48992. /* NULL ctx. */
  48993. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  48994. WOLFSSL_FAILURE);
  48995. /* NULL info is ok. */
  48996. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  48997. WOLFSSL_SUCCESS);
  48998. /* Info length <= 0 */
  48999. /* Length 0 info is ok. */
  49000. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  49001. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  49002. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  49003. WOLFSSL_SUCCESS);
  49004. /* NULL ctx. */
  49005. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49006. WOLFSSL_FAILURE);
  49007. /* Extract and expand (default). */
  49008. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49009. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  49010. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  49011. /* Extract only. */
  49012. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49013. WOLFSSL_SUCCESS);
  49014. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49015. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  49016. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  49017. outKeySz = sizeof(outKey);
  49018. /* Expand only. */
  49019. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  49020. WOLFSSL_SUCCESS);
  49021. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49022. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  49023. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  49024. outKeySz = sizeof(outKey);
  49025. /* Extract and expand with appended additional info. */
  49026. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  49027. WOLFSSL_SUCCESS);
  49028. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  49029. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  49030. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49031. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  49032. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  49033. EVP_PKEY_CTX_free(ctx);
  49034. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  49035. return EXPECT_RESULT();
  49036. }
  49037. #ifndef NO_BIO
  49038. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  49039. {
  49040. EXPECT_DECLS;
  49041. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49042. BIO* bio = NULL;
  49043. X509_INFO* info = NULL;
  49044. STACK_OF(X509_INFO)* sk = NULL;
  49045. char* subject = NULL;
  49046. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  49047. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49048. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  49049. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49050. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  49051. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  49052. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  49053. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  49054. /* using dereference to maintain testing for Apache port*/
  49055. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49056. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49057. 0, 0));
  49058. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  49059. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49060. X509_INFO_free(info);
  49061. info = NULL;
  49062. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49063. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49064. 0, 0));
  49065. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  49066. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49067. X509_INFO_free(info);
  49068. ExpectNull(info = sk_X509_INFO_pop(sk));
  49069. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  49070. BIO_free(bio);
  49071. #endif
  49072. return EXPECT_RESULT();
  49073. }
  49074. #endif /* !NO_BIO */
  49075. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  49076. {
  49077. EXPECT_DECLS;
  49078. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49079. X509 *x509 = NULL;
  49080. X509_NAME* name = NULL;
  49081. int idx = 0;
  49082. X509_NAME_ENTRY *ne = NULL;
  49083. ASN1_OBJECT *object = NULL;
  49084. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  49085. WOLFSSL_FILETYPE_PEM));
  49086. ExpectNotNull(name = X509_get_subject_name(x509));
  49087. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  49088. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  49089. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  49090. X509_free(x509);
  49091. #endif
  49092. return EXPECT_RESULT();
  49093. }
  49094. static int test_wolfSSL_X509_STORE_get1_certs(void)
  49095. {
  49096. EXPECT_DECLS;
  49097. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  49098. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49099. X509_STORE_CTX *storeCtx = NULL;
  49100. X509_STORE *store = NULL;
  49101. X509 *caX509 = NULL;
  49102. X509 *svrX509 = NULL;
  49103. X509_NAME *subject = NULL;
  49104. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  49105. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  49106. SSL_FILETYPE_PEM));
  49107. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49108. SSL_FILETYPE_PEM)));
  49109. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  49110. ExpectNotNull(store = X509_STORE_new());
  49111. ExpectNotNull(subject = X509_get_subject_name(caX509));
  49112. /* Errors */
  49113. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  49114. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  49115. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  49116. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  49117. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  49118. SSL_SUCCESS);
  49119. /* Should find the cert */
  49120. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49121. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  49122. sk_X509_pop_free(certs, NULL);
  49123. certs = NULL;
  49124. /* Should not find the cert */
  49125. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  49126. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49127. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  49128. sk_X509_pop_free(certs, NULL);
  49129. certs = NULL;
  49130. X509_STORE_free(store);
  49131. X509_STORE_CTX_free(storeCtx);
  49132. X509_free(svrX509);
  49133. X509_free(caX509);
  49134. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  49135. return EXPECT_RESULT();
  49136. }
  49137. static int test_wolfSSL_dup_CA_list(void)
  49138. {
  49139. int res = TEST_SKIPPED;
  49140. #if defined(OPENSSL_ALL)
  49141. EXPECT_DECLS;
  49142. STACK_OF(X509_NAME) *originalStack = NULL;
  49143. STACK_OF(X509_NAME) *copyStack = NULL;
  49144. int originalCount = 0;
  49145. int copyCount = 0;
  49146. X509_NAME *name = NULL;
  49147. int i;
  49148. originalStack = sk_X509_NAME_new_null();
  49149. ExpectNotNull(originalStack);
  49150. for (i = 0; i < 3; i++) {
  49151. name = X509_NAME_new();
  49152. ExpectNotNull(name);
  49153. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  49154. }
  49155. copyStack = SSL_dup_CA_list(originalStack);
  49156. ExpectNotNull(copyStack);
  49157. originalCount = sk_X509_NAME_num(originalStack);
  49158. copyCount = sk_X509_NAME_num(copyStack);
  49159. AssertIntEQ(originalCount, copyCount);
  49160. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  49161. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  49162. originalStack = NULL;
  49163. copyStack = NULL;
  49164. res = EXPECT_RESULT();
  49165. #endif /* OPENSSL_ALL */
  49166. return res;
  49167. }
  49168. static int test_ForceZero(void)
  49169. {
  49170. EXPECT_DECLS;
  49171. unsigned char data[32];
  49172. unsigned int i, j, len;
  49173. /* Test case with 0 length */
  49174. ForceZero(data, 0);
  49175. /* Test ForceZero */
  49176. for (i = 0; i < sizeof(data); i++) {
  49177. for (len = 1; len < sizeof(data) - i; len++) {
  49178. for (j = 0; j < sizeof(data); j++)
  49179. data[j] = j + 1;
  49180. ForceZero(data + i, len);
  49181. for (j = 0; j < sizeof(data); j++) {
  49182. if (j < i || j >= i + len) {
  49183. ExpectIntNE(data[j], 0x00);
  49184. }
  49185. else {
  49186. ExpectIntEQ(data[j], 0x00);
  49187. }
  49188. }
  49189. }
  49190. }
  49191. return EXPECT_RESULT();
  49192. }
  49193. #ifndef NO_BIO
  49194. static int test_wolfSSL_X509_print(void)
  49195. {
  49196. EXPECT_DECLS;
  49197. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49198. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49199. X509 *x509 = NULL;
  49200. BIO *bio = NULL;
  49201. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49202. const X509_ALGOR *cert_sig_alg;
  49203. #endif
  49204. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  49205. WOLFSSL_FILETYPE_PEM));
  49206. /* print to memory */
  49207. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49208. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49209. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49210. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49211. /* Will print IP address subject alt name. */
  49212. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49213. #elif defined(NO_ASN_TIME)
  49214. /* Will print IP address subject alt name but not Validity. */
  49215. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49216. #else
  49217. /* Will print IP address subject alt name. */
  49218. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49219. #endif
  49220. #elif defined(NO_ASN_TIME)
  49221. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49222. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49223. #else
  49224. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  49225. #endif
  49226. BIO_free(bio);
  49227. bio = NULL;
  49228. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49229. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49230. /* Print signature */
  49231. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  49232. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  49233. #endif
  49234. /* print to stderr */
  49235. #if !defined(NO_WOLFSSL_DIR)
  49236. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49237. #endif
  49238. /* print again */
  49239. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  49240. X509_free(x509);
  49241. BIO_free(bio);
  49242. #endif
  49243. return EXPECT_RESULT();
  49244. }
  49245. static int test_wolfSSL_X509_CRL_print(void)
  49246. {
  49247. EXPECT_DECLS;
  49248. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  49249. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  49250. X509_CRL* crl = NULL;
  49251. BIO *bio = NULL;
  49252. XFILE fp = XBADFILE;
  49253. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  49254. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49255. NULL, NULL));
  49256. if (fp != XBADFILE)
  49257. XFCLOSE(fp);
  49258. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49259. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  49260. X509_CRL_free(crl);
  49261. BIO_free(bio);
  49262. #endif
  49263. return EXPECT_RESULT();
  49264. }
  49265. static int test_wolfSSL_BIO_get_len(void)
  49266. {
  49267. EXPECT_DECLS;
  49268. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49269. BIO *bio = NULL;
  49270. const char txt[] = "Some example text to push to the BIO.";
  49271. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  49272. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49273. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  49274. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  49275. BIO_free(bio);
  49276. bio = NULL;
  49277. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49278. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  49279. BIO_free(bio);
  49280. #endif
  49281. return EXPECT_RESULT();
  49282. }
  49283. #endif /* !NO_BIO */
  49284. static int test_wolfSSL_RSA(void)
  49285. {
  49286. EXPECT_DECLS;
  49287. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  49288. defined(WOLFSSL_KEY_GEN)
  49289. RSA* rsa = NULL;
  49290. const BIGNUM *n;
  49291. const BIGNUM *e;
  49292. const BIGNUM *d;
  49293. const BIGNUM *p;
  49294. const BIGNUM *q;
  49295. const BIGNUM *dmp1;
  49296. const BIGNUM *dmq1;
  49297. const BIGNUM *iqmp;
  49298. ExpectNotNull(rsa = RSA_new());
  49299. ExpectIntEQ(RSA_size(NULL), 0);
  49300. ExpectIntEQ(RSA_size(rsa), 0);
  49301. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  49302. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  49303. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  49304. #ifdef WOLFSSL_RSA_KEY_CHECK
  49305. ExpectIntEQ(RSA_check_key(rsa), 0);
  49306. #endif
  49307. RSA_free(rsa);
  49308. rsa = NULL;
  49309. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49310. ExpectIntEQ(RSA_size(rsa), 256);
  49311. #ifdef WOLFSSL_RSA_KEY_CHECK
  49312. ExpectIntEQ(RSA_check_key(NULL), 0);
  49313. ExpectIntEQ(RSA_check_key(rsa), 1);
  49314. #endif
  49315. /* sanity check */
  49316. ExpectIntEQ(RSA_bits(NULL), 0);
  49317. /* key */
  49318. ExpectIntEQ(RSA_bits(rsa), 2048);
  49319. RSA_get0_key(rsa, &n, &e, &d);
  49320. ExpectPtrEq(rsa->n, n);
  49321. ExpectPtrEq(rsa->e, e);
  49322. ExpectPtrEq(rsa->d, d);
  49323. n = NULL;
  49324. e = NULL;
  49325. d = NULL;
  49326. ExpectNotNull(n = BN_new());
  49327. ExpectNotNull(e = BN_new());
  49328. ExpectNotNull(d = BN_new());
  49329. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  49330. if (EXPECT_FAIL()) {
  49331. BN_free((BIGNUM*)n);
  49332. BN_free((BIGNUM*)e);
  49333. BN_free((BIGNUM*)d);
  49334. }
  49335. ExpectPtrEq(rsa->n, n);
  49336. ExpectPtrEq(rsa->e, e);
  49337. ExpectPtrEq(rsa->d, d);
  49338. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  49339. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  49340. /* crt_params */
  49341. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  49342. ExpectPtrEq(rsa->dmp1, dmp1);
  49343. ExpectPtrEq(rsa->dmq1, dmq1);
  49344. ExpectPtrEq(rsa->iqmp, iqmp);
  49345. dmp1 = NULL;
  49346. dmq1 = NULL;
  49347. iqmp = NULL;
  49348. ExpectNotNull(dmp1 = BN_new());
  49349. ExpectNotNull(dmq1 = BN_new());
  49350. ExpectNotNull(iqmp = BN_new());
  49351. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49352. (BIGNUM*)iqmp), 1);
  49353. if (EXPECT_FAIL()) {
  49354. BN_free((BIGNUM*)dmp1);
  49355. BN_free((BIGNUM*)dmq1);
  49356. BN_free((BIGNUM*)iqmp);
  49357. }
  49358. ExpectPtrEq(rsa->dmp1, dmp1);
  49359. ExpectPtrEq(rsa->dmq1, dmq1);
  49360. ExpectPtrEq(rsa->iqmp, iqmp);
  49361. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  49362. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49363. (BIGNUM*)iqmp), 0);
  49364. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  49365. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  49366. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  49367. ExpectNull(dmp1);
  49368. ExpectNull(dmq1);
  49369. ExpectNull(iqmp);
  49370. /* factors */
  49371. RSA_get0_factors(rsa, NULL, NULL);
  49372. RSA_get0_factors(rsa, &p, &q);
  49373. ExpectPtrEq(rsa->p, p);
  49374. ExpectPtrEq(rsa->q, q);
  49375. p = NULL;
  49376. q = NULL;
  49377. ExpectNotNull(p = BN_new());
  49378. ExpectNotNull(q = BN_new());
  49379. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  49380. if (EXPECT_FAIL()) {
  49381. BN_free((BIGNUM*)p);
  49382. BN_free((BIGNUM*)q);
  49383. }
  49384. ExpectPtrEq(rsa->p, p);
  49385. ExpectPtrEq(rsa->q, q);
  49386. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  49387. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  49388. RSA_get0_factors(NULL, NULL, NULL);
  49389. RSA_get0_factors(NULL, &p, &q);
  49390. ExpectNull(p);
  49391. ExpectNull(q);
  49392. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  49393. ExpectIntEQ(RSA_bits(rsa), 21);
  49394. RSA_free(rsa);
  49395. rsa = NULL;
  49396. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  49397. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  49398. ExpectIntEQ(RSA_size(rsa), 384);
  49399. ExpectIntEQ(RSA_bits(rsa), 3072);
  49400. RSA_free(rsa);
  49401. rsa = NULL;
  49402. #endif
  49403. /* remove for now with odd key size until adjusting rsa key size check with
  49404. wc_MakeRsaKey()
  49405. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  49406. RSA_free(rsa);
  49407. rsa = NULL;
  49408. */
  49409. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  49410. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  49411. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  49412. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  49413. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  49414. {
  49415. byte buff[FOURK_BUF];
  49416. byte der[FOURK_BUF];
  49417. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  49418. XFILE f = XBADFILE;
  49419. int bytes;
  49420. /* test loading encrypted RSA private pem w/o password */
  49421. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  49422. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  49423. if (f != XBADFILE)
  49424. XFCLOSE(f);
  49425. XMEMSET(der, 0, sizeof(der));
  49426. /* test that error value is returned with no password */
  49427. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  49428. 0);
  49429. }
  49430. #endif
  49431. #endif
  49432. return EXPECT_RESULT();
  49433. }
  49434. static int test_wolfSSL_RSA_DER(void)
  49435. {
  49436. EXPECT_DECLS;
  49437. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49438. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  49439. RSA *rsa = NULL;
  49440. int i;
  49441. const unsigned char *buff = NULL;
  49442. unsigned char *newBuff = NULL;
  49443. struct tbl_s
  49444. {
  49445. const unsigned char *der;
  49446. int sz;
  49447. } tbl[] = {
  49448. #ifdef USE_CERT_BUFFERS_1024
  49449. {client_key_der_1024, sizeof_client_key_der_1024},
  49450. {server_key_der_1024, sizeof_server_key_der_1024},
  49451. #endif
  49452. #ifdef USE_CERT_BUFFERS_2048
  49453. {client_key_der_2048, sizeof_client_key_der_2048},
  49454. {server_key_der_2048, sizeof_server_key_der_2048},
  49455. #endif
  49456. {NULL, 0}
  49457. };
  49458. /* Public Key DER */
  49459. struct tbl_s pub[] = {
  49460. #ifdef USE_CERT_BUFFERS_1024
  49461. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  49462. #endif
  49463. #ifdef USE_CERT_BUFFERS_2048
  49464. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  49465. #endif
  49466. {NULL, 0}
  49467. };
  49468. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  49469. buff = pub[0].der;
  49470. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  49471. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  49472. buff = tbl[0].der;
  49473. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  49474. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  49475. rsa = RSA_new();
  49476. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  49477. RSA_free(rsa);
  49478. rsa = NULL;
  49479. for (i = 0; tbl[i].der != NULL; i++)
  49480. {
  49481. /* Passing in pointer results in pointer moving. */
  49482. buff = tbl[i].der;
  49483. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  49484. ExpectNotNull(rsa);
  49485. RSA_free(rsa);
  49486. rsa = NULL;
  49487. }
  49488. for (i = 0; tbl[i].der != NULL; i++)
  49489. {
  49490. /* Passing in pointer results in pointer moving. */
  49491. buff = tbl[i].der;
  49492. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  49493. ExpectNotNull(rsa);
  49494. RSA_free(rsa);
  49495. rsa = NULL;
  49496. }
  49497. for (i = 0; pub[i].der != NULL; i++)
  49498. {
  49499. buff = pub[i].der;
  49500. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  49501. ExpectNotNull(rsa);
  49502. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  49503. newBuff = NULL;
  49504. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  49505. ExpectNotNull(newBuff);
  49506. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  49507. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49508. RSA_free(rsa);
  49509. rsa = NULL;
  49510. }
  49511. #endif
  49512. return EXPECT_RESULT();
  49513. }
  49514. static int test_wolfSSL_RSA_print(void)
  49515. {
  49516. EXPECT_DECLS;
  49517. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49518. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49519. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  49520. BIO *bio = NULL;
  49521. WOLFSSL_RSA* rsa = NULL;
  49522. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49523. ExpectNotNull(rsa = RSA_new());
  49524. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  49525. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  49526. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  49527. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  49528. /* Some very large number of indent spaces. */
  49529. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  49530. /* RSA is empty. */
  49531. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  49532. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  49533. RSA_free(rsa);
  49534. rsa = NULL;
  49535. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49536. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  49537. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  49538. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  49539. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  49540. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  49541. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  49542. BIO_free(bio);
  49543. RSA_free(rsa);
  49544. #endif
  49545. return EXPECT_RESULT();
  49546. }
  49547. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  49548. {
  49549. EXPECT_DECLS;
  49550. #ifndef NO_RSA
  49551. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49552. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49553. RSA *rsa = NULL;
  49554. const unsigned char *derBuf = client_key_der_2048;
  49555. unsigned char em[256] = {0}; /* len = 2048/8 */
  49556. /* Random data simulating a hash */
  49557. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  49558. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49559. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49560. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49561. };
  49562. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49563. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  49564. RSA_PSS_SALTLEN_DIGEST), 0);
  49565. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  49566. RSA_PSS_SALTLEN_DIGEST), 0);
  49567. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  49568. RSA_PSS_SALTLEN_DIGEST), 0);
  49569. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  49570. RSA_PSS_SALTLEN_DIGEST), 0);
  49571. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  49572. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  49573. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49574. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  49575. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49576. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  49577. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49578. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  49579. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49580. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49581. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49582. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  49583. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49584. RSA_PSS_SALTLEN_DIGEST), 1);
  49585. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49586. RSA_PSS_SALTLEN_DIGEST), 1);
  49587. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49588. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49589. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49590. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49591. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49592. RSA_PSS_SALTLEN_MAX), 1);
  49593. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49594. RSA_PSS_SALTLEN_MAX), 1);
  49595. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  49596. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  49597. RSA_free(rsa);
  49598. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49599. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49600. #endif
  49601. return EXPECT_RESULT();
  49602. }
  49603. static int test_wolfSSL_RSA_sign_sha3(void)
  49604. {
  49605. EXPECT_DECLS;
  49606. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  49607. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49608. RSA* rsa = NULL;
  49609. const unsigned char *derBuf = client_key_der_2048;
  49610. unsigned char sigRet[256] = {0};
  49611. unsigned int sigLen = sizeof(sigRet);
  49612. /* Random data simulating a hash */
  49613. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  49614. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49615. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49616. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49617. };
  49618. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49619. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  49620. rsa), 1);
  49621. RSA_free(rsa);
  49622. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49623. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  49624. return EXPECT_RESULT();
  49625. }
  49626. static int test_wolfSSL_RSA_get0_key(void)
  49627. {
  49628. EXPECT_DECLS;
  49629. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  49630. RSA *rsa = NULL;
  49631. const BIGNUM* n = NULL;
  49632. const BIGNUM* e = NULL;
  49633. const BIGNUM* d = NULL;
  49634. const unsigned char* der;
  49635. int derSz;
  49636. #ifdef USE_CERT_BUFFERS_1024
  49637. der = client_key_der_1024;
  49638. derSz = sizeof_client_key_der_1024;
  49639. #elif defined(USE_CERT_BUFFERS_2048)
  49640. der = client_key_der_2048;
  49641. derSz = sizeof_client_key_der_2048;
  49642. #else
  49643. der = NULL;
  49644. derSz = 0;
  49645. #endif
  49646. if (der != NULL) {
  49647. RSA_get0_key(NULL, NULL, NULL, NULL);
  49648. RSA_get0_key(rsa, NULL, NULL, NULL);
  49649. RSA_get0_key(NULL, &n, &e, &d);
  49650. ExpectNull(n);
  49651. ExpectNull(e);
  49652. ExpectNull(d);
  49653. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  49654. ExpectNotNull(rsa);
  49655. RSA_get0_key(rsa, NULL, NULL, NULL);
  49656. RSA_get0_key(rsa, &n, NULL, NULL);
  49657. ExpectNotNull(n);
  49658. RSA_get0_key(rsa, NULL, &e, NULL);
  49659. ExpectNotNull(e);
  49660. RSA_get0_key(rsa, NULL, NULL, &d);
  49661. ExpectNotNull(d);
  49662. RSA_get0_key(rsa, &n, &e, &d);
  49663. ExpectNotNull(n);
  49664. ExpectNotNull(e);
  49665. ExpectNotNull(d);
  49666. RSA_free(rsa);
  49667. }
  49668. #endif
  49669. return EXPECT_RESULT();
  49670. }
  49671. static int test_wolfSSL_RSA_meth(void)
  49672. {
  49673. EXPECT_DECLS;
  49674. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49675. RSA *rsa = NULL;
  49676. RSA_METHOD *rsa_meth = NULL;
  49677. #ifdef WOLFSSL_KEY_GEN
  49678. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49679. RSA_free(rsa);
  49680. rsa = NULL;
  49681. #else
  49682. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49683. #endif
  49684. ExpectNotNull(RSA_get_default_method());
  49685. wolfSSL_RSA_meth_free(NULL);
  49686. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  49687. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  49688. RSA_METHOD_FLAG_NO_CHECK));
  49689. #ifndef NO_WOLFSSL_STUB
  49690. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  49691. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  49692. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  49693. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  49694. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  49695. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  49696. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  49697. #endif
  49698. ExpectIntEQ(RSA_flags(NULL), 0);
  49699. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49700. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49701. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  49702. ExpectNotNull(rsa = RSA_new());
  49703. /* No method set. */
  49704. ExpectIntEQ(RSA_flags(rsa), 0);
  49705. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49706. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49707. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49708. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  49709. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  49710. if (EXPECT_FAIL()) {
  49711. wolfSSL_RSA_meth_free(rsa_meth);
  49712. }
  49713. ExpectNull(RSA_get_method(NULL));
  49714. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  49715. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  49716. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49717. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49718. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  49719. RSA_METHOD_FLAG_NO_CHECK);
  49720. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49721. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49722. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  49723. /* rsa_meth is freed here */
  49724. RSA_free(rsa);
  49725. #endif
  49726. return EXPECT_RESULT();
  49727. }
  49728. static int test_wolfSSL_RSA_verify(void)
  49729. {
  49730. EXPECT_DECLS;
  49731. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  49732. !defined(NO_FILESYSTEM)
  49733. #ifndef NO_BIO
  49734. XFILE fp = XBADFILE;
  49735. RSA *pKey = NULL;
  49736. RSA *pubKey = NULL;
  49737. X509 *cert = NULL;
  49738. const char *text = "Hello wolfSSL !";
  49739. unsigned char hash[SHA256_DIGEST_LENGTH];
  49740. unsigned char signature[2048/8];
  49741. unsigned int signatureLength;
  49742. byte *buf = NULL;
  49743. BIO *bio = NULL;
  49744. SHA256_CTX c;
  49745. EVP_PKEY *evpPkey = NULL;
  49746. EVP_PKEY *evpPubkey = NULL;
  49747. size_t sz;
  49748. /* generate hash */
  49749. SHA256_Init(&c);
  49750. SHA256_Update(&c, text, strlen(text));
  49751. SHA256_Final(hash, &c);
  49752. #ifdef WOLFSSL_SMALL_STACK_CACHE
  49753. /* workaround for small stack cache case */
  49754. wc_Sha256Free((wc_Sha256*)&c);
  49755. #endif
  49756. /* read privete key file */
  49757. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49758. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  49759. ExpectTrue((sz = XFTELL(fp)) > 0);
  49760. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  49761. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  49762. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  49763. if (fp != XBADFILE) {
  49764. XFCLOSE(fp);
  49765. fp = XBADFILE;
  49766. }
  49767. /* read private key and sign hash data */
  49768. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  49769. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  49770. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  49771. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  49772. signature, &signatureLength, pKey), SSL_SUCCESS);
  49773. /* read public key and verify signed data */
  49774. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  49775. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  49776. if (fp != XBADFILE)
  49777. XFCLOSE(fp);
  49778. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  49779. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  49780. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49781. signatureLength, pubKey), SSL_SUCCESS);
  49782. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  49783. signatureLength, NULL), SSL_FAILURE);
  49784. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  49785. signatureLength, pubKey), SSL_FAILURE);
  49786. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  49787. signatureLength, pubKey), SSL_FAILURE);
  49788. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49789. signatureLength, NULL), SSL_FAILURE);
  49790. RSA_free(pKey);
  49791. EVP_PKEY_free(evpPkey);
  49792. RSA_free(pubKey);
  49793. EVP_PKEY_free(evpPubkey);
  49794. X509_free(cert);
  49795. BIO_free(bio);
  49796. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  49797. #endif
  49798. #endif
  49799. return EXPECT_RESULT();
  49800. }
  49801. static int test_wolfSSL_RSA_sign(void)
  49802. {
  49803. EXPECT_DECLS;
  49804. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49805. RSA *rsa;
  49806. unsigned char hash[SHA256_DIGEST_LENGTH];
  49807. #ifdef USE_CERT_BUFFERS_1024
  49808. const unsigned char* privDer = client_key_der_1024;
  49809. size_t privDerSz = sizeof_client_key_der_1024;
  49810. const unsigned char* pubDer = client_keypub_der_1024;
  49811. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49812. unsigned char signature[1024/8];
  49813. #else
  49814. const unsigned char* privDer = client_key_der_2048;
  49815. size_t privDerSz = sizeof_client_key_der_2048;
  49816. const unsigned char* pubDer = client_keypub_der_2048;
  49817. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49818. unsigned char signature[2048/8];
  49819. #endif
  49820. unsigned int signatureLen;
  49821. const unsigned char* der;
  49822. XMEMSET(hash, 0, sizeof(hash));
  49823. der = privDer;
  49824. rsa = NULL;
  49825. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49826. /* Invalid parameters. */
  49827. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  49828. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  49829. &signatureLen, rsa), 0);
  49830. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  49831. &signatureLen, rsa), 0);
  49832. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  49833. &signatureLen, rsa), 0);
  49834. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49835. NULL, rsa), 0);
  49836. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49837. &signatureLen, NULL), 0);
  49838. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49839. &signatureLen, rsa), 1);
  49840. RSA_free(rsa);
  49841. der = pubDer;
  49842. rsa = NULL;
  49843. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49844. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49845. signatureLen, rsa), 1);
  49846. RSA_free(rsa);
  49847. #endif
  49848. return EXPECT_RESULT();
  49849. }
  49850. static int test_wolfSSL_RSA_sign_ex(void)
  49851. {
  49852. EXPECT_DECLS;
  49853. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49854. RSA *rsa = NULL;
  49855. unsigned char hash[SHA256_DIGEST_LENGTH];
  49856. #ifdef USE_CERT_BUFFERS_1024
  49857. const unsigned char* privDer = client_key_der_1024;
  49858. size_t privDerSz = sizeof_client_key_der_1024;
  49859. const unsigned char* pubDer = client_keypub_der_1024;
  49860. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49861. unsigned char signature[1024/8];
  49862. #else
  49863. const unsigned char* privDer = client_key_der_2048;
  49864. size_t privDerSz = sizeof_client_key_der_2048;
  49865. const unsigned char* pubDer = client_keypub_der_2048;
  49866. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49867. unsigned char signature[2048/8];
  49868. #endif
  49869. unsigned int signatureLen;
  49870. const unsigned char* der;
  49871. unsigned char encodedHash[51];
  49872. unsigned int encodedHashLen;
  49873. const unsigned char expEncHash[] = {
  49874. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  49875. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  49876. 0x00, 0x04, 0x20,
  49877. /* Hash data */
  49878. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49879. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49880. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49881. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49882. };
  49883. XMEMSET(hash, 0, sizeof(hash));
  49884. ExpectNotNull(rsa = wolfSSL_RSA_new());
  49885. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49886. &signatureLen, rsa, 1), 0);
  49887. wolfSSL_RSA_free(rsa);
  49888. der = privDer;
  49889. rsa = NULL;
  49890. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49891. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  49892. -1), 0);
  49893. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  49894. signature, &signatureLen, rsa, 1), 0);
  49895. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49896. &signatureLen, rsa, 1), 0);
  49897. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49898. &signatureLen, rsa, 1), 0);
  49899. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49900. NULL, rsa, 1), 0);
  49901. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49902. &signatureLen, NULL, 1), 0);
  49903. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49904. &signatureLen, rsa, -1), 0);
  49905. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49906. &signatureLen, rsa, 0), 0);
  49907. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49908. &signatureLen, rsa, 0), 0);
  49909. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49910. NULL, rsa, 0), 0);
  49911. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49912. &signatureLen, rsa, 1), 1);
  49913. /* Test returning encoded hash. */
  49914. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  49915. &encodedHashLen, rsa, 0), 1);
  49916. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  49917. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  49918. RSA_free(rsa);
  49919. der = pubDer;
  49920. rsa = NULL;
  49921. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49922. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49923. signatureLen, rsa), 1);
  49924. RSA_free(rsa);
  49925. #endif
  49926. return EXPECT_RESULT();
  49927. }
  49928. static int test_wolfSSL_RSA_public_decrypt(void)
  49929. {
  49930. EXPECT_DECLS;
  49931. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49932. RSA *rsa;
  49933. unsigned char msg[SHA256_DIGEST_LENGTH];
  49934. #ifdef USE_CERT_BUFFERS_1024
  49935. const unsigned char* pubDer = client_keypub_der_1024;
  49936. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49937. unsigned char decMsg[1024/8];
  49938. const unsigned char encMsg[] = {
  49939. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49940. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49941. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49942. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49943. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49944. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49945. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49946. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49947. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49948. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49949. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49950. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49951. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49952. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49953. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49954. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49955. };
  49956. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49957. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49958. defined(WC_RSA_NO_PADDING)
  49959. const unsigned char encMsgNoPad[] = {
  49960. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49961. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49962. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49963. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49964. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49965. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49966. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49967. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49968. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49969. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49970. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49971. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49972. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49973. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49974. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49975. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49976. };
  49977. #endif
  49978. #else
  49979. const unsigned char* pubDer = client_keypub_der_2048;
  49980. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49981. unsigned char decMsg[2048/8];
  49982. const unsigned char encMsg[] = {
  49983. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49984. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49985. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49986. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49987. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49988. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49989. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49990. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49991. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49992. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49993. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49994. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49995. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49996. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49997. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49998. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49999. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50000. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50001. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50002. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50003. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50004. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50005. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50006. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50007. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50008. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50009. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50010. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50011. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50012. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50013. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50014. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50015. };
  50016. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50017. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50018. defined(WC_RSA_NO_PADDING)
  50019. const unsigned char encMsgNoPad[] = {
  50020. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50021. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50022. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50023. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50024. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50025. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50026. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50027. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50028. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50029. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50030. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50031. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50032. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50033. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50034. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50035. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50036. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50037. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50038. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50039. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50040. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50041. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50042. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50043. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50044. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50045. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50046. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50047. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50048. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50049. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50050. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50051. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50052. };
  50053. #endif
  50054. #endif
  50055. const unsigned char* der;
  50056. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50057. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50058. defined(WC_RSA_NO_PADDING)
  50059. int i;
  50060. #endif
  50061. XMEMSET(msg, 0, sizeof(msg));
  50062. der = pubDer;
  50063. rsa = NULL;
  50064. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50065. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  50066. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  50067. RSA_PKCS1_PADDING), -1);
  50068. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  50069. RSA_PKCS1_PADDING), -1);
  50070. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50071. RSA_PKCS1_PADDING), -1);
  50072. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  50073. RSA_PKCS1_PADDING), -1);
  50074. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50075. RSA_PKCS1_PSS_PADDING), -1);
  50076. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50077. RSA_PKCS1_PADDING), 32);
  50078. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  50079. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50080. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50081. defined(WC_RSA_NO_PADDING)
  50082. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  50083. rsa, RSA_NO_PADDING), sizeof(decMsg));
  50084. /* Zeros before actual data. */
  50085. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  50086. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50087. }
  50088. /* Check actual data. */
  50089. XMEMSET(msg, 0x01, sizeof(msg));
  50090. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50091. #endif
  50092. RSA_free(rsa);
  50093. #endif
  50094. return EXPECT_RESULT();
  50095. }
  50096. static int test_wolfSSL_RSA_private_encrypt(void)
  50097. {
  50098. EXPECT_DECLS;
  50099. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50100. RSA *rsa;
  50101. unsigned char msg[SHA256_DIGEST_LENGTH];
  50102. #ifdef USE_CERT_BUFFERS_1024
  50103. const unsigned char* privDer = client_key_der_1024;
  50104. size_t privDerSz = sizeof_client_key_der_1024;
  50105. unsigned char encMsg[1024/8];
  50106. const unsigned char expEncMsg[] = {
  50107. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50108. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50109. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50110. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50111. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50112. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50113. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50114. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50115. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50116. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50117. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50118. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50119. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50120. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50121. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50122. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50123. };
  50124. #ifdef WC_RSA_NO_PADDING
  50125. const unsigned char expEncMsgNoPad[] = {
  50126. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50127. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50128. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50129. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50130. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50131. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50132. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50133. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50134. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50135. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50136. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50137. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50138. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50139. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50140. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50141. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50142. };
  50143. #endif
  50144. #else
  50145. const unsigned char* privDer = client_key_der_2048;
  50146. size_t privDerSz = sizeof_client_key_der_2048;
  50147. unsigned char encMsg[2048/8];
  50148. const unsigned char expEncMsg[] = {
  50149. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50150. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50151. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50152. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50153. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50154. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50155. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50156. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50157. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50158. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50159. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50160. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50161. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50162. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50163. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50164. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50165. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50166. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50167. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50168. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50169. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50170. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50171. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50172. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50173. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50174. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50175. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50176. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50177. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50178. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50179. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50180. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50181. };
  50182. #ifdef WC_RSA_NO_PADDING
  50183. const unsigned char expEncMsgNoPad[] = {
  50184. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50185. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50186. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50187. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50188. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50189. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50190. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50191. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50192. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50193. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50194. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50195. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50196. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50197. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50198. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50199. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50200. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50201. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50202. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50203. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50204. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50205. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50206. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50207. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50208. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50209. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50210. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50211. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50212. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50213. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50214. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50215. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50216. };
  50217. #endif
  50218. #endif
  50219. const unsigned char* der;
  50220. XMEMSET(msg, 0x00, sizeof(msg));
  50221. der = privDer;
  50222. rsa = NULL;
  50223. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50224. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50225. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50226. -1);
  50227. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50228. RSA_PKCS1_PADDING), -1);
  50229. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50230. RSA_PKCS1_PADDING), -1);
  50231. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50232. RSA_PKCS1_PADDING), -1);
  50233. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50234. RSA_PKCS1_PSS_PADDING), -1);
  50235. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50236. RSA_PKCS1_PADDING), sizeof(encMsg));
  50237. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50238. #ifdef WC_RSA_NO_PADDING
  50239. /* Non-zero message. */
  50240. XMEMSET(msg, 0x01, sizeof(msg));
  50241. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50242. RSA_NO_PADDING), sizeof(encMsg));
  50243. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50244. #endif
  50245. RSA_free(rsa);
  50246. #endif
  50247. return EXPECT_RESULT();
  50248. }
  50249. static int test_wolfSSL_RSA_public_encrypt(void)
  50250. {
  50251. EXPECT_DECLS;
  50252. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50253. RSA* rsa = NULL;
  50254. const unsigned char msg[2048/8] = { 0 };
  50255. unsigned char encMsg[2048/8];
  50256. ExpectNotNull(rsa = RSA_new());
  50257. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  50258. RSA_PKCS1_PADDING), -1);
  50259. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50260. RSA_PKCS1_PADDING), -1);
  50261. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  50262. RSA_PKCS1_PADDING), -1);
  50263. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  50264. RSA_PKCS1_PADDING), -1);
  50265. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50266. RSA_PKCS1_PSS_PADDING), -1);
  50267. /* Empty RSA key. */
  50268. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50269. RSA_PKCS1_PADDING), -1);
  50270. RSA_free(rsa);
  50271. #endif
  50272. return EXPECT_RESULT();
  50273. }
  50274. static int test_wolfSSL_RSA_private_decrypt(void)
  50275. {
  50276. EXPECT_DECLS;
  50277. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50278. RSA* rsa = NULL;
  50279. unsigned char msg[2048/8];
  50280. const unsigned char encMsg[2048/8] = { 0 };
  50281. ExpectNotNull(rsa = RSA_new());
  50282. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  50283. RSA_PKCS1_PADDING), -1);
  50284. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  50285. RSA_PKCS1_PADDING), -1);
  50286. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50287. RSA_PKCS1_PADDING), -1);
  50288. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  50289. RSA_PKCS1_PADDING), -1);
  50290. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50291. RSA_PKCS1_PSS_PADDING), -1);
  50292. /* Empty RSA key. */
  50293. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50294. RSA_PKCS1_PADDING), -1);
  50295. RSA_free(rsa);
  50296. #endif
  50297. return EXPECT_RESULT();
  50298. }
  50299. static int test_wolfSSL_RSA_GenAdd(void)
  50300. {
  50301. EXPECT_DECLS;
  50302. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50303. RSA *rsa;
  50304. #ifdef USE_CERT_BUFFERS_1024
  50305. const unsigned char* privDer = client_key_der_1024;
  50306. size_t privDerSz = sizeof_client_key_der_1024;
  50307. const unsigned char* pubDer = client_keypub_der_1024;
  50308. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50309. #else
  50310. const unsigned char* privDer = client_key_der_2048;
  50311. size_t privDerSz = sizeof_client_key_der_2048;
  50312. const unsigned char* pubDer = client_keypub_der_2048;
  50313. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50314. #endif
  50315. const unsigned char* der;
  50316. der = privDer;
  50317. rsa = NULL;
  50318. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50319. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  50320. #ifndef RSA_LOW_MEM
  50321. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  50322. #else
  50323. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  50324. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50325. #endif
  50326. RSA_free(rsa);
  50327. der = pubDer;
  50328. rsa = NULL;
  50329. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50330. /* Need private values. */
  50331. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50332. RSA_free(rsa);
  50333. #endif
  50334. return EXPECT_RESULT();
  50335. }
  50336. static int test_wolfSSL_RSA_blinding_on(void)
  50337. {
  50338. EXPECT_DECLS;
  50339. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  50340. RSA *rsa;
  50341. WOLFSSL_BN_CTX *bnCtx = NULL;
  50342. #ifdef USE_CERT_BUFFERS_1024
  50343. const unsigned char* privDer = client_key_der_1024;
  50344. size_t privDerSz = sizeof_client_key_der_1024;
  50345. #else
  50346. const unsigned char* privDer = client_key_der_2048;
  50347. size_t privDerSz = sizeof_client_key_der_2048;
  50348. #endif
  50349. const unsigned char* der;
  50350. der = privDer;
  50351. rsa = NULL;
  50352. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50353. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  50354. /* Does nothing so all parameters are valid. */
  50355. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  50356. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  50357. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  50358. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  50359. wolfSSL_BN_CTX_free(bnCtx);
  50360. RSA_free(rsa);
  50361. #endif
  50362. return EXPECT_RESULT();
  50363. }
  50364. static int test_wolfSSL_RSA_ex_data(void)
  50365. {
  50366. EXPECT_DECLS;
  50367. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50368. RSA* rsa = NULL;
  50369. unsigned char data[1];
  50370. ExpectNotNull(rsa = RSA_new());
  50371. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  50372. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50373. #ifdef MAX_EX_DATA
  50374. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  50375. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  50376. #endif
  50377. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  50378. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  50379. #ifdef HAVE_EX_DATA
  50380. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  50381. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  50382. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  50383. #else
  50384. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  50385. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  50386. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50387. #endif
  50388. RSA_free(rsa);
  50389. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50390. return EXPECT_RESULT();
  50391. }
  50392. static int test_wolfSSL_RSA_LoadDer(void)
  50393. {
  50394. EXPECT_DECLS;
  50395. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  50396. defined(OPENSSL_EXTRA_X509_SMALL))
  50397. RSA *rsa = NULL;
  50398. #ifdef USE_CERT_BUFFERS_1024
  50399. const unsigned char* privDer = client_key_der_1024;
  50400. size_t privDerSz = sizeof_client_key_der_1024;
  50401. #else
  50402. const unsigned char* privDer = client_key_der_2048;
  50403. size_t privDerSz = sizeof_client_key_der_2048;
  50404. #endif
  50405. ExpectNotNull(rsa = RSA_new());
  50406. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  50407. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  50408. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  50409. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  50410. RSA_free(rsa);
  50411. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50412. return EXPECT_RESULT();
  50413. }
  50414. /* Local API. */
  50415. static int test_wolfSSL_RSA_To_Der(void)
  50416. {
  50417. EXPECT_DECLS;
  50418. #ifdef WOLFSSL_TEST_STATIC_BUILD
  50419. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  50420. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50421. RSA* rsa;
  50422. #ifdef USE_CERT_BUFFERS_1024
  50423. const unsigned char* privDer = client_key_der_1024;
  50424. size_t privDerSz = sizeof_client_key_der_1024;
  50425. const unsigned char* pubDer = client_keypub_der_1024;
  50426. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50427. unsigned char out[sizeof(client_key_der_1024)];
  50428. #else
  50429. const unsigned char* privDer = client_key_der_2048;
  50430. size_t privDerSz = sizeof_client_key_der_2048;
  50431. const unsigned char* pubDer = client_keypub_der_2048;
  50432. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50433. unsigned char out[sizeof(client_key_der_2048)];
  50434. #endif
  50435. const unsigned char* der;
  50436. unsigned char* outDer = NULL;
  50437. der = privDer;
  50438. rsa = NULL;
  50439. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50440. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50441. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  50442. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  50443. outDer = out;
  50444. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50445. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  50446. outDer = NULL;
  50447. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50448. ExpectNotNull(outDer);
  50449. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  50450. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  50451. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  50452. outDer = out;
  50453. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  50454. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  50455. RSA_free(rsa);
  50456. ExpectNotNull(rsa = RSA_new());
  50457. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50458. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  50459. RSA_free(rsa);
  50460. der = pubDer;
  50461. rsa = NULL;
  50462. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50463. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50464. RSA_free(rsa);
  50465. #endif
  50466. #endif
  50467. return EXPECT_RESULT();
  50468. }
  50469. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50470. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  50471. {
  50472. EXPECT_DECLS;
  50473. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  50474. XFILE file = XBADFILE;
  50475. const char* fname = "./certs/server-keyPub.pem";
  50476. RSA *rsa = NULL;
  50477. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  50478. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  50479. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  50480. ExpectIntEQ(RSA_size(rsa), 256);
  50481. RSA_free(rsa);
  50482. if (file != XBADFILE)
  50483. XFCLOSE(file);
  50484. #endif
  50485. return EXPECT_RESULT();
  50486. }
  50487. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50488. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  50489. {
  50490. EXPECT_DECLS;
  50491. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50492. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  50493. RSA* rsa = NULL;
  50494. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  50495. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  50496. /* Valid but stub so returns 0. */
  50497. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  50498. #endif
  50499. return EXPECT_RESULT();
  50500. }
  50501. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  50502. {
  50503. EXPECT_DECLS;
  50504. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50505. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50506. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  50507. RSA* rsa = NULL;
  50508. #ifdef USE_CERT_BUFFERS_1024
  50509. const unsigned char* privDer = client_key_der_1024;
  50510. size_t privDerSz = sizeof_client_key_der_1024;
  50511. #else
  50512. const unsigned char* privDer = client_key_der_2048;
  50513. size_t privDerSz = sizeof_client_key_der_2048;
  50514. #endif
  50515. const unsigned char* der;
  50516. #ifndef NO_AES
  50517. unsigned char passwd[] = "password";
  50518. #endif
  50519. ExpectNotNull(rsa = RSA_new());
  50520. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50521. NULL, NULL), 0);
  50522. RSA_free(rsa);
  50523. der = privDer;
  50524. rsa = NULL;
  50525. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50526. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  50527. NULL, NULL), 0);
  50528. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  50529. NULL, NULL), 0);
  50530. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50531. NULL, NULL), 1);
  50532. #ifndef NO_AES
  50533. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50534. NULL, 0, NULL, NULL), 1);
  50535. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50536. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  50537. #endif
  50538. RSA_free(rsa);
  50539. #endif
  50540. return EXPECT_RESULT();
  50541. }
  50542. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  50543. {
  50544. EXPECT_DECLS;
  50545. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50546. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50547. defined(WOLFSSL_DER_TO_PEM))
  50548. RSA* rsa = NULL;
  50549. #ifdef USE_CERT_BUFFERS_1024
  50550. const unsigned char* privDer = client_key_der_1024;
  50551. size_t privDerSz = sizeof_client_key_der_1024;
  50552. #else
  50553. const unsigned char* privDer = client_key_der_2048;
  50554. size_t privDerSz = sizeof_client_key_der_2048;
  50555. #endif
  50556. const unsigned char* der;
  50557. #ifndef NO_AES
  50558. unsigned char passwd[] = "password";
  50559. #endif
  50560. unsigned char* pem = NULL;
  50561. int plen;
  50562. ExpectNotNull(rsa = RSA_new());
  50563. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50564. &plen), 0);
  50565. RSA_free(rsa);
  50566. der = privDer;
  50567. rsa = NULL;
  50568. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50569. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  50570. &plen), 0);
  50571. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  50572. &plen), 0);
  50573. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50574. NULL), 0);
  50575. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50576. &plen), 1);
  50577. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50578. pem = NULL;
  50579. #ifndef NO_AES
  50580. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50581. NULL, 0, &pem, &plen), 1);
  50582. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50583. pem = NULL;
  50584. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50585. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  50586. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50587. #endif
  50588. RSA_free(rsa);
  50589. #endif
  50590. return EXPECT_RESULT();
  50591. }
  50592. static int test_wolfSSL_DH(void)
  50593. {
  50594. EXPECT_DECLS;
  50595. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50596. DH *dh = NULL;
  50597. BIGNUM* p;
  50598. BIGNUM* q;
  50599. BIGNUM* g;
  50600. BIGNUM* pub = NULL;
  50601. BIGNUM* priv = NULL;
  50602. #if defined(OPENSSL_ALL)
  50603. #if !defined(HAVE_FIPS) || \
  50604. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  50605. FILE* f = NULL;
  50606. unsigned char buf[268];
  50607. const unsigned char* pt = buf;
  50608. long len = 0;
  50609. dh = NULL;
  50610. XMEMSET(buf, 0, sizeof(buf));
  50611. /* Test 2048 bit parameters */
  50612. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  50613. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50614. if (f != XBADFILE)
  50615. XFCLOSE(f);
  50616. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  50617. ExpectNotNull(dh->p);
  50618. ExpectNotNull(dh->g);
  50619. ExpectTrue(pt == buf);
  50620. ExpectIntEQ(DH_generate_key(dh), 1);
  50621. ExpectIntEQ(DH_generate_key(dh), 1);
  50622. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  50623. ExpectNotNull(pub = BN_new());
  50624. ExpectIntEQ(BN_set_word(pub, 1), 1);
  50625. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  50626. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  50627. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  50628. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  50629. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  50630. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  50631. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  50632. BN_free(pub);
  50633. pub = NULL;
  50634. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50635. (const BIGNUM**)&q,
  50636. (const BIGNUM**)&g);
  50637. ExpectPtrEq(p, dh->p);
  50638. ExpectPtrEq(q, dh->q);
  50639. ExpectPtrEq(g, dh->g);
  50640. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50641. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50642. ExpectPtrEq(pub, dh->pub_key);
  50643. ExpectPtrEq(priv, dh->priv_key);
  50644. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  50645. ExpectPtrEq(pub, dh->pub_key);
  50646. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  50647. ExpectPtrEq(priv, dh->priv_key);
  50648. pub = NULL;
  50649. priv = NULL;
  50650. ExpectNotNull(pub = BN_new());
  50651. ExpectNotNull(priv = BN_new());
  50652. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  50653. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  50654. if (EXPECT_FAIL()) {
  50655. BN_free(pub);
  50656. BN_free(priv);
  50657. }
  50658. pub = NULL;
  50659. priv = NULL;
  50660. ExpectNotNull(pub = BN_new());
  50661. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  50662. if (EXPECT_FAIL()) {
  50663. BN_free(pub);
  50664. }
  50665. ExpectNotNull(priv = BN_new());
  50666. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  50667. if (EXPECT_FAIL()) {
  50668. BN_free(priv);
  50669. }
  50670. ExpectPtrEq(pub, dh->pub_key);
  50671. ExpectPtrEq(priv, dh->priv_key);
  50672. pub = NULL;
  50673. priv = NULL;
  50674. DH_free(dh);
  50675. dh = NULL;
  50676. ExpectNotNull(dh = DH_new());
  50677. p = NULL;
  50678. ExpectNotNull(p = BN_new());
  50679. ExpectIntEQ(BN_set_word(p, 1), 1);
  50680. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  50681. ExpectNotNull(pub = BN_new());
  50682. ExpectNotNull(priv = BN_new());
  50683. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  50684. if (EXPECT_FAIL()) {
  50685. BN_free(pub);
  50686. BN_free(priv);
  50687. }
  50688. pub = NULL;
  50689. priv = NULL;
  50690. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  50691. BN_free(p);
  50692. p = NULL;
  50693. DH_free(dh);
  50694. dh = NULL;
  50695. #ifdef WOLFSSL_KEY_GEN
  50696. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  50697. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  50698. DH_free(dh);
  50699. #endif
  50700. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  50701. #endif /* OPENSSL_ALL */
  50702. (void)dh;
  50703. (void)p;
  50704. (void)q;
  50705. (void)g;
  50706. (void)pub;
  50707. (void)priv;
  50708. ExpectNotNull(dh = wolfSSL_DH_new());
  50709. /* invalid parameters test */
  50710. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  50711. (const BIGNUM**)&q,
  50712. (const BIGNUM**)&g);
  50713. DH_get0_pqg(dh, NULL,
  50714. (const BIGNUM**)&q,
  50715. (const BIGNUM**)&g);
  50716. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  50717. DH_get0_pqg(dh, NULL, NULL, NULL);
  50718. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50719. (const BIGNUM**)&q,
  50720. (const BIGNUM**)&g);
  50721. ExpectPtrEq(p, NULL);
  50722. ExpectPtrEq(q, NULL);
  50723. ExpectPtrEq(g, NULL);
  50724. DH_free(dh);
  50725. dh = NULL;
  50726. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  50727. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  50728. #if defined(OPENSSL_ALL) || \
  50729. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50730. dh = wolfSSL_DH_new();
  50731. ExpectNotNull(dh);
  50732. p = wolfSSL_BN_new();
  50733. ExpectNotNull(p);
  50734. ExpectIntEQ(BN_set_word(p, 11), 1);
  50735. g = wolfSSL_BN_new();
  50736. ExpectNotNull(g);
  50737. ExpectIntEQ(BN_set_word(g, 2), 1);
  50738. q = wolfSSL_BN_new();
  50739. ExpectNotNull(q);
  50740. ExpectIntEQ(BN_set_word(q, 5), 1);
  50741. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  50742. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  50743. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  50744. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  50745. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  50746. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  50747. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  50748. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  50749. /* Don't need q. */
  50750. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50751. if (EXPECT_FAIL()) {
  50752. BN_free(p);
  50753. BN_free(g);
  50754. }
  50755. p = NULL;
  50756. g = NULL;
  50757. /* Setting again will free the p and g. */
  50758. wolfSSL_BN_free(q);
  50759. q = NULL;
  50760. DH_free(dh);
  50761. dh = NULL;
  50762. dh = wolfSSL_DH_new();
  50763. ExpectNotNull(dh);
  50764. p = wolfSSL_BN_new();
  50765. ExpectNotNull(p);
  50766. ExpectIntEQ(BN_set_word(p, 11), 1);
  50767. g = wolfSSL_BN_new();
  50768. ExpectNotNull(g);
  50769. ExpectIntEQ(BN_set_word(g, 2), 1);
  50770. q = wolfSSL_BN_new();
  50771. ExpectNotNull(q);
  50772. ExpectIntEQ(BN_set_word(q, 5), 1);
  50773. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  50774. /* p, q and g are now owned by dh - don't free. */
  50775. if (EXPECT_FAIL()) {
  50776. BN_free(p);
  50777. BN_free(q);
  50778. BN_free(g);
  50779. }
  50780. p = NULL;
  50781. q = NULL;
  50782. g = NULL;
  50783. p = wolfSSL_BN_new();
  50784. ExpectNotNull(p);
  50785. ExpectIntEQ(BN_set_word(p, 11), 1);
  50786. g = wolfSSL_BN_new();
  50787. ExpectNotNull(g);
  50788. ExpectIntEQ(BN_set_word(g, 2), 1);
  50789. q = wolfSSL_BN_new();
  50790. ExpectNotNull(q);
  50791. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  50792. if (EXPECT_FAIL()) {
  50793. BN_free(p);
  50794. }
  50795. p = NULL;
  50796. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  50797. if (EXPECT_FAIL()) {
  50798. BN_free(q);
  50799. }
  50800. q = NULL;
  50801. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  50802. if (EXPECT_FAIL()) {
  50803. BN_free(g);
  50804. }
  50805. g = NULL;
  50806. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  50807. /* p, q and g are now owned by dh - don't free. */
  50808. DH_free(dh);
  50809. dh = NULL;
  50810. ExpectIntEQ(DH_generate_key(NULL), 0);
  50811. ExpectNotNull(dh = DH_new());
  50812. ExpectIntEQ(DH_generate_key(dh), 0);
  50813. p = wolfSSL_BN_new();
  50814. ExpectNotNull(p);
  50815. ExpectIntEQ(BN_set_word(p, 0), 1);
  50816. g = wolfSSL_BN_new();
  50817. ExpectNotNull(g);
  50818. ExpectIntEQ(BN_set_word(g, 2), 1);
  50819. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50820. if (EXPECT_FAIL()) {
  50821. BN_free(p);
  50822. BN_free(g);
  50823. }
  50824. p = NULL;
  50825. g = NULL;
  50826. ExpectIntEQ(DH_generate_key(dh), 0);
  50827. DH_free(dh);
  50828. dh = NULL;
  50829. #endif
  50830. #endif
  50831. /* Test DH_up_ref() */
  50832. dh = wolfSSL_DH_new();
  50833. ExpectNotNull(dh);
  50834. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  50835. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  50836. DH_free(dh); /* decrease ref count */
  50837. DH_free(dh); /* free WOLFSSL_DH */
  50838. q = NULL;
  50839. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  50840. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  50841. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  50842. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  50843. #ifdef HAVE_FFDHE_2048
  50844. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50845. DH_free(dh);
  50846. q = NULL;
  50847. #endif
  50848. #ifdef HAVE_FFDHE_3072
  50849. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  50850. DH_free(dh);
  50851. q = NULL;
  50852. #endif
  50853. #ifdef HAVE_FFDHE_4096
  50854. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  50855. DH_free(dh);
  50856. q = NULL;
  50857. #endif
  50858. #else
  50859. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50860. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  50861. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  50862. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  50863. #endif /* OPENSSL_EXTRA && !NO_DH */
  50864. return EXPECT_RESULT();
  50865. }
  50866. static int test_wolfSSL_DH_dup(void)
  50867. {
  50868. EXPECT_DECLS;
  50869. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  50870. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  50871. defined(OPENSSL_EXTRA)
  50872. DH *dh = NULL;
  50873. DH *dhDup = NULL;
  50874. ExpectNotNull(dh = wolfSSL_DH_new());
  50875. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  50876. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  50877. #if defined(OPENSSL_ALL) || \
  50878. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50879. {
  50880. WOLFSSL_BIGNUM* p = NULL;
  50881. WOLFSSL_BIGNUM* g = NULL;
  50882. ExpectNotNull(p = wolfSSL_BN_new());
  50883. ExpectNotNull(g = wolfSSL_BN_new());
  50884. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  50885. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  50886. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50887. if (EXPECT_FAIL()) {
  50888. wolfSSL_BN_free(p);
  50889. wolfSSL_BN_free(g);
  50890. }
  50891. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  50892. wolfSSL_DH_free(dhDup);
  50893. }
  50894. #endif
  50895. wolfSSL_DH_free(dh);
  50896. #endif
  50897. #endif
  50898. return EXPECT_RESULT();
  50899. }
  50900. static int test_wolfSSL_DH_check(void)
  50901. {
  50902. EXPECT_DECLS;
  50903. #ifdef OPENSSL_ALL
  50904. #ifndef NO_DH
  50905. #ifndef NO_BIO
  50906. #ifndef NO_DSA
  50907. byte buf[6000];
  50908. char file[] = "./certs/dsaparams.pem";
  50909. XFILE f = XBADFILE;
  50910. int bytes;
  50911. BIO* bio = NULL;
  50912. DSA* dsa = NULL;
  50913. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50914. static const byte dh2048[] = {
  50915. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50916. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50917. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50918. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50919. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50920. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50921. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50922. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50923. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50924. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50925. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50926. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50927. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50928. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50929. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50930. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50931. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50932. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50933. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50934. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50935. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50936. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50937. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50938. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50939. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50940. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50941. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50942. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50943. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50944. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50945. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50946. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50947. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50948. 0x93, 0x02, 0x01, 0x02
  50949. };
  50950. const byte* params;
  50951. #endif
  50952. DH* dh = NULL;
  50953. WOLFSSL_BIGNUM* p = NULL;
  50954. WOLFSSL_BIGNUM* g = NULL;
  50955. WOLFSSL_BIGNUM* pTmp = NULL;
  50956. WOLFSSL_BIGNUM* gTmp = NULL;
  50957. int codes = -1;
  50958. #ifndef NO_DSA
  50959. /* Initialize DH */
  50960. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  50961. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  50962. if (f != XBADFILE)
  50963. XFCLOSE(f);
  50964. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  50965. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  50966. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  50967. ExpectNotNull(dh);
  50968. BIO_free(bio);
  50969. DSA_free(dsa);
  50970. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50971. params = dh2048;
  50972. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  50973. (long)sizeof(dh2048)));
  50974. #else
  50975. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  50976. #endif
  50977. /* Test assumed to be valid dh.
  50978. * Should return WOLFSSL_SUCCESS
  50979. * codes should be 0
  50980. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  50981. */
  50982. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50983. ExpectIntEQ(codes, 0);
  50984. /* Test NULL dh: expected BAD_FUNC_ARG */
  50985. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  50986. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  50987. if (dh != NULL) {
  50988. pTmp = dh->p;
  50989. dh->p = NULL;
  50990. }
  50991. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50992. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50993. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50994. /* set dh->p back to normal so it won't fail on next tests */
  50995. if (dh != NULL) {
  50996. dh->p = pTmp;
  50997. pTmp = NULL;
  50998. }
  50999. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  51000. if (dh != NULL) {
  51001. gTmp = dh->g;
  51002. dh->g = NULL;
  51003. }
  51004. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51005. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51006. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  51007. if (dh != NULL) {
  51008. dh->g = gTmp;
  51009. gTmp = NULL;
  51010. }
  51011. /* Cleanup */
  51012. DH_free(dh);
  51013. dh = NULL;
  51014. dh = DH_new();
  51015. ExpectNotNull(dh);
  51016. /* Check empty DH. */
  51017. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51018. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51019. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  51020. /* Check non-prime valued p. */
  51021. ExpectNotNull(p = BN_new());
  51022. ExpectIntEQ(BN_set_word(p, 4), 1);
  51023. ExpectNotNull(g = BN_new());
  51024. ExpectIntEQ(BN_set_word(g, 2), 1);
  51025. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  51026. if (EXPECT_FAIL()) {
  51027. wolfSSL_BN_free(p);
  51028. wolfSSL_BN_free(g);
  51029. }
  51030. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51031. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51032. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51033. DH_free(dh);
  51034. #endif
  51035. #endif /* !NO_DH && !NO_DSA */
  51036. #endif
  51037. return EXPECT_RESULT();
  51038. }
  51039. static int test_wolfSSL_DH_prime(void)
  51040. {
  51041. EXPECT_DECLS;
  51042. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51043. WOLFSSL_BIGNUM* bn = NULL;
  51044. #if WOLFSSL_MAX_BN_BITS >= 768
  51045. WOLFSSL_BIGNUM* bn2 = NULL;
  51046. #endif
  51047. bn = wolfSSL_DH_768_prime(NULL);
  51048. #if WOLFSSL_MAX_BN_BITS >= 768
  51049. ExpectNotNull(bn);
  51050. bn2 = wolfSSL_DH_768_prime(bn);
  51051. ExpectNotNull(bn2);
  51052. ExpectTrue(bn == bn2);
  51053. wolfSSL_BN_free(bn);
  51054. bn = NULL;
  51055. #else
  51056. ExpectNull(bn);
  51057. #endif
  51058. bn = wolfSSL_DH_1024_prime(NULL);
  51059. #if WOLFSSL_MAX_BN_BITS >= 1024
  51060. ExpectNotNull(bn);
  51061. wolfSSL_BN_free(bn);
  51062. bn = NULL;
  51063. #else
  51064. ExpectNull(bn);
  51065. #endif
  51066. bn = wolfSSL_DH_2048_prime(NULL);
  51067. #if WOLFSSL_MAX_BN_BITS >= 2048
  51068. ExpectNotNull(bn);
  51069. wolfSSL_BN_free(bn);
  51070. bn = NULL;
  51071. #else
  51072. ExpectNull(bn);
  51073. #endif
  51074. bn = wolfSSL_DH_3072_prime(NULL);
  51075. #if WOLFSSL_MAX_BN_BITS >= 3072
  51076. ExpectNotNull(bn);
  51077. wolfSSL_BN_free(bn);
  51078. bn = NULL;
  51079. #else
  51080. ExpectNull(bn);
  51081. #endif
  51082. bn = wolfSSL_DH_4096_prime(NULL);
  51083. #if WOLFSSL_MAX_BN_BITS >= 4096
  51084. ExpectNotNull(bn);
  51085. wolfSSL_BN_free(bn);
  51086. bn = NULL;
  51087. #else
  51088. ExpectNull(bn);
  51089. #endif
  51090. bn = wolfSSL_DH_6144_prime(NULL);
  51091. #if WOLFSSL_MAX_BN_BITS >= 6144
  51092. ExpectNotNull(bn);
  51093. wolfSSL_BN_free(bn);
  51094. bn = NULL;
  51095. #else
  51096. ExpectNull(bn);
  51097. #endif
  51098. bn = wolfSSL_DH_8192_prime(NULL);
  51099. #if WOLFSSL_MAX_BN_BITS >= 8192
  51100. ExpectNotNull(bn);
  51101. wolfSSL_BN_free(bn);
  51102. bn = NULL;
  51103. #else
  51104. ExpectNull(bn);
  51105. #endif
  51106. #endif
  51107. return EXPECT_RESULT();
  51108. }
  51109. static int test_wolfSSL_DH_1536_prime(void)
  51110. {
  51111. EXPECT_DECLS;
  51112. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51113. BIGNUM* bn = NULL;
  51114. unsigned char bits[200];
  51115. int sz = 192; /* known binary size */
  51116. const byte expected[] = {
  51117. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51118. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  51119. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  51120. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  51121. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  51122. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  51123. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  51124. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  51125. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  51126. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  51127. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  51128. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  51129. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  51130. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  51131. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  51132. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  51133. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  51134. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  51135. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  51136. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  51137. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  51138. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  51139. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  51140. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51141. };
  51142. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  51143. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  51144. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  51145. BN_free(bn);
  51146. #endif
  51147. return EXPECT_RESULT();
  51148. }
  51149. static int test_wolfSSL_DH_get_2048_256(void)
  51150. {
  51151. EXPECT_DECLS;
  51152. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51153. WOLFSSL_DH* dh = NULL;
  51154. const WOLFSSL_BIGNUM* pBn;
  51155. const WOLFSSL_BIGNUM* gBn;
  51156. const WOLFSSL_BIGNUM* qBn;
  51157. const byte pExpected[] = {
  51158. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  51159. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  51160. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  51161. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  51162. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  51163. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  51164. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  51165. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  51166. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  51167. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  51168. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  51169. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  51170. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  51171. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  51172. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  51173. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  51174. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  51175. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  51176. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  51177. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  51178. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  51179. 0x1E, 0x1A, 0x15, 0x97
  51180. };
  51181. const byte gExpected[] = {
  51182. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  51183. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  51184. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  51185. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  51186. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  51187. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  51188. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  51189. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  51190. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  51191. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  51192. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  51193. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  51194. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  51195. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  51196. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  51197. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  51198. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  51199. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  51200. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  51201. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  51202. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  51203. 0x6C, 0xC4, 0x16, 0x59
  51204. };
  51205. const byte qExpected[] = {
  51206. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  51207. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  51208. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  51209. };
  51210. int pSz;
  51211. int qSz;
  51212. int gSz;
  51213. byte* pReturned = NULL;
  51214. byte* qReturned = NULL;
  51215. byte* gReturned = NULL;
  51216. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  51217. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  51218. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  51219. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  51220. DYNAMIC_TYPE_TMP_BUFFER));
  51221. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  51222. ExpectIntEQ(pSz, sizeof(pExpected));
  51223. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  51224. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  51225. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  51226. DYNAMIC_TYPE_TMP_BUFFER));
  51227. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  51228. ExpectIntEQ(qSz, sizeof(qExpected));
  51229. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  51230. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  51231. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  51232. DYNAMIC_TYPE_TMP_BUFFER));
  51233. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  51234. ExpectIntEQ(gSz, sizeof(gExpected));
  51235. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  51236. wolfSSL_DH_free(dh);
  51237. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51238. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51239. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51240. #endif
  51241. return EXPECT_RESULT();
  51242. }
  51243. static int test_wolfSSL_PEM_write_DHparams(void)
  51244. {
  51245. EXPECT_DECLS;
  51246. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  51247. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  51248. DH* dh = NULL;
  51249. BIO* bio = NULL;
  51250. XFILE fp = XBADFILE;
  51251. byte pem[2048];
  51252. int pemSz = 0;
  51253. const char expected[] =
  51254. "-----BEGIN DH PARAMETERS-----\n"
  51255. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  51256. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  51257. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  51258. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  51259. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  51260. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  51261. "-----END DH PARAMETERS-----\n";
  51262. const char badPem[] =
  51263. "-----BEGIN DH PARAMETERS-----\n"
  51264. "-----END DH PARAMETERS-----\n";
  51265. const char emptySeqPem[] =
  51266. "-----BEGIN DH PARAMETERS-----\n"
  51267. "MAA=\n"
  51268. "-----END DH PARAMETERS-----\n";
  51269. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  51270. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51271. if (fp != XBADFILE) {
  51272. XFCLOSE(fp);
  51273. fp = XBADFILE;
  51274. }
  51275. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  51276. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51277. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51278. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51279. (int)sizeof(badPem));
  51280. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51281. BIO_free(bio);
  51282. bio = NULL;
  51283. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51284. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51285. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51286. (int)sizeof(emptySeqPem));
  51287. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51288. BIO_free(bio);
  51289. bio = NULL;
  51290. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51291. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51292. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51293. BIO_free(bio);
  51294. bio = NULL;
  51295. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51296. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51297. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51298. DH_free(dh);
  51299. dh = NULL;
  51300. dh = wolfSSL_DH_new();
  51301. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51302. if (fp != XBADFILE) {
  51303. XFCLOSE(fp);
  51304. fp = XBADFILE;
  51305. }
  51306. wolfSSL_DH_free(dh);
  51307. dh = NULL;
  51308. /* check results */
  51309. XMEMSET(pem, 0, sizeof(pem));
  51310. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  51311. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51312. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51313. if (fp != XBADFILE)
  51314. XFCLOSE(fp);
  51315. #endif
  51316. return EXPECT_RESULT();
  51317. }
  51318. static int test_wolfSSL_d2i_DHparams(void)
  51319. {
  51320. EXPECT_DECLS;
  51321. #ifdef OPENSSL_ALL
  51322. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51323. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51324. XFILE f = XBADFILE;
  51325. unsigned char buf[4096];
  51326. const unsigned char* pt = buf;
  51327. #ifdef HAVE_FFDHE_2048
  51328. const char* params1 = "./certs/dh2048.der";
  51329. #endif
  51330. #ifdef HAVE_FFDHE_3072
  51331. const char* params2 = "./certs/dh3072.der";
  51332. #endif
  51333. long len = 0;
  51334. WOLFSSL_DH* dh = NULL;
  51335. XMEMSET(buf, 0, sizeof(buf));
  51336. /* Test 2048 bit parameters */
  51337. #ifdef HAVE_FFDHE_2048
  51338. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51339. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51340. if (f != XBADFILE) {
  51341. XFCLOSE(f);
  51342. f = XBADFILE;
  51343. }
  51344. /* Valid case */
  51345. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51346. ExpectNotNull(dh->p);
  51347. ExpectNotNull(dh->g);
  51348. ExpectTrue(pt == buf);
  51349. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  51350. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  51351. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  51352. /* Invalid cases */
  51353. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51354. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51355. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  51356. DH_free(dh);
  51357. dh = NULL;
  51358. *buf = 0;
  51359. pt = buf;
  51360. #endif /* HAVE_FFDHE_2048 */
  51361. /* Test 3072 bit parameters */
  51362. #ifdef HAVE_FFDHE_3072
  51363. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51364. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51365. if (f != XBADFILE) {
  51366. XFCLOSE(f);
  51367. f = XBADFILE;
  51368. }
  51369. /* Valid case */
  51370. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  51371. ExpectNotNull(dh->p);
  51372. ExpectNotNull(dh->g);
  51373. ExpectTrue(pt != buf);
  51374. ExpectIntEQ(DH_generate_key(dh), 1);
  51375. /* Invalid cases */
  51376. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51377. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51378. DH_free(dh);
  51379. dh = NULL;
  51380. #endif /* HAVE_FFDHE_3072 */
  51381. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51382. #endif /* !NO_DH */
  51383. #endif
  51384. return EXPECT_RESULT();
  51385. }
  51386. static int test_wolfSSL_DH_LoadDer(void)
  51387. {
  51388. EXPECT_DECLS;
  51389. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  51390. defined(OPENSSL_EXTRA)
  51391. static const byte dh2048[] = {
  51392. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51393. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51394. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51395. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51396. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51397. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51398. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51399. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51400. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51401. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51402. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51403. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51404. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51405. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51406. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51407. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51408. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51409. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51410. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51411. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51412. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51413. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51414. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51415. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51416. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51417. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51418. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51419. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51420. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51421. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51422. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51423. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51424. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51425. 0x93, 0x02, 0x01, 0x02
  51426. };
  51427. WOLFSSL_DH* dh = NULL;
  51428. ExpectNotNull(dh = wolfSSL_DH_new());
  51429. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  51430. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  51431. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  51432. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  51433. wolfSSL_DH_free(dh);
  51434. #endif
  51435. return EXPECT_RESULT();
  51436. }
  51437. static int test_wolfSSL_i2d_DHparams(void)
  51438. {
  51439. EXPECT_DECLS;
  51440. #ifdef OPENSSL_ALL
  51441. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51442. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51443. XFILE f = XBADFILE;
  51444. unsigned char buf[4096];
  51445. const unsigned char* pt;
  51446. unsigned char* pt2;
  51447. #ifdef HAVE_FFDHE_2048
  51448. const char* params1 = "./certs/dh2048.der";
  51449. #endif
  51450. #ifdef HAVE_FFDHE_3072
  51451. const char* params2 = "./certs/dh3072.der";
  51452. #endif
  51453. long len;
  51454. WOLFSSL_DH* dh = NULL;
  51455. /* Test 2048 bit parameters */
  51456. #ifdef HAVE_FFDHE_2048
  51457. pt = buf;
  51458. pt2 = buf;
  51459. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51460. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51461. if (f != XBADFILE) {
  51462. XFCLOSE(f);
  51463. f = XBADFILE;
  51464. }
  51465. /* Valid case */
  51466. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51467. ExpectTrue(pt == buf);
  51468. ExpectIntEQ(DH_generate_key(dh), 1);
  51469. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  51470. /* Invalid case */
  51471. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51472. /* Return length only */
  51473. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  51474. DH_free(dh);
  51475. *buf = 0;
  51476. #endif
  51477. /* Test 3072 bit parameters */
  51478. #ifdef HAVE_FFDHE_3072
  51479. pt = buf;
  51480. pt2 = buf;
  51481. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51482. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51483. if (f != XBADFILE) {
  51484. XFCLOSE(f);
  51485. f = XBADFILE;
  51486. }
  51487. /* Valid case */
  51488. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51489. ExpectTrue(pt == buf);
  51490. ExpectIntEQ(DH_generate_key(dh), 1);
  51491. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  51492. /* Invalid case */
  51493. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51494. /* Return length only */
  51495. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  51496. DH_free(dh);
  51497. #endif
  51498. dh = DH_new();
  51499. ExpectNotNull(dh);
  51500. pt2 = buf;
  51501. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  51502. DH_free(dh);
  51503. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51504. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  51505. #endif
  51506. return EXPECT_RESULT();
  51507. }
  51508. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  51509. /*----------------------------------------------------------------------------*
  51510. | EC
  51511. *----------------------------------------------------------------------------*/
  51512. static int test_wolfSSL_EC_GROUP(void)
  51513. {
  51514. EXPECT_DECLS;
  51515. #ifdef OPENSSL_EXTRA
  51516. EC_GROUP *group = NULL;
  51517. EC_GROUP *group2 = NULL;
  51518. EC_GROUP *group3 = NULL;
  51519. #ifndef HAVE_ECC_BRAINPOOL
  51520. EC_GROUP *group4 = NULL;
  51521. #endif
  51522. WOLFSSL_BIGNUM* order = NULL;
  51523. int group_bits;
  51524. int i;
  51525. static const int knownEccNids[] = {
  51526. NID_X9_62_prime192v1,
  51527. NID_X9_62_prime192v2,
  51528. NID_X9_62_prime192v3,
  51529. NID_X9_62_prime239v1,
  51530. NID_X9_62_prime239v2,
  51531. NID_X9_62_prime239v3,
  51532. NID_X9_62_prime256v1,
  51533. NID_secp112r1,
  51534. NID_secp112r2,
  51535. NID_secp128r1,
  51536. NID_secp128r2,
  51537. NID_secp160r1,
  51538. NID_secp160r2,
  51539. NID_secp224r1,
  51540. NID_secp384r1,
  51541. NID_secp521r1,
  51542. NID_secp160k1,
  51543. NID_secp192k1,
  51544. NID_secp224k1,
  51545. NID_secp256k1,
  51546. NID_brainpoolP160r1,
  51547. NID_brainpoolP192r1,
  51548. NID_brainpoolP224r1,
  51549. NID_brainpoolP256r1,
  51550. NID_brainpoolP320r1,
  51551. NID_brainpoolP384r1,
  51552. NID_brainpoolP512r1,
  51553. };
  51554. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  51555. static const int knownEccEnums[] = {
  51556. ECC_SECP192R1,
  51557. ECC_PRIME192V2,
  51558. ECC_PRIME192V3,
  51559. ECC_PRIME239V1,
  51560. ECC_PRIME239V2,
  51561. ECC_PRIME239V3,
  51562. ECC_SECP256R1,
  51563. ECC_SECP112R1,
  51564. ECC_SECP112R2,
  51565. ECC_SECP128R1,
  51566. ECC_SECP128R2,
  51567. ECC_SECP160R1,
  51568. ECC_SECP160R2,
  51569. ECC_SECP224R1,
  51570. ECC_SECP384R1,
  51571. ECC_SECP521R1,
  51572. ECC_SECP160K1,
  51573. ECC_SECP192K1,
  51574. ECC_SECP224K1,
  51575. ECC_SECP256K1,
  51576. ECC_BRAINPOOLP160R1,
  51577. ECC_BRAINPOOLP192R1,
  51578. ECC_BRAINPOOLP224R1,
  51579. ECC_BRAINPOOLP256R1,
  51580. ECC_BRAINPOOLP320R1,
  51581. ECC_BRAINPOOLP384R1,
  51582. ECC_BRAINPOOLP512R1,
  51583. };
  51584. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  51585. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51586. ExpectNotNull(group2 = EC_GROUP_dup(group));
  51587. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  51588. #ifndef HAVE_ECC_BRAINPOOL
  51589. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  51590. NID_brainpoolP256r1));
  51591. #endif
  51592. ExpectNull(EC_GROUP_dup(NULL));
  51593. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  51594. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51595. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  51596. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  51597. #ifndef HAVE_ECC_BRAINPOOL
  51598. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  51599. #endif
  51600. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  51601. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  51602. ExpectNotNull(order = BN_new());
  51603. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  51604. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  51605. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  51606. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  51607. wolfSSL_BN_free(order);
  51608. ExpectNotNull(EC_GROUP_method_of(group));
  51609. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  51610. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  51611. NID_X9_62_prime_field);
  51612. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  51613. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  51614. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  51615. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  51616. #ifndef NO_WOLFSSL_STUB
  51617. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  51618. #endif
  51619. #ifndef HAVE_ECC_BRAINPOOL
  51620. EC_GROUP_free(group4);
  51621. #endif
  51622. EC_GROUP_free(group3);
  51623. EC_GROUP_free(group2);
  51624. EC_GROUP_free(group);
  51625. for (i = 0; i < knowEccNidsLen; i++) {
  51626. group = NULL;
  51627. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  51628. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  51629. EC_GROUP_free(group);
  51630. }
  51631. for (i = 0; i < knowEccEnumsLen; i++) {
  51632. group = NULL;
  51633. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  51634. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  51635. EC_GROUP_free(group);
  51636. }
  51637. #endif
  51638. return EXPECT_RESULT();
  51639. }
  51640. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  51641. {
  51642. EXPECT_DECLS;
  51643. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51644. EC_GROUP *group = NULL;
  51645. BIO* bio = NULL;
  51646. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51647. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51648. EC_GROUP *ret = NULL;
  51649. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  51650. "BgUrgQQAIg==\n"
  51651. "-----END EC PARAMETERS-----";
  51652. #endif
  51653. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  51654. "MAA=\n"
  51655. "-----END EC PARAMETERS-----";
  51656. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  51657. "BgA=\n"
  51658. "-----END EC PARAMETERS-----";
  51659. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  51660. "BgE=\n"
  51661. "-----END EC PARAMETERS-----";
  51662. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  51663. "BgE*\n"
  51664. "-----END EC PARAMETERS-----";
  51665. /* Test that first parameter, bio, being NULL fails. */
  51666. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  51667. /* Test that reading named parameters works. */
  51668. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51669. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  51670. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51671. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51672. BIO_free(bio);
  51673. bio = NULL;
  51674. EC_GROUP_free(group);
  51675. group = NULL;
  51676. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51677. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51678. /* Test that reusing group works. */
  51679. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51680. sizeof(ec_nc_p384)));
  51681. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51682. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51683. BIO_free(bio);
  51684. bio = NULL;
  51685. EC_GROUP_free(group);
  51686. group = NULL;
  51687. /* Test that returning through group works. */
  51688. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51689. sizeof(ec_nc_p384)));
  51690. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51691. ExpectIntEQ(group == ret, 1);
  51692. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51693. BIO_free(bio);
  51694. bio = NULL;
  51695. EC_GROUP_free(group);
  51696. group = NULL;
  51697. #endif
  51698. /* Test 0x30, 0x00 (not and object id) fails. */
  51699. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  51700. sizeof(ec_nc_bad_1)));
  51701. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51702. BIO_free(bio);
  51703. bio = NULL;
  51704. /* Test 0x06, 0x00 (empty object id) fails. */
  51705. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  51706. sizeof(ec_nc_bad_2)));
  51707. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51708. BIO_free(bio);
  51709. bio = NULL;
  51710. /* Test 0x06, 0x01 (badly formed object id) fails. */
  51711. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  51712. sizeof(ec_nc_bad_3)));
  51713. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51714. BIO_free(bio);
  51715. bio = NULL;
  51716. /* Test invalid PEM encoding - invalid character. */
  51717. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  51718. sizeof(ec_nc_bad_4)));
  51719. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51720. BIO_free(bio);
  51721. #endif
  51722. return EXPECT_RESULT();
  51723. }
  51724. static int test_wolfSSL_EC_POINT(void)
  51725. {
  51726. EXPECT_DECLS;
  51727. #if !defined(WOLFSSL_SP_MATH) && \
  51728. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  51729. #ifdef OPENSSL_EXTRA
  51730. BN_CTX* ctx = NULL;
  51731. EC_GROUP* group = NULL;
  51732. #ifndef HAVE_ECC_BRAINPOOL
  51733. EC_GROUP* group2 = NULL;
  51734. #endif
  51735. EC_POINT* Gxy = NULL;
  51736. EC_POINT* new_point = NULL;
  51737. EC_POINT* set_point = NULL;
  51738. EC_POINT* infinity = NULL;
  51739. BIGNUM* k = NULL;
  51740. BIGNUM* Gx = NULL;
  51741. BIGNUM* Gy = NULL;
  51742. BIGNUM* Gz = NULL;
  51743. BIGNUM* X = NULL;
  51744. BIGNUM* Y = NULL;
  51745. BIGNUM* set_point_bn = NULL;
  51746. char* hexStr = NULL;
  51747. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  51748. "17AF381913FF7A96314EA47055EA0FD0";
  51749. /* NISTP256R1 Gx/Gy */
  51750. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  51751. "77037D812DEB33A0F4A13945D898C296";
  51752. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51753. "2BCE33576B315ECECBB6406837BF51F5";
  51754. #ifndef HAVE_SELFTEST
  51755. EC_POINT *tmp = NULL;
  51756. size_t bin_len;
  51757. unsigned int blen = 0;
  51758. unsigned char* buf = NULL;
  51759. unsigned char bufInf[1] = { 0x00 };
  51760. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  51761. "77037D812DEB33A0F4A13945D898C296"
  51762. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51763. "2BCE33576B315ECECBB6406837BF51F5";
  51764. const unsigned char binUncompG[] = {
  51765. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51766. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51767. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51768. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51769. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51770. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51771. };
  51772. const unsigned char binUncompGBad[] = {
  51773. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51774. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51775. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51776. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51777. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51778. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51779. };
  51780. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  51781. "77037D812DEB33A0F4A13945D898C296";
  51782. #ifdef HAVE_COMP_KEY
  51783. const unsigned char binCompG[] = {
  51784. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51785. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51786. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51787. };
  51788. #endif
  51789. #endif
  51790. ExpectNotNull(ctx = BN_CTX_new());
  51791. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51792. #ifndef HAVE_ECC_BRAINPOOL
  51793. /* Used to make groups curve_idx == -1. */
  51794. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  51795. #endif
  51796. ExpectNull(EC_POINT_new(NULL));
  51797. ExpectNotNull(Gxy = EC_POINT_new(group));
  51798. ExpectNotNull(new_point = EC_POINT_new(group));
  51799. ExpectNotNull(set_point = EC_POINT_new(group));
  51800. ExpectNotNull(X = BN_new());
  51801. ExpectNotNull(Y = BN_new());
  51802. ExpectNotNull(set_point_bn = BN_new());
  51803. ExpectNotNull(infinity = EC_POINT_new(group));
  51804. /* load test values */
  51805. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  51806. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  51807. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  51808. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  51809. /* populate coordinates for input point */
  51810. if (Gxy != NULL) {
  51811. Gxy->X = Gx;
  51812. Gxy->Y = Gy;
  51813. Gxy->Z = Gz;
  51814. }
  51815. /* Test handling of NULL point. */
  51816. EC_POINT_clear_free(NULL);
  51817. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51818. NULL, NULL, ctx), 0);
  51819. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51820. NULL, NULL, ctx), 0);
  51821. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51822. NULL, NULL, ctx), 0);
  51823. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51824. X, NULL, ctx), 0);
  51825. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51826. NULL, Y, ctx), 0);
  51827. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51828. X, Y, ctx), 0);
  51829. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51830. X, Y, ctx), 0);
  51831. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51832. NULL, Y, ctx), 0);
  51833. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51834. X, NULL, ctx), 0);
  51835. /* Getting point at infinity returns an error. */
  51836. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  51837. X, Y, ctx), 0);
  51838. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51839. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51840. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51841. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  51842. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  51843. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  51844. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  51845. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  51846. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  51847. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  51848. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  51849. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  51850. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  51851. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  51852. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  51853. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  51854. /* perform point multiplication */
  51855. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  51856. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51857. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51858. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51859. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  51860. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51861. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51862. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51863. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  51864. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51865. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51866. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51867. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  51868. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  51869. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  51870. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  51871. /* Set point to something. */
  51872. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  51873. #else
  51874. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  51875. ctx), 1);
  51876. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51877. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51878. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51879. #endif
  51880. /* check if point X coordinate is zero */
  51881. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51882. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  51883. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  51884. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  51885. #endif
  51886. /* extract the coordinates from point */
  51887. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51888. ctx), WOLFSSL_SUCCESS);
  51889. /* check if point X coordinate is zero */
  51890. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  51891. /* set the same X and Y points in another object */
  51892. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  51893. ctx), WOLFSSL_SUCCESS);
  51894. /* compare points as they should be the same */
  51895. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  51896. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  51897. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  51898. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  51899. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  51900. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  51901. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  51902. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  51903. /* Test copying */
  51904. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  51905. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  51906. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  51907. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  51908. /* Test inverting */
  51909. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  51910. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  51911. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  51912. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  51913. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51914. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51915. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51916. {
  51917. EC_POINT* orig_point = NULL;
  51918. ExpectNotNull(orig_point = EC_POINT_new(group));
  51919. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  51920. 1);
  51921. /* new_point should be set_point inverted so adding it will revert
  51922. * the point back to set_point */
  51923. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  51924. NULL), 1);
  51925. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  51926. EC_POINT_free(orig_point);
  51927. }
  51928. #endif
  51929. /* Test getting affine converts from projective. */
  51930. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  51931. /* Force non-affine coordinates */
  51932. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51933. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  51934. if (new_point != NULL) {
  51935. new_point->inSet = 0;
  51936. }
  51937. /* extract the coordinates from point */
  51938. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51939. ctx), WOLFSSL_SUCCESS);
  51940. /* check if point ordinates have changed. */
  51941. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  51942. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  51943. /* Test check for infinity */
  51944. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  51945. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  51946. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  51947. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  51948. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  51949. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  51950. #else
  51951. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  51952. #endif
  51953. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  51954. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  51955. /* check bn2hex */
  51956. hexStr = BN_bn2hex(k);
  51957. ExpectStrEQ(hexStr, kTest);
  51958. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51959. BN_print_fp(stderr, k);
  51960. fprintf(stderr, "\n");
  51961. #endif
  51962. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51963. hexStr = BN_bn2hex(Gx);
  51964. ExpectStrEQ(hexStr, kGx);
  51965. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51966. BN_print_fp(stderr, Gx);
  51967. fprintf(stderr, "\n");
  51968. #endif
  51969. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51970. hexStr = BN_bn2hex(Gy);
  51971. ExpectStrEQ(hexStr, kGy);
  51972. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51973. BN_print_fp(stderr, Gy);
  51974. fprintf(stderr, "\n");
  51975. #endif
  51976. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51977. #ifndef HAVE_SELFTEST
  51978. /* Test point to hex */
  51979. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51980. ctx));
  51981. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51982. ctx));
  51983. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51984. ctx));
  51985. #ifndef HAVE_ECC_BRAINPOOL
  51986. /* Group not supported in wolfCrypt. */
  51987. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51988. ctx));
  51989. #endif
  51990. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  51991. ExpectNotNull(hexStr);
  51992. ExpectStrEQ(hexStr, uncompG);
  51993. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51994. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  51995. ExpectNotNull(hexStr);
  51996. ExpectStrEQ(hexStr, compG);
  51997. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51998. /* Test point to oct */
  51999. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52000. NULL, 0, ctx), 0);
  52001. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52002. NULL, 0, ctx), 0);
  52003. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52004. NULL, 0, ctx), 0);
  52005. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52006. NULL, 0, ctx);
  52007. ExpectIntEQ(bin_len, sizeof(binUncompG));
  52008. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52009. DYNAMIC_TYPE_ECC));
  52010. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52011. buf, bin_len, ctx), bin_len);
  52012. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52013. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52014. /* Infinity (x=0, y=0) encodes as '0x00'. */
  52015. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52016. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  52017. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52018. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  52019. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52020. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  52021. ExpectIntEQ(bufInf[0], 0);
  52022. wolfSSL_EC_POINT_dump(NULL, NULL);
  52023. /* Test point i2d */
  52024. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  52025. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  52026. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  52027. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  52028. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  52029. ExpectIntEQ(blen, sizeof(binUncompG));
  52030. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  52031. blen -= 1;
  52032. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  52033. blen += 1;
  52034. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  52035. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52036. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52037. #ifdef HAVE_COMP_KEY
  52038. /* Test point to oct compressed */
  52039. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  52040. 0, ctx);
  52041. ExpectIntEQ(bin_len, sizeof(binCompG));
  52042. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52043. DYNAMIC_TYPE_ECC));
  52044. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  52045. bin_len, ctx), bin_len);
  52046. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  52047. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52048. #endif
  52049. /* Test point BN */
  52050. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  52051. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52052. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  52053. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52054. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  52055. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52056. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  52057. /* Test oct to point */
  52058. ExpectNotNull(tmp = EC_POINT_new(group));
  52059. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  52060. ctx), 0);
  52061. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  52062. ctx), 0);
  52063. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  52064. ctx), 0);
  52065. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  52066. sizeof(binUncompGBad), ctx), 0);
  52067. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  52068. ctx), 1);
  52069. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52070. EC_POINT_free(tmp);
  52071. tmp = NULL;
  52072. /* Test setting BN ordinates. */
  52073. ExpectNotNull(tmp = EC_POINT_new(group));
  52074. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52075. NULL, ctx), 0);
  52076. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  52077. NULL, ctx), 0);
  52078. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  52079. NULL, ctx), 0);
  52080. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  52081. NULL, ctx), 0);
  52082. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52083. Gy, ctx), 0);
  52084. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  52085. ctx), 0);
  52086. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  52087. ctx), 0);
  52088. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  52089. Gy, ctx), 0);
  52090. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  52091. NULL, ctx), 0);
  52092. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  52093. ctx), 1);
  52094. EC_POINT_free(tmp);
  52095. tmp = NULL;
  52096. /* Test point d2i */
  52097. ExpectNotNull(tmp = EC_POINT_new(group));
  52098. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  52099. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  52100. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  52101. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  52102. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  52103. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  52104. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  52105. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  52106. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  52107. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52108. EC_POINT_free(tmp);
  52109. tmp = NULL;
  52110. #ifdef HAVE_COMP_KEY
  52111. /* Test oct compressed to point */
  52112. ExpectNotNull(tmp = EC_POINT_new(group));
  52113. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  52114. 1);
  52115. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52116. EC_POINT_free(tmp);
  52117. tmp = NULL;
  52118. /* Test point d2i - compressed */
  52119. ExpectNotNull(tmp = EC_POINT_new(group));
  52120. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  52121. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52122. EC_POINT_free(tmp);
  52123. tmp = NULL;
  52124. #endif
  52125. #endif
  52126. /* test BN_mod_add */
  52127. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52128. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  52129. 1);
  52130. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52131. /* cleanup */
  52132. BN_free(X);
  52133. BN_free(Y);
  52134. BN_free(k);
  52135. BN_free(set_point_bn);
  52136. EC_POINT_free(infinity);
  52137. EC_POINT_free(new_point);
  52138. EC_POINT_free(set_point);
  52139. EC_POINT_clear_free(Gxy);
  52140. #ifndef HAVE_ECC_BRAINPOOL
  52141. EC_GROUP_free(group2);
  52142. #endif
  52143. EC_GROUP_free(group);
  52144. BN_CTX_free(ctx);
  52145. #endif
  52146. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  52147. return EXPECT_RESULT();
  52148. }
  52149. static int test_wolfSSL_SPAKE(void)
  52150. {
  52151. EXPECT_DECLS;
  52152. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  52153. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  52154. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52155. BIGNUM* x = NULL; /* kdc priv */
  52156. BIGNUM* y = NULL; /* client priv */
  52157. BIGNUM* w = NULL; /* shared value */
  52158. byte M_bytes[] = {
  52159. /* uncompressed */
  52160. 0x04,
  52161. /* x */
  52162. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  52163. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  52164. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  52165. /* y */
  52166. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  52167. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  52168. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  52169. };
  52170. EC_POINT* M = NULL; /* shared value */
  52171. byte N_bytes[] = {
  52172. /* uncompressed */
  52173. 0x04,
  52174. /* x */
  52175. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  52176. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  52177. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  52178. /* y */
  52179. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  52180. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  52181. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  52182. };
  52183. EC_POINT* N = NULL; /* shared value */
  52184. EC_POINT* T = NULL; /* kdc pub */
  52185. EC_POINT* tmp1 = NULL; /* kdc pub */
  52186. EC_POINT* tmp2 = NULL; /* kdc pub */
  52187. EC_POINT* S = NULL; /* client pub */
  52188. EC_POINT* client_secret = NULL;
  52189. EC_POINT* kdc_secret = NULL;
  52190. EC_GROUP* group = NULL;
  52191. BN_CTX* bn_ctx = NULL;
  52192. /* Values taken from a test run of Kerberos 5 */
  52193. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52194. ExpectNotNull(bn_ctx = BN_CTX_new());
  52195. ExpectNotNull(M = EC_POINT_new(group));
  52196. ExpectNotNull(N = EC_POINT_new(group));
  52197. ExpectNotNull(T = EC_POINT_new(group));
  52198. ExpectNotNull(tmp1 = EC_POINT_new(group));
  52199. ExpectNotNull(tmp2 = EC_POINT_new(group));
  52200. ExpectNotNull(S = EC_POINT_new(group));
  52201. ExpectNotNull(client_secret = EC_POINT_new(group));
  52202. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  52203. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  52204. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  52205. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  52206. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  52207. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  52208. "506AB395478F0AAB647752CF117B36250"), 1);
  52209. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  52210. 1);
  52211. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  52212. 1);
  52213. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  52214. /* kdc */
  52215. /* T=x*P+w*M */
  52216. /* All in one function call */
  52217. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  52218. /* Spread into separate calls */
  52219. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  52220. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  52221. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52222. 1);
  52223. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  52224. /* client */
  52225. /* S=y*P+w*N */
  52226. /* All in one function call */
  52227. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  52228. /* Spread into separate calls */
  52229. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  52230. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  52231. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52232. 1);
  52233. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  52234. /* K=y*(T-w*M) */
  52235. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  52236. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  52237. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  52238. 1);
  52239. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  52240. bn_ctx), 1);
  52241. /* kdc */
  52242. /* K=x*(S-w*N) */
  52243. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  52244. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  52245. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  52246. 1);
  52247. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  52248. 1);
  52249. /* kdc_secret == client_secret */
  52250. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  52251. BN_free(x);
  52252. BN_free(y);
  52253. BN_free(w);
  52254. EC_POINT_free(M);
  52255. EC_POINT_free(N);
  52256. EC_POINT_free(T);
  52257. EC_POINT_free(tmp1);
  52258. EC_POINT_free(tmp2);
  52259. EC_POINT_free(S);
  52260. EC_POINT_free(client_secret);
  52261. EC_POINT_free(kdc_secret);
  52262. EC_GROUP_free(group);
  52263. BN_CTX_free(bn_ctx);
  52264. #endif
  52265. return EXPECT_RESULT();
  52266. }
  52267. static int test_wolfSSL_EC_KEY_generate(void)
  52268. {
  52269. EXPECT_DECLS;
  52270. #ifdef OPENSSL_EXTRA
  52271. WOLFSSL_EC_KEY* key = NULL;
  52272. #ifndef HAVE_ECC_BRAINPOOL
  52273. WOLFSSL_EC_GROUP* group = NULL;
  52274. #endif
  52275. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52276. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  52277. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52278. wolfSSL_EC_KEY_free(key);
  52279. key = NULL;
  52280. #ifndef HAVE_ECC_BRAINPOOL
  52281. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  52282. NID_brainpoolP256r1));
  52283. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  52284. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  52285. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  52286. wolfSSL_EC_KEY_free(key);
  52287. wolfSSL_EC_GROUP_free(group);
  52288. #endif
  52289. #endif
  52290. return EXPECT_RESULT();
  52291. }
  52292. static int test_EC_i2d(void)
  52293. {
  52294. EXPECT_DECLS;
  52295. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  52296. EC_KEY *key = NULL;
  52297. EC_KEY *copy = NULL;
  52298. int len = 0;
  52299. unsigned char *buf = NULL;
  52300. unsigned char *p = NULL;
  52301. const unsigned char *tmp = NULL;
  52302. const unsigned char octBad[] = {
  52303. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52304. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52305. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52306. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52307. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52308. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52309. };
  52310. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52311. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  52312. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  52313. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52314. DYNAMIC_TYPE_TMP_BUFFER));
  52315. p = buf;
  52316. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  52317. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  52318. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  52319. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  52320. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52321. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  52322. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  52323. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  52324. tmp = buf;
  52325. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  52326. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  52327. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52328. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  52329. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  52330. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  52331. tmp = buf;
  52332. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  52333. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  52334. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  52335. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  52336. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  52337. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  52338. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  52339. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  52340. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  52341. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  52342. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  52343. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  52344. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  52345. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52346. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  52347. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52348. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  52349. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52350. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  52351. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52352. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  52353. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  52354. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52355. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52356. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52357. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52358. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52359. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52360. buf = NULL;
  52361. buf = NULL;
  52362. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  52363. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52364. DYNAMIC_TYPE_TMP_BUFFER));
  52365. p = buf;
  52366. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52367. p = NULL;
  52368. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52369. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52370. p = NULL;
  52371. /* Bad point is also an invalid private key. */
  52372. tmp = octBad;
  52373. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  52374. tmp = buf;
  52375. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  52376. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52377. buf = NULL;
  52378. buf = NULL;
  52379. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  52380. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52381. DYNAMIC_TYPE_TMP_BUFFER));
  52382. p = buf;
  52383. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52384. p = NULL;
  52385. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52386. tmp = buf;
  52387. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  52388. tmp = octBad;
  52389. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  52390. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  52391. ExpectIntEQ(EC_KEY_check_key(key), 1);
  52392. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  52393. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  52394. EC_KEY_free(key);
  52395. EC_KEY_free(copy);
  52396. #endif
  52397. return EXPECT_RESULT();
  52398. }
  52399. static int test_wolfSSL_EC_curve(void)
  52400. {
  52401. EXPECT_DECLS;
  52402. #if defined(OPENSSL_EXTRA)
  52403. int nid = NID_secp160k1;
  52404. const char* nid_name = NULL;
  52405. ExpectNull(EC_curve_nid2nist(NID_sha256));
  52406. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  52407. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  52408. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  52409. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  52410. #endif
  52411. return EXPECT_RESULT();
  52412. }
  52413. static int test_wolfSSL_EC_KEY_dup(void)
  52414. {
  52415. EXPECT_DECLS;
  52416. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  52417. WOLFSSL_EC_KEY* ecKey = NULL;
  52418. WOLFSSL_EC_KEY* dupKey = NULL;
  52419. ecc_key* srcKey = NULL;
  52420. ecc_key* destKey = NULL;
  52421. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52422. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52423. /* Valid cases */
  52424. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52425. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  52426. /* Compare pubkey */
  52427. if (ecKey != NULL) {
  52428. srcKey = (ecc_key*)ecKey->internal;
  52429. }
  52430. if (dupKey != NULL) {
  52431. destKey = (ecc_key*)dupKey->internal;
  52432. }
  52433. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  52434. /* compare EC_GROUP */
  52435. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  52436. /* compare EC_POINT */
  52437. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  52438. dupKey->pub_key, NULL), MP_EQ);
  52439. /* compare BIGNUM */
  52440. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  52441. wolfSSL_EC_KEY_free(dupKey);
  52442. dupKey = NULL;
  52443. /* Invalid cases */
  52444. /* NULL key */
  52445. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52446. /* NULL ecc_key */
  52447. if (ecKey != NULL) {
  52448. wc_ecc_free((ecc_key*)ecKey->internal);
  52449. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52450. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52451. }
  52452. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52453. wolfSSL_EC_KEY_free(ecKey);
  52454. ecKey = NULL;
  52455. wolfSSL_EC_KEY_free(dupKey);
  52456. dupKey = NULL;
  52457. /* NULL Group */
  52458. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52459. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52460. if (ecKey != NULL) {
  52461. wolfSSL_EC_GROUP_free(ecKey->group);
  52462. ecKey->group = NULL; /* Set group to NULL */
  52463. }
  52464. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52465. wolfSSL_EC_KEY_free(ecKey);
  52466. ecKey = NULL;
  52467. wolfSSL_EC_KEY_free(dupKey);
  52468. dupKey = NULL;
  52469. /* NULL public key */
  52470. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52471. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52472. if (ecKey != NULL) {
  52473. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52474. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52475. }
  52476. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52477. if (ecKey != NULL) {
  52478. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52479. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52480. }
  52481. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52482. wolfSSL_EC_KEY_free(ecKey);
  52483. ecKey = NULL;
  52484. wolfSSL_EC_KEY_free(dupKey);
  52485. dupKey = NULL;
  52486. /* NULL private key */
  52487. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52488. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52489. if (ecKey != NULL) {
  52490. wolfSSL_BN_free(ecKey->priv_key);
  52491. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52492. }
  52493. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52494. wolfSSL_EC_KEY_free(ecKey);
  52495. ecKey = NULL;
  52496. wolfSSL_EC_KEY_free(dupKey);
  52497. dupKey = NULL;
  52498. /* Test EC_KEY_up_ref */
  52499. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52500. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52501. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52502. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52503. /* reference count doesn't follow duplicate */
  52504. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52505. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52506. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52507. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52508. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52509. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52510. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  52511. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  52512. #endif
  52513. return EXPECT_RESULT();
  52514. }
  52515. static int test_wolfSSL_EC_KEY_set_group(void)
  52516. {
  52517. EXPECT_DECLS;
  52518. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52519. defined(OPENSSL_EXTRA)
  52520. EC_KEY *key = NULL;
  52521. EC_GROUP *group = NULL;
  52522. const EC_GROUP *group2 = NULL;
  52523. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52524. ExpectNotNull(key = EC_KEY_new());
  52525. ExpectNull(EC_KEY_get0_group(NULL));
  52526. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  52527. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  52528. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  52529. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  52530. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  52531. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  52532. EC_GROUP_free(group);
  52533. EC_KEY_free(key);
  52534. #endif
  52535. return EXPECT_RESULT();
  52536. }
  52537. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  52538. {
  52539. EXPECT_DECLS;
  52540. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52541. BIO* bio = NULL;
  52542. EC_KEY* key = NULL;
  52543. /* Error condition: NULL key. */
  52544. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  52545. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  52546. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  52547. /* Conversion form defaults to uncompressed. */
  52548. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52549. #ifdef HAVE_COMP_KEY
  52550. /* Explicitly set to compressed. */
  52551. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52552. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  52553. #else
  52554. /* Will still work just won't change anything. */
  52555. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52556. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52557. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  52558. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52559. #endif
  52560. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  52561. BIO_free(bio);
  52562. EC_KEY_free(key);
  52563. #endif
  52564. return EXPECT_RESULT();
  52565. }
  52566. static int test_wolfSSL_EC_KEY_private_key(void)
  52567. {
  52568. EXPECT_DECLS;
  52569. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52570. WOLFSSL_EC_KEY* key = NULL;
  52571. WOLFSSL_BIGNUM* priv = NULL;
  52572. WOLFSSL_BIGNUM* priv2 = NULL;
  52573. WOLFSSL_BIGNUM* bn;
  52574. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52575. ExpectNotNull(priv = wolfSSL_BN_new());
  52576. ExpectNotNull(priv2 = wolfSSL_BN_new());
  52577. ExpectIntNE(BN_set_word(priv, 2), 0);
  52578. ExpectIntNE(BN_set_word(priv2, 2), 0);
  52579. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  52580. /* No private key set. */
  52581. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  52582. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  52583. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  52584. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  52585. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  52586. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52587. ExpectPtrNE(bn, priv);
  52588. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  52589. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52590. ExpectPtrNE(bn, priv2);
  52591. wolfSSL_BN_free(priv2);
  52592. wolfSSL_BN_free(priv);
  52593. wolfSSL_EC_KEY_free(key);
  52594. #endif
  52595. return EXPECT_RESULT();
  52596. }
  52597. static int test_wolfSSL_EC_KEY_public_key(void)
  52598. {
  52599. EXPECT_DECLS;
  52600. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52601. WOLFSSL_EC_KEY* key = NULL;
  52602. WOLFSSL_EC_POINT* pub = NULL;
  52603. WOLFSSL_EC_POINT* point = NULL;
  52604. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52605. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  52606. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  52607. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52608. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  52609. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  52610. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  52611. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  52612. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  52613. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  52614. ExpectPtrEq(point, pub);
  52615. wolfSSL_EC_KEY_free(key);
  52616. #endif
  52617. return EXPECT_RESULT();
  52618. }
  52619. static int test_wolfSSL_EC_KEY_print_fp(void)
  52620. {
  52621. EXPECT_DECLS;
  52622. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  52623. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  52624. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  52625. !defined(NO_STDIO_FILESYSTEM)
  52626. EC_KEY* key = NULL;
  52627. /* Bad file pointer. */
  52628. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  52629. /* NULL key. */
  52630. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  52631. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  52632. /* Negative indent. */
  52633. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  52634. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52635. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52636. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52637. wolfSSL_EC_KEY_free(key);
  52638. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  52639. NID_X9_62_prime256v1)));
  52640. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52641. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52642. wolfSSL_EC_KEY_free(key);
  52643. #endif
  52644. return EXPECT_RESULT();
  52645. }
  52646. static int test_wolfSSL_EC_get_builtin_curves(void)
  52647. {
  52648. EXPECT_DECLS;
  52649. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  52650. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52651. EC_builtin_curve* curves = NULL;
  52652. size_t crv_len = 0;
  52653. size_t i = 0;
  52654. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  52655. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  52656. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  52657. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  52658. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  52659. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  52660. if (curves[i].comment != NULL) {
  52661. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  52662. }
  52663. }
  52664. if (crv_len > 1) {
  52665. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  52666. }
  52667. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52668. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52669. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  52670. return EXPECT_RESULT();
  52671. }
  52672. static int test_wolfSSL_ECDSA_SIG(void)
  52673. {
  52674. EXPECT_DECLS;
  52675. #ifdef OPENSSL_EXTRA
  52676. WOLFSSL_ECDSA_SIG* sig = NULL;
  52677. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  52678. WOLFSSL_BIGNUM* r = NULL;
  52679. WOLFSSL_BIGNUM* s = NULL;
  52680. const WOLFSSL_BIGNUM* r2 = NULL;
  52681. const WOLFSSL_BIGNUM* s2 = NULL;
  52682. const unsigned char* cp = NULL;
  52683. unsigned char* p = NULL;
  52684. unsigned char outSig[8];
  52685. unsigned char sigData[8] =
  52686. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52687. unsigned char sigDataBad[8] =
  52688. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52689. wolfSSL_ECDSA_SIG_free(NULL);
  52690. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  52691. ExpectNotNull(r = wolfSSL_BN_new());
  52692. ExpectNotNull(s = wolfSSL_BN_new());
  52693. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  52694. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  52695. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  52696. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  52697. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  52698. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52699. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  52700. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  52701. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  52702. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  52703. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  52704. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  52705. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  52706. r2 = NULL;
  52707. s2 = NULL;
  52708. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52709. ExpectNull(r2);
  52710. ExpectNull(s2);
  52711. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  52712. if (EXPECT_FAIL()) {
  52713. wolfSSL_BN_free(r);
  52714. wolfSSL_BN_free(s);
  52715. }
  52716. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  52717. ExpectPtrEq(r2, r);
  52718. ExpectPtrEq(s2, s);
  52719. r2 = NULL;
  52720. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  52721. ExpectPtrEq(r2, r);
  52722. s2 = NULL;
  52723. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  52724. ExpectPtrEq(s2, s);
  52725. /* r and s are freed when sig is freed. */
  52726. wolfSSL_ECDSA_SIG_free(sig);
  52727. sig = NULL;
  52728. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  52729. cp = sigDataBad;
  52730. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  52731. cp = sigData;
  52732. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  52733. ExpectIntEQ((cp == sigData + 8), 1);
  52734. cp = sigData;
  52735. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  52736. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  52737. ExpectIntEQ((sig == sig2), 1);
  52738. cp = outSig;
  52739. p = outSig;
  52740. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  52741. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  52742. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  52743. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  52744. ExpectIntEQ((p == outSig + 8), 1);
  52745. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  52746. wolfSSL_ECDSA_SIG_free(sig);
  52747. #endif
  52748. return EXPECT_RESULT();
  52749. }
  52750. static int test_ECDSA_size_sign(void)
  52751. {
  52752. EXPECT_DECLS;
  52753. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  52754. EC_KEY* key = NULL;
  52755. ECDSA_SIG* ecdsaSig = NULL;
  52756. int id;
  52757. byte hash[WC_MAX_DIGEST_SIZE];
  52758. byte hash2[WC_MAX_DIGEST_SIZE];
  52759. byte sig[ECC_MAX_SIG_SIZE];
  52760. unsigned int sigSz = sizeof(sig);
  52761. XMEMSET(hash, 123, sizeof(hash));
  52762. XMEMSET(hash2, 234, sizeof(hash2));
  52763. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  52764. ExpectIntEQ(id, ECC_SECP256R1);
  52765. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52766. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  52767. ExpectIntGE(ECDSA_size(NULL), 0);
  52768. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  52769. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  52770. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  52771. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  52772. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  52773. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  52774. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  52775. ExpectIntGE(ECDSA_size(key), sigSz);
  52776. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  52777. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  52778. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  52779. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  52780. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  52781. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  52782. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  52783. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  52784. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  52785. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  52786. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  52787. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  52788. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  52789. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  52790. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  52791. ECDSA_SIG_free(ecdsaSig);
  52792. EC_KEY_free(key);
  52793. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  52794. return EXPECT_RESULT();
  52795. }
  52796. static int test_ECDH_compute_key(void)
  52797. {
  52798. EXPECT_DECLS;
  52799. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52800. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52801. EC_KEY* key1 = NULL;
  52802. EC_KEY* key2 = NULL;
  52803. EC_POINT* pub1 = NULL;
  52804. EC_POINT* pub2 = NULL;
  52805. byte secret1[32];
  52806. byte secret2[32];
  52807. int i;
  52808. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52809. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  52810. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  52811. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52812. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  52813. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  52814. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  52815. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  52816. 0);
  52817. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  52818. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  52819. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  52820. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  52821. 0);
  52822. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  52823. 0);
  52824. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  52825. NULL), 0);
  52826. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  52827. sizeof(secret1));
  52828. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  52829. sizeof(secret2));
  52830. for (i = 0; i < (int)sizeof(secret1); i++) {
  52831. ExpectIntEQ(secret1[i], secret2[i]);
  52832. }
  52833. EC_KEY_free(key2);
  52834. EC_KEY_free(key1);
  52835. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  52836. * !WOLF_CRYPTO_CB_ONLY_ECC */
  52837. return EXPECT_RESULT();
  52838. }
  52839. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  52840. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  52841. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  52842. !defined(NO_ASN_TIME)
  52843. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  52844. int expectedDerSz)
  52845. {
  52846. EXPECT_DECLS;
  52847. X509* x509 = NULL;
  52848. BIGNUM* serial_number = NULL;
  52849. X509_NAME* name = NULL;
  52850. time_t epoch_off = 0;
  52851. ASN1_INTEGER* asn1_serial_number;
  52852. long not_before, not_after;
  52853. int derSz;
  52854. ExpectNotNull(x509 = X509_new());
  52855. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  52856. ExpectNotNull(serial_number = BN_new());
  52857. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  52858. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  52859. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  52860. /* version 3 */
  52861. ExpectIntNE(X509_set_version(x509, 2L), 0);
  52862. ExpectNotNull(name = X509_NAME_new());
  52863. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  52864. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  52865. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  52866. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  52867. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  52868. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  52869. not_before = (long)wc_Time(NULL);
  52870. not_after = not_before + (365 * 24 * 60 * 60);
  52871. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  52872. &epoch_off));
  52873. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  52874. &epoch_off));
  52875. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  52876. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  52877. ExpectIntGE(derSz, expectedDerSz);
  52878. BN_free(serial_number);
  52879. X509_NAME_free(name);
  52880. X509_free(x509);
  52881. return EXPECT_RESULT();
  52882. }
  52883. #endif
  52884. static int test_openssl_generate_key_and_cert(void)
  52885. {
  52886. EXPECT_DECLS;
  52887. #if defined(OPENSSL_EXTRA)
  52888. int expectedDerSz;
  52889. EVP_PKEY* pkey = NULL;
  52890. #ifdef HAVE_ECC
  52891. EC_KEY* ec_key = NULL;
  52892. #endif
  52893. #if !defined(NO_RSA)
  52894. int key_length = 2048;
  52895. BIGNUM* exponent = NULL;
  52896. RSA* rsa = NULL;
  52897. ExpectNotNull(pkey = EVP_PKEY_new());
  52898. ExpectNotNull(exponent = BN_new());
  52899. ExpectNotNull(rsa = RSA_new());
  52900. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  52901. #ifndef WOLFSSL_KEY_GEN
  52902. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52903. #if defined(USE_CERT_BUFFERS_1024)
  52904. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  52905. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52906. key_length = 1024;
  52907. #elif defined(USE_CERT_BUFFERS_2048)
  52908. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  52909. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52910. #else
  52911. RSA_free(rsa);
  52912. rsa = NULL;
  52913. #endif
  52914. #else
  52915. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  52916. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  52917. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  52918. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52919. #endif
  52920. if (rsa) {
  52921. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  52922. if (EXPECT_FAIL()) {
  52923. RSA_free(rsa);
  52924. }
  52925. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52926. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52927. expectedDerSz = 743;
  52928. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  52929. expectedDerSz), TEST_SUCCESS);
  52930. #endif
  52931. }
  52932. EVP_PKEY_free(pkey);
  52933. pkey = NULL;
  52934. BN_free(exponent);
  52935. #endif /* !NO_RSA */
  52936. #ifdef HAVE_ECC
  52937. ExpectNotNull(pkey = EVP_PKEY_new());
  52938. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52939. #ifndef NO_WOLFSSL_STUB
  52940. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  52941. #endif
  52942. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  52943. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  52944. if (EXPECT_FAIL()) {
  52945. EC_KEY_free(ec_key);
  52946. }
  52947. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52948. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52949. expectedDerSz = 344;
  52950. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  52951. TEST_SUCCESS);
  52952. #endif
  52953. EVP_PKEY_free(pkey);
  52954. #endif /* HAVE_ECC */
  52955. (void)pkey;
  52956. (void)expectedDerSz;
  52957. #endif /* OPENSSL_EXTRA */
  52958. return EXPECT_RESULT();
  52959. }
  52960. static int test_stubs_are_stubs(void)
  52961. {
  52962. EXPECT_DECLS;
  52963. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  52964. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  52965. WOLFSSL_CTX* ctx = NULL;
  52966. WOLFSSL_CTX* ctxN = NULL;
  52967. #ifndef NO_WOLFSSL_CLIENT
  52968. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52969. #elif !defined(NO_WOLFSSL_SERVER)
  52970. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52971. #endif
  52972. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  52973. ExpectIntEQ((int) x(z), 0)
  52974. /* test logic, all stubs return same result regardless of ctx being NULL
  52975. * as there are no sanity checks, it's just a stub! If at some
  52976. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  52977. * if invalid inputs are supplied. Test calling both
  52978. * with and without valid inputs, if a stub functionality remains unchanged.
  52979. */
  52980. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  52981. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  52982. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  52983. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  52984. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  52985. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  52986. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  52987. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  52988. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  52989. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  52990. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  52991. wolfSSL_CTX_free(ctx);
  52992. ctx = NULL;
  52993. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  52994. * !NO_WOLFSSL_SERVER) */
  52995. return EXPECT_RESULT();
  52996. }
  52997. static int test_CONF_modules_xxx(void)
  52998. {
  52999. int res = TEST_SKIPPED;
  53000. #if defined(OPENSSL_EXTRA)
  53001. CONF_modules_free();
  53002. CONF_modules_unload(0);
  53003. CONF_modules_unload(1);
  53004. CONF_modules_unload(-1);
  53005. res = TEST_SUCCESS;
  53006. #endif /* OPENSSL_EXTRA */
  53007. return res;
  53008. }
  53009. static int test_CRYPTO_set_dynlock_xxx(void)
  53010. {
  53011. int res = TEST_SKIPPED;
  53012. #if defined(OPENSSL_EXTRA)
  53013. CRYPTO_set_dynlock_create_callback(
  53014. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  53015. CRYPTO_set_dynlock_create_callback(
  53016. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  53017. CRYPTO_set_dynlock_destroy_callback(
  53018. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  53019. CRYPTO_set_dynlock_destroy_callback(
  53020. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  53021. CRYPTO_set_dynlock_lock_callback(
  53022. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  53023. CRYPTO_set_dynlock_lock_callback(
  53024. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  53025. res = TEST_SUCCESS;
  53026. #endif /* OPENSSL_EXTRA */
  53027. return res;
  53028. }
  53029. static int test_CRYPTO_THREADID_xxx(void)
  53030. {
  53031. EXPECT_DECLS;
  53032. #if defined(OPENSSL_EXTRA)
  53033. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  53034. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  53035. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  53036. #endif /* OPENSSL_EXTRA */
  53037. return EXPECT_RESULT();
  53038. }
  53039. static int test_ENGINE_cleanup(void)
  53040. {
  53041. int res = TEST_SKIPPED;
  53042. #if defined(OPENSSL_EXTRA)
  53043. ENGINE_cleanup();
  53044. res = TEST_SUCCESS;
  53045. #endif /* OPENSSL_EXTRA */
  53046. return res;
  53047. }
  53048. static int test_wolfSSL_CTX_LoadCRL(void)
  53049. {
  53050. EXPECT_DECLS;
  53051. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  53052. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  53053. WOLFSSL_CTX* ctx = NULL;
  53054. WOLFSSL* ssl = NULL;
  53055. const char* badPath = "dummypath";
  53056. const char* validPath = "./certs/crl";
  53057. const char* validFilePath = "./certs/crl/cliCrl.pem";
  53058. const char* issuerCert = "./certs/client-cert.pem";
  53059. int derType = WOLFSSL_FILETYPE_ASN1;
  53060. int pemType = WOLFSSL_FILETYPE_PEM;
  53061. #ifdef HAVE_CRL_MONITOR
  53062. int monitor = WOLFSSL_CRL_MONITOR;
  53063. #else
  53064. int monitor = 0;
  53065. #endif
  53066. WOLFSSL_CERT_MANAGER* cm = NULL;
  53067. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53068. BAD_FUNC_ARG)
  53069. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53070. NOT_COMPILED_IN)
  53071. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53072. WOLFSSL_SUCCESS)
  53073. #ifndef NO_WOLFSSL_CLIENT
  53074. #define NEW_CTX(ctx) ExpectNotNull( \
  53075. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  53076. #elif !defined(NO_WOLFSSL_SERVER)
  53077. #define NEW_CTX(ctx) ExpectNotNull( \
  53078. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  53079. #else
  53080. #define NEW_CTX(ctx) return
  53081. #endif
  53082. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53083. NEW_CTX(ctx);
  53084. #ifndef HAVE_CRL_MONITOR
  53085. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  53086. wolfSSL_CTX_free(ctx);
  53087. NEW_CTX(ctx);
  53088. #endif
  53089. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53090. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  53091. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  53092. wolfSSL_CTX_free(ctx);
  53093. ctx = NULL;
  53094. NEW_CTX(ctx);
  53095. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53096. WOLFSSL_SUCCESS);
  53097. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  53098. wolfSSL_CTX_free(ctx);
  53099. ctx = NULL;
  53100. NEW_CTX(ctx);
  53101. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53102. WOLFSSL_SUCCESS);
  53103. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53104. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  53105. wolfSSL_free(ssl);
  53106. ssl = NULL;
  53107. wolfSSL_CTX_free(ctx);
  53108. ctx = NULL;
  53109. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53110. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  53111. WOLFSSL_SUCCESS);
  53112. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  53113. WOLFSSL_SUCCESS);
  53114. wolfSSL_CertManagerFree(cm);
  53115. #endif
  53116. return EXPECT_RESULT();
  53117. }
  53118. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53119. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  53120. {
  53121. EXPECT_DECLS;
  53122. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53123. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  53124. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53125. return EXPECT_RESULT();
  53126. }
  53127. #endif
  53128. static int test_multiple_crls_same_issuer(void)
  53129. {
  53130. EXPECT_DECLS;
  53131. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53132. test_ssl_cbf client_cbs, server_cbs;
  53133. struct {
  53134. const char* server_cert;
  53135. const char* server_key;
  53136. } test_params[] = {
  53137. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  53138. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  53139. };
  53140. size_t i;
  53141. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  53142. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53143. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53144. server_cbs.certPemFile = test_params[i].server_cert;
  53145. server_cbs.keyPemFile = test_params[i].server_key;
  53146. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  53147. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  53148. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  53149. &server_cbs, NULL), TEST_FAIL);
  53150. }
  53151. #endif
  53152. return EXPECT_RESULT();
  53153. }
  53154. static int test_SetTmpEC_DHE_Sz(void)
  53155. {
  53156. EXPECT_DECLS;
  53157. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  53158. WOLFSSL_CTX *ctx = NULL;
  53159. WOLFSSL *ssl = NULL;
  53160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53161. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  53162. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53163. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  53164. wolfSSL_free(ssl);
  53165. wolfSSL_CTX_free(ctx);
  53166. #endif
  53167. return EXPECT_RESULT();
  53168. }
  53169. static int test_wolfSSL_CTX_get0_privatekey(void)
  53170. {
  53171. EXPECT_DECLS;
  53172. #ifdef OPENSSL_ALL
  53173. WOLFSSL_CTX* ctx = NULL;
  53174. (void)ctx;
  53175. #ifndef NO_RSA
  53176. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53177. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53178. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53179. WOLFSSL_FILETYPE_PEM));
  53180. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53181. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53182. WOLFSSL_FILETYPE_PEM));
  53183. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53184. wolfSSL_CTX_free(ctx);
  53185. ctx = NULL;
  53186. #endif
  53187. #ifdef HAVE_ECC
  53188. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53189. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53190. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  53191. WOLFSSL_FILETYPE_PEM));
  53192. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53193. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  53194. WOLFSSL_FILETYPE_PEM));
  53195. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53196. wolfSSL_CTX_free(ctx);
  53197. #endif
  53198. #endif
  53199. return EXPECT_RESULT();
  53200. }
  53201. static int test_wolfSSL_dtls_set_mtu(void)
  53202. {
  53203. EXPECT_DECLS;
  53204. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  53205. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  53206. !defined(WOLFSSL_NO_TLS12)
  53207. WOLFSSL_CTX* ctx = NULL;
  53208. WOLFSSL* ssl = NULL;
  53209. const char* testCertFile;
  53210. const char* testKeyFile;
  53211. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  53212. #ifndef NO_RSA
  53213. testCertFile = svrCertFile;
  53214. testKeyFile = svrKeyFile;
  53215. #elif defined(HAVE_ECC)
  53216. testCertFile = eccCertFile;
  53217. testKeyFile = eccKeyFile;
  53218. #endif
  53219. if (testCertFile != NULL && testKeyFile != NULL) {
  53220. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  53221. WOLFSSL_FILETYPE_PEM));
  53222. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  53223. WOLFSSL_FILETYPE_PEM));
  53224. }
  53225. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53226. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53227. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53228. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  53229. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  53230. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  53231. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  53232. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  53233. wolfSSL_free(ssl);
  53234. wolfSSL_CTX_free(ctx);
  53235. #endif
  53236. return EXPECT_RESULT();
  53237. }
  53238. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53239. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53240. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  53241. enum HandShakeType hsType, word16 length)
  53242. {
  53243. size_t idx = 0;
  53244. byte* l;
  53245. /* record layer */
  53246. /* handshake type */
  53247. out[idx++] = handshake;
  53248. /* protocol version */
  53249. out[idx++] = 0xfe;
  53250. out[idx++] = 0xfd; /* DTLS 1.2 */
  53251. /* epoch 0 */
  53252. XMEMSET(out + idx, 0, 2);
  53253. idx += 2;
  53254. /* sequence number */
  53255. XMEMSET(out + idx, 0, 6);
  53256. c32toa(seq, out + idx + 2);
  53257. idx += 6;
  53258. /* length in BE */
  53259. if (length)
  53260. c16toa(length, out + idx);
  53261. else
  53262. c16toa(outSz - idx - 2, out + idx);
  53263. idx += 2;
  53264. /* handshake layer */
  53265. /* handshake type */
  53266. out[idx++] = (byte)hsType;
  53267. /* length */
  53268. l = out + idx;
  53269. idx += 3;
  53270. /* message seq */
  53271. c16toa(0, out + idx);
  53272. idx += 2;
  53273. /* frag offset */
  53274. c32to24(0, out + idx);
  53275. idx += 3;
  53276. /* frag length */
  53277. c32to24((word32)outSz - (word32)idx - 3, l);
  53278. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  53279. idx += 3;
  53280. XMEMSET(out + idx, 0, outSz - idx);
  53281. }
  53282. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  53283. {
  53284. byte msg[] = "This is a msg for the client";
  53285. byte reply[40];
  53286. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53287. reply[sizeof(reply) - 1] = '\0';
  53288. fprintf(stderr, "Client message: %s\n", reply);
  53289. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53290. }
  53291. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  53292. {
  53293. byte ch[50];
  53294. int fd = wolfSSL_get_fd(ssl);
  53295. byte msg[] = "This is a msg for the server";
  53296. byte reply[40];
  53297. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  53298. /* Server should ignore this datagram */
  53299. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53300. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  53301. /* Server should ignore this datagram */
  53302. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53303. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53304. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53305. reply[sizeof(reply) - 1] = '\0';
  53306. fprintf(stderr, "Server response: %s\n", reply);
  53307. }
  53308. static int test_wolfSSL_dtls_plaintext(void)
  53309. {
  53310. callback_functions func_cb_client;
  53311. callback_functions func_cb_server;
  53312. size_t i;
  53313. struct test_params {
  53314. method_provider client_meth;
  53315. method_provider server_meth;
  53316. ssl_callback on_result_server;
  53317. ssl_callback on_result_client;
  53318. } params[] = {
  53319. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53320. test_wolfSSL_dtls_plaintext_server,
  53321. test_wolfSSL_dtls_plaintext_client},
  53322. };
  53323. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53324. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53325. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53326. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53327. func_cb_server.method = params[i].server_meth;
  53328. func_cb_client.method = params[i].client_meth;
  53329. func_cb_client.on_result = params[i].on_result_client;
  53330. func_cb_server.on_result = params[i].on_result_server;
  53331. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53332. if (!func_cb_client.return_code)
  53333. return TEST_FAIL;
  53334. if (!func_cb_server.return_code)
  53335. return TEST_FAIL;
  53336. }
  53337. return TEST_RES_CHECK(1);
  53338. }
  53339. #else
  53340. static int test_wolfSSL_dtls_plaintext(void) {
  53341. return TEST_SKIPPED;
  53342. }
  53343. #endif
  53344. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53345. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53346. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  53347. {
  53348. byte b[1100]; /* buffer for the messages to send */
  53349. size_t idx = 0;
  53350. size_t seq_offset = 0;
  53351. size_t msg_offset = 0;
  53352. int i;
  53353. int fd = wolfSSL_get_fd(ssl);
  53354. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53355. word32 seq_number = 100; /* start high so server definitely reads this */
  53356. word16 msg_number = 50; /* start high so server has to buffer this */
  53357. AssertIntEQ(ret, 1);
  53358. /* Now let's start spamming the peer with fragments it needs to store */
  53359. XMEMSET(b, -1, sizeof(b));
  53360. /* record layer */
  53361. /* handshake type */
  53362. b[idx++] = 22;
  53363. /* protocol version */
  53364. b[idx++] = 0xfe;
  53365. b[idx++] = 0xfd; /* DTLS 1.2 */
  53366. /* epoch 0 */
  53367. XMEMSET(b + idx, 0, 2);
  53368. idx += 2;
  53369. /* sequence number */
  53370. XMEMSET(b + idx, 0, 6);
  53371. seq_offset = idx + 2; /* increment only the low 32 bits */
  53372. idx += 6;
  53373. /* static length in BE */
  53374. c16toa(42, b + idx);
  53375. idx += 2;
  53376. /* handshake layer */
  53377. /* cert type */
  53378. b[idx++] = 11;
  53379. /* length */
  53380. c32to24(1000, b + idx);
  53381. idx += 3;
  53382. /* message seq */
  53383. c16toa(0, b + idx);
  53384. msg_offset = idx;
  53385. idx += 2;
  53386. /* frag offset */
  53387. c32to24(500, b + idx);
  53388. idx += 3;
  53389. /* frag length */
  53390. c32to24(30, b + idx);
  53391. idx += 3;
  53392. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  53393. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  53394. seq_number++, msg_number++, i++) {
  53395. struct timespec delay;
  53396. XMEMSET(&delay, 0, sizeof(delay));
  53397. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53398. c32toa(seq_number, b + seq_offset);
  53399. c16toa(msg_number, b + msg_offset);
  53400. ret = (int)send(fd, b, 55, 0);
  53401. nanosleep(&delay, NULL);
  53402. }
  53403. }
  53404. #ifdef WOLFSSL_DTLS13
  53405. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  53406. {
  53407. const word16 sendCountMax = 100;
  53408. byte b[150]; /* buffer for the messages to send */
  53409. size_t idx = 0;
  53410. size_t msg_offset = 0;
  53411. int fd = wolfSSL_get_fd(ssl);
  53412. word16 msg_number = 10; /* start high so server has to buffer this */
  53413. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53414. AssertIntEQ(ret, 1);
  53415. /* Now let's start spamming the peer with fragments it needs to store */
  53416. XMEMSET(b, -1, sizeof(b));
  53417. /* handshake type */
  53418. b[idx++] = 11;
  53419. /* length */
  53420. c32to24(10000, b + idx);
  53421. idx += 3;
  53422. /* message_seq */
  53423. msg_offset = idx;
  53424. idx += 2;
  53425. /* fragment_offset */
  53426. c32to24(5000, b + idx);
  53427. idx += 3;
  53428. /* fragment_length */
  53429. c32to24(100, b + idx);
  53430. idx += 3;
  53431. /* fragment contents */
  53432. idx += 100;
  53433. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  53434. byte sendBuf[150];
  53435. int sendSz = sizeof(sendBuf);
  53436. struct timespec delay;
  53437. XMEMSET(&delay, 0, sizeof(delay));
  53438. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53439. c16toa(msg_number, b + msg_offset);
  53440. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  53441. (int)idx, handshake, 0, 0, 0);
  53442. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  53443. nanosleep(&delay, NULL);
  53444. }
  53445. }
  53446. #endif
  53447. static int test_wolfSSL_dtls_fragments(void)
  53448. {
  53449. EXPECT_DECLS;
  53450. callback_functions func_cb_client;
  53451. callback_functions func_cb_server;
  53452. size_t i;
  53453. struct test_params {
  53454. method_provider client_meth;
  53455. method_provider server_meth;
  53456. ssl_callback spammer;
  53457. } params[] = {
  53458. #if !defined(WOLFSSL_NO_TLS12)
  53459. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53460. test_wolfSSL_dtls12_fragments_spammer},
  53461. #endif
  53462. #ifdef WOLFSSL_DTLS13
  53463. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53464. test_wolfSSL_dtls13_fragments_spammer},
  53465. #endif
  53466. };
  53467. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53468. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53469. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53470. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53471. func_cb_server.method = params[i].server_meth;
  53472. func_cb_client.method = params[i].client_meth;
  53473. func_cb_client.ssl_ready = params[i].spammer;
  53474. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53475. ExpectFalse(func_cb_client.return_code);
  53476. ExpectFalse(func_cb_server.return_code);
  53477. /* The socket should be closed by the server resulting in a
  53478. * socket error, fatal error or reading a close notify alert */
  53479. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  53480. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  53481. func_cb_client.last_err != FATAL_ERROR) {
  53482. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  53483. }
  53484. /* Check the server returned an error indicating the msg buffer
  53485. * was full */
  53486. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  53487. if (EXPECT_FAIL())
  53488. break;
  53489. }
  53490. return EXPECT_RESULT();
  53491. }
  53492. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  53493. {
  53494. int fd, ret;
  53495. byte alert_msg[] = {
  53496. 0x15, /* alert type */
  53497. 0xfe, 0xfd, /* version */
  53498. 0x00, 0x00, /* epoch */
  53499. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  53500. 0x00, 0x02, /* length */
  53501. 0x02, /* level: fatal */
  53502. 0x46 /* protocol version */
  53503. };
  53504. fd = wolfSSL_get_fd(ssl);
  53505. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  53506. AssertIntGT(ret, 0);
  53507. }
  53508. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  53509. {
  53510. callback_functions client_cbs, server_cbs;
  53511. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53512. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53513. client_cbs.doUdp = server_cbs.doUdp = 1;
  53514. if (version12) {
  53515. #if !defined(WOLFSSL_NO_TLS12)
  53516. client_cbs.method = wolfDTLSv1_2_client_method;
  53517. server_cbs.method = wolfDTLSv1_2_server_method;
  53518. #else
  53519. return TEST_SKIPPED;
  53520. #endif
  53521. }
  53522. else
  53523. {
  53524. #ifdef WOLFSSL_DTLS13
  53525. client_cbs.method = wolfDTLSv1_3_client_method;
  53526. server_cbs.method = wolfDTLSv1_3_server_method;
  53527. #else
  53528. return TEST_SKIPPED;
  53529. #endif /* WOLFSSL_DTLS13 */
  53530. }
  53531. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53532. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53533. if (!client_cbs.return_code)
  53534. return TEST_FAIL;
  53535. if (!server_cbs.return_code)
  53536. return TEST_FAIL;
  53537. return TEST_SUCCESS;
  53538. }
  53539. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53540. {
  53541. int ret;
  53542. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53543. if (ret != 0)
  53544. return ret;
  53545. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53546. if (ret != 0)
  53547. return ret;
  53548. return 0;
  53549. }
  53550. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53551. {
  53552. int ret;
  53553. int fd;
  53554. byte bad_msg[] = {
  53555. 0x17, /* app data */
  53556. 0xaa, 0xfd, /* bad version */
  53557. 0x00, 0x01, /* epoch 1 */
  53558. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53559. 0x00, 0x26, /* length: 38 bytes */
  53560. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53561. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53562. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  53563. 0x03, 0x18, 0x72
  53564. };
  53565. fd = wolfSSL_get_fd(ssl);
  53566. AssertIntGE(fd, 0);
  53567. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  53568. AssertIntEQ(ret, sizeof(bad_msg));
  53569. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  53570. AssertIntEQ(ret, sizeof("badrecordtest"));
  53571. }
  53572. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  53573. {
  53574. byte buf[100];
  53575. int ret;
  53576. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  53577. AssertIntGT(ret, 0);
  53578. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  53579. }
  53580. static int _test_wolfSSL_dtls_bad_record(
  53581. method_provider client_method, method_provider server_method)
  53582. {
  53583. callback_functions client_cbs, server_cbs;
  53584. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53585. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53586. client_cbs.doUdp = server_cbs.doUdp = 1;
  53587. client_cbs.method = client_method;
  53588. server_cbs.method = server_method;
  53589. client_cbs.on_result = test_wolfSSL_send_bad_record;
  53590. server_cbs.on_result = test_wolfSSL_read_string;
  53591. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53592. if (!client_cbs.return_code)
  53593. return TEST_FAIL;
  53594. if (!server_cbs.return_code)
  53595. return TEST_FAIL;
  53596. return TEST_SUCCESS;
  53597. }
  53598. static int test_wolfSSL_dtls_bad_record(void)
  53599. {
  53600. int ret = TEST_SUCCESS;
  53601. #if !defined(WOLFSSL_NO_TLS12)
  53602. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  53603. wolfDTLSv1_2_server_method);
  53604. #endif
  53605. #ifdef WOLFSSL_DTLS13
  53606. if (ret == TEST_SUCCESS) {
  53607. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  53608. wolfDTLSv1_3_server_method);
  53609. }
  53610. #endif /* WOLFSSL_DTLS13 */
  53611. return ret;
  53612. }
  53613. #else
  53614. static int test_wolfSSL_dtls_fragments(void) {
  53615. return TEST_SKIPPED;
  53616. }
  53617. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  53618. return TEST_SKIPPED;
  53619. }
  53620. static int test_wolfSSL_dtls_bad_record(void) {
  53621. return TEST_SKIPPED;
  53622. }
  53623. #endif
  53624. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  53625. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53626. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53627. static byte test_AEAD_fail_decryption = 0;
  53628. static byte test_AEAD_seq_num = 0;
  53629. static byte test_AEAD_done = 0;
  53630. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  53631. {
  53632. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  53633. if (ret > 0) {
  53634. if (test_AEAD_fail_decryption) {
  53635. /* Modify the packet to trigger a decryption failure */
  53636. buf[ret/2] ^= 0xFF;
  53637. if (test_AEAD_fail_decryption == 1)
  53638. test_AEAD_fail_decryption = 0;
  53639. }
  53640. }
  53641. (void)ctx;
  53642. return ret;
  53643. }
  53644. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  53645. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  53646. {
  53647. if (sendLimit)
  53648. w64Zero(sendLimit);
  53649. switch (ssl->specs.bulk_cipher_algorithm) {
  53650. case wolfssl_aes_gcm:
  53651. if (sendLimit)
  53652. *sendLimit = AEAD_AES_LIMIT;
  53653. FALL_THROUGH;
  53654. case wolfssl_chacha:
  53655. if (hardLimit)
  53656. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  53657. if (keyUpdateLimit)
  53658. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  53659. break;
  53660. case wolfssl_aes_ccm:
  53661. if (sendLimit)
  53662. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  53663. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  53664. if (hardLimit)
  53665. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  53666. if (keyUpdateLimit)
  53667. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  53668. }
  53669. else {
  53670. if (hardLimit)
  53671. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  53672. if (keyUpdateLimit)
  53673. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  53674. }
  53675. break;
  53676. default:
  53677. fprintf(stderr, "Unrecognized bulk cipher");
  53678. AssertFalse(1);
  53679. break;
  53680. }
  53681. }
  53682. static void test_AEAD_limit_client(WOLFSSL* ssl)
  53683. {
  53684. int ret;
  53685. int i;
  53686. int didReKey = 0;
  53687. char msgBuf[20];
  53688. w64wrapper hardLimit;
  53689. w64wrapper keyUpdateLimit;
  53690. w64wrapper counter;
  53691. w64wrapper sendLimit;
  53692. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  53693. w64Zero(&counter);
  53694. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  53695. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  53696. for (i = 0; i < 10; i++) {
  53697. /* Test some failed decryptions */
  53698. test_AEAD_fail_decryption = 1;
  53699. w64Increment(&counter);
  53700. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53701. /* Should succeed since decryption failures are dropped */
  53702. AssertIntGT(ret, 0);
  53703. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  53704. }
  53705. test_AEAD_fail_decryption = 1;
  53706. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  53707. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53708. /* 100 read calls should be enough to complete the key update */
  53709. w64Zero(&counter);
  53710. for (i = 0; i < 100; i++) {
  53711. /* Key update should be sent and negotiated */
  53712. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53713. AssertIntGT(ret, 0);
  53714. /* Epoch after one key update is 4 */
  53715. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  53716. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  53717. didReKey = 1;
  53718. break;
  53719. }
  53720. }
  53721. AssertTrue(didReKey);
  53722. if (!w64IsZero(sendLimit)) {
  53723. /* Test the sending limit for AEAD ciphers */
  53724. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  53725. test_AEAD_seq_num = 1;
  53726. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53727. AssertIntGT(ret, 0);
  53728. didReKey = 0;
  53729. w64Zero(&counter);
  53730. /* 100 read calls should be enough to complete the key update */
  53731. for (i = 0; i < 100; i++) {
  53732. /* Key update should be sent and negotiated */
  53733. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53734. AssertIntGT(ret, 0);
  53735. /* Epoch after another key update is 5 */
  53736. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  53737. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  53738. didReKey = 1;
  53739. break;
  53740. }
  53741. }
  53742. AssertTrue(didReKey);
  53743. }
  53744. test_AEAD_fail_decryption = 2;
  53745. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  53746. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53747. /* Connection should fail with a DECRYPT_ERROR */
  53748. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53749. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  53750. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  53751. test_AEAD_done = 1;
  53752. }
  53753. int counter = 0;
  53754. static void test_AEAD_limit_server(WOLFSSL* ssl)
  53755. {
  53756. char msgBuf[] = "Sending data";
  53757. int ret = WOLFSSL_SUCCESS;
  53758. w64wrapper sendLimit;
  53759. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53760. struct timespec delay;
  53761. XMEMSET(&delay, 0, sizeof(delay));
  53762. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  53763. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  53764. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53765. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  53766. while (!test_AEAD_done && ret > 0) {
  53767. counter++;
  53768. if (test_AEAD_seq_num) {
  53769. /* We need to update the seq number so that we can understand the
  53770. * peer. Otherwise we will incorrectly interpret the seq number. */
  53771. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  53772. AssertNotNull(e);
  53773. e->nextPeerSeqNumber = sendLimit;
  53774. test_AEAD_seq_num = 0;
  53775. }
  53776. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53777. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53778. nanosleep(&delay, NULL);
  53779. }
  53780. }
  53781. static int test_wolfSSL_dtls_AEAD_limit(void)
  53782. {
  53783. callback_functions func_cb_client;
  53784. callback_functions func_cb_server;
  53785. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53786. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53787. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53788. func_cb_server.method = wolfDTLSv1_3_server_method;
  53789. func_cb_client.method = wolfDTLSv1_3_client_method;
  53790. func_cb_server.on_result = test_AEAD_limit_server;
  53791. func_cb_client.on_result = test_AEAD_limit_client;
  53792. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53793. if (!func_cb_client.return_code)
  53794. return TEST_FAIL;
  53795. if (!func_cb_server.return_code)
  53796. return TEST_FAIL;
  53797. return TEST_SUCCESS;
  53798. }
  53799. #else
  53800. static int test_wolfSSL_dtls_AEAD_limit(void)
  53801. {
  53802. return TEST_SKIPPED;
  53803. }
  53804. #endif
  53805. #if defined(WOLFSSL_DTLS) && \
  53806. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  53807. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  53808. {
  53809. int fd, ret;
  53810. byte ch_msg[] = {
  53811. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53812. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53813. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  53814. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  53815. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  53816. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53817. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53818. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53819. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53820. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53821. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53822. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53823. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53824. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  53825. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  53826. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  53827. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  53828. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  53829. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  53830. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  53831. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  53832. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  53833. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  53834. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  53835. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  53836. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  53837. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  53838. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  53839. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  53840. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  53841. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  53842. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  53843. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  53844. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  53845. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  53846. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  53847. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  53848. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  53849. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  53850. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  53851. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  53852. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  53853. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  53854. 0x31, 0x68, 0x4c
  53855. };
  53856. fd = wolfSSL_get_fd(ssl);
  53857. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  53858. AssertIntGT(ret, 0);
  53859. /* consume the HRR otherwise handshake will fail */
  53860. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  53861. AssertIntGT(ret, 0);
  53862. }
  53863. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53864. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  53865. {
  53866. int fd, ret;
  53867. byte ch_msh_invalid_cookie[] = {
  53868. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  53869. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  53870. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  53871. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  53872. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  53873. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53874. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53875. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53876. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53877. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53878. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53879. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53880. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53881. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  53882. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53883. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53884. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53885. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53886. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53887. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  53888. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  53889. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  53890. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  53891. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  53892. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  53893. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  53894. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  53895. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  53896. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  53897. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  53898. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  53899. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  53900. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  53901. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  53902. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  53903. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  53904. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  53905. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  53906. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  53907. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  53908. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  53909. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  53910. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  53911. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  53912. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  53913. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  53914. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  53915. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  53916. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  53917. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  53918. 0x23, 0x00, 0x00
  53919. };
  53920. byte alert_reply[50];
  53921. byte expected_alert_reply[] = {
  53922. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  53923. 0x02, 0x02, 0x2f
  53924. };
  53925. fd = wolfSSL_get_fd(ssl);
  53926. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  53927. AssertIntGT(ret, 0);
  53928. /* should reply with an illegal_parameter reply */
  53929. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  53930. AssertIntEQ(ret, sizeof(expected_alert_reply));
  53931. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  53932. }
  53933. #endif
  53934. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  53935. {
  53936. #ifndef NO_MD5
  53937. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  53938. #elif !defined(NO_SHA)
  53939. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  53940. #elif !defined(NO_SHA256)
  53941. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  53942. #else
  53943. #error "We need a digest to hash the WOLFSSL object"
  53944. #endif
  53945. byte hashBuf[WC_MAX_DIGEST_SIZE];
  53946. wc_HashAlg hash;
  53947. const TLSX* exts = ssl->extensions;
  53948. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  53949. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  53950. * InitHandshakeHashes */
  53951. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  53952. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  53953. /* Following fields are not important to compare */
  53954. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53955. sslCopy.buffers.inputBuffer.buffer = NULL;
  53956. sslCopy.buffers.inputBuffer.bufferSize = 0;
  53957. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  53958. sslCopy.buffers.inputBuffer.offset = 0;
  53959. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53960. sslCopy.buffers.outputBuffer.buffer = NULL;
  53961. sslCopy.buffers.outputBuffer.bufferSize = 0;
  53962. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  53963. sslCopy.buffers.outputBuffer.offset = 0;
  53964. sslCopy.error = 0;
  53965. sslCopy.curSize = 0;
  53966. sslCopy.keys.curSeq_lo = 0;
  53967. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  53968. #ifdef WOLFSSL_DTLS13
  53969. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  53970. sslCopy.dtls13FastTimeout = 0;
  53971. #endif
  53972. sslCopy.keys.dtls_peer_handshake_number = 0;
  53973. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  53974. sslCopy.hsHashes = NULL;
  53975. #ifdef WOLFSSL_ASYNC_IO
  53976. #ifdef WOLFSSL_ASYNC_CRYPT
  53977. sslCopy.asyncDev = NULL;
  53978. #endif
  53979. sslCopy.async = NULL;
  53980. #endif
  53981. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  53982. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  53983. /* hash extension list */
  53984. while (exts != NULL) {
  53985. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  53986. exts = exts->next;
  53987. }
  53988. /* Hash suites */
  53989. if (sslCopy.suites != NULL) {
  53990. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  53991. sizeof(struct Suites)), 0);
  53992. }
  53993. /* Hash hsHashes */
  53994. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  53995. sizeof(*hsHashes)), 0);
  53996. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  53997. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  53998. return MakeWordFromHash(hashBuf);
  53999. }
  54000. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  54001. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  54002. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  54003. char *buf, int sz, void *ctx)
  54004. {
  54005. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  54006. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  54007. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  54008. }
  54009. else {
  54010. return WOLFSSL_CBIO_ERR_WANT_READ;
  54011. }
  54012. }
  54013. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  54014. {
  54015. /* Compare the ssl object before and after one ClientHello msg */
  54016. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54017. int res;
  54018. int err;
  54019. word32 initHash;
  54020. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  54021. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  54022. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54023. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  54024. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  54025. (void)initHash;
  54026. res = tcp_select(fd, 5);
  54027. /* We are expecting a msg. A timeout indicates failure. */
  54028. AssertIntEQ(res, TEST_RECV_READY);
  54029. res = wolfSSL_accept(ssl);
  54030. err = wolfSSL_get_error(ssl, res);
  54031. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  54032. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  54033. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  54034. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  54035. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  54036. }
  54037. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54038. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  54039. {
  54040. int ret;
  54041. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  54042. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54043. test_wolfSSL_dtls_compare_stateless(ssl);
  54044. }
  54045. #endif
  54046. static int test_wolfSSL_dtls_stateless(void)
  54047. {
  54048. callback_functions client_cbs, server_cbs;
  54049. size_t i;
  54050. struct {
  54051. method_provider client_meth;
  54052. method_provider server_meth;
  54053. ssl_callback client_ssl_ready;
  54054. ssl_callback server_ssl_ready;
  54055. } test_params[] = {
  54056. #if !defined(WOLFSSL_NO_TLS12)
  54057. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54058. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  54059. #endif
  54060. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54061. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54062. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  54063. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54064. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  54065. #endif
  54066. };
  54067. if (0 == sizeof(test_params)){
  54068. return TEST_SKIPPED;
  54069. }
  54070. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  54071. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54072. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54073. client_cbs.doUdp = server_cbs.doUdp = 1;
  54074. client_cbs.method = test_params[i].client_meth;
  54075. server_cbs.method = test_params[i].server_meth;
  54076. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  54077. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  54078. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54079. if (!client_cbs.return_code)
  54080. return TEST_FAIL;
  54081. if (!server_cbs.return_code)
  54082. return TEST_FAIL;
  54083. }
  54084. return TEST_SUCCESS;
  54085. }
  54086. #else
  54087. static int test_wolfSSL_dtls_stateless(void)
  54088. {
  54089. return TEST_SKIPPED;
  54090. }
  54091. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  54092. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  54093. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54094. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54095. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54096. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54097. {
  54098. int ret;
  54099. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  54100. fprintf(stderr, "loading cert %s failed\n", certA);
  54101. fprintf(stderr, "Error: (%d): %s\n", ret,
  54102. wolfSSL_ERR_reason_error_string(ret));
  54103. return -1;
  54104. }
  54105. return 0;
  54106. }
  54107. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54108. {
  54109. int ret;
  54110. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  54111. != WOLFSSL_SUCCESS) {
  54112. fprintf(stderr, "could not verify the cert: %s\n", certA);
  54113. fprintf(stderr, "Error: (%d): %s\n", ret,
  54114. wolfSSL_ERR_reason_error_string(ret));
  54115. return -1;
  54116. }
  54117. else {
  54118. fprintf(stderr, "successfully verified: %s\n", certA);
  54119. }
  54120. return 0;
  54121. }
  54122. #define LOAD_ONE_CA(a, b, c, d) \
  54123. do { \
  54124. (a) = load_ca_into_cm(c, d); \
  54125. if ((a) != 0) \
  54126. return (b); \
  54127. else \
  54128. (b)--; \
  54129. } while(0)
  54130. #define VERIFY_ONE_CERT(a, b, c, d) \
  54131. do { \
  54132. (a) = verify_cert_with_cm(c, d);\
  54133. if ((a) != 0) \
  54134. return (b); \
  54135. else \
  54136. (b)--; \
  54137. } while(0)
  54138. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  54139. {
  54140. int ret;
  54141. int i = -1;
  54142. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  54143. char chainGArr[9][50] = {"certs/ca-cert.pem",
  54144. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  54145. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  54146. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  54147. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  54148. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  54149. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  54150. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  54151. "certs/test-pathlen/chainG-entity.pem"};
  54152. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  54153. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  54154. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  54155. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  54156. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  54157. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  54158. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  54159. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  54160. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  54161. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  54162. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  54163. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  54164. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  54165. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  54166. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  54167. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  54168. /* test validating the entity twice, should have no effect on pathLen since
  54169. * entity/leaf cert */
  54170. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  54171. return ret;
  54172. }
  54173. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  54174. {
  54175. int ret;
  54176. int i = -1;
  54177. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  54178. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  54179. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  54180. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  54181. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  54182. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54183. */
  54184. char chainHArr[6][50] = {"certs/ca-cert.pem",
  54185. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  54186. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  54187. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  54188. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  54189. "certs/test-pathlen/chainH-entity.pem"};
  54190. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  54191. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  54192. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  54193. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  54194. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  54195. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  54196. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  54197. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  54198. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  54199. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  54200. return ret;
  54201. }
  54202. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  54203. {
  54204. int ret;
  54205. int i = -1;
  54206. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  54207. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  54208. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  54209. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  54210. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54211. */
  54212. char chainIArr[5][50] = {"certs/ca-cert.pem",
  54213. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  54214. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  54215. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  54216. "certs/test-pathlen/chainI-entity.pem"};
  54217. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  54218. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  54219. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  54220. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  54221. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  54222. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  54223. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  54224. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  54225. return ret;
  54226. }
  54227. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  54228. {
  54229. int ret;
  54230. int i = -1;
  54231. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  54232. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  54233. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  54234. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  54235. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  54236. */
  54237. char chainJArr[6][50] = {"certs/ca-cert.pem",
  54238. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  54239. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  54240. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  54241. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  54242. "certs/test-pathlen/chainJ-entity.pem"};
  54243. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  54244. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  54245. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  54246. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  54247. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  54248. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  54249. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  54250. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  54251. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  54252. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  54253. return ret;
  54254. }
  54255. static int test_various_pathlen_chains(void)
  54256. {
  54257. EXPECT_DECLS;
  54258. WOLFSSL_CERT_MANAGER* cm = NULL;
  54259. /* Test chain G (large chain with varying pathLens) */
  54260. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54261. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54262. ExpectIntEQ(test_chainG(cm), -1);
  54263. #else
  54264. ExpectIntEQ(test_chainG(cm), 0);
  54265. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54266. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54267. wolfSSL_CertManagerFree(cm);
  54268. /* end test chain G */
  54269. /* Test chain H (5 chain with same pathLens) */
  54270. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54271. ExpectIntLT(test_chainH(cm), 0);
  54272. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54273. wolfSSL_CertManagerFree(cm);
  54274. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54275. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54276. wolfSSL_CertManagerFree(cm);
  54277. /* end test chain H */
  54278. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  54279. * followed by 2 ICA's, should pass) */
  54280. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54281. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54282. ExpectIntEQ(test_chainI(cm), -1);
  54283. #else
  54284. ExpectIntEQ(test_chainI(cm), 0);
  54285. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54286. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54287. wolfSSL_CertManagerFree(cm);
  54288. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54289. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54290. wolfSSL_CertManagerFree(cm);
  54291. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  54292. * this time followed by 3 ICA's, should fail */
  54293. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54294. ExpectIntLT(test_chainJ(cm), 0);
  54295. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54296. wolfSSL_CertManagerFree(cm);
  54297. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54298. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54299. wolfSSL_CertManagerFree(cm);
  54300. return EXPECT_RESULT();
  54301. }
  54302. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  54303. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54304. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  54305. {
  54306. EXPECT_DECLS;
  54307. byte ekm[100] = {0};
  54308. (void)ctx;
  54309. /* Success Cases */
  54310. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54311. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  54312. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54313. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  54314. /* Use some random context */
  54315. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54316. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  54317. /* Failure cases */
  54318. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54319. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  54320. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54321. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  54322. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54323. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  54324. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54325. "extended master secret", XSTR_SIZEOF("extended master secret"),
  54326. NULL, 0, 0), 0);
  54327. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54328. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  54329. return EXPECT_RESULT();
  54330. }
  54331. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  54332. {
  54333. wolfSSL_KeepArrays(ssl);
  54334. return TEST_SUCCESS;
  54335. }
  54336. static int test_export_keying_material(void)
  54337. {
  54338. EXPECT_DECLS;
  54339. test_ssl_cbf serverCb;
  54340. test_ssl_cbf clientCb;
  54341. XMEMSET(&serverCb, 0, sizeof(serverCb));
  54342. XMEMSET(&clientCb, 0, sizeof(clientCb));
  54343. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  54344. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  54345. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  54346. return EXPECT_RESULT();
  54347. }
  54348. #endif /* HAVE_KEYING_MATERIAL */
  54349. static int test_wolfSSL_THREADID_hash(void)
  54350. {
  54351. EXPECT_DECLS;
  54352. #if defined(OPENSSL_EXTRA)
  54353. CRYPTO_THREADID id;
  54354. CRYPTO_THREADID_current(NULL);
  54355. /* Hash result is unsigned long. */
  54356. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  54357. XMEMSET(&id, 0, sizeof(id));
  54358. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  54359. #endif /* OPENSSL_EXTRA */
  54360. return EXPECT_RESULT();
  54361. }
  54362. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  54363. {
  54364. EXPECT_DECLS;
  54365. #if defined(OPENSSL_EXTRA)
  54366. WOLFSSL_CTX* ctx = NULL;
  54367. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  54368. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  54369. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  54370. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  54371. #endif /* OPENSSL_EXTRA */
  54372. return EXPECT_RESULT();
  54373. }
  54374. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54375. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54376. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  54377. {
  54378. EXPECT_DECLS;
  54379. callback_functions* callbacks = NULL;
  54380. WOLFSSL_CTX* ctx = NULL;
  54381. WOLFSSL* ssl = NULL;
  54382. SOCKET_T sfd = 0;
  54383. SOCKET_T cfd = 0;
  54384. word16 port;
  54385. char msg[] = "I hear you fa shizzle!";
  54386. int len = (int) XSTRLEN(msg);
  54387. char input[1024];
  54388. int ret;
  54389. int err = 0;
  54390. if (!args)
  54391. WOLFSSL_RETURN_FROM_THREAD(0);
  54392. ((func_args*)args)->return_code = TEST_FAIL;
  54393. callbacks = ((func_args*)args)->callbacks;
  54394. ctx = wolfSSL_CTX_new(callbacks->method());
  54395. #if defined(USE_WINDOWS_API)
  54396. port = ((func_args*)args)->signal->port;
  54397. #else
  54398. /* Let tcp_listen assign port */
  54399. port = 0;
  54400. #endif
  54401. #ifdef WOLFSSL_TIRTOS
  54402. fdOpenSession(Task_self());
  54403. #endif
  54404. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54405. caCertFile, 0));
  54406. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54407. svrCertFile, WOLFSSL_FILETYPE_PEM));
  54408. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54409. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  54410. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  54411. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  54412. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54413. #elif !defined(NO_DH)
  54414. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  54415. #endif
  54416. if (callbacks->ctx_ready)
  54417. callbacks->ctx_ready(ctx);
  54418. ssl = wolfSSL_new(ctx);
  54419. ExpectNotNull(ssl);
  54420. /* listen and accept */
  54421. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  54422. CloseSocket(sfd);
  54423. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  54424. if (callbacks->ssl_ready)
  54425. callbacks->ssl_ready(ssl);
  54426. if (EXPECT_SUCCESS()) {
  54427. do {
  54428. err = 0; /* Reset error */
  54429. ret = wolfSSL_accept(ssl);
  54430. if (ret != WOLFSSL_SUCCESS) {
  54431. err = wolfSSL_get_error(ssl, 0);
  54432. }
  54433. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54434. }
  54435. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54436. /* read and write data */
  54437. XMEMSET(input, 0, sizeof(input));
  54438. while (EXPECT_SUCCESS()) {
  54439. ret = wolfSSL_read(ssl, input, sizeof(input));
  54440. if (ret > 0) {
  54441. break;
  54442. }
  54443. else {
  54444. err = wolfSSL_get_error(ssl,ret);
  54445. if (err == WOLFSSL_ERROR_WANT_READ) {
  54446. continue;
  54447. }
  54448. break;
  54449. }
  54450. }
  54451. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  54452. do {
  54453. ret = wolfSSL_write(ssl, msg, len);
  54454. if (ret > 0) {
  54455. break;
  54456. }
  54457. } while (ret < 0);
  54458. }
  54459. /* bidirectional shutdown */
  54460. while (EXPECT_SUCCESS()) {
  54461. ret = wolfSSL_shutdown(ssl);
  54462. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  54463. if (ret == WOLFSSL_SUCCESS) {
  54464. break;
  54465. }
  54466. }
  54467. if (EXPECT_SUCCESS()) {
  54468. /* wait for the peer to disconnect the tcp connection */
  54469. do {
  54470. ret = wolfSSL_read(ssl, input, sizeof(input));
  54471. err = wolfSSL_get_error(ssl, ret);
  54472. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54473. }
  54474. /* detect TCP disconnect */
  54475. ExpectIntLE(ret,WOLFSSL_FAILURE);
  54476. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54477. ((func_args*)args)->return_code = EXPECT_RESULT();
  54478. wolfSSL_free(ssl);
  54479. wolfSSL_CTX_free(ctx);
  54480. CloseSocket(cfd);
  54481. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54482. wc_ecc_fp_free(); /* free per thread cache */
  54483. #endif
  54484. WOLFSSL_RETURN_FROM_THREAD(0);
  54485. }
  54486. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54487. {
  54488. EXPECT_DECLS;
  54489. callback_functions* callbacks = NULL;
  54490. WOLFSSL_CTX* ctx = NULL;
  54491. WOLFSSL* ssl = NULL;
  54492. SOCKET_T sfd = 0;
  54493. char msg[] = "hello wolfssl server!";
  54494. int len = (int) XSTRLEN(msg);
  54495. char input[1024];
  54496. int idx;
  54497. int ret, err;
  54498. if (!args)
  54499. WOLFSSL_RETURN_FROM_THREAD(0);
  54500. ((func_args*)args)->return_code = TEST_FAIL;
  54501. callbacks = ((func_args*)args)->callbacks;
  54502. ctx = wolfSSL_CTX_new(callbacks->method());
  54503. #ifdef WOLFSSL_TIRTOS
  54504. fdOpenSession(Task_self());
  54505. #endif
  54506. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54507. caCertFile, 0));
  54508. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54509. cliCertFile, WOLFSSL_FILETYPE_PEM));
  54510. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54511. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  54512. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  54513. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54514. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54515. if (EXPECT_SUCCESS()) {
  54516. do {
  54517. err = 0; /* Reset error */
  54518. ret = wolfSSL_connect(ssl);
  54519. if (ret != WOLFSSL_SUCCESS) {
  54520. err = wolfSSL_get_error(ssl, 0);
  54521. }
  54522. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54523. }
  54524. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  54525. if (EXPECT_SUCCESS()) {
  54526. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54527. input[idx] = 0;
  54528. }
  54529. }
  54530. if (EXPECT_SUCCESS()) {
  54531. ret = wolfSSL_shutdown(ssl);
  54532. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54533. ret = wolfSSL_shutdown(ssl);
  54534. }
  54535. }
  54536. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54537. ((func_args*)args)->return_code = EXPECT_RESULT();
  54538. wolfSSL_free(ssl);
  54539. wolfSSL_CTX_free(ctx);
  54540. CloseSocket(sfd);
  54541. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54542. wc_ecc_fp_free(); /* free per thread cache */
  54543. #endif
  54544. WOLFSSL_RETURN_FROM_THREAD(0);
  54545. }
  54546. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  54547. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  54548. /* This test is to check wolfSSL_read behaves as same as
  54549. * openSSL when it is called after SSL_shutdown completes.
  54550. */
  54551. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  54552. {
  54553. EXPECT_DECLS;
  54554. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54555. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54556. tcp_ready ready;
  54557. func_args client_args;
  54558. func_args server_args;
  54559. THREAD_TYPE serverThread;
  54560. THREAD_TYPE clientThread;
  54561. callback_functions server_cbf;
  54562. callback_functions client_cbf;
  54563. #ifdef WOLFSSL_TIRTOS
  54564. fdOpenSession(Task_self());
  54565. #endif
  54566. StartTCP();
  54567. InitTcpReady(&ready);
  54568. #if defined(USE_WINDOWS_API)
  54569. /* use RNG to get random port if using windows */
  54570. ready.port = GetRandomPort();
  54571. #endif
  54572. XMEMSET(&client_args, 0, sizeof(func_args));
  54573. XMEMSET(&server_args, 0, sizeof(func_args));
  54574. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  54575. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  54576. server_cbf.method = wolfTLSv1_2_server_method;
  54577. client_cbf.method = wolfTLSv1_2_client_method;
  54578. server_args.callbacks = &server_cbf;
  54579. client_args.callbacks = &client_cbf;
  54580. server_args.signal = &ready;
  54581. client_args.signal = &ready;
  54582. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  54583. wait_tcp_ready(&server_args);
  54584. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  54585. join_thread(clientThread);
  54586. join_thread(serverThread);
  54587. ExpectTrue(client_args.return_code);
  54588. ExpectTrue(server_args.return_code);
  54589. FreeTcpReady(&ready);
  54590. #endif
  54591. return EXPECT_RESULT();
  54592. }
  54593. static int test_wolfSSL_CTX_get_min_proto_version(void)
  54594. {
  54595. EXPECT_DECLS;
  54596. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54597. WOLFSSL_CTX *ctx = NULL;
  54598. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54599. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  54600. WOLFSSL_SUCCESS);
  54601. #ifdef WOLFSSL_ALLOW_SSLV3
  54602. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54603. #else
  54604. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54605. #endif
  54606. wolfSSL_CTX_free(ctx);
  54607. ctx = NULL;
  54608. #ifdef WOLFSSL_ALLOW_TLSV10
  54609. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  54610. #else
  54611. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54612. #endif
  54613. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  54614. WOLFSSL_SUCCESS);
  54615. #ifdef WOLFSSL_ALLOW_TLSV10
  54616. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54617. #else
  54618. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54619. #endif
  54620. wolfSSL_CTX_free(ctx);
  54621. ctx = NULL;
  54622. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54623. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  54624. WOLFSSL_SUCCESS);
  54625. #ifndef NO_OLD_TLS
  54626. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54627. #else
  54628. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54629. #endif
  54630. wolfSSL_CTX_free(ctx);
  54631. ctx = NULL;
  54632. #ifndef WOLFSSL_NO_TLS12
  54633. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  54634. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  54635. WOLFSSL_SUCCESS);
  54636. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  54637. wolfSSL_CTX_free(ctx);
  54638. ctx = NULL;
  54639. #endif
  54640. #ifdef WOLFSSL_TLS13
  54641. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  54642. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  54643. WOLFSSL_SUCCESS);
  54644. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  54645. wolfSSL_CTX_free(ctx);
  54646. ctx = NULL;
  54647. #endif
  54648. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54649. return EXPECT_RESULT();
  54650. }
  54651. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54652. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54653. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54654. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  54655. static int test_wolfSSL_set_SSL_CTX(void)
  54656. {
  54657. EXPECT_DECLS;
  54658. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  54659. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  54660. !defined(NO_RSA)
  54661. WOLFSSL_CTX *ctx1 = NULL;
  54662. WOLFSSL_CTX *ctx2 = NULL;
  54663. WOLFSSL *ssl = NULL;
  54664. const byte *session_id1 = (const byte *)"CTX1";
  54665. const byte *session_id2 = (const byte *)"CTX2";
  54666. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54667. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  54668. WOLFSSL_FILETYPE_PEM));
  54669. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  54670. WOLFSSL_FILETYPE_PEM));
  54671. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  54672. WOLFSSL_SUCCESS);
  54673. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  54674. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  54675. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  54676. WOLFSSL_SUCCESS);
  54677. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54678. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  54679. WOLFSSL_FILETYPE_PEM));
  54680. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  54681. WOLFSSL_FILETYPE_PEM));
  54682. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  54683. WOLFSSL_SUCCESS);
  54684. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  54685. WOLFSSL_SUCCESS);
  54686. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  54687. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  54688. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  54689. WOLFSSL_SUCCESS);
  54690. #ifdef HAVE_SESSION_TICKET
  54691. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  54692. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  54693. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  54694. #endif
  54695. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  54696. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54697. #ifdef WOLFSSL_INT_H
  54698. #ifdef WOLFSSL_SESSION_ID_CTX
  54699. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  54700. #endif
  54701. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  54702. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  54703. #endif
  54704. #ifdef HAVE_SESSION_TICKET
  54705. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54706. #endif
  54707. /* Set the ctx1 that has TLSv1.3 as max proto version */
  54708. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  54709. /* MUST not change proto versions of ssl */
  54710. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54711. #ifdef HAVE_SESSION_TICKET
  54712. /* MUST not change */
  54713. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54714. #endif
  54715. /* MUST change */
  54716. #ifdef WOLFSSL_INT_H
  54717. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  54718. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  54719. #ifdef WOLFSSL_SESSION_ID_CTX
  54720. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  54721. #endif
  54722. #endif
  54723. wolfSSL_free(ssl);
  54724. wolfSSL_CTX_free(ctx1);
  54725. wolfSSL_CTX_free(ctx2);
  54726. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54727. return EXPECT_RESULT();
  54728. }
  54729. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54730. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54731. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54732. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  54733. static int test_wolfSSL_security_level(void)
  54734. {
  54735. EXPECT_DECLS;
  54736. #if defined(OPENSSL_EXTRA)
  54737. SSL_CTX *ctx = NULL;
  54738. #ifdef WOLFSSL_TLS13
  54739. #ifdef NO_WOLFSSL_SERVER
  54740. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54741. #else
  54742. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54743. #endif
  54744. SSL_CTX_set_security_level(NULL, 1);
  54745. SSL_CTX_set_security_level(ctx, 1);
  54746. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  54747. /* Stub so nothing happens. */
  54748. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  54749. SSL_CTX_free(ctx);
  54750. #else
  54751. (void)ctx;
  54752. #endif
  54753. #endif
  54754. return EXPECT_RESULT();
  54755. }
  54756. static int test_wolfSSL_SSL_in_init(void)
  54757. {
  54758. EXPECT_DECLS;
  54759. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  54760. SSL_CTX* ctx = NULL;
  54761. SSL* ssl = NULL;
  54762. const char* testCertFile;
  54763. const char* testKeyFile;
  54764. #ifdef WOLFSSL_TLS13
  54765. #ifdef NO_WOLFSSL_SERVER
  54766. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54767. #else
  54768. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54769. #endif
  54770. #else
  54771. #ifdef NO_WOLFSSL_SERVER
  54772. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54773. #else
  54774. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54775. #endif
  54776. #endif
  54777. #ifndef NO_RSA
  54778. testCertFile = svrCertFile;
  54779. testKeyFile = svrKeyFile;
  54780. #elif defined(HAVE_ECC)
  54781. testCertFile = eccCertFile;
  54782. testKeyFile = eccKeyFile;
  54783. #else
  54784. testCertFile = NULL;
  54785. testKeyFile = NULL;
  54786. #endif
  54787. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  54788. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54789. SSL_FILETYPE_PEM));
  54790. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54791. SSL_FILETYPE_PEM));
  54792. }
  54793. ExpectNotNull(ssl = SSL_new(ctx));
  54794. ExpectIntEQ(SSL_in_init(ssl), 1);
  54795. SSL_CTX_free(ctx);
  54796. SSL_free(ssl);
  54797. #endif
  54798. return EXPECT_RESULT();
  54799. }
  54800. static int test_wolfSSL_CTX_set_timeout(void)
  54801. {
  54802. EXPECT_DECLS;
  54803. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  54804. int timeout;
  54805. WOLFSSL_CTX* ctx = NULL;
  54806. (void)timeout;
  54807. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54808. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  54809. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  54810. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  54811. */
  54812. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54813. /* giving 0 as timeout value sets default timeout */
  54814. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  54815. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  54816. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  54817. #else
  54818. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54819. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  54820. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  54821. #endif
  54822. wolfSSL_CTX_free(ctx);
  54823. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  54824. return EXPECT_RESULT();
  54825. }
  54826. static int test_wolfSSL_OpenSSL_version(void)
  54827. {
  54828. EXPECT_DECLS;
  54829. #if defined(OPENSSL_EXTRA)
  54830. const char* ver;
  54831. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  54832. ExpectNotNull(ver = OpenSSL_version(0));
  54833. #else
  54834. ExpectNotNull(ver = OpenSSL_version());
  54835. #endif
  54836. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  54837. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  54838. #endif
  54839. return EXPECT_RESULT();
  54840. }
  54841. static int test_CONF_CTX_CMDLINE(void)
  54842. {
  54843. EXPECT_DECLS;
  54844. #if defined(OPENSSL_ALL)
  54845. SSL_CTX* ctx = NULL;
  54846. SSL_CONF_CTX* cctx = NULL;
  54847. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54848. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54849. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54850. /* set flags */
  54851. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  54852. WOLFSSL_CONF_FLAG_CMDLINE);
  54853. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54854. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54855. /* cmd invalid command */
  54856. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54857. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54858. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54859. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54860. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54861. /* cmd Certificate and Private Key*/
  54862. {
  54863. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54864. const char* ourCert = svrCertFile;
  54865. const char* ourKey = svrKeyFile;
  54866. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  54867. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  54868. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  54869. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  54870. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54871. #endif
  54872. }
  54873. /* cmd curves */
  54874. {
  54875. #if defined(HAVE_ECC)
  54876. const char* curve = "secp256r1";
  54877. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  54878. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  54879. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54880. #endif
  54881. }
  54882. /* cmd CipherString */
  54883. {
  54884. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54885. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  54886. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  54887. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54888. }
  54889. /* cmd DH parameter */
  54890. {
  54891. #if !defined(NO_DH) && !defined(NO_BIO)
  54892. const char* ourdhcert = "./certs/dh2048.pem";
  54893. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  54894. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  54895. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54896. #endif
  54897. }
  54898. SSL_CTX_free(ctx);
  54899. SSL_CONF_CTX_free(cctx);
  54900. #endif /* OPENSSL_EXTRA */
  54901. return EXPECT_RESULT();
  54902. }
  54903. static int test_CONF_CTX_FILE(void)
  54904. {
  54905. EXPECT_DECLS;
  54906. #if defined(OPENSSL_ALL)
  54907. SSL_CTX* ctx = NULL;
  54908. SSL_CONF_CTX* cctx = NULL;
  54909. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54910. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54911. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54912. /* set flags */
  54913. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  54914. WOLFSSL_CONF_FLAG_FILE);
  54915. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54916. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54917. /* sanity check */
  54918. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54919. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54920. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54921. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54922. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54923. /* cmd Certificate and Private Key*/
  54924. {
  54925. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54926. const char* ourCert = svrCertFile;
  54927. const char* ourKey = svrKeyFile;
  54928. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  54929. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  54930. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  54931. WOLFSSL_SUCCESS);
  54932. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  54933. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54934. #endif
  54935. }
  54936. /* cmd curves */
  54937. {
  54938. #if defined(HAVE_ECC)
  54939. const char* curve = "secp256r1";
  54940. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  54941. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  54942. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54943. #endif
  54944. }
  54945. /* cmd CipherString */
  54946. {
  54947. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54948. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  54949. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  54950. WOLFSSL_SUCCESS);
  54951. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54952. }
  54953. /* cmd DH parameter */
  54954. {
  54955. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  54956. const char* ourdhcert = "./certs/dh3072.pem";
  54957. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  54958. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  54959. WOLFSSL_SUCCESS);
  54960. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54961. #endif
  54962. }
  54963. SSL_CTX_free(ctx);
  54964. SSL_CONF_CTX_free(cctx);
  54965. #endif /* OPENSSL_EXTRA */
  54966. return EXPECT_RESULT();
  54967. }
  54968. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  54969. {
  54970. EXPECT_DECLS;
  54971. #ifdef HAVE_EX_DATA
  54972. int idx1, idx2;
  54973. /* test for unsupported class index */
  54974. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  54975. 0,NULL, NULL, NULL, NULL ), -1);
  54976. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  54977. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  54978. 0,NULL, NULL, NULL, NULL ), -1);
  54979. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  54980. 0,NULL, NULL, NULL, NULL ), -1);
  54981. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  54982. 0,NULL, NULL, NULL, NULL ), -1);
  54983. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  54984. 0,NULL, NULL, NULL, NULL ), -1);
  54985. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  54986. 0,NULL, NULL, NULL, NULL ), -1);
  54987. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  54988. 0,NULL, NULL, NULL, NULL ), -1);
  54989. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  54990. 0,NULL, NULL, NULL, NULL ), -1);
  54991. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  54992. 0,NULL, NULL, NULL, NULL ), -1);
  54993. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  54994. 0,NULL, NULL, NULL, NULL ), -1);
  54995. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  54996. 0,NULL, NULL, NULL, NULL ), -1);
  54997. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  54998. 0,NULL, NULL, NULL, NULL ), -1);
  54999. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  55000. 0,NULL, NULL, NULL, NULL ), -1);
  55001. /* test for supported class index */
  55002. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55003. 0,NULL, NULL, NULL, NULL );
  55004. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55005. 0,NULL, NULL, NULL, NULL );
  55006. ExpectIntNE(idx1, -1);
  55007. ExpectIntNE(idx2, -1);
  55008. ExpectIntNE(idx1, idx2);
  55009. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55010. 0,NULL, NULL, NULL, NULL );
  55011. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55012. 0,NULL, NULL, NULL, NULL );
  55013. ExpectIntNE(idx1, -1);
  55014. ExpectIntNE(idx2, -1);
  55015. ExpectIntNE(idx1, idx2);
  55016. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55017. 0,NULL, NULL, NULL, NULL );
  55018. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55019. 0,NULL, NULL, NULL, NULL );
  55020. ExpectIntNE(idx1, -1);
  55021. ExpectIntNE(idx2, -1);
  55022. ExpectIntNE(idx1, idx2);
  55023. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55024. 0,NULL, NULL, NULL, NULL );
  55025. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55026. 0,NULL, NULL, NULL, NULL );
  55027. ExpectIntNE(idx1, -1);
  55028. ExpectIntNE(idx2, -1);
  55029. ExpectIntNE(idx1, idx2);
  55030. #endif /* HAVE_EX_DATA */
  55031. return EXPECT_RESULT();
  55032. }
  55033. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  55034. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55035. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55036. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55037. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  55038. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  55039. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  55040. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  55041. #define SESSION_NEW_IDX_PTR "Testing"
  55042. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  55043. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55044. {
  55045. AssertNotNull(p);
  55046. AssertNull(ptr);
  55047. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  55048. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55049. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55050. }
  55051. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  55052. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  55053. void* arg)
  55054. {
  55055. EXPECT_DECLS;
  55056. ExpectNotNull(out);
  55057. ExpectNotNull(in);
  55058. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  55059. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  55060. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  55061. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  55062. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55063. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  55064. if (EXPECT_SUCCESS()) {
  55065. return SSL_SUCCESS;
  55066. }
  55067. else {
  55068. return SSL_FAILURE;
  55069. }
  55070. }
  55071. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  55072. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  55073. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55074. {
  55075. EXPECT_DECLS;
  55076. ExpectNotNull(p);
  55077. ExpectNull(ptr);
  55078. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  55079. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55080. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55081. if (EXPECT_SUCCESS()) {
  55082. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  55083. }
  55084. }
  55085. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55086. {
  55087. EXPECT_DECLS;
  55088. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  55089. (void*)SESSION_NEW_IDX_PTR,
  55090. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  55091. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  55092. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  55093. SSL_SESSION* s = SSL_SESSION_new();
  55094. SSL_SESSION* d = NULL;
  55095. ExpectNotNull(s);
  55096. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  55097. ExpectNotNull(d = SSL_SESSION_dup(s));
  55098. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  55099. SSL_SESSION_free(s);
  55100. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  55101. SSL_SESSION_free(d);
  55102. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  55103. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  55104. crypto_ex_cb_ctx_session = NULL;
  55105. return EXPECT_RESULT();
  55106. }
  55107. #else
  55108. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55109. {
  55110. return TEST_SKIPPED;
  55111. }
  55112. #endif
  55113. static int test_wolfSSL_set_psk_use_session_callback(void)
  55114. {
  55115. EXPECT_DECLS;
  55116. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  55117. SSL_CTX* ctx = NULL;
  55118. SSL* ssl = NULL;
  55119. const char* testCertFile;
  55120. const char* testKeyFile;
  55121. #ifdef WOLFSSL_TLS13
  55122. #ifdef NO_WOLFSSL_SERVER
  55123. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55124. #else
  55125. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55126. #endif
  55127. #else
  55128. #ifdef NO_WOLFSSL_SERVER
  55129. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55130. #else
  55131. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55132. #endif
  55133. #endif
  55134. #ifndef NO_RSA
  55135. testCertFile = svrCertFile;
  55136. testKeyFile = svrKeyFile;
  55137. #elif defined(HAVE_ECC)
  55138. testCertFile = eccCertFile;
  55139. testKeyFile = eccKeyFile;
  55140. #else
  55141. testCertFile = NULL;
  55142. testKeyFile = NULL;
  55143. #endif
  55144. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55145. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55146. SSL_FILETYPE_PEM));
  55147. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55148. SSL_FILETYPE_PEM));
  55149. }
  55150. ExpectNotNull(ssl = SSL_new(ctx));
  55151. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  55152. SSL_CTX_free(ctx);
  55153. SSL_free(ssl);
  55154. #endif
  55155. return EXPECT_RESULT();
  55156. }
  55157. static int test_wolfSSL_ERR_strings(void)
  55158. {
  55159. EXPECT_DECLS;
  55160. #if !defined(NO_ERROR_STRINGS)
  55161. const char* err1 = "unsupported cipher suite";
  55162. const char* err2 = "wolfSSL PEM routines";
  55163. const char* err = NULL;
  55164. (void)err;
  55165. (void)err1;
  55166. (void)err2;
  55167. #if defined(OPENSSL_EXTRA)
  55168. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  55169. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55170. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  55171. ExpectIntEQ((*err == '\0'), 1);
  55172. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  55173. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  55174. #else
  55175. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  55176. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55177. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  55178. ExpectIntEQ((*err == '\0'), 1);
  55179. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  55180. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  55181. ExpectIntEQ((*err == '\0'), 1);
  55182. #endif
  55183. #endif
  55184. return EXPECT_RESULT();
  55185. }
  55186. static int test_wolfSSL_EVP_shake128(void)
  55187. {
  55188. EXPECT_DECLS;
  55189. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55190. defined(WOLFSSL_SHAKE128)
  55191. const EVP_MD* md = NULL;
  55192. ExpectNotNull(md = EVP_shake128());
  55193. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  55194. #endif
  55195. return EXPECT_RESULT();
  55196. }
  55197. static int test_wolfSSL_EVP_shake256(void)
  55198. {
  55199. EXPECT_DECLS;
  55200. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55201. defined(WOLFSSL_SHAKE256)
  55202. const EVP_MD* md = NULL;
  55203. ExpectNotNull(md = EVP_shake256());
  55204. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  55205. #endif
  55206. return EXPECT_RESULT();
  55207. }
  55208. /*
  55209. * Testing EVP digest API with SM3
  55210. */
  55211. static int test_wolfSSL_EVP_sm3(void)
  55212. {
  55213. int res = TEST_SKIPPED;
  55214. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  55215. EXPECT_DECLS;
  55216. const EVP_MD* md = NULL;
  55217. EVP_MD_CTX* mdCtx = NULL;
  55218. byte data[WC_SM3_BLOCK_SIZE * 4];
  55219. byte hash[WC_SM3_DIGEST_SIZE];
  55220. byte calcHash[WC_SM3_DIGEST_SIZE];
  55221. byte expHash[WC_SM3_DIGEST_SIZE] = {
  55222. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  55223. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  55224. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  55225. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  55226. };
  55227. word32 chunk;
  55228. word32 i;
  55229. unsigned int sz;
  55230. int ret;
  55231. XMEMSET(data, 0, sizeof(data));
  55232. md = EVP_sm3();
  55233. ExpectTrue(md != NULL);
  55234. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  55235. mdCtx = EVP_MD_CTX_new();
  55236. ExpectTrue(mdCtx != NULL);
  55237. /* Invalid Parameters */
  55238. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  55239. /* Valid Parameters */
  55240. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  55241. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  55242. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  55243. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  55244. /* Valid Parameters */
  55245. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  55246. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55247. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55248. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  55249. WOLFSSL_SUCCESS);
  55250. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  55251. WOLFSSL_SUCCESS);
  55252. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  55253. WOLFSSL_SUCCESS);
  55254. /* Ensure too many bytes for lengths. */
  55255. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  55256. WOLFSSL_SUCCESS);
  55257. /* Invalid Parameters */
  55258. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  55259. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55260. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55261. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55262. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55263. /* Valid Parameters */
  55264. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55265. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  55266. /* Chunk tests. */
  55267. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  55268. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  55269. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  55270. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  55271. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  55272. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  55273. WOLFSSL_SUCCESS);
  55274. }
  55275. if (i < (word32)sizeof(data)) {
  55276. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  55277. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  55278. }
  55279. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55280. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  55281. }
  55282. /* Not testing when the low 32-bit length overflows. */
  55283. ret = EVP_MD_CTX_cleanup(mdCtx);
  55284. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55285. wolfSSL_EVP_MD_CTX_free(mdCtx);
  55286. res = EXPECT_RESULT();
  55287. #endif
  55288. return res;
  55289. } /* END test_EVP_sm3 */
  55290. static int test_EVP_blake2(void)
  55291. {
  55292. EXPECT_DECLS;
  55293. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  55294. const EVP_MD* md = NULL;
  55295. (void)md;
  55296. #if defined(HAVE_BLAKE2)
  55297. ExpectNotNull(md = EVP_blake2b512());
  55298. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  55299. #endif
  55300. #if defined(HAVE_BLAKE2S)
  55301. ExpectNotNull(md = EVP_blake2s256());
  55302. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  55303. #endif
  55304. #endif
  55305. return EXPECT_RESULT();
  55306. }
  55307. #if defined(OPENSSL_EXTRA)
  55308. static void list_md_fn(const EVP_MD* m, const char* from,
  55309. const char* to, void* arg)
  55310. {
  55311. const char* mn;
  55312. BIO *bio;
  55313. (void) from;
  55314. (void) to;
  55315. (void) arg;
  55316. (void) mn;
  55317. (void) bio;
  55318. if (!m) {
  55319. /* alias */
  55320. AssertNull(m);
  55321. AssertNotNull(to);
  55322. }
  55323. else {
  55324. AssertNotNull(m);
  55325. AssertNull(to);
  55326. }
  55327. AssertNotNull(from);
  55328. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55329. mn = EVP_get_digestbyname(from);
  55330. /* print to stderr */
  55331. AssertNotNull(arg);
  55332. bio = BIO_new(BIO_s_file());
  55333. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55334. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  55335. BIO_free(bio);
  55336. #endif
  55337. }
  55338. #endif
  55339. static int test_EVP_MD_do_all(void)
  55340. {
  55341. int res = TEST_SKIPPED;
  55342. #if defined(OPENSSL_EXTRA)
  55343. EVP_MD_do_all(NULL, stderr);
  55344. EVP_MD_do_all(list_md_fn, stderr);
  55345. res = TEST_SUCCESS;
  55346. #endif
  55347. return res;
  55348. }
  55349. #if defined(OPENSSL_EXTRA)
  55350. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  55351. {
  55352. (void)arg;
  55353. (void)nm;
  55354. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  55355. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55356. /* print to stderr */
  55357. AssertNotNull(arg);
  55358. BIO *bio = BIO_new(BIO_s_file());
  55359. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55360. BIO_printf(bio, "%s\n", nm);
  55361. BIO_free(bio);
  55362. #endif
  55363. }
  55364. #endif
  55365. static int test_OBJ_NAME_do_all(void)
  55366. {
  55367. int res = TEST_SKIPPED;
  55368. #if defined(OPENSSL_EXTRA)
  55369. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  55370. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  55371. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  55372. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  55373. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  55374. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  55375. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  55376. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  55377. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  55378. res = TEST_SUCCESS;
  55379. #endif
  55380. return res;
  55381. }
  55382. static int test_SSL_CIPHER_get_xxx(void)
  55383. {
  55384. EXPECT_DECLS;
  55385. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  55386. !defined(NO_FILESYSTEM)
  55387. const SSL_CIPHER* cipher = NULL;
  55388. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  55389. int i, numCiphers = 0;
  55390. SSL_CTX* ctx = NULL;
  55391. SSL* ssl = NULL;
  55392. const char* testCertFile;
  55393. const char* testKeyFile;
  55394. char buf[256] = {0};
  55395. const char* cipher_id = NULL;
  55396. int expect_nid1 = NID_undef;
  55397. int expect_nid2 = NID_undef;
  55398. int expect_nid3 = NID_undef;
  55399. int expect_nid4 = NID_undef;
  55400. int expect_nid5 = 0;
  55401. const char* cipher_id2 = NULL;
  55402. int expect_nid21 = NID_undef;
  55403. int expect_nid22 = NID_undef;
  55404. int expect_nid23 = NID_undef;
  55405. int expect_nid24 = NID_undef;
  55406. int expect_nid25 = 0;
  55407. (void)cipher;
  55408. (void)supportedCiphers;
  55409. (void)i;
  55410. (void)numCiphers;
  55411. (void)ctx;
  55412. (void)ssl;
  55413. (void)testCertFile;
  55414. (void)testKeyFile;
  55415. #if defined(WOLFSSL_TLS13)
  55416. cipher_id = "TLS13-AES128-GCM-SHA256";
  55417. expect_nid1 = NID_auth_rsa;
  55418. expect_nid2 = NID_aes_128_gcm;
  55419. expect_nid3 = NID_sha256;
  55420. expect_nid4 = NID_kx_any;
  55421. expect_nid5 = 1;
  55422. #if !defined(WOLFSSL_NO_TLS12)
  55423. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  55424. expect_nid21 = NID_auth_rsa;
  55425. expect_nid22 = NID_aes_256_gcm;
  55426. expect_nid23 = NID_sha384;
  55427. expect_nid24 = NID_kx_ecdhe;
  55428. expect_nid25 = 1;
  55429. #endif
  55430. #endif
  55431. #ifdef NO_WOLFSSL_SERVER
  55432. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55433. #else
  55434. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55435. #endif
  55436. if (cipher_id) {
  55437. #ifndef NO_RSA
  55438. testCertFile = svrCertFile;
  55439. testKeyFile = svrKeyFile;
  55440. #elif defined(HAVE_ECC)
  55441. testCertFile = eccCertFile;
  55442. testKeyFile = eccKeyFile;
  55443. #else
  55444. testCertFile = NULL;
  55445. testKeyFile = NULL;
  55446. #endif
  55447. if (testCertFile != NULL && testKeyFile != NULL) {
  55448. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55449. SSL_FILETYPE_PEM));
  55450. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55451. SSL_FILETYPE_PEM));
  55452. }
  55453. ExpectNotNull(ssl = SSL_new(ctx));
  55454. ExpectIntEQ(SSL_in_init(ssl), 1);
  55455. supportedCiphers = SSL_get_ciphers(ssl);
  55456. numCiphers = sk_num(supportedCiphers);
  55457. for (i = 0; i < numCiphers; ++i) {
  55458. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55459. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55460. }
  55461. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  55462. break;
  55463. }
  55464. }
  55465. /* test case for */
  55466. if (i != numCiphers) {
  55467. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  55468. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  55469. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  55470. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  55471. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  55472. }
  55473. if (cipher_id2) {
  55474. for (i = 0; i < numCiphers; ++i) {
  55475. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55476. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55477. }
  55478. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  55479. break;
  55480. }
  55481. }
  55482. /* test case for */
  55483. if (i != numCiphers) {
  55484. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  55485. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  55486. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  55487. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  55488. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  55489. }
  55490. }
  55491. }
  55492. SSL_CTX_free(ctx);
  55493. SSL_free(ssl);
  55494. #endif
  55495. return EXPECT_RESULT();
  55496. }
  55497. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55498. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  55499. int* keyFormat)
  55500. {
  55501. int ret;
  55502. byte* key_buf = NULL;
  55503. size_t key_sz = 0;
  55504. EncryptedInfo encInfo;
  55505. XMEMSET(&encInfo, 0, sizeof(encInfo));
  55506. ret = load_file(privKeyFile, &key_buf, &key_sz);
  55507. if (ret == 0) {
  55508. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  55509. NULL, &encInfo, keyFormat);
  55510. }
  55511. if (key_buf != NULL) {
  55512. free(key_buf); key_buf = NULL;
  55513. }
  55514. (void)encInfo; /* not used in this test */
  55515. #ifdef DEBUG_WOLFSSL
  55516. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  55517. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  55518. (*pDer)->length);
  55519. #endif
  55520. return ret;
  55521. }
  55522. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  55523. {
  55524. int ret = CRYPTOCB_UNAVAILABLE;
  55525. const char* privKeyFile = (const char*)ctx;
  55526. DerBuffer* pDer = NULL;
  55527. int keyFormat = 0;
  55528. if (info->algo_type == WC_ALGO_TYPE_PK) {
  55529. #ifdef DEBUG_WOLFSSL
  55530. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  55531. #endif
  55532. #ifndef NO_RSA
  55533. if (info->pk.type == WC_PK_TYPE_RSA) {
  55534. switch (info->pk.rsa.type) {
  55535. case RSA_PUBLIC_ENCRYPT:
  55536. case RSA_PUBLIC_DECRYPT:
  55537. /* perform software based RSA public op */
  55538. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55539. break;
  55540. case RSA_PRIVATE_ENCRYPT:
  55541. case RSA_PRIVATE_DECRYPT:
  55542. {
  55543. RsaKey key;
  55544. /* perform software based RSA private op */
  55545. #ifdef DEBUG_WOLFSSL
  55546. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55547. #endif
  55548. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55549. &keyFormat);
  55550. if (ret != 0) {
  55551. return ret;
  55552. }
  55553. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55554. if (ret == 0) {
  55555. word32 keyIdx = 0;
  55556. /* load RSA private key and perform private transform */
  55557. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55558. &key, pDer->length);
  55559. if (ret == 0) {
  55560. ret = wc_RsaFunction(
  55561. info->pk.rsa.in, info->pk.rsa.inLen,
  55562. info->pk.rsa.out, info->pk.rsa.outLen,
  55563. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55564. }
  55565. else {
  55566. /* if decode fails, then fall-back to software based crypto */
  55567. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55568. "key decode failed %d, falling back to "
  55569. "software\n", ret);
  55570. ret = CRYPTOCB_UNAVAILABLE;
  55571. }
  55572. wc_FreeRsaKey(&key);
  55573. }
  55574. wc_FreeDer(&pDer); pDer = NULL;
  55575. break;
  55576. }
  55577. }
  55578. #ifdef DEBUG_WOLFSSL
  55579. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55580. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55581. #endif
  55582. }
  55583. #endif /* !NO_RSA */
  55584. #ifdef HAVE_ECC
  55585. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55586. /* mark this key as ephemeral */
  55587. if (info->pk.eckg.key != NULL) {
  55588. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  55589. sizeof(info->pk.eckg.key->label));
  55590. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  55591. }
  55592. }
  55593. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  55594. ecc_key key;
  55595. /* perform software based ECC sign */
  55596. #ifdef DEBUG_WOLFSSL
  55597. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  55598. #endif
  55599. if (info->pk.eccsign.key != NULL &&
  55600. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  55601. /* this is an empheral key */
  55602. #ifdef DEBUG_WOLFSSL
  55603. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  55604. "ephemeral key\n");
  55605. #endif
  55606. return CRYPTOCB_UNAVAILABLE;
  55607. }
  55608. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55609. if (ret != 0) {
  55610. return ret;
  55611. }
  55612. ret = wc_ecc_init(&key);
  55613. if (ret == 0) {
  55614. word32 keyIdx = 0;
  55615. /* load ECC private key and perform private transform */
  55616. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  55617. &key, pDer->length);
  55618. if (ret == 0) {
  55619. ret = wc_ecc_sign_hash(
  55620. info->pk.eccsign.in, info->pk.eccsign.inlen,
  55621. info->pk.eccsign.out, info->pk.eccsign.outlen,
  55622. info->pk.eccsign.rng, &key);
  55623. }
  55624. else {
  55625. /* if decode fails, then fall-back to software based crypto */
  55626. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  55627. "decode failed %d, falling back to software\n", ret);
  55628. ret = CRYPTOCB_UNAVAILABLE;
  55629. }
  55630. wc_ecc_free(&key);
  55631. }
  55632. wc_FreeDer(&pDer); pDer = NULL;
  55633. #ifdef DEBUG_WOLFSSL
  55634. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  55635. ret, *info->pk.eccsign.outlen);
  55636. #endif
  55637. }
  55638. #endif /* HAVE_ECC */
  55639. #ifdef HAVE_ED25519
  55640. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  55641. ed25519_key key;
  55642. /* perform software based ED25519 sign */
  55643. #ifdef DEBUG_WOLFSSL
  55644. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  55645. #endif
  55646. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55647. if (ret != 0) {
  55648. return ret;
  55649. }
  55650. ret = wc_ed25519_init(&key);
  55651. if (ret == 0) {
  55652. word32 keyIdx = 0;
  55653. /* load ED25519 private key and perform private transform */
  55654. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  55655. &key, pDer->length);
  55656. if (ret == 0) {
  55657. /* calculate public key */
  55658. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  55659. if (ret == 0) {
  55660. key.pubKeySet = 1;
  55661. ret = wc_ed25519_sign_msg_ex(
  55662. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  55663. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  55664. &key, info->pk.ed25519sign.type,
  55665. info->pk.ed25519sign.context,
  55666. info->pk.ed25519sign.contextLen);
  55667. }
  55668. }
  55669. else {
  55670. /* if decode fails, then fall-back to software based crypto */
  55671. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  55672. "decode failed %d, falling back to software\n", ret);
  55673. ret = CRYPTOCB_UNAVAILABLE;
  55674. }
  55675. wc_ed25519_free(&key);
  55676. }
  55677. wc_FreeDer(&pDer); pDer = NULL;
  55678. #ifdef DEBUG_WOLFSSL
  55679. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  55680. ret, *info->pk.ed25519sign.outLen);
  55681. #endif
  55682. }
  55683. #endif /* HAVE_ED25519 */
  55684. }
  55685. (void)thisDevId;
  55686. (void)keyFormat;
  55687. return ret;
  55688. }
  55689. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55690. static int test_wc_CryptoCb_TLS(int tlsVer,
  55691. const char* cliCaPemFile, const char* cliCertPemFile,
  55692. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  55693. const char* svrCaPemFile, const char* svrCertPemFile,
  55694. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  55695. {
  55696. EXPECT_DECLS;
  55697. callback_functions client_cbf;
  55698. callback_functions server_cbf;
  55699. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55700. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55701. if (tlsVer == WOLFSSL_TLSV1_3) {
  55702. #ifdef WOLFSSL_TLS13
  55703. server_cbf.method = wolfTLSv1_3_server_method;
  55704. client_cbf.method = wolfTLSv1_3_client_method;
  55705. #endif
  55706. }
  55707. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55708. #ifndef WOLFSSL_NO_TLS12
  55709. server_cbf.method = wolfTLSv1_2_server_method;
  55710. client_cbf.method = wolfTLSv1_2_client_method;
  55711. #endif
  55712. }
  55713. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55714. #ifndef NO_OLD_TLS
  55715. server_cbf.method = wolfTLSv1_1_server_method;
  55716. client_cbf.method = wolfTLSv1_1_client_method;
  55717. #endif
  55718. }
  55719. else if (tlsVer == WOLFSSL_TLSV1) {
  55720. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55721. server_cbf.method = wolfTLSv1_server_method;
  55722. client_cbf.method = wolfTLSv1_client_method;
  55723. #endif
  55724. }
  55725. else if (tlsVer == WOLFSSL_SSLV3) {
  55726. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55727. defined(WOLFSSL_STATIC_RSA)
  55728. server_cbf.method = wolfSSLv3_server_method;
  55729. client_cbf.method = wolfSSLv3_client_method;
  55730. #endif
  55731. }
  55732. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55733. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55734. server_cbf.method = wolfDTLSv1_2_server_method;
  55735. client_cbf.method = wolfDTLSv1_2_client_method;
  55736. #endif
  55737. }
  55738. else if (tlsVer == WOLFSSL_DTLSV1) {
  55739. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55740. server_cbf.method = wolfDTLSv1_server_method;
  55741. client_cbf.method = wolfDTLSv1_client_method;
  55742. #endif
  55743. }
  55744. if (server_cbf.method == NULL) {
  55745. /* not enabled */
  55746. return TEST_SUCCESS;
  55747. }
  55748. /* Setup the keys for the TLS test */
  55749. client_cbf.certPemFile = cliCertPemFile;
  55750. client_cbf.keyPemFile = cliPubKeyPemFile;
  55751. client_cbf.caPemFile = cliCaPemFile;
  55752. server_cbf.certPemFile = svrCertPemFile;
  55753. server_cbf.keyPemFile = svrPubKeyPemFile;
  55754. server_cbf.caPemFile = svrCaPemFile;
  55755. /* Setup a crypto callback with pointer to private key file for testing */
  55756. client_cbf.devId = 1;
  55757. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  55758. (void*)cliPrivKeyPemFile);
  55759. server_cbf.devId = 2;
  55760. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  55761. (void*)svrPrivKeyPemFile);
  55762. /* Perform TLS server and client test */
  55763. /* First test is at WOLFSSL_CTX level */
  55764. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55765. /* Check for success */
  55766. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55767. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55768. if (EXPECT_SUCCESS()) {
  55769. /* Second test is a WOLFSSL object level */
  55770. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55771. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55772. }
  55773. /* Check for success */
  55774. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55775. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55776. /* Un register the devId's */
  55777. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  55778. client_cbf.devId = INVALID_DEVID;
  55779. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  55780. server_cbf.devId = INVALID_DEVID;
  55781. return EXPECT_RESULT();
  55782. }
  55783. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  55784. static int test_wc_CryptoCb(void)
  55785. {
  55786. EXPECT_DECLS;
  55787. #ifdef WOLF_CRYPTO_CB
  55788. /* TODO: Add crypto callback API tests */
  55789. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55790. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55791. int tlsVer;
  55792. #endif
  55793. #ifndef NO_RSA
  55794. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55795. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55796. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  55797. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  55798. TEST_SUCCESS);
  55799. }
  55800. #endif
  55801. #ifdef HAVE_ECC
  55802. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55803. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55804. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  55805. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  55806. TEST_SUCCESS);
  55807. }
  55808. #endif
  55809. #ifdef HAVE_ED25519
  55810. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55811. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55812. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  55813. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  55814. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  55815. TEST_SUCCESS);
  55816. }
  55817. #endif
  55818. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55819. #endif /* WOLF_CRYPTO_CB */
  55820. return EXPECT_RESULT();
  55821. }
  55822. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55823. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55824. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  55825. const char* cliCaPemFile, const char* cliCertPemFile,
  55826. const char* cliPrivKeyPemFile,
  55827. const char* svrCaPemFile, const char* svrCertPemFile,
  55828. const char* svrPrivKeyPemFile,
  55829. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  55830. {
  55831. EXPECT_DECLS;
  55832. callback_functions client_cbf;
  55833. callback_functions server_cbf;
  55834. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55835. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55836. if (tlsVer == WOLFSSL_TLSV1_3) {
  55837. #ifdef WOLFSSL_TLS13
  55838. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  55839. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  55840. #endif
  55841. }
  55842. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55843. #ifndef WOLFSSL_NO_TLS12
  55844. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  55845. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  55846. #endif
  55847. }
  55848. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55849. #ifndef NO_OLD_TLS
  55850. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  55851. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  55852. #endif
  55853. }
  55854. else if (tlsVer == WOLFSSL_TLSV1) {
  55855. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55856. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  55857. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  55858. #endif
  55859. }
  55860. else if (tlsVer == WOLFSSL_SSLV3) {
  55861. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55862. defined(WOLFSSL_STATIC_RSA)
  55863. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  55864. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  55865. #endif
  55866. }
  55867. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55868. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55869. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  55870. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  55871. #endif
  55872. }
  55873. else if (tlsVer == WOLFSSL_DTLSV1) {
  55874. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55875. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  55876. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  55877. #endif
  55878. }
  55879. if (server_cbf.method_ex == NULL) {
  55880. /* not enabled */
  55881. return TEST_SUCCESS;
  55882. }
  55883. /* Setup the keys for the TLS test */
  55884. client_cbf.certPemFile = cliCertPemFile;
  55885. client_cbf.keyPemFile = cliPrivKeyPemFile;
  55886. client_cbf.caPemFile = cliCaPemFile;
  55887. server_cbf.certPemFile = svrCertPemFile;
  55888. server_cbf.keyPemFile = svrPrivKeyPemFile;
  55889. server_cbf.caPemFile = svrCaPemFile;
  55890. client_cbf.mem = cliMem;
  55891. client_cbf.memSz = cliMemSz;
  55892. server_cbf.mem = svrMem;
  55893. server_cbf.memSz = svrMemSz;
  55894. client_cbf.devId = INVALID_DEVID;
  55895. server_cbf.devId = INVALID_DEVID;
  55896. /* Perform TLS server and client test */
  55897. /* First test is at WOLFSSL_CTX level */
  55898. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55899. /* Check for success */
  55900. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55901. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55902. if (EXPECT_SUCCESS()) {
  55903. /* Second test is a WOLFSSL object level */
  55904. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55905. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55906. }
  55907. /* Check for success */
  55908. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55909. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55910. return EXPECT_RESULT();
  55911. }
  55912. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  55913. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55914. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  55915. {
  55916. EXPECT_DECLS;
  55917. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  55918. WOLFSSL_MEM_STATS mem_stats;
  55919. WOLFSSL_MEM_CONN_STATS ssl_stats;
  55920. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  55921. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55922. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55923. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55924. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55925. #endif
  55926. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  55927. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  55928. /* this should fail because kMaxCtxClients == 2 */
  55929. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  55930. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  55931. #ifdef DEBUG_WOLFSSL
  55932. wolfSSL_PrintStatsConn(&ssl_stats);
  55933. #endif
  55934. (void)ssl_stats;
  55935. }
  55936. /* display collected statistics */
  55937. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  55938. #ifdef DEBUG_WOLFSSL
  55939. wolfSSL_PrintStats(&mem_stats);
  55940. #endif
  55941. (void)mem_stats;
  55942. }
  55943. wolfSSL_free(ssl1);
  55944. wolfSSL_free(ssl2);
  55945. return EXPECT_RESULT();
  55946. }
  55947. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55948. static int test_wolfSSL_CTX_StaticMemory(void)
  55949. {
  55950. EXPECT_DECLS;
  55951. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55952. wolfSSL_method_func method_func;
  55953. WOLFSSL_CTX* ctx;
  55954. const int kMaxCtxClients = 2;
  55955. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55956. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55957. int tlsVer;
  55958. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  55959. #endif
  55960. #endif
  55961. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  55962. #ifndef NO_WOLFSSL_SERVER
  55963. #ifndef WOLFSSL_NO_TLS12
  55964. method_func = wolfTLSv1_2_server_method_ex;
  55965. #else
  55966. method_func = wolfTLSv1_3_server_method_ex;
  55967. #endif
  55968. #else
  55969. #ifndef WOLFSSL_NO_TLS12
  55970. method_func = wolfTLSv1_2_client_method_ex;
  55971. #else
  55972. method_func = wolfTLSv1_3_client_method_ex;
  55973. #endif
  55974. #endif
  55975. /* Test creating CTX directly from static memory pool */
  55976. ctx = NULL;
  55977. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  55978. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55979. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55980. wolfSSL_CTX_free(ctx);
  55981. ctx = NULL;
  55982. /* Test for heap allocated CTX, then assigning static pool to it */
  55983. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  55984. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  55985. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55986. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55987. wolfSSL_CTX_free(ctx);
  55988. /* TLS Level Tests using static memory */
  55989. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55990. #ifndef NO_RSA
  55991. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55992. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55993. svrCertFile, cliCertFile, cliKeyFile,
  55994. cliCertFile, svrCertFile, svrKeyFile,
  55995. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55996. TEST_SUCCESS);
  55997. }
  55998. #endif
  55999. #ifdef HAVE_ECC
  56000. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56001. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56002. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  56003. cliEccCertFile, eccCertFile, eccKeyFile,
  56004. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56005. TEST_SUCCESS);
  56006. }
  56007. #endif
  56008. #ifdef HAVE_ED25519
  56009. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56010. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56011. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56012. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  56013. cliEdCertFile, edCertFile, edKeyFile,
  56014. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56015. TEST_SUCCESS);
  56016. }
  56017. #endif
  56018. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56019. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56020. return EXPECT_RESULT();
  56021. }
  56022. static int test_openssl_FIPS_drbg(void)
  56023. {
  56024. EXPECT_DECLS;
  56025. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  56026. DRBG_CTX* dctx = NULL;
  56027. byte data1[32], data2[32], zeroData[32];
  56028. byte testSeed[16];
  56029. size_t dlen = sizeof(data1);
  56030. int i;
  56031. XMEMSET(data1, 0, dlen);
  56032. XMEMSET(data2, 0, dlen);
  56033. XMEMSET(zeroData, 0, sizeof(zeroData));
  56034. for (i = 0; i < (int)sizeof(testSeed); i++) {
  56035. testSeed[i] = (byte)i;
  56036. }
  56037. ExpectNotNull(dctx = FIPS_get_default_drbg());
  56038. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  56039. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  56040. WOLFSSL_SUCCESS);
  56041. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  56042. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  56043. WOLFSSL_SUCCESS);
  56044. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56045. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  56046. WOLFSSL_SUCCESS);
  56047. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  56048. WOLFSSL_SUCCESS);
  56049. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56050. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  56051. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  56052. #ifndef HAVE_GLOBAL_RNG
  56053. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  56054. wolfSSL_FIPS_drbg_free(dctx);
  56055. #endif
  56056. #endif
  56057. return EXPECT_RESULT();
  56058. }
  56059. static int test_wolfSSL_FIPS_mode(void)
  56060. {
  56061. EXPECT_DECLS;
  56062. #if defined(OPENSSL_ALL)
  56063. #ifdef HAVE_FIPS
  56064. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  56065. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  56066. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  56067. #else
  56068. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  56069. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  56070. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  56071. #endif
  56072. #endif
  56073. return EXPECT_RESULT();
  56074. }
  56075. #ifdef WOLFSSL_DTLS
  56076. /* Prints out the current window */
  56077. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  56078. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56079. int i;
  56080. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  56081. word32 b = w[i];
  56082. int j;
  56083. /* Prints out a 32 bit binary number in big endian order */
  56084. for (j = 0; j < 32; j++, b <<= 1) {
  56085. if (b & (((word32)1) << 31))
  56086. fprintf(stderr, "1");
  56087. else
  56088. fprintf(stderr, "0");
  56089. }
  56090. fprintf(stderr, " ");
  56091. }
  56092. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  56093. #else
  56094. (void)h;
  56095. (void)l;
  56096. (void)w;
  56097. #endif
  56098. }
  56099. /* a - cur_hi
  56100. * b - cur_lo
  56101. * c - next_hi
  56102. * d - next_lo
  56103. * e - window
  56104. * f - expected next_hi
  56105. * g - expected next_lo
  56106. * h - expected window[1]
  56107. * i - expected window[0]
  56108. */
  56109. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  56110. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  56111. DUW_TEST_print_window_binary((a), (b), (e)); \
  56112. ExpectIntEQ((c), (f)); \
  56113. ExpectIntEQ((d), (g)); \
  56114. ExpectIntEQ((e)[1], (h)); \
  56115. ExpectIntEQ((e)[0], (i)); \
  56116. } while (0)
  56117. static int test_wolfSSL_DtlsUpdateWindow(void)
  56118. {
  56119. EXPECT_DECLS;
  56120. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  56121. word32 next_lo = 0;
  56122. word16 next_hi = 0;
  56123. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56124. fprintf(stderr, "\n");
  56125. #endif
  56126. XMEMSET(window, 0, sizeof window);
  56127. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  56128. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  56129. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  56130. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  56131. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  56132. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  56133. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  56134. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  56135. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  56136. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  56137. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  56138. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  56139. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  56140. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  56141. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  56142. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  56143. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  56144. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  56145. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  56146. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  56147. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  56148. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56149. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56150. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  56151. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  56152. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56153. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56154. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56155. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56156. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56157. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  56158. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  56159. return EXPECT_RESULT();
  56160. }
  56161. #endif /* WOLFSSL_DTLS */
  56162. #ifdef WOLFSSL_DTLS
  56163. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  56164. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  56165. {
  56166. DtlsMsg* cur;
  56167. static byte msg[100];
  56168. static byte msgInit = 0;
  56169. if (!msgInit) {
  56170. int i;
  56171. for (i = 0; i < 100; i++)
  56172. msg[i] = i + 1;
  56173. msgInit = 1;
  56174. }
  56175. /* Sanitize test parameters */
  56176. if (len > sizeof(msg))
  56177. return -1;
  56178. if (f_offset + f_len > sizeof(msg))
  56179. return -1;
  56180. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  56181. if (ssl->dtls_rx_msg_list == NULL)
  56182. return -100;
  56183. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  56184. return -200;
  56185. if (cur->fragBucketListCount != f_count)
  56186. return -300;
  56187. if (cur->ready != ready)
  56188. return -400;
  56189. if (cur->bytesReceived != bytesReceived)
  56190. return -500;
  56191. if (ready) {
  56192. if (cur->fragBucketList != NULL)
  56193. return -600;
  56194. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  56195. return -700;
  56196. }
  56197. else {
  56198. DtlsFragBucket* fb;
  56199. if (cur->fragBucketList == NULL)
  56200. return -800;
  56201. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  56202. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  56203. return -900;
  56204. }
  56205. }
  56206. return 0;
  56207. }
  56208. static int test_wolfSSL_DTLS_fragment_buckets(void)
  56209. {
  56210. EXPECT_DECLS;
  56211. WOLFSSL ssl[1];
  56212. XMEMSET(ssl, 0, sizeof(*ssl));
  56213. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  56214. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56215. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  56216. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  56217. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  56218. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  56219. /* Test all permutations of 3 regions */
  56220. /* 1 2 3 */
  56221. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56222. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  56223. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56224. /* 1 3 2 */
  56225. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56226. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  56227. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56228. /* 2 1 3 */
  56229. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56230. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  56231. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56232. /* 2 3 1 */
  56233. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56234. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  56235. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56236. /* 3 1 2 */
  56237. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56238. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  56239. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56240. /* 3 2 1 */
  56241. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56242. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  56243. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56244. /* Test overlapping regions */
  56245. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56246. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  56247. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  56248. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  56249. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  56250. /* Test overlapping multiple regions */
  56251. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56252. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  56253. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  56254. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  56255. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  56256. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  56257. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  56258. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  56259. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  56260. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  56261. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56262. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  56263. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  56264. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  56265. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  56266. ssl->dtls_rx_msg_list = NULL;
  56267. ssl->dtls_rx_msg_list_sz = 0;
  56268. return EXPECT_RESULT();
  56269. }
  56270. #endif
  56271. #if !defined(NO_FILESYSTEM) && \
  56272. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56273. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56274. static int test_wolfSSL_dtls_stateless2(void)
  56275. {
  56276. EXPECT_DECLS;
  56277. WOLFSSL *ssl_c = NULL;
  56278. WOLFSSL *ssl_c2 = NULL;
  56279. WOLFSSL *ssl_s = NULL;
  56280. struct test_memio_ctx test_ctx;
  56281. WOLFSSL_CTX *ctx_c = NULL;
  56282. WOLFSSL_CTX *ctx_s = NULL;
  56283. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56284. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56285. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56286. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56287. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56288. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56289. /* send CH */
  56290. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56291. (ssl_c2->error == WANT_READ));
  56292. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56293. (ssl_s->error == WANT_READ));
  56294. ExpectIntNE(test_ctx.c_len, 0);
  56295. /* consume HRR */
  56296. test_ctx.c_len = 0;
  56297. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56298. wolfSSL_free(ssl_c2);
  56299. wolfSSL_free(ssl_c);
  56300. wolfSSL_free(ssl_s);
  56301. wolfSSL_CTX_free(ctx_c);
  56302. wolfSSL_CTX_free(ctx_s);
  56303. return EXPECT_RESULT();
  56304. }
  56305. #ifdef HAVE_MAX_FRAGMENT
  56306. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  56307. {
  56308. EXPECT_DECLS;
  56309. WOLFSSL *ssl_c = NULL;
  56310. WOLFSSL *ssl_c2 = NULL;
  56311. WOLFSSL *ssl_s = NULL;
  56312. struct test_memio_ctx test_ctx;
  56313. WOLFSSL_CTX *ctx_c = NULL;
  56314. WOLFSSL_CTX *ctx_s = NULL;
  56315. word16 max_fragment = 0;
  56316. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56317. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56318. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56319. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56320. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  56321. WOLFSSL_SUCCESS);
  56322. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56323. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56324. if (ssl_s != NULL) {
  56325. max_fragment = ssl_s->max_fragment;
  56326. }
  56327. /* send CH */
  56328. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56329. (ssl_c2->error == WANT_READ));
  56330. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56331. (ssl_s->error == WANT_READ));
  56332. /* CH without cookie shouldn't change state */
  56333. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  56334. ExpectIntNE(test_ctx.c_len, 0);
  56335. /* consume HRR from buffer */
  56336. test_ctx.c_len = 0;
  56337. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56338. wolfSSL_free(ssl_c2);
  56339. wolfSSL_free(ssl_c);
  56340. wolfSSL_free(ssl_s);
  56341. wolfSSL_CTX_free(ctx_c);
  56342. wolfSSL_CTX_free(ctx_s);
  56343. return EXPECT_RESULT();
  56344. }
  56345. #endif /* HAVE_MAX_FRAGMENT */
  56346. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  56347. #define ROUNDS_WITH_HVR 4
  56348. #define ROUNDS_WITHOUT_HVR 2
  56349. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  56350. static int buf_is_hvr(const byte *data, int len)
  56351. {
  56352. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  56353. return 0;
  56354. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  56355. }
  56356. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  56357. {
  56358. EXPECT_DECLS;
  56359. struct test_memio_ctx test_ctx;
  56360. WOLFSSL_CTX *ctx_c = NULL;
  56361. WOLFSSL_CTX *ctx_s = NULL;
  56362. WOLFSSL *ssl_c = NULL;
  56363. WOLFSSL *ssl_s = NULL;
  56364. WOLFSSL_SESSION *sess = NULL;
  56365. int round_trips;
  56366. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56367. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  56368. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56369. #ifdef HAVE_SESSION_TICKET
  56370. if (useticket) {
  56371. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  56372. }
  56373. #endif
  56374. round_trips = ROUNDS_WITH_HVR;
  56375. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  56376. &round_trips), 0);
  56377. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  56378. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56379. wolfSSL_shutdown(ssl_c);
  56380. wolfSSL_shutdown(ssl_s);
  56381. wolfSSL_free(ssl_c);
  56382. ssl_c = NULL;
  56383. wolfSSL_free(ssl_s);
  56384. ssl_s = NULL;
  56385. test_ctx.c_len = test_ctx.s_len = 0;
  56386. /* make resumption invalid */
  56387. if (bad && (sess != NULL)) {
  56388. if (useticket) {
  56389. #ifdef HAVE_SESSION_TICKET
  56390. if (sess->ticket != NULL) {
  56391. sess->ticket[0] = !sess->ticket[0];
  56392. }
  56393. #endif /* HAVE_SESSION_TICKET */
  56394. }
  56395. else {
  56396. sess->sessionID[0] = !sess->sessionID[0];
  56397. }
  56398. }
  56399. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  56400. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  56401. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56402. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56403. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56404. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56405. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  56406. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56407. (ssl_c->error == WANT_READ));
  56408. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56409. (ssl_s->error == WANT_READ));
  56410. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56411. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56412. if (!useticket) {
  56413. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  56414. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  56415. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  56416. }
  56417. wolfSSL_SESSION_free(sess);
  56418. wolfSSL_free(ssl_c);
  56419. wolfSSL_free(ssl_s);
  56420. wolfSSL_CTX_free(ctx_c);
  56421. wolfSSL_CTX_free(ctx_s);
  56422. return EXPECT_RESULT();
  56423. }
  56424. static int test_wolfSSL_dtls_stateless_resume(void)
  56425. {
  56426. EXPECT_DECLS;
  56427. #ifdef HAVE_SESSION_TICKET
  56428. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  56429. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  56430. #endif /* HAVE_SESION_TICKET */
  56431. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  56432. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  56433. return EXPECT_RESULT();
  56434. }
  56435. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  56436. #if !defined(NO_OLD_TLS)
  56437. static int test_wolfSSL_dtls_stateless_downgrade(void)
  56438. {
  56439. EXPECT_DECLS;
  56440. WOLFSSL_CTX *ctx_c = NULL;
  56441. WOLFSSL_CTX *ctx_c2 = NULL;
  56442. WOLFSSL_CTX *ctx_s = NULL;
  56443. WOLFSSL *ssl_c = NULL;
  56444. WOLFSSL *ssl_c2 = NULL;
  56445. WOLFSSL *ssl_s = NULL;
  56446. struct test_memio_ctx test_ctx;
  56447. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56448. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56449. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56450. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  56451. WOLFSSL_SUCCESS);
  56452. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  56453. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  56454. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  56455. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  56456. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56457. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56458. /* send CH */
  56459. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56460. (ssl_c2->error == WANT_READ));
  56461. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56462. (ssl_s->error == WANT_READ));
  56463. ExpectIntNE(test_ctx.c_len, 0);
  56464. /* consume HRR */
  56465. test_ctx.c_len = 0;
  56466. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56467. wolfSSL_free(ssl_c2);
  56468. wolfSSL_free(ssl_c);
  56469. wolfSSL_free(ssl_s);
  56470. wolfSSL_CTX_free(ctx_c);
  56471. wolfSSL_CTX_free(ctx_c2);
  56472. wolfSSL_CTX_free(ctx_s);
  56473. return EXPECT_RESULT();
  56474. }
  56475. #endif /* !defined(NO_OLD_TLS) */
  56476. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56477. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  56478. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56479. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56480. static int test_WOLFSSL_dtls_version_alert(void)
  56481. {
  56482. EXPECT_DECLS;
  56483. struct test_memio_ctx test_ctx;
  56484. WOLFSSL_CTX *ctx_c = NULL;
  56485. WOLFSSL_CTX *ctx_s = NULL;
  56486. WOLFSSL *ssl_c = NULL;
  56487. WOLFSSL *ssl_s = NULL;
  56488. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56489. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56490. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  56491. /* client hello */
  56492. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56493. (ssl_c->error == WANT_READ));
  56494. /* hrr */
  56495. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56496. (ssl_s->error == WANT_READ));
  56497. /* client hello 1 */
  56498. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56499. (ssl_c->error == WANT_READ));
  56500. /* server hello */
  56501. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56502. (ssl_s->error == WANT_READ));
  56503. /* should fail */
  56504. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56505. (ssl_c->error == VERSION_ERROR));
  56506. /* shuould fail */
  56507. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56508. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  56509. wolfSSL_free(ssl_c);
  56510. wolfSSL_free(ssl_s);
  56511. wolfSSL_CTX_free(ctx_c);
  56512. wolfSSL_CTX_free(ctx_s);
  56513. return EXPECT_RESULT();
  56514. }
  56515. #else
  56516. static int test_WOLFSSL_dtls_version_alert(void)
  56517. {
  56518. return TEST_SKIPPED;
  56519. }
  56520. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56521. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56522. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  56523. */
  56524. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56525. && defined(WOLFSSL_TLS13) && \
  56526. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  56527. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56528. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56529. {
  56530. struct test_memio_ctx *test_ctx;
  56531. byte buf[2048];
  56532. int idx, sz;
  56533. word32 tmp;
  56534. int ret;
  56535. idx = 5; /* space for record header */
  56536. buf[idx] = session_ticket; /* type */
  56537. idx++;
  56538. tmp = OPAQUE32_LEN +
  56539. OPAQUE32_LEN +
  56540. OPAQUE8_LEN + nonceLength +
  56541. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56542. c32to24(tmp, buf + idx);
  56543. idx += OPAQUE24_LEN;
  56544. c32toa((word32)12345, buf+idx); /* lifetime */
  56545. idx += OPAQUE32_LEN;
  56546. c32toa((word32)12345, buf+idx); /* add */
  56547. idx += OPAQUE32_LEN;
  56548. buf[idx] = nonceLength; /* nonce length */
  56549. idx++;
  56550. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56551. idx += nonceLength;
  56552. tmp = 1; /* ticket len */
  56553. c16toa((word16)tmp, buf+idx);
  56554. idx += 2;
  56555. buf[idx] = 0xFF; /* ticket */
  56556. idx++;
  56557. tmp = 0; /* ext len */
  56558. c16toa((word16)tmp, buf+idx);
  56559. idx += 2;
  56560. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56561. handshake, 0, 0, 0);
  56562. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56563. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56564. return !(ret == sz);
  56565. }
  56566. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56567. {
  56568. int ret = 0;
  56569. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  56570. ret = -1;
  56571. }
  56572. else {
  56573. int i;
  56574. for (i = 0; i < len; i++) {
  56575. if (sess->ticketNonce.data[i] != len) {
  56576. ret = -1;
  56577. break;
  56578. }
  56579. }
  56580. }
  56581. return ret;
  56582. }
  56583. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56584. {
  56585. EXPECT_DECLS;
  56586. char *buf[1024];
  56587. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  56588. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  56589. (ssl_c->error == WANT_READ));
  56590. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  56591. return EXPECT_RESULT();
  56592. }
  56593. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56594. {
  56595. EXPECT_DECLS;
  56596. WOLFSSL_SESSION *sess = NULL;
  56597. WOLFSSL_SESSION *cached = NULL;
  56598. WOLFSSL_CTX *ctx = ssl_c->ctx;
  56599. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  56600. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56601. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  56602. NULL, ssl_c->options.side, 1,NULL), 0);
  56603. ExpectNotNull(cached = wolfSSL_SESSION_new());
  56604. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  56605. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  56606. wolfSSL_SESSION_free(cached);
  56607. wolfSSL_SESSION_free(sess);
  56608. return EXPECT_RESULT();
  56609. }
  56610. static int test_ticket_nonce_malloc(void)
  56611. {
  56612. EXPECT_DECLS;
  56613. struct test_memio_ctx test_ctx;
  56614. WOLFSSL_CTX *ctx_c = NULL;
  56615. WOLFSSL_CTX *ctx_s = NULL;
  56616. WOLFSSL *ssl_c = NULL;
  56617. WOLFSSL *ssl_s = NULL;
  56618. byte small;
  56619. byte medium;
  56620. byte big;
  56621. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56622. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56623. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  56624. /* will send ticket manually */
  56625. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  56626. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56627. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56628. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  56629. (ssl_s->options.handShakeDone == 0)) {
  56630. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  56631. (ssl_c->error == WANT_READ));
  56632. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  56633. (ssl_s->error == WANT_READ));
  56634. }
  56635. small = TLS13_TICKET_NONCE_STATIC_SZ;
  56636. medium = small + 20 <= 255 ? small + 20 : 255;
  56637. big = medium + 20 <= 255 ? small + 20 : 255;
  56638. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  56639. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  56640. ssl_c->session->ticketNonce.dataStatic);
  56641. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  56642. TEST_SUCCESS);
  56643. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  56644. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  56645. TEST_SUCCESS);
  56646. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  56647. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  56648. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  56649. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  56650. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  56651. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  56652. wolfSSL_free(ssl_c);
  56653. wolfSSL_free(ssl_s);
  56654. wolfSSL_CTX_free(ctx_c);
  56655. wolfSSL_CTX_free(ctx_s);
  56656. return EXPECT_RESULT();
  56657. }
  56658. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  56659. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  56660. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  56661. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56662. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  56663. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56664. static int test_ticket_ret_create(void)
  56665. {
  56666. EXPECT_DECLS;
  56667. WOLFSSL_CTX *ctx_c = NULL;
  56668. WOLFSSL_CTX *ctx_s = NULL;
  56669. WOLFSSL *ssl_c = NULL;
  56670. WOLFSSL *ssl_s = NULL;
  56671. byte ticket[SESSION_TICKET_LEN];
  56672. struct test_memio_ctx test_ctx;
  56673. WOLFSSL_SESSION *sess = NULL;
  56674. word16 ticketLen = 0;
  56675. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56676. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56677. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  56678. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56679. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56680. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  56681. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56682. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56683. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  56684. if (sess != NULL) {
  56685. ticketLen = sess->ticketLen;
  56686. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  56687. }
  56688. wolfSSL_free(ssl_c);
  56689. ssl_c = NULL;
  56690. wolfSSL_free(ssl_s);
  56691. ssl_s = NULL;
  56692. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  56693. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56694. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56695. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  56696. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56697. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56698. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  56699. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56700. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  56701. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  56702. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  56703. wolfSSL_SESSION_free(sess);
  56704. wolfSSL_free(ssl_c);
  56705. wolfSSL_free(ssl_s);
  56706. wolfSSL_CTX_free(ctx_c);
  56707. wolfSSL_CTX_free(ctx_s);
  56708. return EXPECT_RESULT();
  56709. }
  56710. #else
  56711. static int test_ticket_ret_create(void)
  56712. {
  56713. return TEST_SKIPPED;
  56714. }
  56715. #endif
  56716. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  56717. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  56718. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56719. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56720. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56721. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  56722. {
  56723. int ret;
  56724. WOLFSSL_SESSION* session = NULL;
  56725. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56726. if (!wolfSSL_is_server(ssl)) {
  56727. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56728. AssertNotNull(session);
  56729. }
  56730. do {
  56731. ret = wolfSSL_shutdown(ssl);
  56732. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56733. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56734. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56735. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56736. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56737. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56738. #endif
  56739. if (!wolfSSL_is_server(ssl)) {
  56740. /* client */
  56741. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56742. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56743. wolfSSL_set_session(ssl, session);
  56744. wolfSSL_SESSION_free(session);
  56745. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56746. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56747. }
  56748. else {
  56749. /* server */
  56750. /* Different ciphersuite so that the ticket will be invalidated based on
  56751. * the ciphersuite */
  56752. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  56753. WOLFSSL_SUCCESS);
  56754. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  56755. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56756. }
  56757. }
  56758. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  56759. {
  56760. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  56761. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56762. WOLFSSL_SUCCESS);
  56763. }
  56764. static int test_ticket_and_psk_mixing(void)
  56765. {
  56766. EXPECT_DECLS;
  56767. /* Test mixing tickets and regular PSK */
  56768. callback_functions client_cbs, server_cbs;
  56769. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56770. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56771. client_cbs.method = wolfTLSv1_3_client_method;
  56772. server_cbs.method = wolfTLSv1_3_server_method;
  56773. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  56774. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56775. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56776. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56777. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56778. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56779. return EXPECT_RESULT();
  56780. }
  56781. #else
  56782. static int test_ticket_and_psk_mixing(void)
  56783. {
  56784. return TEST_SKIPPED;
  56785. }
  56786. #endif
  56787. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56788. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56789. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56790. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56791. static int test_prioritize_psk_cb_called = FALSE;
  56792. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  56793. const char* identity, unsigned char* key, unsigned int key_max_len,
  56794. const char** ciphersuite)
  56795. {
  56796. test_prioritize_psk_cb_called = TRUE;
  56797. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  56798. }
  56799. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  56800. {
  56801. int ret;
  56802. WOLFSSL_SESSION* session = NULL;
  56803. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56804. if (!wolfSSL_is_server(ssl)) {
  56805. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56806. AssertNotNull(session);
  56807. }
  56808. do {
  56809. ret = wolfSSL_shutdown(ssl);
  56810. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56811. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56812. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56813. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  56814. * important. */
  56815. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56816. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56817. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56818. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56819. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56820. #endif
  56821. if (!wolfSSL_is_server(ssl)) {
  56822. /* client */
  56823. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56824. wolfSSL_set_session(ssl, session);
  56825. wolfSSL_SESSION_free(session);
  56826. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56827. }
  56828. else {
  56829. /* server */
  56830. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  56831. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56832. #ifdef WOLFSSL_PRIORITIZE_PSK
  56833. /* The ticket should be first tried with all ciphersuites and chosen */
  56834. AssertFalse(test_prioritize_psk_cb_called);
  56835. #else
  56836. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  56837. * callback that sets this var. */
  56838. AssertTrue(test_prioritize_psk_cb_called);
  56839. #endif
  56840. }
  56841. }
  56842. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  56843. {
  56844. if (!wolfSSL_is_server(ssl))
  56845. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56846. WOLFSSL_SUCCESS);
  56847. else
  56848. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56849. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56850. }
  56851. static int test_prioritize_psk(void)
  56852. {
  56853. EXPECT_DECLS;
  56854. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  56855. * of the PSK's will be followed instead of the ciphersuite. */
  56856. callback_functions client_cbs, server_cbs;
  56857. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56858. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56859. client_cbs.method = wolfTLSv1_3_client_method;
  56860. server_cbs.method = wolfTLSv1_3_server_method;
  56861. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56862. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56863. client_cbs.on_result = test_prioritize_psk_on_result;
  56864. server_cbs.on_result = test_prioritize_psk_on_result;
  56865. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56866. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56867. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56868. return EXPECT_RESULT();
  56869. }
  56870. #else
  56871. static int test_prioritize_psk(void)
  56872. {
  56873. return TEST_SKIPPED;
  56874. }
  56875. #endif
  56876. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  56877. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56878. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56879. !defined(WOLFSSL_NO_TLS12)
  56880. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  56881. {
  56882. EXPECT_DECLS;
  56883. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  56884. /* Set TLS 1.3 specific suite */
  56885. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  56886. return EXPECT_RESULT();
  56887. }
  56888. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56889. {
  56890. EXPECT_DECLS;
  56891. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  56892. * do a 1.2 connection. */
  56893. test_ssl_cbf client_cbs, server_cbs;
  56894. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56895. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56896. client_cbs.method = wolfTLSv1_2_client_method;
  56897. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  56898. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  56899. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56900. &server_cbs, NULL), TEST_SUCCESS);
  56901. return EXPECT_RESULT();
  56902. }
  56903. #else
  56904. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56905. {
  56906. return TEST_SKIPPED;
  56907. }
  56908. #endif
  56909. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  56910. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56911. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  56912. {
  56913. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56914. return TEST_SUCCESS;
  56915. }
  56916. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  56917. {
  56918. EXPECT_DECLS;
  56919. WOLFSSL_ALERT_HISTORY h;
  56920. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56921. ExpectIntEQ(h.last_rx.level, alert_fatal);
  56922. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  56923. return EXPECT_RESULT();
  56924. }
  56925. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56926. {
  56927. EXPECT_DECLS;
  56928. test_ssl_cbf client_cbs, server_cbs;
  56929. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56930. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56931. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  56932. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  56933. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  56934. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  56935. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  56936. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56937. &server_cbs, NULL), TEST_FAIL);
  56938. return EXPECT_RESULT();
  56939. }
  56940. #else
  56941. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56942. {
  56943. return TEST_SKIPPED;
  56944. }
  56945. #endif
  56946. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  56947. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56948. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56949. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56950. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56951. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56952. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  56953. static int test_TLS_13_ticket_different_ciphers_run = 0;
  56954. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  56955. {
  56956. EXPECT_DECLS;
  56957. switch (test_TLS_13_ticket_different_ciphers_run) {
  56958. case 0:
  56959. /* First run */
  56960. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56961. WOLFSSL_SUCCESS);
  56962. if (wolfSSL_is_server(ssl)) {
  56963. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  56964. wolfSSL_get_SSL_CTX(ssl));
  56965. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  56966. test_TLS_13_ticket_different_ciphers_ctx));
  56967. }
  56968. break;
  56969. case 1:
  56970. /* Second run */
  56971. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56972. "TLS13-AES128-GCM-SHA256"),
  56973. WOLFSSL_SUCCESS);
  56974. if (!wolfSSL_is_server(ssl)) {
  56975. ExpectIntEQ(wolfSSL_set_session(ssl,
  56976. test_TLS_13_ticket_different_ciphers_session),
  56977. WOLFSSL_SUCCESS);
  56978. }
  56979. break;
  56980. default:
  56981. /* Bad state? */
  56982. Fail(("Should not enter here"), ("Should not enter here"));
  56983. }
  56984. return EXPECT_RESULT();
  56985. }
  56986. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  56987. {
  56988. EXPECT_DECLS;
  56989. switch (test_TLS_13_ticket_different_ciphers_run) {
  56990. case 0:
  56991. /* First run */
  56992. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  56993. wolfSSL_get1_session(ssl));
  56994. break;
  56995. case 1:
  56996. /* Second run */
  56997. ExpectTrue(wolfSSL_session_reused(ssl));
  56998. break;
  56999. default:
  57000. /* Bad state? */
  57001. Fail(("Should not enter here"), ("Should not enter here"));
  57002. }
  57003. return EXPECT_RESULT();
  57004. }
  57005. static int test_TLS_13_ticket_different_ciphers(void)
  57006. {
  57007. EXPECT_DECLS;
  57008. /* Check that we handle the connection when the ticket doesn't match
  57009. * the first ciphersuite. */
  57010. test_ssl_cbf client_cbs, server_cbs;
  57011. struct test_params {
  57012. method_provider client_meth;
  57013. method_provider server_meth;
  57014. int doUdp;
  57015. } params[] = {
  57016. #ifdef WOLFSSL_DTLS13
  57017. /* Test that the stateless code handles sessions correctly */
  57018. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  57019. #endif
  57020. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  57021. };
  57022. size_t i;
  57023. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57024. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57025. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57026. test_TLS_13_ticket_different_ciphers_run = 0;
  57027. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  57028. client_cbs.method = params[i].client_meth;
  57029. server_cbs.method = params[i].server_meth;
  57030. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57031. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57032. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  57033. server_cbs.ticNoInit = 1;
  57034. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57035. &server_cbs, NULL), TEST_SUCCESS);
  57036. test_TLS_13_ticket_different_ciphers_run++;
  57037. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  57038. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57039. &server_cbs, NULL), TEST_SUCCESS);
  57040. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  57041. test_TLS_13_ticket_different_ciphers_session = NULL;
  57042. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  57043. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57044. }
  57045. return EXPECT_RESULT();
  57046. }
  57047. #else
  57048. static int test_TLS_13_ticket_different_ciphers(void)
  57049. {
  57050. return TEST_SKIPPED;
  57051. }
  57052. #endif
  57053. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  57054. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57055. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  57056. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  57057. byte test_extra_alerts_wrong_cs_sh[] = {
  57058. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  57059. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  57060. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  57061. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  57062. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  57063. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  57064. 0xbb, 0x07, 0x54, 0x1c,
  57065. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  57066. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  57067. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  57068. };
  57069. static int test_extra_alerts_wrong_cs(void)
  57070. {
  57071. EXPECT_DECLS;
  57072. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  57073. struct test_memio_ctx test_ctx;
  57074. WOLFSSL_CTX *ctx_c = NULL;
  57075. WOLFSSL_ALERT_HISTORY h;
  57076. WOLFSSL *ssl_c = NULL;
  57077. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57078. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57079. wolfTLSv1_2_client_method, NULL), 0);
  57080. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  57081. WOLFSSL_SUCCESS);
  57082. /* CH */
  57083. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57084. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57085. WOLFSSL_ERROR_WANT_READ);
  57086. /* consume CH */
  57087. test_ctx.s_len = 0;
  57088. /* inject SH */
  57089. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  57090. sizeof(test_extra_alerts_wrong_cs_sh));
  57091. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  57092. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57093. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57094. WOLFSSL_ERROR_WANT_READ);
  57095. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57096. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  57097. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57098. wolfSSL_free(ssl_c);
  57099. wolfSSL_CTX_free(ctx_c);
  57100. #endif
  57101. return EXPECT_RESULT();
  57102. }
  57103. #else
  57104. static int test_extra_alerts_wrong_cs(void)
  57105. {
  57106. return TEST_SKIPPED;
  57107. }
  57108. #endif
  57109. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  57110. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  57111. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  57112. {
  57113. tail_len -= msg_length;
  57114. XMEMMOVE(msg, msg + msg_length, tail_len);
  57115. *len = *len - msg_length;
  57116. }
  57117. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  57118. byte *found)
  57119. {
  57120. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  57121. const unsigned int _RECORD_HEADER_SZ = 5;
  57122. const int _change_cipher_hs = 55;
  57123. const int _change_cipher = 20;
  57124. const int _handshake = 22;
  57125. unsigned int tail_len;
  57126. byte *idx, *curr;
  57127. word8 currType;
  57128. word16 rLength;
  57129. word32 hLength;
  57130. idx = buf;
  57131. tail_len = *len;
  57132. *found = 0;
  57133. while (tail_len > _RECORD_HEADER_SZ) {
  57134. curr = idx;
  57135. currType = *idx;
  57136. ato16(idx + 3, &rLength);
  57137. idx += _RECORD_HEADER_SZ;
  57138. tail_len -= _RECORD_HEADER_SZ;
  57139. if (tail_len < rLength)
  57140. return -1;
  57141. if (type == _change_cipher_hs && currType == _change_cipher) {
  57142. if (rLength != 1)
  57143. return -1;
  57144. /* match */
  57145. test_remove_msg(curr, *len - (int)(curr - buf),
  57146. len, _RECORD_HEADER_SZ + 1);
  57147. *found = 1;
  57148. return 0;
  57149. }
  57150. if (currType != _handshake) {
  57151. idx += rLength;
  57152. tail_len -= rLength;
  57153. continue;
  57154. }
  57155. if (rLength < _HANDSHAKE_HEADER_SZ)
  57156. return -1;
  57157. currType = *idx;
  57158. ato24(idx+1, &hLength);
  57159. hLength += _HANDSHAKE_HEADER_SZ;
  57160. if (tail_len < hLength)
  57161. return -1;
  57162. if (currType != type) {
  57163. idx += hLength;
  57164. tail_len -= hLength;
  57165. continue;
  57166. }
  57167. /* match */
  57168. test_remove_msg(curr, *len - (int)(curr - buf), len,
  57169. hLength + _RECORD_HEADER_SZ);
  57170. *found = 1;
  57171. return 0;
  57172. }
  57173. /* not found */
  57174. return 0;
  57175. }
  57176. static int test_remove_hs_message(byte hs_message_type,
  57177. int extra_round, byte alert_type)
  57178. {
  57179. EXPECT_DECLS;
  57180. WOLFSSL_CTX *ctx_c = NULL;
  57181. WOLFSSL_CTX *ctx_s = NULL;
  57182. WOLFSSL *ssl_c = NULL;
  57183. WOLFSSL *ssl_s = NULL;
  57184. struct test_memio_ctx test_ctx;
  57185. WOLFSSL_ALERT_HISTORY h;
  57186. byte found = 0;
  57187. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57188. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57189. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57190. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57191. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57192. WOLFSSL_ERROR_WANT_READ);
  57193. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57194. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57195. WOLFSSL_ERROR_WANT_READ);
  57196. if (extra_round) {
  57197. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57198. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57199. WOLFSSL_ERROR_WANT_READ);
  57200. /* this will complete handshake from server side */
  57201. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57202. }
  57203. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  57204. &test_ctx.c_len, hs_message_type, &found), 0);
  57205. if (!found) {
  57206. wolfSSL_free(ssl_c);
  57207. wolfSSL_CTX_free(ctx_c);
  57208. wolfSSL_free(ssl_s);
  57209. wolfSSL_CTX_free(ctx_s);
  57210. return TEST_SKIPPED;
  57211. }
  57212. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57213. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57214. WOLFSSL_ERROR_WANT_READ);
  57215. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57216. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  57217. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57218. wolfSSL_free(ssl_c);
  57219. wolfSSL_CTX_free(ctx_c);
  57220. wolfSSL_free(ssl_s);
  57221. wolfSSL_CTX_free(ctx_s);
  57222. return EXPECT_RESULT();
  57223. }
  57224. static int test_extra_alerts_skip_hs(void)
  57225. {
  57226. EXPECT_DECLS;
  57227. const byte _server_key_exchange = 12;
  57228. const byte _server_hello = 2;
  57229. const byte _certificate = 11;
  57230. /* server_hello */
  57231. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  57232. unexpected_message), TEST_FAIL);
  57233. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  57234. 0xff), TEST_FAIL);
  57235. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  57236. unexpected_message), TEST_FAIL);
  57237. return EXPECT_RESULT();
  57238. }
  57239. #else
  57240. static int test_extra_alerts_skip_hs(void)
  57241. {
  57242. return TEST_SKIPPED;
  57243. }
  57244. #endif
  57245. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  57246. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  57247. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  57248. unsigned char* key, unsigned int key_max_len)
  57249. {
  57250. (void)ssl;
  57251. (void)id;
  57252. (void)key_max_len;
  57253. /* zero means error */
  57254. key[0] = 0x10;
  57255. return 1;
  57256. }
  57257. static int test_extra_alerts_bad_psk(void)
  57258. {
  57259. EXPECT_DECLS;
  57260. WOLFSSL_CTX *ctx_c = NULL;
  57261. WOLFSSL_CTX *ctx_s = NULL;
  57262. WOLFSSL *ssl_c = NULL;
  57263. WOLFSSL *ssl_s = NULL;
  57264. struct test_memio_ctx test_ctx;
  57265. WOLFSSL_ALERT_HISTORY h;
  57266. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57267. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57268. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57269. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  57270. WOLFSSL_SUCCESS);
  57271. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  57272. WOLFSSL_SUCCESS);
  57273. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  57274. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57275. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57276. WOLFSSL_ERROR_WANT_READ);
  57277. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57278. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  57279. WOLFSSL_ERROR_WANT_READ);
  57280. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57281. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57282. WOLFSSL_ERROR_WANT_READ);
  57283. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57284. ExpectIntEQ(h.last_tx.code, handshake_failure);
  57285. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57286. wolfSSL_free(ssl_c);
  57287. wolfSSL_CTX_free(ctx_c);
  57288. wolfSSL_free(ssl_s);
  57289. wolfSSL_CTX_free(ctx_s);
  57290. return EXPECT_RESULT();
  57291. }
  57292. #else
  57293. static int test_extra_alerts_bad_psk(void)
  57294. {
  57295. return TEST_SKIPPED;
  57296. }
  57297. #endif
  57298. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  57299. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57300. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  57301. int sz, void *ctx)
  57302. {
  57303. static int sentServerHello = FALSE;
  57304. if (!sentServerHello) {
  57305. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  57306. size_t i;
  57307. if (sz < (int)sizeof(renegExt))
  57308. return WOLFSSL_CBIO_ERR_GENERAL;
  57309. /* Remove SCR from ServerHello */
  57310. for (i = 0; i < sz - sizeof(renegExt); i++) {
  57311. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  57312. /* Found the extension. Change it to something unrecognized. */
  57313. buf[i+1] = 0x11;
  57314. break;
  57315. }
  57316. }
  57317. sentServerHello = TRUE;
  57318. }
  57319. return EmbedSend(ssl, buf, sz, ctx);
  57320. }
  57321. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  57322. {
  57323. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  57324. }
  57325. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  57326. {
  57327. WOLFSSL_ALERT_HISTORY h;
  57328. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57329. AssertIntEQ(h.last_rx.code, handshake_failure);
  57330. AssertIntEQ(h.last_rx.level, alert_fatal);
  57331. }
  57332. static int test_harden_no_secure_renegotiation(void)
  57333. {
  57334. EXPECT_DECLS;
  57335. callback_functions client_cbs, server_cbs;
  57336. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57337. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57338. client_cbs.method = wolfTLSv1_2_client_method;
  57339. server_cbs.method = wolfTLSv1_2_server_method;
  57340. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  57341. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  57342. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57343. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  57344. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  57345. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  57346. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  57347. server_cbs.last_err == FATAL_ERROR);
  57348. return EXPECT_RESULT();
  57349. }
  57350. #else
  57351. static int test_harden_no_secure_renegotiation(void)
  57352. {
  57353. return TEST_SKIPPED;
  57354. }
  57355. #endif
  57356. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57357. static int test_override_alt_cert_chain_cert_cb(int preverify,
  57358. WOLFSSL_X509_STORE_CTX* store)
  57359. {
  57360. fprintf(stderr, "preverify: %d\n", preverify);
  57361. fprintf(stderr, "store->error: %d\n", store->error);
  57362. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  57363. if (store->error == OCSP_INVALID_STATUS) {
  57364. fprintf(stderr, "Overriding OCSP error\n");
  57365. return 1;
  57366. }
  57367. #ifndef WOLFSSL_ALT_CERT_CHAINS
  57368. else if ((store->error == ASN_NO_SIGNER_E ||
  57369. store->error == ASN_SELF_SIGNED_E
  57370. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  57371. defined(HAVE_WEBSERVER)
  57372. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  57373. #endif
  57374. ) && store->error_depth == store->totalCerts - 1) {
  57375. fprintf(stderr, "Overriding no signer error only for root cert\n");
  57376. return 1;
  57377. }
  57378. #endif
  57379. else
  57380. return preverify;
  57381. }
  57382. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  57383. int urlSz, unsigned char* request, int requestSz,
  57384. unsigned char** response)
  57385. {
  57386. (void)ioCtx;
  57387. (void)url;
  57388. (void)urlSz;
  57389. (void)request;
  57390. (void)requestSz;
  57391. (void)response;
  57392. return -1;
  57393. }
  57394. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  57395. {
  57396. EXPECT_DECLS;
  57397. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  57398. test_override_alt_cert_chain_cert_cb);
  57399. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57400. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57401. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57402. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57403. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57404. WOLFSSL_SUCCESS);
  57405. return EXPECT_RESULT();
  57406. }
  57407. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  57408. {
  57409. EXPECT_DECLS;
  57410. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57411. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57412. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57413. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57414. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57415. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57416. WOLFSSL_SUCCESS);
  57417. return EXPECT_RESULT();
  57418. }
  57419. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  57420. {
  57421. EXPECT_DECLS;
  57422. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  57423. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  57424. return EXPECT_RESULT();
  57425. }
  57426. static int test_override_alt_cert_chain(void)
  57427. {
  57428. EXPECT_DECLS;
  57429. size_t i;
  57430. struct test_params {
  57431. ctx_cb client_ctx_cb;
  57432. ctx_cb server_ctx_cb;
  57433. int result;
  57434. } params[] = {
  57435. {test_override_alt_cert_chain_client_ctx_ready,
  57436. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  57437. {test_override_alt_cert_chain_client_ctx_ready2,
  57438. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  57439. };
  57440. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57441. test_ssl_cbf client_cbs, server_cbs;
  57442. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57443. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57444. fprintf(stderr, "test config: %d\n", (int)i);
  57445. client_cbs.ctx_ready = params[i].client_ctx_cb;
  57446. server_cbs.ctx_ready = params[i].server_ctx_cb;
  57447. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57448. &server_cbs, NULL), params[i].result);
  57449. ExpectIntEQ(client_cbs.return_code, params[i].result);
  57450. ExpectIntEQ(server_cbs.return_code, params[i].result);
  57451. }
  57452. return EXPECT_RESULT();
  57453. }
  57454. #else
  57455. static int test_override_alt_cert_chain(void)
  57456. {
  57457. return TEST_SKIPPED;
  57458. }
  57459. #endif
  57460. #if defined(HAVE_RPK)
  57461. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  57462. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  57463. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  57464. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  57465. {
  57466. int ret = WOLFSSL_SUCCESS;
  57467. (void)mode;
  57468. (void)strctx;
  57469. WOLFSSL_ENTER("MyRpkVerifyCb");
  57470. return ret;
  57471. }
  57472. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  57473. static WC_INLINE int test_rpk_memio_setup(
  57474. struct test_memio_ctx *ctx,
  57475. WOLFSSL_CTX **ctx_c,
  57476. WOLFSSL_CTX **ctx_s,
  57477. WOLFSSL **ssl_c,
  57478. WOLFSSL **ssl_s,
  57479. method_provider method_c,
  57480. method_provider method_s,
  57481. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  57482. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  57483. const char* pkey_c, int fmt_kc, /* client private key and format */
  57484. const char* pkey_s, int fmt_ks /* server private key and format */
  57485. )
  57486. {
  57487. int ret;
  57488. if (ctx_c != NULL && *ctx_c == NULL) {
  57489. *ctx_c = wolfSSL_CTX_new(method_c());
  57490. if (*ctx_c == NULL) {
  57491. return -1;
  57492. }
  57493. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  57494. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  57495. if (ret != WOLFSSL_SUCCESS) {
  57496. return -1;
  57497. }
  57498. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  57499. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  57500. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  57501. if (ret != WOLFSSL_SUCCESS) {
  57502. return -1;
  57503. }
  57504. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  57505. if (ret != WOLFSSL_SUCCESS) {
  57506. return -1;
  57507. }
  57508. }
  57509. if (ctx_s != NULL && *ctx_s == NULL) {
  57510. *ctx_s = wolfSSL_CTX_new(method_s());
  57511. if (*ctx_s == NULL) {
  57512. return -1;
  57513. }
  57514. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  57515. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  57516. if (ret != WOLFSSL_SUCCESS) {
  57517. return -1;
  57518. }
  57519. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  57520. if (ret != WOLFSSL_SUCCESS) {
  57521. return -1;
  57522. }
  57523. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  57524. if (ret != WOLFSSL_SUCCESS) {
  57525. return -1;
  57526. }
  57527. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  57528. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  57529. if (ctx->s_ciphers != NULL) {
  57530. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  57531. if (ret != WOLFSSL_SUCCESS) {
  57532. return -1;
  57533. }
  57534. }
  57535. }
  57536. if (ctx_c != NULL && ssl_c != NULL) {
  57537. *ssl_c = wolfSSL_new(*ctx_c);
  57538. if (*ssl_c == NULL) {
  57539. return -1;
  57540. }
  57541. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  57542. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  57543. }
  57544. if (ctx_s != NULL && ssl_s != NULL) {
  57545. *ssl_s = wolfSSL_new(*ctx_s);
  57546. if (*ssl_s == NULL) {
  57547. return -1;
  57548. }
  57549. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  57550. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  57551. #if !defined(NO_DH)
  57552. SetDH(*ssl_s);
  57553. #endif
  57554. }
  57555. return 0;
  57556. }
  57557. #endif /* HAVE_RPK */
  57558. static int test_rpk_set_xxx_cert_type(void)
  57559. {
  57560. EXPECT_DECLS;
  57561. #if defined(HAVE_RPK)
  57562. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  57563. WOLFSSL_CTX* ctx = NULL;
  57564. WOLFSSL* ssl = NULL;
  57565. int tp;
  57566. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  57567. ExpectNotNull(ctx);
  57568. ssl = wolfSSL_new(ctx);
  57569. ExpectNotNull(ssl);
  57570. /*--------------------------------------------*/
  57571. /* tests for wolfSSL_CTX_set_client_cert_type */
  57572. /*--------------------------------------------*/
  57573. /* illegal parameter test caces */
  57574. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  57575. MAX_CLIENT_CERT_TYPE_CNT),
  57576. BAD_FUNC_ARG);
  57577. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57578. sizeof(ctype)),
  57579. BAD_FUNC_ARG);
  57580. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57581. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57582. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57583. MAX_CLIENT_CERT_TYPE_CNT),
  57584. BAD_FUNC_ARG);
  57585. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57586. ctype[1] = 10; /* set unknown cert type */
  57587. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57588. MAX_CLIENT_CERT_TYPE_CNT),
  57589. BAD_FUNC_ARG);
  57590. /* pass larger type count */
  57591. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57592. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57593. ctype[2] = 1; /* pass unacceptable type count */
  57594. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57595. MAX_CLIENT_CERT_TYPE_CNT + 1),
  57596. BAD_FUNC_ARG);
  57597. /* should accept NULL for type buffer */
  57598. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  57599. MAX_CLIENT_CERT_TYPE_CNT),
  57600. WOLFSSL_SUCCESS);
  57601. /* should accept zero for type count */
  57602. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57603. 0),
  57604. WOLFSSL_SUCCESS);
  57605. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57606. MAX_CLIENT_CERT_TYPE_CNT),
  57607. WOLFSSL_SUCCESS);
  57608. /*--------------------------------------------*/
  57609. /* tests for wolfSSL_CTX_set_server_cert_type */
  57610. /*--------------------------------------------*/
  57611. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  57612. MAX_SERVER_CERT_TYPE_CNT),
  57613. BAD_FUNC_ARG);
  57614. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57615. sizeof(ctype)),
  57616. BAD_FUNC_ARG);
  57617. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57618. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57619. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57620. MAX_SERVER_CERT_TYPE_CNT),
  57621. BAD_FUNC_ARG);
  57622. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57623. ctype[1] = 10; /* set unknown cert type */
  57624. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57625. MAX_SERVER_CERT_TYPE_CNT),
  57626. BAD_FUNC_ARG);
  57627. /* pass larger type count */
  57628. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57629. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57630. ctype[2] = 1; /* pass unacceptable type count */
  57631. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57632. MAX_SERVER_CERT_TYPE_CNT + 1),
  57633. BAD_FUNC_ARG);
  57634. /* should accept NULL for type buffer */
  57635. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  57636. MAX_SERVER_CERT_TYPE_CNT),
  57637. WOLFSSL_SUCCESS);
  57638. /* should accept zero for type count */
  57639. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57640. 0),
  57641. WOLFSSL_SUCCESS);
  57642. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  57643. MAX_CLIENT_CERT_TYPE_CNT),
  57644. WOLFSSL_SUCCESS);
  57645. /*--------------------------------------------*/
  57646. /* tests for wolfSSL_set_client_cert_type */
  57647. /*--------------------------------------------*/
  57648. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  57649. MAX_CLIENT_CERT_TYPE_CNT),
  57650. BAD_FUNC_ARG);
  57651. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57652. sizeof(ctype)),
  57653. BAD_FUNC_ARG);
  57654. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57655. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57656. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57657. MAX_CLIENT_CERT_TYPE_CNT),
  57658. BAD_FUNC_ARG);
  57659. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57660. ctype[1] = 10; /* set unknown cert type */
  57661. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57662. MAX_CLIENT_CERT_TYPE_CNT),
  57663. BAD_FUNC_ARG);
  57664. /* pass larger type count */
  57665. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57666. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57667. ctype[2] = 1; /* pass unacceptable type count */
  57668. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57669. MAX_CLIENT_CERT_TYPE_CNT + 1),
  57670. BAD_FUNC_ARG);
  57671. /* should accept NULL for type buffer */
  57672. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  57673. MAX_CLIENT_CERT_TYPE_CNT),
  57674. WOLFSSL_SUCCESS);
  57675. /* should accept zero for type count */
  57676. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57677. 0),
  57678. WOLFSSL_SUCCESS);
  57679. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  57680. MAX_CLIENT_CERT_TYPE_CNT),
  57681. WOLFSSL_SUCCESS);
  57682. /*--------------------------------------------*/
  57683. /* tests for wolfSSL_CTX_set_server_cert_type */
  57684. /*--------------------------------------------*/
  57685. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  57686. MAX_SERVER_CERT_TYPE_CNT),
  57687. BAD_FUNC_ARG);
  57688. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57689. sizeof(ctype)),
  57690. BAD_FUNC_ARG);
  57691. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57692. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57693. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57694. MAX_SERVER_CERT_TYPE_CNT),
  57695. BAD_FUNC_ARG);
  57696. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57697. ctype[1] = 10; /* set unknown cert type */
  57698. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57699. MAX_SERVER_CERT_TYPE_CNT),
  57700. BAD_FUNC_ARG);
  57701. /* pass larger type count */
  57702. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  57703. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  57704. ctype[2] = 1; /* pass unacceptable type count */
  57705. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57706. MAX_SERVER_CERT_TYPE_CNT + 1),
  57707. BAD_FUNC_ARG);
  57708. /* should accept NULL for type buffer */
  57709. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  57710. MAX_SERVER_CERT_TYPE_CNT),
  57711. WOLFSSL_SUCCESS);
  57712. /* should accept zero for type count */
  57713. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57714. 0),
  57715. WOLFSSL_SUCCESS);
  57716. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  57717. MAX_SERVER_CERT_TYPE_CNT),
  57718. WOLFSSL_SUCCESS);
  57719. /*------------------------------------------------*/
  57720. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  57721. /*------------------------------------------------*/
  57722. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  57723. BAD_FUNC_ARG);
  57724. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  57725. BAD_FUNC_ARG);
  57726. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  57727. BAD_FUNC_ARG);
  57728. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  57729. BAD_FUNC_ARG);
  57730. /* clean up */
  57731. wolfSSL_free(ssl);
  57732. wolfSSL_CTX_free(ctx);
  57733. #endif
  57734. return EXPECT_RESULT();
  57735. }
  57736. static int test_tls13_rpk_handshake(void)
  57737. {
  57738. EXPECT_DECLS;
  57739. #if defined(HAVE_RPK)
  57740. int ret = 0;
  57741. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57742. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57743. struct test_memio_ctx test_ctx;
  57744. int err;
  57745. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  57746. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  57747. int typeCnt_c;
  57748. int typeCnt_s;
  57749. int tp;
  57750. (void)err;
  57751. (void)typeCnt_c;
  57752. (void)typeCnt_s;
  57753. (void)certType_c;
  57754. (void)certType_s;
  57755. /* TLS1.2
  57756. * Both client and server load x509 cert and start handshaking.
  57757. * Check no negotiation occurred.
  57758. */
  57759. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57760. ExpectIntEQ(
  57761. test_rpk_memio_setup(
  57762. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57763. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  57764. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57765. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57766. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57767. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  57768. , 0);
  57769. /* set client certificate type in client end */
  57770. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57771. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57772. typeCnt_c = 2;
  57773. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57774. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57775. typeCnt_s = 2;
  57776. /* both clien and server do not call client/server_cert_type APIs,
  57777. * expecting default settings works and no negotiation performed.
  57778. */
  57779. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57780. return TEST_FAIL;
  57781. /* confirm no negotiation occurred */
  57782. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57783. WOLFSSL_SUCCESS);
  57784. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57785. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57786. WOLFSSL_SUCCESS);
  57787. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57788. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57789. WOLFSSL_SUCCESS);
  57790. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57791. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57792. WOLFSSL_SUCCESS);
  57793. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57794. wolfSSL_free(ssl_c);
  57795. wolfSSL_CTX_free(ctx_c);
  57796. wolfSSL_free(ssl_s);
  57797. wolfSSL_CTX_free(ctx_s);
  57798. ssl_c = ssl_s = NULL;
  57799. ctx_c = ctx_s = NULL;
  57800. /* Both client and server load x509 cert and start handshaking.
  57801. * Check no negotiation occurred.
  57802. */
  57803. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57804. ExpectIntEQ(
  57805. test_rpk_memio_setup(
  57806. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57807. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57808. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57809. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57810. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57811. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57812. , 0);
  57813. /* set client certificate type in client end */
  57814. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57815. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57816. typeCnt_c = 2;
  57817. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57818. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57819. typeCnt_s = 2;
  57820. /* both clien and server do not call client/server_cert_type APIs,
  57821. * expecting default settings works and no negotiation performed.
  57822. */
  57823. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57824. return TEST_FAIL;
  57825. /* confirm no negotiation occurred */
  57826. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57827. WOLFSSL_SUCCESS);
  57828. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57829. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57830. WOLFSSL_SUCCESS);
  57831. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57832. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57833. WOLFSSL_SUCCESS);
  57834. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57835. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57836. WOLFSSL_SUCCESS);
  57837. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57838. wolfSSL_free(ssl_c);
  57839. wolfSSL_CTX_free(ctx_c);
  57840. wolfSSL_free(ssl_s);
  57841. wolfSSL_CTX_free(ctx_s);
  57842. ssl_c = ssl_s = NULL;
  57843. ctx_c = ctx_s = NULL;
  57844. /* Both client and server load RPK cert and start handshaking.
  57845. * Confirm negotiated cert types match as expected.
  57846. */
  57847. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57848. ExpectIntEQ(
  57849. test_rpk_memio_setup(
  57850. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57851. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57852. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57853. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57854. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57855. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57856. , 0);
  57857. /* set client certificate type in client end */
  57858. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57859. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57860. typeCnt_c = 2;
  57861. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57862. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57863. typeCnt_s = 2;
  57864. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57865. WOLFSSL_SUCCESS);
  57866. /* set server certificate type in client end */
  57867. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57868. WOLFSSL_SUCCESS);
  57869. /* set client certificate type in server end */
  57870. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57871. WOLFSSL_SUCCESS);
  57872. /* set server certificate type in server end */
  57873. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57874. WOLFSSL_SUCCESS);
  57875. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57876. return TEST_FAIL;
  57877. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57878. WOLFSSL_SUCCESS);
  57879. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57880. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57881. WOLFSSL_SUCCESS);
  57882. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57883. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57884. WOLFSSL_SUCCESS);
  57885. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57886. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57887. WOLFSSL_SUCCESS);
  57888. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57889. wolfSSL_free(ssl_c);
  57890. wolfSSL_CTX_free(ctx_c);
  57891. wolfSSL_free(ssl_s);
  57892. wolfSSL_CTX_free(ctx_s);
  57893. ssl_c = ssl_s = NULL;
  57894. ctx_c = ctx_s = NULL;
  57895. /* TLS1.2
  57896. * Both client and server load RPK cert and start handshaking.
  57897. * Confirm negotiated cert types match as expected.
  57898. */
  57899. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57900. ExpectIntEQ(
  57901. test_rpk_memio_setup(
  57902. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57903. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  57904. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57905. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57906. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57907. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57908. , 0);
  57909. /* set client certificate type in client end */
  57910. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57911. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57912. typeCnt_c = 2;
  57913. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57914. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57915. typeCnt_s = 2;
  57916. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57917. WOLFSSL_SUCCESS);
  57918. /* set server certificate type in client end */
  57919. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57920. WOLFSSL_SUCCESS);
  57921. /* set client certificate type in server end */
  57922. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57923. WOLFSSL_SUCCESS);
  57924. /* set server certificate type in server end */
  57925. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57926. WOLFSSL_SUCCESS);
  57927. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57928. return TEST_FAIL;
  57929. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57930. WOLFSSL_SUCCESS);
  57931. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57932. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57933. WOLFSSL_SUCCESS);
  57934. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57935. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57936. WOLFSSL_SUCCESS);
  57937. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57938. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57939. WOLFSSL_SUCCESS);
  57940. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57941. wolfSSL_free(ssl_c);
  57942. wolfSSL_CTX_free(ctx_c);
  57943. wolfSSL_free(ssl_s);
  57944. wolfSSL_CTX_free(ctx_s);
  57945. ssl_c = ssl_s = NULL;
  57946. ctx_c = ctx_s = NULL;
  57947. /* Both client and server load x509 cert.
  57948. * Have client call set_client_cert_type with both RPK and x509.
  57949. * This doesn't makes client add client cert type extension to ClientHello,
  57950. * since it does not load RPK cert actually.
  57951. */
  57952. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57953. ExpectIntEQ(
  57954. test_rpk_memio_setup(
  57955. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57956. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57957. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57958. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57959. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57960. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57961. , 0);
  57962. /* set client certificate type in client end */
  57963. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57964. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57965. typeCnt_c = 2;
  57966. /* client indicates both RPK and x509 certs are available but loaded RPK
  57967. * cert only. It does not have client add client-cert-type extension in CH.
  57968. */
  57969. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57970. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57971. typeCnt_c = 2;
  57972. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57973. WOLFSSL_SUCCESS);
  57974. /* client indicates both RPK and x509 certs are acceptable */
  57975. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57976. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57977. typeCnt_s = 2;
  57978. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57979. WOLFSSL_SUCCESS);
  57980. /* server indicates both RPK and x509 certs are acceptable */
  57981. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57982. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57983. typeCnt_c = 2;
  57984. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57985. WOLFSSL_SUCCESS);
  57986. /* server should indicate only RPK cert is available */
  57987. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57988. certType_s[1] = -1;
  57989. typeCnt_s = 1;
  57990. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57991. WOLFSSL_SUCCESS);
  57992. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57993. return TEST_FAIL;
  57994. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  57995. * be returned as cert type.
  57996. */
  57997. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57998. WOLFSSL_SUCCESS);
  57999. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58000. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58001. WOLFSSL_SUCCESS);
  58002. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58003. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58004. WOLFSSL_SUCCESS);
  58005. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58006. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58007. WOLFSSL_SUCCESS);
  58008. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58009. wolfSSL_free(ssl_c);
  58010. wolfSSL_CTX_free(ctx_c);
  58011. wolfSSL_free(ssl_s);
  58012. wolfSSL_CTX_free(ctx_s);
  58013. ssl_c = ssl_s = NULL;
  58014. ctx_c = ctx_s = NULL;
  58015. /* Have client load RPK cert and have server load x509 cert.
  58016. * Check the negotiation result from both ends.
  58017. */
  58018. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58019. ExpectIntEQ(
  58020. test_rpk_memio_setup(
  58021. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58022. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58023. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58024. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58025. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58026. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58027. , 0);
  58028. /* have client tell to use RPK cert */
  58029. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58030. certType_c[1] = -1;
  58031. typeCnt_c = 1;
  58032. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58033. WOLFSSL_SUCCESS);
  58034. /* have client tell to accept both RPK and x509 cert */
  58035. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58036. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58037. typeCnt_s = 2;
  58038. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58039. WOLFSSL_SUCCESS);
  58040. /* have server accept to both RPK and x509 cert */
  58041. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58042. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58043. typeCnt_c = 2;
  58044. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58045. WOLFSSL_SUCCESS);
  58046. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  58047. * end, expecting the default setting works.
  58048. */
  58049. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58050. return TEST_FAIL;
  58051. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58052. WOLFSSL_SUCCESS);
  58053. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58054. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58055. WOLFSSL_SUCCESS);
  58056. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58057. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58058. WOLFSSL_SUCCESS);
  58059. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58060. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58061. WOLFSSL_SUCCESS);
  58062. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58063. wolfSSL_free(ssl_c);
  58064. wolfSSL_CTX_free(ctx_c);
  58065. wolfSSL_free(ssl_s);
  58066. wolfSSL_CTX_free(ctx_s);
  58067. ssl_c = ssl_s = NULL;
  58068. ctx_c = ctx_s = NULL;
  58069. /* Have both client and server load RPK cert, however, have server
  58070. * indicate its cert type x509.
  58071. * Client is expected to detect the cert type mismatch then to send alert
  58072. * with "unsupported_certificate".
  58073. */
  58074. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58075. ExpectIntEQ(
  58076. test_rpk_memio_setup(
  58077. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58078. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58079. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58080. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  58081. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58082. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58083. , 0);
  58084. /* have client tell to use RPK cert */
  58085. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58086. certType_c[1] = -1;
  58087. typeCnt_c = 1;
  58088. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58089. WOLFSSL_SUCCESS);
  58090. /* have client tell to accept both RPK and x509 cert */
  58091. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58092. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58093. typeCnt_s = 2;
  58094. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58095. WOLFSSL_SUCCESS);
  58096. /* have server accept to both RPK and x509 cert */
  58097. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58098. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58099. typeCnt_c = 2;
  58100. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58101. WOLFSSL_SUCCESS);
  58102. /* have server tell to use x509 cert intentionally. This will bring
  58103. * certificate type mismatch in client side.
  58104. */
  58105. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58106. certType_s[1] = -1;
  58107. typeCnt_s = 1;
  58108. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58109. WOLFSSL_SUCCESS);
  58110. /* expect client detect cert type mismatch then send Alert */
  58111. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58112. if (ret != -1)
  58113. return TEST_FAIL;
  58114. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  58115. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58116. WOLFSSL_SUCCESS);
  58117. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58118. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58119. WOLFSSL_SUCCESS);
  58120. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58121. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58122. WOLFSSL_SUCCESS);
  58123. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58124. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58125. WOLFSSL_SUCCESS);
  58126. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58127. wolfSSL_free(ssl_c);
  58128. wolfSSL_CTX_free(ctx_c);
  58129. wolfSSL_free(ssl_s);
  58130. wolfSSL_CTX_free(ctx_s);
  58131. ssl_c = ssl_s = NULL;
  58132. ctx_c = ctx_s = NULL;
  58133. /* Have client load x509 cert and server load RPK cert,
  58134. * however, have client indicate its cert type RPK.
  58135. * Server is expected to detect the cert type mismatch then to send alert
  58136. * with "unsupported_certificate".
  58137. */
  58138. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58139. ExpectIntEQ(
  58140. test_rpk_memio_setup(
  58141. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58142. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58143. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58144. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58145. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58146. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58147. , 0);
  58148. /* have client tell to use RPK cert intentionally */
  58149. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58150. certType_c[1] = -1;
  58151. typeCnt_c = 1;
  58152. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58153. WOLFSSL_SUCCESS);
  58154. /* have client tell to accept both RPK and x509 cert */
  58155. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58156. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58157. typeCnt_s = 2;
  58158. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58159. WOLFSSL_SUCCESS);
  58160. /* have server accept to both RPK and x509 cert */
  58161. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58162. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58163. typeCnt_c = 2;
  58164. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58165. WOLFSSL_SUCCESS);
  58166. /* have server tell to use x509 cert intentionally. This will bring
  58167. * certificate type mismatch in client side.
  58168. */
  58169. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58170. certType_s[1] = -1;
  58171. typeCnt_s = 1;
  58172. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58173. WOLFSSL_SUCCESS);
  58174. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58175. /* expect server detect cert type mismatch then send Alert */
  58176. ExpectIntNE(ret, 0);
  58177. err = wolfSSL_get_error(ssl_c, ret);
  58178. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  58179. /* client did not load RPK cert actually, so negotiation did not happen */
  58180. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58181. WOLFSSL_SUCCESS);
  58182. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58183. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58184. WOLFSSL_SUCCESS);
  58185. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58186. /* client did not load RPK cert actually, so negotiation did not happen */
  58187. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58188. WOLFSSL_SUCCESS);
  58189. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58190. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58191. WOLFSSL_SUCCESS);
  58192. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58193. wolfSSL_free(ssl_c);
  58194. wolfSSL_CTX_free(ctx_c);
  58195. wolfSSL_free(ssl_s);
  58196. wolfSSL_CTX_free(ctx_s);
  58197. ssl_c = ssl_s = NULL;
  58198. ctx_c = ctx_s = NULL;
  58199. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58200. /* Both client and server load RPK cert and set certificate verify
  58201. * callbacks then start handshaking.
  58202. * Confirm both side can refer the peer's cert.
  58203. */
  58204. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58205. ExpectIntEQ(
  58206. test_rpk_memio_setup(
  58207. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58208. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58209. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58210. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58211. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58212. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58213. , 0);
  58214. /* set client certificate type in client end */
  58215. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58216. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58217. typeCnt_c = 2;
  58218. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58219. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58220. typeCnt_s = 2;
  58221. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58222. WOLFSSL_SUCCESS);
  58223. /* set server certificate type in client end */
  58224. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58225. WOLFSSL_SUCCESS);
  58226. /* set client certificate type in server end */
  58227. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58228. WOLFSSL_SUCCESS);
  58229. /* set server certificate type in server end */
  58230. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58231. WOLFSSL_SUCCESS);
  58232. /* set certificate verify callback to both client and server */
  58233. int isServer = 0;
  58234. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58235. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58236. isServer = 1;
  58237. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58238. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58239. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58240. if (ret != 0)
  58241. return TEST_FAIL;
  58242. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58243. WOLFSSL_SUCCESS);
  58244. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58245. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58246. WOLFSSL_SUCCESS);
  58247. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58248. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58249. WOLFSSL_SUCCESS);
  58250. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58251. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58252. WOLFSSL_SUCCESS);
  58253. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58254. wolfSSL_free(ssl_c);
  58255. wolfSSL_CTX_free(ctx_c);
  58256. wolfSSL_free(ssl_s);
  58257. wolfSSL_CTX_free(ctx_s);
  58258. ssl_c = ssl_s = NULL;
  58259. ctx_c = ctx_s = NULL;
  58260. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58261. #endif /* HAVE_RPK */
  58262. return EXPECT_RESULT();
  58263. }
  58264. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58265. static int test_dtls13_bad_epoch_ch(void)
  58266. {
  58267. EXPECT_DECLS;
  58268. WOLFSSL_CTX *ctx_c = NULL;
  58269. WOLFSSL_CTX *ctx_s = NULL;
  58270. WOLFSSL *ssl_c = NULL;
  58271. WOLFSSL *ssl_s = NULL;
  58272. struct test_memio_ctx test_ctx;
  58273. const int EPOCH_OFF = 3;
  58274. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58275. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58276. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58277. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  58278. * with just one message */
  58279. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  58280. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58281. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58282. WOLFSSL_ERROR_WANT_READ);
  58283. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  58284. /* first CH should use epoch 0x0 */
  58285. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  58286. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  58287. /* change epoch to 2 */
  58288. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  58289. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58290. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58291. WOLFSSL_ERROR_WANT_READ);
  58292. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  58293. /* resend the CH */
  58294. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58295. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58296. wolfSSL_free(ssl_c);
  58297. wolfSSL_CTX_free(ctx_c);
  58298. wolfSSL_free(ssl_s);
  58299. wolfSSL_CTX_free(ctx_s);
  58300. return EXPECT_RESULT();
  58301. }
  58302. #else
  58303. static int test_dtls13_bad_epoch_ch(void)
  58304. {
  58305. return TEST_SKIPPED;
  58306. }
  58307. #endif
  58308. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  58309. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  58310. {
  58311. EXPECT_DECLS;
  58312. WOLFSSL_SESSION *sess = NULL;
  58313. /* Setup the session to avoid errors */
  58314. ssl->session->timeout = -1;
  58315. ssl->session->side = WOLFSSL_CLIENT_END;
  58316. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  58317. defined(HAVE_SESSION_TICKET))
  58318. ssl->session->version = ssl->version;
  58319. #endif
  58320. /* Force a short session ID to be sent */
  58321. ssl->session->sessionIDSz = 4;
  58322. #ifndef NO_SESSION_CACHE_REF
  58323. /* Allow the client cache to be used */
  58324. ssl->session->idLen = 4;
  58325. #endif
  58326. ssl->session->isSetup = 1;
  58327. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  58328. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  58329. return EXPECT_RESULT();
  58330. }
  58331. static int test_short_session_id(void)
  58332. {
  58333. EXPECT_DECLS;
  58334. test_ssl_cbf client_cbf;
  58335. test_ssl_cbf server_cbf;
  58336. size_t i;
  58337. struct {
  58338. method_provider client_meth;
  58339. method_provider server_meth;
  58340. const char* tls_version;
  58341. } params[] = {
  58342. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  58343. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  58344. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  58345. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  58346. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  58347. #ifdef WOLFSSL_DTLS13
  58348. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  58349. #endif
  58350. #endif
  58351. #ifndef WOLFSSL_NO_TLS12
  58352. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  58353. #ifdef WOLFSSL_DTLS
  58354. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  58355. #endif
  58356. #endif
  58357. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  58358. !defined(NO_DES3))
  58359. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  58360. #ifdef WOLFSSL_DTLS
  58361. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  58362. #endif
  58363. #endif
  58364. };
  58365. fprintf(stderr, "\n");
  58366. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  58367. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58368. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58369. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  58370. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  58371. client_cbf.method = params[i].client_meth;
  58372. server_cbf.method = params[i].server_meth;
  58373. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  58374. &server_cbf, NULL), TEST_SUCCESS);
  58375. }
  58376. return EXPECT_RESULT();
  58377. }
  58378. #else
  58379. static int test_short_session_id(void)
  58380. {
  58381. return TEST_SKIPPED;
  58382. }
  58383. #endif
  58384. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  58385. && defined(WOLFSSL_DTLS13)
  58386. static byte* test_find_string(const char *string,
  58387. byte *buf, int buf_size)
  58388. {
  58389. int string_size, i;
  58390. string_size = (int)XSTRLEN(string);
  58391. for (i = 0; i < buf_size - string_size - 1; i++) {
  58392. if (XSTRCMP((char*)&buf[i], string) == 0)
  58393. return &buf[i];
  58394. }
  58395. return NULL;
  58396. }
  58397. static int test_wolfSSL_dtls13_null_cipher(void)
  58398. {
  58399. EXPECT_DECLS;
  58400. WOLFSSL_CTX *ctx_c = NULL;
  58401. WOLFSSL_CTX *ctx_s = NULL;
  58402. WOLFSSL *ssl_c = NULL;
  58403. WOLFSSL *ssl_s = NULL;
  58404. struct test_memio_ctx test_ctx;
  58405. const char *test_str = "test";
  58406. int test_str_size;
  58407. byte buf[255], *ptr = NULL;
  58408. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58409. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  58410. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58411. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58412. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58413. test_str_size = XSTRLEN("test") + 1;
  58414. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58415. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  58416. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  58417. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58418. /* check that the packet was sent cleartext */
  58419. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  58420. test_ctx.s_len));
  58421. if (ptr != NULL) {
  58422. /* modify the message */
  58423. *ptr = 'H';
  58424. /* bad messages should be ignored in DTLS */
  58425. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  58426. ExpectIntEQ(ssl_s->error, WANT_READ);
  58427. }
  58428. wolfSSL_free(ssl_c);
  58429. wolfSSL_free(ssl_s);
  58430. wolfSSL_CTX_free(ctx_c);
  58431. wolfSSL_CTX_free(ctx_s);
  58432. return TEST_SUCCESS;
  58433. }
  58434. #else
  58435. static int test_wolfSSL_dtls13_null_cipher(void)
  58436. {
  58437. return TEST_SKIPPED;
  58438. }
  58439. #endif
  58440. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58441. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58442. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  58443. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  58444. {
  58445. SOCKADDR_S peer;
  58446. XSOCKLENT len;
  58447. int ret;
  58448. XMEMSET((byte*)&peer, 0, sizeof(peer));
  58449. len = sizeof(peer);
  58450. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  58451. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  58452. return -1;
  58453. switch (peer.ss_family) {
  58454. #ifdef WOLFSSL_IPV6
  58455. case WOLFSSL_IP6: {
  58456. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  58457. break;
  58458. }
  58459. #endif /* WOLFSSL_IPV6 */
  58460. case WOLFSSL_IP4:
  58461. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  58462. break;
  58463. default:
  58464. return -1;
  58465. }
  58466. return 0;
  58467. }
  58468. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58469. {
  58470. char buf[1] = {'t'};
  58471. SOCKADDR_IN_T addr;
  58472. int sock_fd;
  58473. word16 port;
  58474. int err;
  58475. (void)ssl;
  58476. (void)ctx;
  58477. if (ssl == NULL)
  58478. return -1;
  58479. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  58480. if (err != 0)
  58481. return -1;
  58482. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  58483. if (sock_fd == -1)
  58484. return -1;
  58485. build_addr(&addr, wolfSSLIP, port, 1, 0);
  58486. /* send a packet to the server. Being another socket, the kernel will ensure
  58487. * the source port will be different. */
  58488. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  58489. sizeof(addr));
  58490. close(sock_fd);
  58491. if (err == -1)
  58492. return -1;
  58493. return 0;
  58494. }
  58495. /* setup a SSL session but just after the handshake send a packet to the server
  58496. * with a source address different than the one of the connected client. The I/O
  58497. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  58498. * is done in test_dtls_msg_from_other_peer_cb */
  58499. static int test_dtls_msg_from_other_peer(void)
  58500. {
  58501. EXPECT_DECLS;
  58502. callback_functions client_cbs;
  58503. callback_functions server_cbs;
  58504. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  58505. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  58506. client_cbs.method = wolfDTLSv1_2_client_method;
  58507. server_cbs.method = wolfDTLSv1_2_server_method;
  58508. client_cbs.doUdp = 1;
  58509. server_cbs.doUdp = 1;
  58510. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  58511. test_dtls_msg_from_other_peer_cb);
  58512. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  58513. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  58514. return EXPECT_RESULT();
  58515. }
  58516. #else
  58517. static int test_dtls_msg_from_other_peer(void)
  58518. {
  58519. return TEST_SKIPPED;
  58520. }
  58521. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58522. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58523. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  58524. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  58525. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58526. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  58527. static int test_dtls_ipv6_check(void)
  58528. {
  58529. EXPECT_DECLS;
  58530. WOLFSSL_CTX *ctx_c = NULL;
  58531. WOLFSSL_CTX *ctx_s = NULL;
  58532. WOLFSSL *ssl_c = NULL;
  58533. WOLFSSL *ssl_s = NULL;
  58534. SOCKADDR_IN fake_addr6;
  58535. int sockfd = -1;
  58536. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  58537. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58538. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  58539. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  58540. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58541. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  58542. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58543. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58544. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  58545. /* mimic a sockaddr_in6 struct, this way we can't test without
  58546. * WOLFSSL_IPV6 */
  58547. fake_addr6.sin_family = WOLFSSL_IP6;
  58548. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  58549. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58550. /* can't return error here, as the peer is opaque for wolfssl library at
  58551. * this point */
  58552. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  58553. WOLFSSL_SUCCESS);
  58554. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  58555. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  58556. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58557. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  58558. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  58559. WOLFSSL_SUCCESS);
  58560. /* reuse the socket */
  58561. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58562. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  58563. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58564. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  58565. if (sockfd != -1)
  58566. close(sockfd);
  58567. wolfSSL_free(ssl_c);
  58568. wolfSSL_CTX_free(ctx_c);
  58569. wolfSSL_free(ssl_s);
  58570. wolfSSL_CTX_free(ctx_s);
  58571. return EXPECT_RESULT();
  58572. }
  58573. #else
  58574. static int test_dtls_ipv6_check(void)
  58575. {
  58576. return TEST_SKIPPED;
  58577. }
  58578. #endif
  58579. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58580. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58581. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  58582. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  58583. {
  58584. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58585. }
  58586. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  58587. {
  58588. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  58589. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  58590. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  58591. }
  58592. else {
  58593. char testMsg[] = "Message after SCR";
  58594. char msgBuf[sizeof(testMsg)];
  58595. int ret;
  58596. if (!wolfSSL_is_server(ssl)) {
  58597. AssertIntEQ(WOLFSSL_SUCCESS,
  58598. wolfSSL_set_session(ssl,
  58599. test_wolfSSL_SCR_after_resumption_session));
  58600. }
  58601. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58602. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58603. sizeof(testMsg));
  58604. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58605. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  58606. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58607. AssertIntEQ(ret, sizeof(msgBuf));
  58608. }
  58609. }
  58610. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  58611. {
  58612. AssertIntEQ(WOLFSSL_SUCCESS,
  58613. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  58614. }
  58615. static int test_wolfSSL_SCR_after_resumption(void)
  58616. {
  58617. EXPECT_DECLS;
  58618. callback_functions func_cb_client;
  58619. callback_functions func_cb_server;
  58620. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58621. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58622. func_cb_client.method = wolfTLSv1_2_client_method;
  58623. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  58624. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  58625. func_cb_server.method = wolfTLSv1_2_server_method;
  58626. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  58627. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58628. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58629. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58630. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  58631. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  58632. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58633. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58634. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58635. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  58636. return EXPECT_RESULT();
  58637. }
  58638. #else
  58639. static int test_wolfSSL_SCR_after_resumption(void)
  58640. {
  58641. return TEST_SKIPPED;
  58642. }
  58643. #endif
  58644. static int test_wolfSSL_configure_args(void)
  58645. {
  58646. EXPECT_DECLS;
  58647. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  58648. ExpectNotNull(wolfSSL_configure_args());
  58649. #endif
  58650. return EXPECT_RESULT();
  58651. }
  58652. static int test_dtls_no_extensions(void)
  58653. {
  58654. EXPECT_DECLS;
  58655. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  58656. !defined(WOLFSSL_NO_TLS12)
  58657. WOLFSSL *ssl_s = NULL;
  58658. WOLFSSL_CTX *ctx_s = NULL;
  58659. struct test_memio_ctx test_ctx;
  58660. const byte chNoExtensions[] = {
  58661. /* Handshake type */
  58662. 0x16,
  58663. /* Version */
  58664. 0xfe, 0xff,
  58665. /* Epoch */
  58666. 0x00, 0x00,
  58667. /* Seq number */
  58668. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58669. /* Length */
  58670. 0x00, 0x40,
  58671. /* CH type */
  58672. 0x01,
  58673. /* Length */
  58674. 0x00, 0x00, 0x34,
  58675. /* Msg Seq */
  58676. 0x00, 0x00,
  58677. /* Frag offset */
  58678. 0x00, 0x00, 0x00,
  58679. /* Frag length */
  58680. 0x00, 0x00, 0x34,
  58681. /* Version */
  58682. 0xfe, 0xff,
  58683. /* Random */
  58684. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  58685. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  58686. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  58687. /* Session Length */
  58688. 0x00,
  58689. /* Cookie Length */
  58690. 0x00,
  58691. /* CS Length */
  58692. 0x00, 0x0c,
  58693. /* CS */
  58694. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  58695. /* Comp Meths Length */
  58696. 0x01,
  58697. /* Comp Meths */
  58698. 0x00
  58699. /* And finally... no extensions */
  58700. };
  58701. int i;
  58702. #ifdef OPENSSL_EXTRA
  58703. int repeats = 2;
  58704. #else
  58705. int repeats = 1;
  58706. #endif
  58707. for (i = 0; i < repeats; i++) {
  58708. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58709. ssl_s = NULL;
  58710. ctx_s = NULL;
  58711. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  58712. NULL, wolfDTLS_server_method), 0);
  58713. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  58714. test_ctx.s_len = sizeof(chNoExtensions);
  58715. #ifdef OPENSSL_EXTRA
  58716. if (i > 0) {
  58717. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  58718. WOLFSSL_SUCCESS);
  58719. }
  58720. #endif
  58721. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  58722. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58723. /* Expecting a handshake msg. Either HVR or SH. */
  58724. ExpectIntGT(test_ctx.c_len, 0);
  58725. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  58726. wolfSSL_free(ssl_s);
  58727. wolfSSL_CTX_free(ctx_s);
  58728. }
  58729. #endif
  58730. return EXPECT_RESULT();
  58731. }
  58732. static int test_TLSX_CA_NAMES_bad_extension(void)
  58733. {
  58734. EXPECT_DECLS;
  58735. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58736. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  58737. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  58738. defined(HAVE_NULL_CIPHER)
  58739. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  58740. * parse the CA Names extension. Otherwise it will return other non-related
  58741. * errors. If CA Names will be parsed in more configurations, that should
  58742. * be reflected in the macro guard above. */
  58743. WOLFSSL *ssl_c = NULL;
  58744. WOLFSSL_CTX *ctx_c = NULL;
  58745. struct test_memio_ctx test_ctx;
  58746. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  58747. const byte shBadCaNamesExt[] = {
  58748. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  58749. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  58750. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  58751. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  58752. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  58753. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  58754. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  58755. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  58756. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  58757. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  58758. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  58759. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  58760. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  58761. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  58762. };
  58763. const byte shBadCaNamesExt2[] = {
  58764. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  58765. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  58766. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  58767. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  58768. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  58769. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  58770. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  58771. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  58772. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  58773. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  58774. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  58775. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  58776. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  58777. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  58778. };
  58779. int i = 0;
  58780. for (i = 0; i < 2; i++) {
  58781. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58782. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  58783. wolfTLSv1_3_client_method, NULL), 0);
  58784. switch (i) {
  58785. case 0:
  58786. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  58787. sizeof(shBadCaNamesExt));
  58788. test_ctx.c_len = sizeof(shBadCaNamesExt);
  58789. break;
  58790. case 1:
  58791. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  58792. sizeof(shBadCaNamesExt2));
  58793. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  58794. break;
  58795. }
  58796. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  58797. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  58798. wolfSSL_free(ssl_c);
  58799. ssl_c = NULL;
  58800. wolfSSL_CTX_free(ctx_c);
  58801. ctx_c = NULL;
  58802. }
  58803. #endif
  58804. return EXPECT_RESULT();
  58805. }
  58806. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58807. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58808. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  58809. {
  58810. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58811. WOLFSSL_SUCCESS);
  58812. }
  58813. static int test_dtls_1_0_hvr_downgrade(void)
  58814. {
  58815. EXPECT_DECLS;
  58816. callback_functions func_cb_client;
  58817. callback_functions func_cb_server;
  58818. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58819. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58820. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58821. func_cb_client.method = wolfDTLS_client_method;
  58822. func_cb_server.method = wolfDTLSv1_2_server_method;
  58823. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  58824. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58825. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58826. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58827. return EXPECT_RESULT();
  58828. }
  58829. #else
  58830. static int test_dtls_1_0_hvr_downgrade(void)
  58831. {
  58832. EXPECT_DECLS;
  58833. return EXPECT_RESULT();
  58834. }
  58835. #endif
  58836. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  58837. defined(HAVE_SESSION_TICKET)
  58838. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  58839. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  58840. {
  58841. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  58842. AssertNotNull(test_session_ticket_no_id_session);
  58843. }
  58844. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  58845. {
  58846. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  58847. }
  58848. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  58849. {
  58850. test_session_ticket_no_id_session->sessionIDSz = 0;
  58851. AssertIntEQ(WOLFSSL_SUCCESS,
  58852. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  58853. }
  58854. static int test_session_ticket_no_id(void)
  58855. {
  58856. /* We are testing an expired (invalid crypto context in out case since the
  58857. * ctx changes) session ticket being sent with the session ID being 0
  58858. * length. */
  58859. EXPECT_DECLS;
  58860. callback_functions func_cb_client;
  58861. callback_functions func_cb_server;
  58862. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58863. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58864. func_cb_client.method = wolfTLSv1_2_client_method;
  58865. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58866. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  58867. func_cb_server.method = wolfTLSv1_2_server_method;
  58868. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58869. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58870. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58871. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58872. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58873. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58874. func_cb_client.method = wolfTLSv1_2_client_method;
  58875. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58876. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  58877. func_cb_server.method = wolfTLSv1_2_server_method;
  58878. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58879. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58880. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58881. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58882. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  58883. return EXPECT_RESULT();
  58884. }
  58885. #else
  58886. static int test_session_ticket_no_id(void)
  58887. {
  58888. EXPECT_DECLS;
  58889. return EXPECT_RESULT();
  58890. }
  58891. #endif
  58892. static int test_session_ticket_hs_update(void)
  58893. {
  58894. EXPECT_DECLS;
  58895. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58896. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  58897. struct test_memio_ctx test_ctx;
  58898. struct test_memio_ctx test_ctx2;
  58899. struct test_memio_ctx test_ctx3;
  58900. WOLFSSL_CTX *ctx_c = NULL;
  58901. WOLFSSL_CTX *ctx_s = NULL;
  58902. WOLFSSL *ssl_c = NULL;
  58903. WOLFSSL *ssl_c2 = NULL;
  58904. WOLFSSL *ssl_c3 = NULL;
  58905. WOLFSSL *ssl_s = NULL;
  58906. WOLFSSL *ssl_s2 = NULL;
  58907. WOLFSSL *ssl_s3 = NULL;
  58908. WOLFSSL_SESSION *sess = NULL;
  58909. byte read_data[1];
  58910. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58911. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  58912. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  58913. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58914. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58915. /* Generate tickets */
  58916. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58917. wolfSSL_SetLoggingPrefix("client");
  58918. /* Read the ticket msg */
  58919. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  58920. WOLFSSL_FATAL_ERROR);
  58921. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58922. WOLFSSL_ERROR_WANT_READ);
  58923. wolfSSL_SetLoggingPrefix(NULL);
  58924. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  58925. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58926. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  58927. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58928. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58929. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  58930. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  58931. wolfSSL_SetLoggingPrefix("client");
  58932. /* Exchange initial flights for the second connection */
  58933. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  58934. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  58935. WOLFSSL_ERROR_WANT_READ);
  58936. wolfSSL_SetLoggingPrefix(NULL);
  58937. wolfSSL_SetLoggingPrefix("server");
  58938. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  58939. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  58940. WOLFSSL_ERROR_WANT_READ);
  58941. wolfSSL_SetLoggingPrefix(NULL);
  58942. /* Complete third connection so that new tickets are exchanged */
  58943. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  58944. /* Read the ticket msg */
  58945. wolfSSL_SetLoggingPrefix("client");
  58946. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  58947. WOLFSSL_FATAL_ERROR);
  58948. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  58949. WOLFSSL_ERROR_WANT_READ);
  58950. wolfSSL_SetLoggingPrefix(NULL);
  58951. /* Complete second connection */
  58952. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  58953. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  58954. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  58955. wolfSSL_free(ssl_c);
  58956. wolfSSL_free(ssl_c2);
  58957. wolfSSL_free(ssl_c3);
  58958. wolfSSL_free(ssl_s);
  58959. wolfSSL_free(ssl_s2);
  58960. wolfSSL_free(ssl_s3);
  58961. wolfSSL_CTX_free(ctx_c);
  58962. wolfSSL_CTX_free(ctx_s);
  58963. wolfSSL_SESSION_free(sess);
  58964. #endif
  58965. return EXPECT_RESULT();
  58966. }
  58967. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58968. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58969. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  58970. {
  58971. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58972. WOLFSSL_SUCCESS);
  58973. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58974. }
  58975. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  58976. {
  58977. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58978. }
  58979. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  58980. {
  58981. char testMsg[] = "Message after SCR";
  58982. char msgBuf[sizeof(testMsg)];
  58983. if (wolfSSL_is_server(ssl)) {
  58984. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  58985. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  58986. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58987. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58988. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58989. sizeof(testMsg));
  58990. }
  58991. else {
  58992. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58993. sizeof(testMsg));
  58994. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58995. }
  58996. }
  58997. static int test_dtls_downgrade_scr_server(void)
  58998. {
  58999. EXPECT_DECLS;
  59000. callback_functions func_cb_client;
  59001. callback_functions func_cb_server;
  59002. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59003. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59004. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59005. func_cb_client.method = wolfDTLSv1_2_client_method;
  59006. func_cb_server.method = wolfDTLS_server_method;
  59007. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  59008. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  59009. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  59010. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  59011. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59012. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59013. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59014. return EXPECT_RESULT();
  59015. }
  59016. #else
  59017. static int test_dtls_downgrade_scr_server(void)
  59018. {
  59019. EXPECT_DECLS;
  59020. return EXPECT_RESULT();
  59021. }
  59022. #endif
  59023. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59024. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59025. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  59026. {
  59027. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59028. WOLFSSL_SUCCESS);
  59029. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59030. }
  59031. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  59032. {
  59033. char testMsg[] = "Message after SCR";
  59034. char msgBuf[sizeof(testMsg)];
  59035. if (wolfSSL_is_server(ssl)) {
  59036. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  59037. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  59038. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59039. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59040. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59041. sizeof(testMsg));
  59042. }
  59043. else {
  59044. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59045. sizeof(testMsg));
  59046. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59047. }
  59048. }
  59049. static int test_dtls_downgrade_scr(void)
  59050. {
  59051. EXPECT_DECLS;
  59052. callback_functions func_cb_client;
  59053. callback_functions func_cb_server;
  59054. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59055. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59056. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59057. func_cb_client.method = wolfDTLS_client_method;
  59058. func_cb_server.method = wolfDTLSv1_2_server_method;
  59059. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  59060. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  59061. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  59062. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59063. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59064. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59065. return EXPECT_RESULT();
  59066. }
  59067. #else
  59068. static int test_dtls_downgrade_scr(void)
  59069. {
  59070. EXPECT_DECLS;
  59071. return EXPECT_RESULT();
  59072. }
  59073. #endif
  59074. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59075. && !defined(WOLFSSL_NO_TLS12)
  59076. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  59077. char *data, int sz, void *ctx)
  59078. {
  59079. static int call_counter = 0;
  59080. call_counter++;
  59081. (void)ssl;
  59082. (void)data;
  59083. (void)sz;
  59084. (void)ctx;
  59085. switch (call_counter) {
  59086. case 1:
  59087. case 2:
  59088. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59089. case 3:
  59090. return WOLFSSL_CBIO_ERR_WANT_READ;
  59091. default:
  59092. AssertIntLE(call_counter, 3);
  59093. return -1;
  59094. }
  59095. }
  59096. #endif
  59097. /* Make sure we don't send acks before getting a server hello */
  59098. static int test_dtls_client_hello_timeout_downgrade(void)
  59099. {
  59100. EXPECT_DECLS;
  59101. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59102. && !defined(WOLFSSL_NO_TLS12)
  59103. WOLFSSL_CTX *ctx_c = NULL;
  59104. WOLFSSL_CTX *ctx_s = NULL;
  59105. WOLFSSL *ssl_c = NULL;
  59106. WOLFSSL *ssl_s = NULL;
  59107. struct test_memio_ctx test_ctx;
  59108. DtlsRecordLayerHeader* dtlsRH;
  59109. size_t len;
  59110. byte sequence_number[8];
  59111. int i;
  59112. for (i = 0; i < 2; i++) {
  59113. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59114. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59115. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  59116. if (i == 0) {
  59117. /* First time simulate timeout in IO layer */
  59118. /* CH1 */
  59119. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59120. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59121. /* HVR */
  59122. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59123. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59124. /* CH2 */
  59125. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59126. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59127. /* SH flight */
  59128. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59129. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59130. /* Drop the SH */
  59131. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59132. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59133. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59134. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59135. (sizeof(DtlsRecordLayerHeader) + len));
  59136. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59137. /* Read the remainder of the flight */
  59138. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59139. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59140. wolfSSL_SSLSetIORecv(ssl_c,
  59141. test_dtls_client_hello_timeout_downgrade_read_cb);
  59142. /* CH3 */
  59143. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59144. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59145. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  59146. }
  59147. else {
  59148. /* Second time call wolfSSL_dtls_got_timeout */
  59149. /* CH1 */
  59150. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59151. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59152. /* HVR */
  59153. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59154. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59155. /* CH2 */
  59156. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59157. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59158. /* SH flight */
  59159. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59160. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59161. /* Drop the SH */
  59162. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59163. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59164. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59165. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59166. (sizeof(DtlsRecordLayerHeader) + len));
  59167. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59168. /* Read the remainder of the flight */
  59169. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59170. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59171. /* Quick timeout should be set as we received at least one msg */
  59172. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  59173. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59174. /* Quick timeout should be cleared after a quick timeout */
  59175. /* CH3 */
  59176. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  59177. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59178. }
  59179. /* Parse out to make sure we got exactly one ClientHello message */
  59180. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59181. /* Second ClientHello after HVR */
  59182. sequence_number[7] = 2;
  59183. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  59184. ExpectIntEQ(dtlsRH->type, handshake);
  59185. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59186. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59187. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59188. sizeof(sequence_number)), 0);
  59189. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59190. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59191. /* Connection should be able to continue */
  59192. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59193. wolfSSL_free(ssl_c);
  59194. wolfSSL_free(ssl_s);
  59195. wolfSSL_CTX_free(ctx_c);
  59196. wolfSSL_CTX_free(ctx_s);
  59197. ssl_c = NULL;
  59198. ssl_s = NULL;
  59199. ctx_c = NULL;
  59200. ctx_s = NULL;
  59201. if (!EXPECT_SUCCESS())
  59202. break;
  59203. }
  59204. #endif
  59205. return EXPECT_RESULT();
  59206. }
  59207. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59208. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  59209. int sz, void *ctx)
  59210. {
  59211. static int call_counter = 0;
  59212. call_counter++;
  59213. (void)ssl;
  59214. (void)data;
  59215. (void)sz;
  59216. (void)ctx;
  59217. switch (call_counter) {
  59218. case 1:
  59219. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59220. case 2:
  59221. return WOLFSSL_CBIO_ERR_WANT_READ;
  59222. default:
  59223. AssertIntLE(call_counter, 2);
  59224. return -1;
  59225. }
  59226. }
  59227. #endif
  59228. /* Make sure we don't send acks before getting a server hello */
  59229. static int test_dtls_client_hello_timeout(void)
  59230. {
  59231. EXPECT_DECLS;
  59232. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59233. WOLFSSL *ssl_c = NULL;
  59234. WOLFSSL_CTX *ctx_c = NULL;
  59235. struct test_memio_ctx test_ctx;
  59236. DtlsRecordLayerHeader* dtlsRH;
  59237. size_t idx;
  59238. size_t len;
  59239. byte sequence_number[8];
  59240. int i;
  59241. for (i = 0; i < 2; i++) {
  59242. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59243. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59244. wolfDTLSv1_3_client_method, NULL), 0);
  59245. if (i == 0) {
  59246. /* First time simulate timeout in IO layer */
  59247. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  59248. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59249. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59250. }
  59251. else {
  59252. /* Second time call wolfSSL_dtls_got_timeout */
  59253. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59254. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59255. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59256. }
  59257. /* Parse out to make sure we got exactly two ClientHello messages */
  59258. idx = 0;
  59259. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59260. /* First ClientHello */
  59261. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59262. ExpectIntEQ(dtlsRH->type, handshake);
  59263. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59264. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59265. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59266. sizeof(sequence_number)), 0);
  59267. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59268. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59269. idx += sizeof(DtlsRecordLayerHeader) + len;
  59270. /* Second ClientHello */
  59271. sequence_number[7] = 1;
  59272. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59273. ExpectIntEQ(dtlsRH->type, handshake);
  59274. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59275. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59276. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59277. sizeof(sequence_number)), 0);
  59278. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59279. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59280. wolfSSL_free(ssl_c);
  59281. wolfSSL_CTX_free(ctx_c);
  59282. ssl_c = NULL;
  59283. ctx_c = NULL;
  59284. if (!EXPECT_SUCCESS())
  59285. break;
  59286. }
  59287. #endif
  59288. return EXPECT_RESULT();
  59289. }
  59290. /* DTLS test when dropping the changed cipher spec message */
  59291. static int test_dtls_dropped_ccs(void)
  59292. {
  59293. EXPECT_DECLS;
  59294. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59295. && !defined(WOLFSSL_NO_TLS12)
  59296. WOLFSSL_CTX *ctx_c = NULL;
  59297. WOLFSSL_CTX *ctx_s = NULL;
  59298. WOLFSSL *ssl_c = NULL;
  59299. WOLFSSL *ssl_s = NULL;
  59300. struct test_memio_ctx test_ctx;
  59301. DtlsRecordLayerHeader* dtlsRH;
  59302. size_t len;
  59303. byte data[1];
  59304. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59305. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59306. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59307. /* CH1 */
  59308. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59309. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59310. /* HVR */
  59311. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59312. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59313. /* CH2 */
  59314. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59315. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59316. /* Server first flight */
  59317. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59318. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59319. /* Client flight */
  59320. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59321. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59322. /* Server ccs + finished */
  59323. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  59324. /* Drop the ccs */
  59325. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  59326. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59327. ExpectIntEQ(len, 1);
  59328. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  59329. if (EXPECT_SUCCESS()) {
  59330. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59331. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59332. (sizeof(DtlsRecordLayerHeader) + len));
  59333. }
  59334. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59335. /* Client rtx flight */
  59336. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59337. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59338. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59339. /* Server ccs + finished rtx */
  59340. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  59341. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59342. /* Client processes finished */
  59343. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  59344. wolfSSL_free(ssl_c);
  59345. wolfSSL_free(ssl_s);
  59346. wolfSSL_CTX_free(ctx_c);
  59347. wolfSSL_CTX_free(ctx_s);
  59348. #endif
  59349. return EXPECT_RESULT();
  59350. }
  59351. /**
  59352. * Make sure we don't send RSA Signature Hash Algorithms in the
  59353. * CertificateRequest when we don't have any such ciphers set.
  59354. * @return EXPECT_RESULT()
  59355. */
  59356. static int test_certreq_sighash_algos(void)
  59357. {
  59358. EXPECT_DECLS;
  59359. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  59360. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  59361. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59362. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  59363. WOLFSSL_CTX *ctx_c = NULL;
  59364. WOLFSSL_CTX *ctx_s = NULL;
  59365. WOLFSSL *ssl_c = NULL;
  59366. WOLFSSL *ssl_s = NULL;
  59367. struct test_memio_ctx test_ctx;
  59368. int idx = 0;
  59369. int maxIdx = 0;
  59370. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59371. test_ctx.c_ciphers = test_ctx.s_ciphers =
  59372. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  59373. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59374. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59375. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  59376. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  59377. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  59378. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  59379. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59380. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  59381. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59382. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  59383. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59384. WOLFSSL_ERROR_WANT_READ);
  59385. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  59386. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59387. WOLFSSL_ERROR_WANT_READ);
  59388. /* Find the CertificateRequest message */
  59389. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  59390. word16 len;
  59391. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  59392. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  59393. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  59394. ato16(test_ctx.c_buff + idx, &len);
  59395. idx += OPAQUE16_LEN;
  59396. if (test_ctx.c_buff[idx] == certificate_request) {
  59397. idx++;
  59398. /* length */
  59399. idx += OPAQUE24_LEN;
  59400. /* cert types */
  59401. idx += 1 + test_ctx.c_buff[idx];
  59402. /* Sig algos */
  59403. ato16(test_ctx.c_buff + idx, &len);
  59404. idx += OPAQUE16_LEN;
  59405. maxIdx = idx + (int)len;
  59406. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  59407. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  59408. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  59409. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  59410. else
  59411. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  59412. }
  59413. break;
  59414. }
  59415. else {
  59416. idx += (int)len;
  59417. }
  59418. }
  59419. ExpectIntLT(idx, test_ctx.c_len);
  59420. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59421. wolfSSL_free(ssl_c);
  59422. wolfSSL_free(ssl_s);
  59423. wolfSSL_CTX_free(ctx_c);
  59424. wolfSSL_CTX_free(ctx_s);
  59425. #endif
  59426. return EXPECT_RESULT();
  59427. }
  59428. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59429. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  59430. {
  59431. EXPECT_DECLS;
  59432. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59433. myVerifyAction = VERIFY_USE_PREVERFIY;
  59434. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59435. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59436. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59437. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59438. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59439. WOLFSSL_SUCCESS);
  59440. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59441. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59442. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59443. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59444. "./certs/crl/ca-int.pem",
  59445. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59446. return EXPECT_RESULT();
  59447. }
  59448. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  59449. {
  59450. EXPECT_DECLS;
  59451. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59452. myVerifyAction = VERIFY_USE_PREVERFIY;
  59453. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59454. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59455. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59456. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59457. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59458. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59459. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59460. WOLFSSL_SUCCESS);
  59461. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59462. "./certs/crl/ca-int2.pem",
  59463. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59464. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59465. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59466. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59467. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59468. "./certs/crl/ca-int.pem",
  59469. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59470. return EXPECT_RESULT();
  59471. }
  59472. #endif
  59473. static int test_revoked_loaded_int_cert(void)
  59474. {
  59475. EXPECT_DECLS;
  59476. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59477. test_ssl_cbf client_cbf;
  59478. test_ssl_cbf server_cbf;
  59479. struct {
  59480. const char* certPemFile;
  59481. const char* keyPemFile;
  59482. ctx_cb client_ctx_ready;
  59483. } test_params[] = {
  59484. {"./certs/intermediate/ca-int2-cert.pem",
  59485. "./certs/intermediate/ca-int2-key.pem",
  59486. test_revoked_loaded_int_cert_ctx_ready1},
  59487. {"./certs/intermediate/server-chain.pem",
  59488. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59489. {"./certs/intermediate/server-chain-short.pem",
  59490. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59491. };
  59492. size_t i;
  59493. printf("\n");
  59494. for (i = 0; i < XELEM_CNT(test_params); i++) {
  59495. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59496. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59497. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  59498. server_cbf.certPemFile = test_params[i].certPemFile;
  59499. server_cbf.keyPemFile = test_params[i].keyPemFile;
  59500. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  59501. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59502. &server_cbf, NULL), TEST_FAIL);
  59503. #ifndef WOLFSSL_HAPROXY
  59504. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  59505. #else
  59506. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  59507. #endif
  59508. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  59509. if (!EXPECT_SUCCESS())
  59510. break;
  59511. printf("\t%s passed\n", test_params[i].certPemFile);
  59512. }
  59513. #endif
  59514. return EXPECT_RESULT();
  59515. }
  59516. static int test_dtls13_frag_ch_pq(void)
  59517. {
  59518. EXPECT_DECLS;
  59519. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59520. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  59521. WOLFSSL_CTX *ctx_c = NULL;
  59522. WOLFSSL_CTX *ctx_s = NULL;
  59523. WOLFSSL *ssl_c = NULL;
  59524. WOLFSSL *ssl_s = NULL;
  59525. struct test_memio_ctx test_ctx;
  59526. const char *test_str = "test";
  59527. int test_str_size;
  59528. byte buf[255];
  59529. int group = WOLFSSL_KYBER_LEVEL5;
  59530. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59531. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59532. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59533. /* Add in a large post-quantum key share to make the CH long. */
  59534. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  59535. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  59536. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  59537. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59538. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  59539. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  59540. test_str_size = XSTRLEN("test") + 1;
  59541. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59542. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59543. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59544. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59545. wolfSSL_free(ssl_c);
  59546. wolfSSL_free(ssl_s);
  59547. wolfSSL_CTX_free(ctx_c);
  59548. wolfSSL_CTX_free(ctx_s);
  59549. #endif
  59550. return EXPECT_RESULT();
  59551. }
  59552. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59553. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59554. static int test_dtls_frag_ch_count_records(byte* b, int len)
  59555. {
  59556. DtlsRecordLayerHeader* dtlsRH;
  59557. int records = 0;
  59558. size_t recordLen;
  59559. while (len > 0) {
  59560. records++;
  59561. dtlsRH = (DtlsRecordLayerHeader*)b;
  59562. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  59563. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  59564. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  59565. }
  59566. return records;
  59567. }
  59568. #endif
  59569. static int test_dtls_frag_ch(void)
  59570. {
  59571. EXPECT_DECLS;
  59572. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59573. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59574. WOLFSSL_CTX *ctx_c = NULL;
  59575. WOLFSSL_CTX *ctx_s = NULL;
  59576. WOLFSSL *ssl_c = NULL;
  59577. WOLFSSL *ssl_s = NULL;
  59578. struct test_memio_ctx test_ctx;
  59579. static unsigned int DUMMY_MTU = 256;
  59580. unsigned char four_frag_CH[] = {
  59581. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59582. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59583. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  59584. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  59585. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  59586. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  59587. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  59588. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  59589. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  59590. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  59591. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  59592. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  59593. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  59594. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  59595. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  59596. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  59597. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  59598. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  59599. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  59600. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59601. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  59602. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  59603. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  59604. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  59605. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  59606. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  59607. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  59608. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  59609. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  59610. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  59611. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  59612. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  59613. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  59614. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  59615. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  59616. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  59617. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  59618. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  59619. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  59620. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  59621. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  59622. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  59623. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  59624. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  59625. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  59626. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  59627. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  59628. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  59629. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  59630. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  59631. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  59632. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  59633. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  59634. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  59635. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  59636. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  59637. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  59638. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  59639. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  59640. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  59641. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  59642. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  59643. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  59644. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  59645. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  59646. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  59647. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  59648. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  59649. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  59650. 0xa5, 0xcb, 0x68, 0xf3
  59651. };
  59652. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59653. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59654. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59655. /* Fragment msgs */
  59656. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  59657. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  59658. /* Add in some key shares to make the CH long */
  59659. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  59660. WOLFSSL_SUCCESS);
  59661. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  59662. WOLFSSL_SUCCESS);
  59663. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  59664. WOLFSSL_SUCCESS);
  59665. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  59666. WOLFSSL_SUCCESS);
  59667. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  59668. /* Reject fragmented first CH */
  59669. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  59670. sizeof(four_frag_CH)), 4);
  59671. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  59672. test_ctx.s_len = sizeof(four_frag_CH);
  59673. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  59674. int s_len = test_ctx.s_len;
  59675. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59676. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59677. /* Fail if we didn't advance the buffer to avoid infinite loops */
  59678. ExpectIntLT(test_ctx.s_len, s_len);
  59679. }
  59680. /* Expect all fragments read */
  59681. ExpectIntEQ(test_ctx.s_len, 0);
  59682. /* Expect quietly dropping fragmented first CH */
  59683. ExpectIntEQ(test_ctx.c_len, 0);
  59684. /* CH1 */
  59685. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59686. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59687. /* Count records. Expect 1 unfragmented CH */
  59688. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  59689. test_ctx.s_len), 1);
  59690. /* HRR */
  59691. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59692. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59693. /* CH2 */
  59694. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59695. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59696. /* Count records. Expect fragmented CH */
  59697. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  59698. test_ctx.s_len), 1);
  59699. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59700. wolfSSL_free(ssl_c);
  59701. wolfSSL_free(ssl_s);
  59702. wolfSSL_CTX_free(ctx_c);
  59703. wolfSSL_CTX_free(ctx_s);
  59704. ssl_c = ssl_s = NULL;
  59705. ctx_c = ctx_s = NULL;
  59706. #endif
  59707. return EXPECT_RESULT();
  59708. }
  59709. static int test_dtls_empty_keyshare_with_cookie(void)
  59710. {
  59711. EXPECT_DECLS;
  59712. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59713. WOLFSSL_CTX *ctx_s = NULL;
  59714. WOLFSSL *ssl_s = NULL;
  59715. struct test_memio_ctx test_ctx;
  59716. unsigned char ch_empty_keyshare_with_cookie[] = {
  59717. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  59718. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  59719. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  59720. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  59721. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  59722. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  59723. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  59724. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  59725. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  59726. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  59727. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  59728. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  59729. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  59730. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  59731. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  59732. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  59733. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  59734. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  59735. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  59736. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  59737. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  59738. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  59739. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  59740. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  59741. };
  59742. DtlsRecordLayerHeader* dtlsRH;
  59743. byte sequence_number[8];
  59744. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59745. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59746. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  59747. sizeof(ch_empty_keyshare_with_cookie));
  59748. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  59749. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  59750. NULL, wolfDTLSv1_3_server_method), 0);
  59751. /* CH1 */
  59752. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59753. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59754. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  59755. ExpectIntEQ(test_ctx.c_len, 15);
  59756. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  59757. ExpectIntEQ(dtlsRH->type, alert);
  59758. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59759. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59760. sequence_number[7] = 1;
  59761. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59762. sizeof(sequence_number)), 0);
  59763. ExpectIntEQ(dtlsRH->length[0], 0);
  59764. ExpectIntEQ(dtlsRH->length[1], 2);
  59765. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  59766. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  59767. wolfSSL_free(ssl_s);
  59768. wolfSSL_CTX_free(ctx_s);
  59769. #endif
  59770. return EXPECT_RESULT();
  59771. }
  59772. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59773. defined(HAVE_LIBOQS)
  59774. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  59775. {
  59776. int group = WOLFSSL_KYBER_LEVEL5;
  59777. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  59778. }
  59779. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  59780. {
  59781. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  59782. }
  59783. #endif
  59784. static int test_tls13_pq_groups(void)
  59785. {
  59786. EXPECT_DECLS;
  59787. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59788. defined(HAVE_LIBOQS)
  59789. callback_functions func_cb_client;
  59790. callback_functions func_cb_server;
  59791. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59792. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59793. func_cb_client.method = wolfTLSv1_3_client_method;
  59794. func_cb_server.method = wolfTLSv1_3_server_method;
  59795. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  59796. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  59797. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  59798. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59799. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59800. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59801. #endif
  59802. return EXPECT_RESULT();
  59803. }
  59804. static int test_dtls13_early_data(void)
  59805. {
  59806. EXPECT_DECLS;
  59807. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) && \
  59808. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  59809. struct test_memio_ctx test_ctx;
  59810. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59811. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59812. WOLFSSL_SESSION *sess = NULL;
  59813. int written = 0;
  59814. int read = 0;
  59815. char msg[] = "This is early data";
  59816. char msg2[] = "This is client data";
  59817. char msg3[] = "This is server data";
  59818. char msgBuf[50];
  59819. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59820. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59821. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59822. /* Get a ticket so that we can do 0-RTT on the next connection */
  59823. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59824. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59825. wolfSSL_free(ssl_c);
  59826. ssl_c = NULL;
  59827. wolfSSL_free(ssl_s);
  59828. ssl_s = NULL;
  59829. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59830. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59831. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59832. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59833. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  59834. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  59835. #else
  59836. /* Let's test this but we generally don't recommend turning off the
  59837. * cookie exchange */
  59838. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59839. #endif
  59840. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  59841. &written), sizeof(msg));
  59842. ExpectIntEQ(written, sizeof(msg));
  59843. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59844. &read), sizeof(msg));
  59845. ExpectIntEQ(read, sizeof(msg));
  59846. ExpectStrEQ(msg, msgBuf);
  59847. /* Complete handshake */
  59848. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59849. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59850. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  59851. * a user would loop until it is true but here we control both sides so we
  59852. * just assert the expected value. wolfSSL_read_early_data does not provide
  59853. * handshake status to us with non-blocking IO and we can't use
  59854. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  59855. * parsing logic. */
  59856. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  59857. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59858. &read), WOLFSSL_FAILURE);
  59859. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  59860. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59861. /* Test bi-directional write */
  59862. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  59863. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  59864. ExpectStrEQ(msg2, msgBuf);
  59865. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  59866. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  59867. ExpectStrEQ(msg3, msgBuf);
  59868. ExpectTrue(wolfSSL_session_reused(ssl_c));
  59869. ExpectTrue(wolfSSL_session_reused(ssl_s));
  59870. #endif
  59871. return EXPECT_RESULT();
  59872. }
  59873. /*----------------------------------------------------------------------------*
  59874. | Main
  59875. *----------------------------------------------------------------------------*/
  59876. typedef int (*TEST_FUNC)(void);
  59877. typedef struct {
  59878. const char *name;
  59879. TEST_FUNC func;
  59880. byte run:1;
  59881. byte fail:1;
  59882. } TEST_CASE;
  59883. #define TEST_DECL(func) { #func, func, 0, 0 }
  59884. int testAll = 1;
  59885. TEST_CASE testCases[] = {
  59886. TEST_DECL(test_fileAccess),
  59887. /*********************************
  59888. * wolfcrypt
  59889. *********************************/
  59890. TEST_DECL(test_ForceZero),
  59891. TEST_DECL(test_wolfCrypt_Init),
  59892. /* Locking with Compat Mutex */
  59893. TEST_DECL(test_wc_SetMutexCb),
  59894. TEST_DECL(test_wc_LockMutex_ex),
  59895. /* Digests */
  59896. TEST_DECL(test_wc_InitMd5),
  59897. TEST_DECL(test_wc_Md5Update),
  59898. TEST_DECL(test_wc_Md5Final),
  59899. TEST_DECL(test_wc_InitSha),
  59900. TEST_DECL(test_wc_ShaUpdate),
  59901. TEST_DECL(test_wc_ShaFinal),
  59902. TEST_DECL(test_wc_InitSha256),
  59903. TEST_DECL(test_wc_Sha256Update),
  59904. TEST_DECL(test_wc_Sha256Final),
  59905. TEST_DECL(test_wc_Sha256FinalRaw),
  59906. TEST_DECL(test_wc_Sha256GetFlags),
  59907. TEST_DECL(test_wc_Sha256Free),
  59908. TEST_DECL(test_wc_Sha256GetHash),
  59909. TEST_DECL(test_wc_Sha256Copy),
  59910. TEST_DECL(test_wc_InitSha224),
  59911. TEST_DECL(test_wc_Sha224Update),
  59912. TEST_DECL(test_wc_Sha224Final),
  59913. TEST_DECL(test_wc_Sha224SetFlags),
  59914. TEST_DECL(test_wc_Sha224GetFlags),
  59915. TEST_DECL(test_wc_Sha224Free),
  59916. TEST_DECL(test_wc_Sha224GetHash),
  59917. TEST_DECL(test_wc_Sha224Copy),
  59918. TEST_DECL(test_wc_InitSha512),
  59919. TEST_DECL(test_wc_Sha512Update),
  59920. TEST_DECL(test_wc_Sha512Final),
  59921. TEST_DECL(test_wc_Sha512GetFlags),
  59922. TEST_DECL(test_wc_Sha512FinalRaw),
  59923. TEST_DECL(test_wc_Sha512Free),
  59924. TEST_DECL(test_wc_Sha512GetHash),
  59925. TEST_DECL(test_wc_Sha512Copy),
  59926. TEST_DECL(test_wc_InitSha512_224),
  59927. TEST_DECL(test_wc_Sha512_224Update),
  59928. TEST_DECL(test_wc_Sha512_224Final),
  59929. TEST_DECL(test_wc_Sha512_224GetFlags),
  59930. TEST_DECL(test_wc_Sha512_224FinalRaw),
  59931. TEST_DECL(test_wc_Sha512_224Free),
  59932. TEST_DECL(test_wc_Sha512_224GetHash),
  59933. TEST_DECL(test_wc_Sha512_224Copy),
  59934. TEST_DECL(test_wc_InitSha512_256),
  59935. TEST_DECL(test_wc_Sha512_256Update),
  59936. TEST_DECL(test_wc_Sha512_256Final),
  59937. TEST_DECL(test_wc_Sha512_256GetFlags),
  59938. TEST_DECL(test_wc_Sha512_256FinalRaw),
  59939. TEST_DECL(test_wc_Sha512_256Free),
  59940. TEST_DECL(test_wc_Sha512_256GetHash),
  59941. TEST_DECL(test_wc_Sha512_256Copy),
  59942. TEST_DECL(test_wc_InitSha384),
  59943. TEST_DECL(test_wc_Sha384Update),
  59944. TEST_DECL(test_wc_Sha384Final),
  59945. TEST_DECL(test_wc_Sha384GetFlags),
  59946. TEST_DECL(test_wc_Sha384FinalRaw),
  59947. TEST_DECL(test_wc_Sha384Free),
  59948. TEST_DECL(test_wc_Sha384GetHash),
  59949. TEST_DECL(test_wc_Sha384Copy),
  59950. TEST_DECL(test_wc_InitBlake2b),
  59951. TEST_DECL(test_wc_InitBlake2b_WithKey),
  59952. TEST_DECL(test_wc_InitBlake2s_WithKey),
  59953. TEST_DECL(test_wc_InitRipeMd),
  59954. TEST_DECL(test_wc_RipeMdUpdate),
  59955. TEST_DECL(test_wc_RipeMdFinal),
  59956. TEST_DECL(test_wc_InitSha3),
  59957. TEST_DECL(testing_wc_Sha3_Update),
  59958. TEST_DECL(test_wc_Sha3_224_Final),
  59959. TEST_DECL(test_wc_Sha3_256_Final),
  59960. TEST_DECL(test_wc_Sha3_384_Final),
  59961. TEST_DECL(test_wc_Sha3_512_Final),
  59962. TEST_DECL(test_wc_Sha3_224_Copy),
  59963. TEST_DECL(test_wc_Sha3_256_Copy),
  59964. TEST_DECL(test_wc_Sha3_384_Copy),
  59965. TEST_DECL(test_wc_Sha3_512_Copy),
  59966. TEST_DECL(test_wc_Sha3_GetFlags),
  59967. TEST_DECL(test_wc_InitShake256),
  59968. TEST_DECL(testing_wc_Shake256_Update),
  59969. TEST_DECL(test_wc_Shake256_Final),
  59970. TEST_DECL(test_wc_Shake256_Copy),
  59971. TEST_DECL(test_wc_Shake256Hash),
  59972. /* SM3 Digest */
  59973. TEST_DECL(test_wc_InitSm3Free),
  59974. TEST_DECL(test_wc_Sm3UpdateFinal),
  59975. TEST_DECL(test_wc_Sm3GetHash),
  59976. TEST_DECL(test_wc_Sm3Copy),
  59977. TEST_DECL(test_wc_Sm3FinalRaw),
  59978. TEST_DECL(test_wc_Sm3GetSetFlags),
  59979. TEST_DECL(test_wc_Sm3Hash),
  59980. TEST_DECL(test_wc_HashInit),
  59981. TEST_DECL(test_wc_HashSetFlags),
  59982. TEST_DECL(test_wc_HashGetFlags),
  59983. /* HMAC */
  59984. TEST_DECL(test_wc_Md5HmacSetKey),
  59985. TEST_DECL(test_wc_Md5HmacUpdate),
  59986. TEST_DECL(test_wc_Md5HmacFinal),
  59987. TEST_DECL(test_wc_ShaHmacSetKey),
  59988. TEST_DECL(test_wc_ShaHmacUpdate),
  59989. TEST_DECL(test_wc_ShaHmacFinal),
  59990. TEST_DECL(test_wc_Sha224HmacSetKey),
  59991. TEST_DECL(test_wc_Sha224HmacUpdate),
  59992. TEST_DECL(test_wc_Sha224HmacFinal),
  59993. TEST_DECL(test_wc_Sha256HmacSetKey),
  59994. TEST_DECL(test_wc_Sha256HmacUpdate),
  59995. TEST_DECL(test_wc_Sha256HmacFinal),
  59996. TEST_DECL(test_wc_Sha384HmacSetKey),
  59997. TEST_DECL(test_wc_Sha384HmacUpdate),
  59998. TEST_DECL(test_wc_Sha384HmacFinal),
  59999. /* CMAC */
  60000. TEST_DECL(test_wc_InitCmac),
  60001. TEST_DECL(test_wc_CmacUpdate),
  60002. TEST_DECL(test_wc_CmacFinal),
  60003. TEST_DECL(test_wc_AesCmacGenerate),
  60004. /* Cipher */
  60005. TEST_DECL(test_wc_AesGcmStream),
  60006. TEST_DECL(test_wc_Des3_SetIV),
  60007. TEST_DECL(test_wc_Des3_SetKey),
  60008. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  60009. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  60010. TEST_DECL(test_wc_Des3_EcbEncrypt),
  60011. TEST_DECL(test_wc_Chacha_SetKey),
  60012. TEST_DECL(test_wc_Chacha_Process),
  60013. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  60014. TEST_DECL(test_wc_Poly1305SetKey),
  60015. TEST_DECL(test_wc_CamelliaSetKey),
  60016. TEST_DECL(test_wc_CamelliaSetIV),
  60017. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  60018. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  60019. TEST_DECL(test_wc_Arc4SetKey),
  60020. TEST_DECL(test_wc_Arc4Process),
  60021. TEST_DECL(test_wc_Rc2SetKey),
  60022. TEST_DECL(test_wc_Rc2SetIV),
  60023. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  60024. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  60025. /* AES cipher and GMAC. */
  60026. TEST_DECL(test_wc_AesSetKey),
  60027. TEST_DECL(test_wc_AesSetIV),
  60028. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  60029. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  60030. TEST_DECL(test_wc_AesGcmSetKey),
  60031. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  60032. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  60033. TEST_DECL(test_wc_GmacSetKey),
  60034. TEST_DECL(test_wc_GmacUpdate),
  60035. TEST_DECL(test_wc_AesCcmSetKey),
  60036. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  60037. #if defined(WOLFSSL_AES_EAX) && \
  60038. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  60039. TEST_DECL(test_wc_AesEaxVectors),
  60040. TEST_DECL(test_wc_AesEaxEncryptAuth),
  60041. TEST_DECL(test_wc_AesEaxDecryptAuth),
  60042. #endif /* WOLFSSL_AES_EAX */
  60043. /* SM4 cipher */
  60044. TEST_DECL(test_wc_Sm4),
  60045. TEST_DECL(test_wc_Sm4Ecb),
  60046. TEST_DECL(test_wc_Sm4Cbc),
  60047. TEST_DECL(test_wc_Sm4Ctr),
  60048. TEST_DECL(test_wc_Sm4Gcm),
  60049. TEST_DECL(test_wc_Sm4Ccm),
  60050. /* RNG tests */
  60051. #ifdef HAVE_HASHDRBG
  60052. #ifdef TEST_RESEED_INTERVAL
  60053. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  60054. #endif
  60055. TEST_DECL(test_wc_RNG_GenerateBlock),
  60056. #endif
  60057. TEST_DECL(test_get_rand_digit),
  60058. TEST_DECL(test_wc_InitRngNonce),
  60059. TEST_DECL(test_wc_InitRngNonce_ex),
  60060. /* MP API tests */
  60061. TEST_DECL(test_get_digit_count),
  60062. TEST_DECL(test_mp_cond_copy),
  60063. TEST_DECL(test_mp_rand),
  60064. TEST_DECL(test_get_digit),
  60065. TEST_DECL(test_wc_export_int),
  60066. /* RSA */
  60067. TEST_DECL(test_wc_InitRsaKey),
  60068. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  60069. TEST_DECL(test_wc_RsaPublicKeyDecode),
  60070. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  60071. TEST_DECL(test_wc_MakeRsaKey),
  60072. TEST_DECL(test_wc_CheckProbablePrime),
  60073. TEST_DECL(test_wc_RsaPSS_Verify),
  60074. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  60075. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  60076. TEST_DECL(test_wc_RsaKeyToDer),
  60077. TEST_DECL(test_wc_RsaKeyToPublicDer),
  60078. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  60079. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  60080. TEST_DECL(test_wc_RsaEncryptSize),
  60081. TEST_DECL(test_wc_RsaSSL_SignVerify),
  60082. TEST_DECL(test_wc_RsaFlattenPublicKey),
  60083. TEST_DECL(test_RsaDecryptBoundsCheck),
  60084. /* DSA */
  60085. TEST_DECL(test_wc_InitDsaKey),
  60086. TEST_DECL(test_wc_DsaSignVerify),
  60087. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  60088. TEST_DECL(test_wc_MakeDsaKey),
  60089. TEST_DECL(test_wc_DsaKeyToDer),
  60090. TEST_DECL(test_wc_DsaKeyToPublicDer),
  60091. TEST_DECL(test_wc_DsaImportParamsRaw),
  60092. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  60093. TEST_DECL(test_wc_DsaExportParamsRaw),
  60094. TEST_DECL(test_wc_DsaExportKeyRaw),
  60095. /* DH */
  60096. TEST_DECL(test_wc_DhPublicKeyDecode),
  60097. /* wolfCrypt ECC tests */
  60098. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  60099. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  60100. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  60101. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  60102. !defined(HAVE_SELFTEST) && \
  60103. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  60104. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  60105. #endif
  60106. TEST_DECL(test_wc_ecc_make_key),
  60107. TEST_DECL(test_wc_ecc_init),
  60108. TEST_DECL(test_wc_ecc_check_key),
  60109. TEST_DECL(test_wc_ecc_get_generator),
  60110. TEST_DECL(test_wc_ecc_size),
  60111. TEST_DECL(test_wc_ecc_params),
  60112. TEST_DECL(test_wc_ecc_signVerify_hash),
  60113. TEST_DECL(test_wc_ecc_shared_secret),
  60114. TEST_DECL(test_wc_ecc_export_x963),
  60115. TEST_DECL(test_wc_ecc_export_x963_ex),
  60116. TEST_DECL(test_wc_ecc_import_x963),
  60117. TEST_DECL(test_wc_ecc_import_private_key),
  60118. TEST_DECL(test_wc_ecc_export_private_only),
  60119. TEST_DECL(test_wc_ecc_rs_to_sig),
  60120. TEST_DECL(test_wc_ecc_import_raw),
  60121. TEST_DECL(test_wc_ecc_import_unsigned),
  60122. TEST_DECL(test_wc_ecc_sig_size),
  60123. TEST_DECL(test_wc_ecc_ctx_new),
  60124. TEST_DECL(test_wc_ecc_ctx_reset),
  60125. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  60126. TEST_DECL(test_wc_ecc_ctx_set_info),
  60127. TEST_DECL(test_wc_ecc_encryptDecrypt),
  60128. TEST_DECL(test_wc_ecc_del_point),
  60129. TEST_DECL(test_wc_ecc_pointFns),
  60130. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  60131. TEST_DECL(test_wc_ecc_verify_hash_ex),
  60132. TEST_DECL(test_wc_ecc_mulmod),
  60133. TEST_DECL(test_wc_ecc_is_valid_idx),
  60134. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  60135. TEST_DECL(test_wc_ecc_sig_size_calc),
  60136. TEST_DECL(test_wc_EccPrivateKeyToDer),
  60137. /* SM2 elliptic curve */
  60138. TEST_DECL(test_wc_ecc_sm2_make_key),
  60139. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  60140. TEST_DECL(test_wc_ecc_sm2_create_digest),
  60141. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  60142. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  60143. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  60144. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  60145. /* Curve25519 */
  60146. TEST_DECL(test_wc_curve25519_init),
  60147. TEST_DECL(test_wc_curve25519_size),
  60148. TEST_DECL(test_wc_curve25519_export_key_raw),
  60149. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  60150. TEST_DECL(test_wc_curve25519_make_key),
  60151. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  60152. TEST_DECL(test_wc_curve25519_make_pub),
  60153. TEST_DECL(test_wc_curve25519_export_public_ex),
  60154. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  60155. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  60156. TEST_DECL(test_wc_curve25519_import_private),
  60157. /* ED25519 */
  60158. TEST_DECL(test_wc_ed25519_make_key),
  60159. TEST_DECL(test_wc_ed25519_init),
  60160. TEST_DECL(test_wc_ed25519_sign_msg),
  60161. TEST_DECL(test_wc_ed25519_import_public),
  60162. TEST_DECL(test_wc_ed25519_import_private_key),
  60163. TEST_DECL(test_wc_ed25519_export),
  60164. TEST_DECL(test_wc_ed25519_size),
  60165. TEST_DECL(test_wc_ed25519_exportKey),
  60166. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  60167. TEST_DECL(test_wc_Ed25519KeyToDer),
  60168. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  60169. /* Curve448 */
  60170. TEST_DECL(test_wc_curve448_make_key),
  60171. TEST_DECL(test_wc_curve448_shared_secret_ex),
  60172. TEST_DECL(test_wc_curve448_export_public_ex),
  60173. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  60174. TEST_DECL(test_wc_curve448_export_key_raw),
  60175. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  60176. TEST_DECL(test_wc_curve448_import_private),
  60177. TEST_DECL(test_wc_curve448_init),
  60178. TEST_DECL(test_wc_curve448_size),
  60179. /* Ed448 */
  60180. TEST_DECL(test_wc_ed448_make_key),
  60181. TEST_DECL(test_wc_ed448_init),
  60182. TEST_DECL(test_wc_ed448_sign_msg),
  60183. TEST_DECL(test_wc_ed448_import_public),
  60184. TEST_DECL(test_wc_ed448_import_private_key),
  60185. TEST_DECL(test_wc_ed448_export),
  60186. TEST_DECL(test_wc_ed448_size),
  60187. TEST_DECL(test_wc_ed448_exportKey),
  60188. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  60189. TEST_DECL(test_wc_Ed448KeyToDer),
  60190. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  60191. /* Signature API */
  60192. TEST_DECL(test_wc_SignatureGetSize_ecc),
  60193. TEST_DECL(test_wc_SignatureGetSize_rsa),
  60194. /* PEM and DER APIs. */
  60195. TEST_DECL(test_wc_PemToDer),
  60196. TEST_DECL(test_wc_AllocDer),
  60197. TEST_DECL(test_wc_CertPemToDer),
  60198. TEST_DECL(test_wc_KeyPemToDer),
  60199. TEST_DECL(test_wc_PubKeyPemToDer),
  60200. TEST_DECL(test_wc_PemPubKeyToDer),
  60201. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  60202. TEST_DECL(test_wc_CheckCertSigPubKey),
  60203. /* wolfCrypt ASN tests */
  60204. TEST_DECL(test_ToTraditional),
  60205. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  60206. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  60207. /* Certificate */
  60208. TEST_DECL(test_wc_SetSubjectRaw),
  60209. TEST_DECL(test_wc_GetSubjectRaw),
  60210. TEST_DECL(test_wc_SetIssuerRaw),
  60211. TEST_DECL(test_wc_SetIssueBuffer),
  60212. TEST_DECL(test_wc_SetSubjectKeyId),
  60213. TEST_DECL(test_wc_SetSubject),
  60214. TEST_DECL(test_CheckCertSignature),
  60215. TEST_DECL(test_wc_ParseCert),
  60216. TEST_DECL(test_wc_ParseCert_Error),
  60217. TEST_DECL(test_MakeCertWithPathLen),
  60218. TEST_DECL(test_MakeCertWithCaFalse),
  60219. TEST_DECL(test_wc_SetKeyUsage),
  60220. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  60221. TEST_DECL(test_wc_SetSubjectBuffer),
  60222. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  60223. /* wolfcrypt PKCS#7 */
  60224. TEST_DECL(test_wc_PKCS7_New),
  60225. TEST_DECL(test_wc_PKCS7_Init),
  60226. TEST_DECL(test_wc_PKCS7_InitWithCert),
  60227. TEST_DECL(test_wc_PKCS7_EncodeData),
  60228. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  60229. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  60230. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  60231. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  60232. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  60233. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  60234. TEST_DECL(test_wc_PKCS7_Degenerate),
  60235. TEST_DECL(test_wc_PKCS7_BER),
  60236. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  60237. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  60238. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  60239. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  60240. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  60241. /* wolfCrypt PKCS#12 */
  60242. TEST_DECL(test_wc_i2d_PKCS12),
  60243. /*
  60244. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  60245. * avoid memory leaks.
  60246. */
  60247. TEST_DECL(test_wolfCrypt_Cleanup),
  60248. TEST_DECL(test_wolfSSL_Init),
  60249. /*********************************
  60250. * OpenSSL compatibility API tests
  60251. *********************************/
  60252. /* If at some point a stub get implemented this test should fail indicating
  60253. * a need to implement a new test case
  60254. */
  60255. TEST_DECL(test_stubs_are_stubs),
  60256. /* ASN.1 compatibility API tests */
  60257. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  60258. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  60259. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  60260. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  60261. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  60262. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  60263. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  60264. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  60265. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  60266. TEST_DECL(test_wolfSSL_ASN1_get_object),
  60267. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  60268. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  60269. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  60270. TEST_DECL(test_wolfSSL_ASN1_STRING),
  60271. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  60272. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  60273. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  60274. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  60275. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  60276. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  60277. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  60278. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  60279. TEST_DECL(test_wolfSSL_ASN1_TIME),
  60280. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  60281. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  60282. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  60283. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  60284. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  60285. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  60286. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  60287. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  60288. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  60289. TEST_DECL(test_wolfSSL_lhash),
  60290. TEST_DECL(test_wolfSSL_certs),
  60291. TEST_DECL(test_wolfSSL_private_keys),
  60292. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  60293. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  60294. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  60295. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  60296. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  60297. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  60298. #ifndef NO_BIO
  60299. TEST_DECL(test_wolfSSL_BIO),
  60300. TEST_DECL(test_wolfSSL_PEM_read_bio),
  60301. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  60302. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  60303. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  60304. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  60305. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  60306. #endif
  60307. /* EVP API testing */
  60308. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  60309. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  60310. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  60311. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  60312. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  60313. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  60314. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  60315. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  60316. TEST_DECL(test_wolfSSL_EVP_shake128),
  60317. TEST_DECL(test_wolfSSL_EVP_shake256),
  60318. TEST_DECL(test_wolfSSL_EVP_sm3),
  60319. TEST_DECL(test_EVP_blake2),
  60320. #ifdef OPENSSL_ALL
  60321. TEST_DECL(test_wolfSSL_EVP_md4),
  60322. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  60323. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  60324. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  60325. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  60326. #endif
  60327. TEST_DECL(test_EVP_MD_do_all),
  60328. TEST_DECL(test_wolfSSL_EVP_MD_size),
  60329. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  60330. TEST_DECL(test_wolfSSL_EVP_Digest),
  60331. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  60332. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  60333. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  60334. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  60335. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  60336. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  60337. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  60338. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  60339. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  60340. TEST_DECL(test_wolfssl_EVP_chacha20),
  60341. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  60342. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  60343. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  60344. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  60345. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  60346. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  60347. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  60348. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  60349. #ifdef OPENSSL_ALL
  60350. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  60351. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  60352. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  60353. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  60354. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  60355. TEST_DECL(test_wolfSSL_EVP_rc4),
  60356. TEST_DECL(test_wolfSSL_EVP_enc_null),
  60357. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  60358. TEST_DECL(test_wolfSSL_EVP_mdc2),
  60359. TEST_DECL(test_evp_cipher_aes_gcm),
  60360. #endif
  60361. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  60362. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  60363. #ifdef OPENSSL_EXTRA
  60364. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  60365. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  60366. #endif
  60367. #ifdef OPENSSL_ALL
  60368. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  60369. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  60370. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  60371. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  60372. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  60373. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  60374. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  60375. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  60376. #endif
  60377. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  60378. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  60379. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  60380. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  60381. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  60382. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  60383. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  60384. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  60385. #ifndef NO_BIO
  60386. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  60387. #endif
  60388. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  60389. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  60390. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  60391. #ifndef NO_BIO
  60392. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  60393. #endif /* !NO_BIO */
  60394. #endif
  60395. #ifdef OPENSSL_ALL
  60396. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  60397. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  60398. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  60399. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  60400. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  60401. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  60402. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  60403. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  60404. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  60405. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  60406. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  60407. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  60408. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  60409. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  60410. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  60411. #endif
  60412. TEST_DECL(test_EVP_PKEY_rsa),
  60413. TEST_DECL(test_EVP_PKEY_ec),
  60414. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  60415. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  60416. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  60417. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  60418. TEST_DECL(test_EVP_PKEY_cmp),
  60419. #ifdef OPENSSL_ALL
  60420. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  60421. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  60422. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  60423. #endif
  60424. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  60425. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  60426. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60427. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  60428. #endif
  60429. #ifndef NO_BIO
  60430. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  60431. TEST_DECL(test_wolfSSL_GetLoggingCb),
  60432. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  60433. TEST_DECL(test_wc_ERR_remove_state),
  60434. TEST_DECL(test_wc_ERR_print_errors_fp),
  60435. #endif
  60436. TEST_DECL(test_wolfSSL_configure_args),
  60437. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  60438. TEST_DECL(test_wolfSSL_set1_curves_list),
  60439. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  60440. TEST_DECL(test_wolfSSL_OtherName),
  60441. TEST_DECL(test_wolfSSL_FPKI),
  60442. TEST_DECL(test_wolfSSL_URI),
  60443. TEST_DECL(test_wolfSSL_TBS),
  60444. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  60445. TEST_DECL(test_X509_STORE_untrusted),
  60446. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  60447. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  60448. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  60449. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  60450. TEST_DECL(test_wolfSSL_X509_Name_canon),
  60451. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  60452. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  60453. TEST_DECL(test_wolfSSL_X509_NID),
  60454. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  60455. TEST_DECL(test_wolfSSL_get0_param),
  60456. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  60457. TEST_DECL(test_wolfSSL_set1_host),
  60458. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  60459. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  60460. TEST_DECL(test_wolfSSL_X509_STORE),
  60461. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  60462. TEST_DECL(test_X509_STORE_get0_objects),
  60463. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  60464. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  60465. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  60466. TEST_DECL(test_wolfSSL_X509_cmp_time),
  60467. TEST_DECL(test_wolfSSL_X509_time_adj),
  60468. /* X509 tests */
  60469. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  60470. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  60471. TEST_DECL(test_wolfSSL_X509_check_host),
  60472. TEST_DECL(test_wolfSSL_X509_check_email),
  60473. TEST_DECL(test_wolfSSL_X509_check_private_key),
  60474. TEST_DECL(test_wolfSSL_X509),
  60475. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  60476. TEST_DECL(test_wolfSSL_X509_sign),
  60477. TEST_DECL(test_wolfSSL_X509_sign2),
  60478. TEST_DECL(test_wolfSSL_X509_verify),
  60479. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  60480. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  60481. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  60482. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  60483. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  60484. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  60485. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  60486. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  60487. TEST_DECL(test_wolfSSL_X509_NAME),
  60488. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  60489. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  60490. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  60491. TEST_DECL(test_wolfSSL_X509_set_name),
  60492. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  60493. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  60494. TEST_DECL(test_wolfSSL_X509_set_version),
  60495. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  60496. TEST_DECL(test_wolfSSL_X509_CRL),
  60497. TEST_DECL(test_wolfSSL_i2d_X509),
  60498. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  60499. TEST_DECL(test_wolfSSL_PEM_read_X509),
  60500. TEST_DECL(test_wolfSSL_X509_check_ca),
  60501. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  60502. TEST_DECL(test_wolfSSL_make_cert),
  60503. #ifndef NO_BIO
  60504. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  60505. TEST_DECL(test_wolfSSL_X509_INFO),
  60506. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  60507. #endif
  60508. #ifdef OPENSSL_ALL
  60509. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  60510. #endif
  60511. TEST_DECL(test_wolfSSL_X509_CA_num),
  60512. TEST_DECL(test_wolfSSL_X509_get_version),
  60513. #ifndef NO_BIO
  60514. TEST_DECL(test_wolfSSL_X509_print),
  60515. TEST_DECL(test_wolfSSL_X509_CRL_print),
  60516. #endif
  60517. TEST_DECL(test_X509_get_signature_nid),
  60518. /* X509 extension testing. */
  60519. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  60520. TEST_DECL(test_wolfSSL_X509_get_ext),
  60521. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  60522. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  60523. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  60524. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  60525. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  60526. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  60527. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  60528. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  60529. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  60530. TEST_DECL(test_wolfSSL_X509V3_EXT),
  60531. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  60532. TEST_DECL(test_wolfSSL_X509_cmp),
  60533. TEST_DECL(test_GENERAL_NAME_set0_othername),
  60534. TEST_DECL(test_othername_and_SID_ext),
  60535. TEST_DECL(test_wolfSSL_dup_CA_list),
  60536. /* OpenSSL sk_X509 API test */
  60537. TEST_DECL(test_sk_X509),
  60538. /* OpenSSL sk_X509_CRL API test */
  60539. TEST_DECL(test_sk_X509_CRL),
  60540. /* OpenSSL X509 REQ API test */
  60541. TEST_DECL(test_X509_REQ),
  60542. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  60543. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  60544. TEST_DECL(test_X509_LOOKUP_add_dir),
  60545. /* RAND compatibility API */
  60546. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  60547. TEST_DECL(test_wolfSSL_RAND_bytes),
  60548. TEST_DECL(test_wolfSSL_RAND),
  60549. /* BN compatibility API */
  60550. TEST_DECL(test_wolfSSL_BN_CTX),
  60551. TEST_DECL(test_wolfSSL_BN),
  60552. TEST_DECL(test_wolfSSL_BN_init),
  60553. TEST_DECL(test_wolfSSL_BN_enc_dec),
  60554. TEST_DECL(test_wolfSSL_BN_word),
  60555. TEST_DECL(test_wolfSSL_BN_bits),
  60556. TEST_DECL(test_wolfSSL_BN_shift),
  60557. TEST_DECL(test_wolfSSL_BN_math),
  60558. TEST_DECL(test_wolfSSL_BN_math_mod),
  60559. TEST_DECL(test_wolfSSL_BN_math_other),
  60560. TEST_DECL(test_wolfSSL_BN_rand),
  60561. TEST_DECL(test_wolfSSL_BN_prime),
  60562. /* OpenSSL PKCS5 API test */
  60563. TEST_DECL(test_wolfSSL_PKCS5),
  60564. /* OpenSSL PKCS8 API test */
  60565. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  60566. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  60567. /* OpenSSL PKCS7 API test */
  60568. TEST_DECL(test_wolfssl_PKCS7),
  60569. TEST_DECL(test_wolfSSL_PKCS7_certs),
  60570. TEST_DECL(test_wolfSSL_PKCS7_sign),
  60571. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  60572. #ifndef NO_BIO
  60573. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  60574. #ifdef HAVE_SMIME
  60575. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  60576. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  60577. #endif /* HAVE_SMIME */
  60578. #endif /* !NO_BIO */
  60579. /* OpenSSL PKCS12 API test */
  60580. TEST_DECL(test_wolfSSL_PKCS12),
  60581. /* Can't memory test as callbacks use Assert. */
  60582. TEST_DECL(test_error_queue_per_thread),
  60583. TEST_DECL(test_wolfSSL_ERR_put_error),
  60584. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  60585. #ifndef NO_BIO
  60586. TEST_DECL(test_wolfSSL_ERR_print_errors),
  60587. #endif
  60588. TEST_DECL(test_OBJ_NAME_do_all),
  60589. TEST_DECL(test_wolfSSL_OBJ),
  60590. TEST_DECL(test_wolfSSL_OBJ_cmp),
  60591. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  60592. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  60593. #ifdef OPENSSL_ALL
  60594. TEST_DECL(test_wolfSSL_OBJ_ln),
  60595. TEST_DECL(test_wolfSSL_OBJ_sn),
  60596. #endif
  60597. #ifndef NO_BIO
  60598. TEST_DECL(test_wolfSSL_BIO_gets),
  60599. TEST_DECL(test_wolfSSL_BIO_puts),
  60600. TEST_DECL(test_wolfSSL_BIO_dump),
  60601. /* Can't memory test as server hangs. */
  60602. TEST_DECL(test_wolfSSL_BIO_should_retry),
  60603. TEST_DECL(test_wolfSSL_BIO_write),
  60604. TEST_DECL(test_wolfSSL_BIO_printf),
  60605. TEST_DECL(test_wolfSSL_BIO_f_md),
  60606. TEST_DECL(test_wolfSSL_BIO_up_ref),
  60607. TEST_DECL(test_wolfSSL_BIO_reset),
  60608. TEST_DECL(test_wolfSSL_BIO_get_len),
  60609. #endif
  60610. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60611. TEST_DECL(test_wolfSSL_check_domain),
  60612. #endif
  60613. TEST_DECL(test_wolfSSL_cert_cb),
  60614. /* Can't memory test as tcp_connect aborts. */
  60615. TEST_DECL(test_wolfSSL_SESSION),
  60616. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  60617. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  60618. TEST_DECL(test_wolfSSL_ticket_keys),
  60619. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  60620. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  60621. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  60622. TEST_DECL(test_wolfSSL_verify_mode),
  60623. TEST_DECL(test_wolfSSL_verify_depth),
  60624. TEST_DECL(test_wolfSSL_verify_result),
  60625. TEST_DECL(test_wolfSSL_msg_callback),
  60626. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  60627. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  60628. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  60629. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  60630. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  60631. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  60632. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  60633. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  60634. TEST_DECL(test_wolfSSL_PEM_read),
  60635. TEST_DECL(test_wolfSSL_OpenSSL_version),
  60636. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  60637. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  60638. TEST_DECL(test_CONF_modules_xxx),
  60639. #ifdef OPENSSL_ALL
  60640. TEST_DECL(test_wolfSSL_TXT_DB),
  60641. TEST_DECL(test_wolfSSL_NCONF),
  60642. #endif
  60643. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  60644. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  60645. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  60646. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  60647. TEST_DECL(test_CRYPTO_THREADID_xxx),
  60648. TEST_DECL(test_ENGINE_cleanup),
  60649. /* test the no op functions for compatibility */
  60650. TEST_DECL(test_no_op_functions),
  60651. /* OpenSSL error API tests */
  60652. TEST_DECL(test_ERR_load_crypto_strings),
  60653. #ifdef OPENSSL_ALL
  60654. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  60655. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  60656. TEST_DECL(test_wolfSSL_CTX_ctrl),
  60657. #endif /* OPENSSL_ALL */
  60658. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  60659. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  60660. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  60661. /*********************************
  60662. * Crypto API tests
  60663. *********************************/
  60664. TEST_DECL(test_wolfSSL_MD4),
  60665. TEST_DECL(test_wolfSSL_MD5),
  60666. TEST_DECL(test_wolfSSL_MD5_Transform),
  60667. TEST_DECL(test_wolfSSL_SHA),
  60668. TEST_DECL(test_wolfSSL_SHA_Transform),
  60669. TEST_DECL(test_wolfSSL_SHA224),
  60670. TEST_DECL(test_wolfSSL_SHA256),
  60671. TEST_DECL(test_wolfSSL_SHA256_Transform),
  60672. TEST_DECL(test_wolfSSL_SHA512_Transform),
  60673. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  60674. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  60675. TEST_DECL(test_wolfSSL_HMAC_CTX),
  60676. TEST_DECL(test_wolfSSL_HMAC),
  60677. TEST_DECL(test_wolfSSL_CMAC),
  60678. TEST_DECL(test_wolfSSL_DES),
  60679. TEST_DECL(test_wolfSSL_DES_ncbc),
  60680. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  60681. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  60682. TEST_DECL(test_wolfSSL_AES_encrypt),
  60683. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  60684. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  60685. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  60686. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  60687. TEST_DECL(test_wolfSSL_RC4),
  60688. TEST_DECL(test_wolfSSL_RSA),
  60689. TEST_DECL(test_wolfSSL_RSA_DER),
  60690. TEST_DECL(test_wolfSSL_RSA_print),
  60691. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  60692. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  60693. TEST_DECL(test_wolfSSL_RSA_get0_key),
  60694. TEST_DECL(test_wolfSSL_RSA_meth),
  60695. TEST_DECL(test_wolfSSL_RSA_verify),
  60696. TEST_DECL(test_wolfSSL_RSA_sign),
  60697. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  60698. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  60699. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  60700. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  60701. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  60702. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  60703. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  60704. TEST_DECL(test_wolfSSL_RSA_ex_data),
  60705. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  60706. TEST_DECL(test_wolfSSL_RSA_To_Der),
  60707. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  60708. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  60709. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  60710. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  60711. TEST_DECL(test_wolfSSL_DH),
  60712. TEST_DECL(test_wolfSSL_DH_dup),
  60713. TEST_DECL(test_wolfSSL_DH_check),
  60714. TEST_DECL(test_wolfSSL_DH_prime),
  60715. TEST_DECL(test_wolfSSL_DH_1536_prime),
  60716. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  60717. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  60718. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  60719. TEST_DECL(test_wolfSSL_d2i_DHparams),
  60720. TEST_DECL(test_wolfSSL_DH_LoadDer),
  60721. TEST_DECL(test_wolfSSL_i2d_DHparams),
  60722. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  60723. TEST_DECL(test_wolfSSL_EC_GROUP),
  60724. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  60725. TEST_DECL(test_wolfSSL_EC_POINT),
  60726. TEST_DECL(test_wolfSSL_SPAKE),
  60727. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  60728. TEST_DECL(test_EC_i2d),
  60729. TEST_DECL(test_wolfSSL_EC_curve),
  60730. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  60731. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  60732. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  60733. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  60734. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  60735. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  60736. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  60737. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  60738. TEST_DECL(test_ECDSA_size_sign),
  60739. TEST_DECL(test_ECDH_compute_key),
  60740. #endif
  60741. #ifdef OPENSSL_EXTRA
  60742. TEST_DECL(test_ED25519),
  60743. TEST_DECL(test_ED448),
  60744. #endif
  60745. TEST_DECL(test_DSA_do_sign_verify),
  60746. #ifdef OPENSSL_ALL
  60747. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  60748. TEST_DECL(test_wolfSSL_DSA_SIG),
  60749. #endif
  60750. TEST_DECL(test_openssl_generate_key_and_cert),
  60751. TEST_DECL(test_wolfSSL_FIPS_mode),
  60752. TEST_DECL(test_openssl_FIPS_drbg),
  60753. /*********************************
  60754. * CertManager API tests
  60755. *********************************/
  60756. TEST_DECL(test_wolfSSL_CertManagerAPI),
  60757. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  60758. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  60759. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  60760. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  60761. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  60762. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  60763. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  60764. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  60765. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  60766. TEST_DECL(test_wolfSSL_CertManagerCRL),
  60767. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  60768. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  60769. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  60770. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  60771. !defined(WOLFSSL_NO_CLIENT_AUTH))
  60772. TEST_DECL(test_various_pathlen_chains),
  60773. #endif
  60774. /*********************************
  60775. * SSL/TLS API tests
  60776. *********************************/
  60777. TEST_DECL(test_wolfSSL_Method_Allocators),
  60778. #ifndef NO_WOLFSSL_SERVER
  60779. TEST_DECL(test_wolfSSL_CTX_new),
  60780. #endif
  60781. TEST_DECL(test_server_wolfSSL_new),
  60782. TEST_DECL(test_client_wolfSSL_new),
  60783. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  60784. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  60785. TEST_DECL(test_for_double_Free),
  60786. #endif
  60787. TEST_DECL(test_wolfSSL_set_options),
  60788. #ifdef WOLFSSL_TLS13
  60789. /* TLS v1.3 API tests */
  60790. TEST_DECL(test_tls13_apis),
  60791. TEST_DECL(test_tls13_cipher_suites),
  60792. #endif
  60793. TEST_DECL(test_wolfSSL_tmp_dh),
  60794. TEST_DECL(test_wolfSSL_ctrl),
  60795. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  60796. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  60797. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  60798. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  60799. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  60800. #endif
  60801. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  60802. TEST_DECL(test_wolfSSL_security_level),
  60803. TEST_DECL(test_wolfSSL_SSL_in_init),
  60804. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  60805. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  60806. TEST_DECL(test_CONF_CTX_FILE),
  60807. TEST_DECL(test_CONF_CTX_CMDLINE),
  60808. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  60809. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  60810. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  60811. /* Bad certificate signature tests */
  60812. TEST_DECL(test_EccSigFailure_cm),
  60813. TEST_DECL(test_RsaSigFailure_cm),
  60814. #endif /* NO_CERTS */
  60815. /* PKCS8 testing */
  60816. TEST_DECL(test_wolfSSL_no_password_cb),
  60817. TEST_DECL(test_wolfSSL_PKCS8),
  60818. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  60819. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  60820. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60821. TEST_DECL(test_wolfSSL_get_finished),
  60822. /* Uses Assert in handshake callback. */
  60823. TEST_DECL(test_wolfSSL_CTX_add_session),
  60824. /* Large number of memory allocations. */
  60825. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  60826. /* Large number of memory allocations. */
  60827. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  60828. /* Large number of memory allocations. */
  60829. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  60830. /* Large number of memory allocations. */
  60831. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  60832. /* Large number of memory allocations. */
  60833. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  60834. /* Large number of memory allocations. */
  60835. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  60836. #endif
  60837. TEST_DECL(test_SSL_CIPHER_get_xxx),
  60838. TEST_DECL(test_wolfSSL_ERR_strings),
  60839. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  60840. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  60841. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  60842. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  60843. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  60844. /* Large number of memory allocations. */
  60845. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  60846. TEST_DECL(test_wolfSSL_CertRsaPss),
  60847. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  60848. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  60849. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  60850. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  60851. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  60852. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  60853. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  60854. TEST_DECL(test_multiple_crls_same_issuer),
  60855. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  60856. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  60857. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  60858. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  60859. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  60860. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  60861. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  60862. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  60863. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  60864. TEST_DECL(test_SetTmpEC_DHE_Sz),
  60865. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  60866. #ifdef WOLFSSL_DTLS
  60867. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  60868. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  60869. #endif
  60870. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  60871. /* Uses Assert in handshake callback. */
  60872. TEST_DECL(test_wolfSSL_dtls_plaintext),
  60873. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60874. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60875. TEST_DECL(test_wolfSSL_read_write),
  60876. /* Can't memory test as server hangs if client fails before second connect.
  60877. */
  60878. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  60879. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  60880. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  60881. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  60882. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  60883. /* Can't memory test as server hangs. */
  60884. TEST_DECL(test_wolfSSL_dtls_export),
  60885. /* Uses Assert in handshake callback. */
  60886. TEST_DECL(test_wolfSSL_tls_export),
  60887. #endif
  60888. TEST_DECL(test_wolfSSL_SetMinVersion),
  60889. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  60890. /* wolfSSL handshake APIs. */
  60891. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  60892. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  60893. TEST_DECL(test_wolfSSL_BUF),
  60894. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  60895. /* Can't memory test as server hangs. */
  60896. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  60897. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  60898. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  60899. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  60900. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  60901. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  60902. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  60903. /* Can't memory test as server hangs. */
  60904. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  60905. TEST_DECL(test_wolfSSL_Tls13_postauth),
  60906. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  60907. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  60908. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  60909. TEST_DECL(test_wolfSSL_THREADID_hash),
  60910. /* TLS extensions tests */
  60911. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60912. #ifdef HAVE_SNI
  60913. TEST_DECL(test_wolfSSL_UseSNI_params),
  60914. /* Uses Assert in handshake callback. */
  60915. TEST_DECL(test_wolfSSL_UseSNI_connection),
  60916. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  60917. #endif /* HAVE_SNI */
  60918. #endif
  60919. TEST_DECL(test_wolfSSL_UseTrustedCA),
  60920. TEST_DECL(test_wolfSSL_UseMaxFragment),
  60921. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  60922. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  60923. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60924. /* Uses Assert in handshake callback. */
  60925. TEST_DECL(test_wolfSSL_UseALPN_connection),
  60926. TEST_DECL(test_wolfSSL_UseALPN_params),
  60927. #endif
  60928. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  60929. /* Uses Assert in handshake callback. */
  60930. TEST_DECL(test_wolfSSL_set_alpn_protos),
  60931. #endif
  60932. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  60933. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  60934. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  60935. TEST_DECL(test_tls_ext_duplicate),
  60936. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  60937. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60938. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  60939. /* Uses Assert in handshake callback. */
  60940. TEST_DECL(test_wolfSSL_Tls13_ECH),
  60941. #endif
  60942. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  60943. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  60944. /* OCSP Stapling */
  60945. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  60946. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  60947. /* Multicast */
  60948. TEST_DECL(test_wolfSSL_mcast),
  60949. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  60950. TEST_DECL(test_wolfSSL_msgCb),
  60951. TEST_DECL(test_wolfSSL_either_side),
  60952. TEST_DECL(test_wolfSSL_DTLS_either_side),
  60953. /* Uses Assert in handshake callback. */
  60954. TEST_DECL(test_wolfSSL_dtls_fragments),
  60955. /* Uses Assert in handshake callback. */
  60956. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  60957. /* Uses Assert in handshake callback. */
  60958. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  60959. /* Uses Assert in handshake callback. */
  60960. TEST_DECL(test_wolfSSL_dtls_bad_record),
  60961. /* Uses Assert in handshake callback. */
  60962. TEST_DECL(test_wolfSSL_dtls_stateless),
  60963. TEST_DECL(test_generate_cookie),
  60964. #ifndef NO_BIO
  60965. /* Can't memory test as server hangs. */
  60966. TEST_DECL(test_wolfSSL_BIO_connect),
  60967. /* Can't memory test as server Asserts in thread. */
  60968. TEST_DECL(test_wolfSSL_BIO_accept),
  60969. TEST_DECL(test_wolfSSL_BIO_tls),
  60970. #endif
  60971. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  60972. TEST_DECL(test_DhCallbacks),
  60973. #endif
  60974. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60975. TEST_DECL(test_export_keying_material),
  60976. #endif
  60977. /* Can't memory test as client/server Asserts in thread. */
  60978. TEST_DECL(test_ticket_and_psk_mixing),
  60979. /* Can't memory test as client/server Asserts in thread. */
  60980. TEST_DECL(test_prioritize_psk),
  60981. /* Can't memory test as client/server hangs. */
  60982. TEST_DECL(test_wc_CryptoCb),
  60983. /* Can't memory test as client/server hangs. */
  60984. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  60985. #if !defined(NO_FILESYSTEM) && \
  60986. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60987. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60988. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  60989. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  60990. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  60991. #ifdef HAVE_MAX_FRAGMENT
  60992. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  60993. #endif /* HAVE_MAX_FRAGMENT */
  60994. #ifndef NO_RSA
  60995. TEST_DECL(test_wolfSSL_dtls_stateless2),
  60996. #if !defined(NO_OLD_TLS)
  60997. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  60998. #endif /* !defined(NO_OLD_TLS) */
  60999. #endif /* ! NO_RSA */
  61000. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61001. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  61002. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  61003. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  61004. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  61005. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  61006. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  61007. && defined(WOLFSSL_TLS13) && \
  61008. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  61009. TEST_DECL(test_ticket_nonce_malloc),
  61010. #endif
  61011. TEST_DECL(test_ticket_ret_create),
  61012. TEST_DECL(test_extra_alerts_wrong_cs),
  61013. TEST_DECL(test_extra_alerts_skip_hs),
  61014. TEST_DECL(test_extra_alerts_bad_psk),
  61015. /* Can't memory test as client/server Asserts. */
  61016. TEST_DECL(test_harden_no_secure_renegotiation),
  61017. TEST_DECL(test_override_alt_cert_chain),
  61018. TEST_DECL(test_rpk_set_xxx_cert_type),
  61019. TEST_DECL(test_tls13_rpk_handshake),
  61020. TEST_DECL(test_dtls13_bad_epoch_ch),
  61021. TEST_DECL(test_short_session_id),
  61022. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  61023. /* Can't memory test as client/server hangs. */
  61024. TEST_DECL(test_dtls_msg_from_other_peer),
  61025. TEST_DECL(test_dtls_ipv6_check),
  61026. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  61027. TEST_DECL(test_dtls_no_extensions),
  61028. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  61029. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  61030. TEST_DECL(test_session_ticket_no_id),
  61031. TEST_DECL(test_session_ticket_hs_update),
  61032. TEST_DECL(test_dtls_downgrade_scr_server),
  61033. TEST_DECL(test_dtls_downgrade_scr),
  61034. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  61035. TEST_DECL(test_dtls_client_hello_timeout),
  61036. TEST_DECL(test_dtls_dropped_ccs),
  61037. TEST_DECL(test_certreq_sighash_algos),
  61038. TEST_DECL(test_revoked_loaded_int_cert),
  61039. TEST_DECL(test_dtls_frag_ch),
  61040. TEST_DECL(test_dtls13_frag_ch_pq),
  61041. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  61042. TEST_DECL(test_tls13_pq_groups),
  61043. TEST_DECL(test_dtls13_early_data),
  61044. /* This test needs to stay at the end to clean up any caches allocated. */
  61045. TEST_DECL(test_wolfSSL_Cleanup)
  61046. };
  61047. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  61048. static void TestSetup(void)
  61049. {
  61050. /* Stub, for now. Add common test setup code here. */
  61051. }
  61052. static void TestCleanup(void)
  61053. {
  61054. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  61055. /* Clear any errors added to the error queue during the test run. */
  61056. wolfSSL_ERR_clear_error();
  61057. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  61058. }
  61059. /* Print out all API test cases with numeric identifier.
  61060. */
  61061. void ApiTest_PrintTestCases(void)
  61062. {
  61063. int i;
  61064. printf("All Test Cases:\n");
  61065. for (i = 0; i < TEST_CASE_CNT; i++) {
  61066. printf("%3d: %s\n", i + 1, testCases[i].name);
  61067. }
  61068. }
  61069. /* Add test case with index to the list to run.
  61070. *
  61071. * @param [in] idx Index of test case to run starting at 1.
  61072. * @return 0 on success.
  61073. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  61074. */
  61075. int ApiTest_RunIdx(int idx)
  61076. {
  61077. if (idx < 1 || idx > TEST_CASE_CNT) {
  61078. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  61079. return BAD_FUNC_ARG;
  61080. }
  61081. testAll = 0;
  61082. testCases[idx-1].run = 1;
  61083. return 0;
  61084. }
  61085. /* Add test case with name to the list to run.
  61086. *
  61087. * @param [in] name Name of test case to run.
  61088. * @return 0 on success.
  61089. * @return BAD_FUNC_ARG when name is not a known test case name.
  61090. */
  61091. int ApiTest_RunName(char* name)
  61092. {
  61093. int i;
  61094. for (i = 0; i < TEST_CASE_CNT; i++) {
  61095. if (XSTRCMP(testCases[i].name, name) == 0) {
  61096. testAll = 0;
  61097. testCases[i].run = 1;
  61098. return 0;
  61099. }
  61100. }
  61101. printf("Test case name not found: %s\n", name);
  61102. printf("Use --list to see all test case names.\n");
  61103. return BAD_FUNC_ARG;
  61104. }
  61105. /* Converts the result code to a string.
  61106. *
  61107. * @param [in] res Test result code.
  61108. * @return String describing test result.
  61109. */
  61110. static const char* apitest_res_string(int res)
  61111. {
  61112. const char* str = "invalid result";
  61113. switch (res) {
  61114. case TEST_SUCCESS:
  61115. str = "passed";
  61116. break;
  61117. case TEST_FAIL:
  61118. str = "failed";
  61119. break;
  61120. case TEST_SKIPPED:
  61121. str = "skipped";
  61122. break;
  61123. }
  61124. return str;
  61125. }
  61126. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61127. static double gettime_secs(void)
  61128. #if defined(_MSC_VER) && defined(_WIN32)
  61129. {
  61130. /* there's no gettimeofday for Windows, so we'll use system time */
  61131. #define EPOCH_DIFF 11644473600LL
  61132. FILETIME currentFileTime;
  61133. GetSystemTimePreciseAsFileTime(&currentFileTime);
  61134. ULARGE_INTEGER uli = { 0, 0 };
  61135. uli.LowPart = currentFileTime.dwLowDateTime;
  61136. uli.HighPart = currentFileTime.dwHighDateTime;
  61137. /* Convert to seconds since Unix epoch */
  61138. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  61139. }
  61140. #else
  61141. {
  61142. struct timeval tv;
  61143. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  61144. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  61145. }
  61146. #endif
  61147. #endif
  61148. int ApiTest(void)
  61149. {
  61150. int i;
  61151. int ret;
  61152. int res = 0;
  61153. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61154. double timeDiff;
  61155. #endif
  61156. printf(" Begin API Tests\n");
  61157. fflush(stdout);
  61158. /* we must perform init and cleanup if not all tests are running */
  61159. if (!testAll) {
  61160. #ifdef WOLFCRYPT_ONLY
  61161. if (wolfCrypt_Init() != 0) {
  61162. printf("wolfCrypt Initialization failed\n");
  61163. res = 1;
  61164. }
  61165. #else
  61166. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  61167. printf("wolfSSL Initialization failed\n");
  61168. res = 1;
  61169. }
  61170. #endif
  61171. }
  61172. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61173. if (res == 0) {
  61174. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  61175. printf("failed to create tmp dir\n");
  61176. res = 1;
  61177. }
  61178. else {
  61179. tmpDirNameSet = 1;
  61180. }
  61181. }
  61182. #endif
  61183. if (res == 0) {
  61184. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61185. EXPECT_DECLS;
  61186. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61187. currentTestName = testCases[i].name;
  61188. #endif
  61189. /* When not testing all cases then skip if not marked for running.
  61190. */
  61191. if (!testAll && !testCases[i].run) {
  61192. continue;
  61193. }
  61194. TestSetup();
  61195. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  61196. fflush(stdout);
  61197. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61198. timeDiff = gettime_secs();
  61199. #endif
  61200. ret = testCases[i].func();
  61201. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61202. timeDiff = gettime_secs() - timeDiff;
  61203. #endif
  61204. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61205. if (ret != TEST_SKIPPED) {
  61206. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  61207. }
  61208. else
  61209. #endif
  61210. {
  61211. printf(" %s\n", apitest_res_string(ret));
  61212. }
  61213. fflush(stdout);
  61214. /* if return code is < 0 and not skipped then assert error */
  61215. Expect((ret > 0 || ret == TEST_SKIPPED),
  61216. ("Test failed\n"),
  61217. ("ret %d", ret));
  61218. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  61219. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  61220. TestCleanup();
  61221. }
  61222. }
  61223. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  61224. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  61225. wc_ecc_fp_free(); /* free per thread cache */
  61226. #endif
  61227. if (!testAll) {
  61228. #ifdef WOLFCRYPT_ONLY
  61229. wolfCrypt_Cleanup();
  61230. #else
  61231. wolfSSL_Cleanup();
  61232. #endif
  61233. }
  61234. (void)testDevId;
  61235. if (res != 0) {
  61236. printf("\nFAILURES:\n");
  61237. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61238. if (testCases[i].fail) {
  61239. printf(" %3d: %s\n", i + 1, testCases[i].name);
  61240. }
  61241. }
  61242. printf("\n");
  61243. fflush(stdout);
  61244. }
  61245. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61246. if (tmpDirNameSet) {
  61247. printf("\nBinary dumps of the memio streams can be found in the\n"
  61248. "%s directory. This can be imported into\n"
  61249. "Wireshark by transforming the file with\n"
  61250. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  61251. "And then loading test_output.dump.hex into Wireshark using\n"
  61252. "the \"Import from Hex Dump...\" option and selecting the\n"
  61253. "TCP encapsulation option.\n", tmpDirName);
  61254. }
  61255. #endif
  61256. printf(" End API Tests\n");
  61257. fflush(stdout);
  61258. return res;
  61259. }