api.c 2.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122
  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. #endif /* WOLFSSL_STATIC_MEMORY */
  45. #ifndef HEAP_HINT
  46. #define HEAP_HINT NULL
  47. #endif /* WOLFSSL_STAIC_MEMORY */
  48. #ifdef WOLFSSL_ASNC_CRYPT
  49. #include <wolfssl/wolfcrypt/async.h>
  50. #endif
  51. #ifdef HAVE_ECC
  52. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  53. #ifndef ECC_ASN963_MAX_BUF_SZ
  54. #define ECC_ASN963_MAX_BUF_SZ 133
  55. #endif
  56. #ifndef ECC_PRIV_KEY_BUF
  57. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  58. #endif
  59. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  60. /* logic to choose right key ECC size */
  61. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  62. #define KEY14 14
  63. #else
  64. #define KEY14 32
  65. #endif
  66. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  67. #define KEY16 16
  68. #else
  69. #define KEY16 32
  70. #endif
  71. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  72. #define KEY20 20
  73. #else
  74. #define KEY20 32
  75. #endif
  76. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  77. #define KEY24 24
  78. #else
  79. #define KEY24 32
  80. #endif
  81. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  82. #define KEY28 28
  83. #else
  84. #define KEY28 32
  85. #endif
  86. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  87. #define KEY30 30
  88. #else
  89. #define KEY30 32
  90. #endif
  91. #define KEY32 32
  92. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  93. #define KEY40 40
  94. #else
  95. #define KEY40 32
  96. #endif
  97. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  98. #define KEY48 48
  99. #else
  100. #define KEY48 32
  101. #endif
  102. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  103. #define KEY64 64
  104. #else
  105. #define KEY64 32
  106. #endif
  107. #if !defined(HAVE_COMP_KEY)
  108. #if !defined(NOCOMP)
  109. #define NOCOMP 0
  110. #endif
  111. #else
  112. #if !defined(COMP)
  113. #define COMP 1
  114. #endif
  115. #endif
  116. #if !defined(DER_SZ)
  117. #define DER_SZ(ks) ((ks) * 2 + 1)
  118. #endif
  119. #endif
  120. #ifndef NO_ASN
  121. #include <wolfssl/wolfcrypt/asn_public.h>
  122. #endif
  123. #include <wolfssl/error-ssl.h>
  124. #include <stdlib.h>
  125. #include <wolfssl/ssl.h> /* compatibility layer */
  126. #include <wolfssl/test.h>
  127. #include <tests/unit.h>
  128. #include "examples/server/server.h"
  129. /* for testing compatibility layer callbacks */
  130. #ifndef NO_MD5
  131. #include <wolfssl/wolfcrypt/md5.h>
  132. #endif
  133. #ifndef NO_SHA
  134. #include <wolfssl/wolfcrypt/sha.h>
  135. #endif
  136. #ifndef NO_SHA256
  137. #include <wolfssl/wolfcrypt/sha256.h>
  138. #endif
  139. #ifdef WOLFSSL_SHA512
  140. #include <wolfssl/wolfcrypt/sha512.h>
  141. #endif
  142. #ifdef WOLFSSL_SHA384
  143. #include <wolfssl/wolfcrypt/sha512.h>
  144. #endif
  145. #ifdef WOLFSSL_SHA3
  146. #include <wolfssl/wolfcrypt/sha3.h>
  147. #ifndef HEAP_HINT
  148. #define HEAP_HINT NULL
  149. #endif
  150. #endif
  151. #ifndef NO_AES
  152. #include <wolfssl/wolfcrypt/aes.h>
  153. #ifdef HAVE_AES_DECRYPT
  154. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  155. #endif
  156. #endif
  157. #ifdef WOLFSSL_RIPEMD
  158. #include <wolfssl/wolfcrypt/ripemd.h>
  159. #endif
  160. #ifndef NO_DES3
  161. #include <wolfssl/wolfcrypt/des3.h>
  162. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  163. #endif
  164. #ifdef WC_RC2
  165. #include <wolfssl/wolfcrypt/rc2.h>
  166. #endif
  167. #ifndef NO_HMAC
  168. #include <wolfssl/wolfcrypt/hmac.h>
  169. #endif
  170. #ifdef HAVE_CHACHA
  171. #include <wolfssl/wolfcrypt/chacha.h>
  172. #endif
  173. #ifdef HAVE_POLY1305
  174. #include <wolfssl/wolfcrypt/poly1305.h>
  175. #endif
  176. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  177. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  178. #endif
  179. #ifdef HAVE_CAMELLIA
  180. #include <wolfssl/wolfcrypt/camellia.h>
  181. #endif
  182. #ifndef NO_RC4
  183. #include <wolfssl/wolfcrypt/arc4.h>
  184. #endif
  185. #ifdef HAVE_BLAKE2
  186. #include <wolfssl/wolfcrypt/blake2.h>
  187. #endif
  188. #include <wolfssl/wolfcrypt/hash.h>
  189. #ifndef NO_RSA
  190. #include <wolfssl/wolfcrypt/rsa.h>
  191. #define FOURK_BUF 4096
  192. #define GEN_BUF 294
  193. #ifndef USER_CRYPTO_ERROR
  194. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  195. #endif
  196. #endif
  197. #ifndef NO_SIG_WRAPPER
  198. #include <wolfssl/wolfcrypt/signature.h>
  199. #endif
  200. #ifdef HAVE_AESCCM
  201. #include <wolfssl/wolfcrypt/aes.h>
  202. #endif
  203. #ifdef HAVE_PKCS7
  204. #include <wolfssl/wolfcrypt/pkcs7.h>
  205. #include <wolfssl/wolfcrypt/asn.h>
  206. #ifdef HAVE_LIBZ
  207. #include <wolfssl/wolfcrypt/compress.h>
  208. #endif
  209. #endif
  210. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  211. #include <wolfssl/wolfcrypt/asn.h>
  212. #endif
  213. #ifndef NO_DSA
  214. #include <wolfssl/wolfcrypt/dsa.h>
  215. #ifndef ONEK_BUF
  216. #define ONEK_BUF 1024
  217. #endif
  218. #ifndef TWOK_BUF
  219. #define TWOK_BUF 2048
  220. #endif
  221. #ifndef FOURK_BUF
  222. #define FOURK_BUF 4096
  223. #endif
  224. #ifndef DSA_SIG_SIZE
  225. #define DSA_SIG_SIZE 40
  226. #endif
  227. #ifndef MAX_DSA_PARAM_SIZE
  228. #define MAX_DSA_PARAM_SIZE 256
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_CMAC
  232. #include <wolfssl/wolfcrypt/cmac.h>
  233. #endif
  234. #ifdef HAVE_ED25519
  235. #include <wolfssl/wolfcrypt/ed25519.h>
  236. #endif
  237. #ifdef HAVE_CURVE25519
  238. #include <wolfssl/wolfcrypt/curve25519.h>
  239. #endif
  240. #ifdef HAVE_ED448
  241. #include <wolfssl/wolfcrypt/ed448.h>
  242. #endif
  243. #ifdef HAVE_CURVE448
  244. #include <wolfssl/wolfcrypt/curve448.h>
  245. #endif
  246. #ifdef HAVE_PKCS12
  247. #include <wolfssl/wolfcrypt/pkcs12.h>
  248. #endif
  249. #include <wolfssl/wolfcrypt/logging.h>
  250. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  251. #include <wolfssl/openssl/ssl.h>
  252. #ifndef NO_ASN
  253. /* for ASN_COMMON_NAME DN_tags enum */
  254. #include <wolfssl/wolfcrypt/asn.h>
  255. #endif
  256. #ifdef HAVE_OCSP
  257. #include <wolfssl/openssl/ocsp.h>
  258. #endif
  259. #endif
  260. #ifdef OPENSSL_EXTRA
  261. #include <wolfssl/openssl/cmac.h>
  262. #include <wolfssl/openssl/x509v3.h>
  263. #include <wolfssl/openssl/asn1.h>
  264. #include <wolfssl/openssl/crypto.h>
  265. #include <wolfssl/openssl/pkcs12.h>
  266. #include <wolfssl/openssl/evp.h>
  267. #include <wolfssl/openssl/dh.h>
  268. #include <wolfssl/openssl/bn.h>
  269. #include <wolfssl/openssl/buffer.h>
  270. #include <wolfssl/openssl/pem.h>
  271. #include <wolfssl/openssl/ec.h>
  272. #include <wolfssl/openssl/ecdh.h>
  273. #include <wolfssl/openssl/engine.h>
  274. #include <wolfssl/openssl/hmac.h>
  275. #include <wolfssl/openssl/objects.h>
  276. #include <wolfssl/openssl/rand.h>
  277. #include <wolfssl/openssl/modes.h>
  278. #include <wolfssl/openssl/fips_rand.h>
  279. #include <wolfssl/openssl/kdf.h>
  280. #ifdef OPENSSL_ALL
  281. #include <wolfssl/openssl/txt_db.h>
  282. #include <wolfssl/openssl/lhash.h>
  283. #endif
  284. #ifndef NO_AES
  285. #include <wolfssl/openssl/aes.h>
  286. #endif
  287. #ifndef NO_DES3
  288. #include <wolfssl/openssl/des.h>
  289. #endif
  290. #ifdef HAVE_ECC
  291. #include <wolfssl/openssl/ecdsa.h>
  292. #endif
  293. #ifdef HAVE_PKCS7
  294. #include <wolfssl/openssl/pkcs7.h>
  295. #endif
  296. #ifdef HAVE_ED25519
  297. #include <wolfssl/openssl/ed25519.h>
  298. #endif
  299. #ifdef HAVE_ED448
  300. #include <wolfssl/openssl/ed448.h>
  301. #endif
  302. #endif /* OPENSSL_EXTRA */
  303. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  304. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  305. #include <wolfssl/wolfcrypt/srp.h>
  306. #endif
  307. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  308. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  309. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  310. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  311. defined(HAVE_ECH) || defined(HAVE_EX_DATA)
  312. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  313. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  314. * number tracking */
  315. #include "wolfssl/internal.h"
  316. #endif
  317. /* force enable test buffers */
  318. #ifndef USE_CERT_BUFFERS_2048
  319. #define USE_CERT_BUFFERS_2048
  320. #endif
  321. #ifndef USE_CERT_BUFFERS_256
  322. #define USE_CERT_BUFFERS_256
  323. #endif
  324. #include <wolfssl/certs_test.h>
  325. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  326. /* FIPS build has replaced ecc.h. */
  327. #define wc_ecc_key_get_priv(key) (&((key)->k))
  328. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  329. #endif
  330. typedef struct testVector {
  331. const char* input;
  332. const char* output;
  333. size_t inLen;
  334. size_t outLen;
  335. } testVector;
  336. #if defined(HAVE_PKCS7)
  337. typedef struct {
  338. const byte* content;
  339. word32 contentSz;
  340. int contentOID;
  341. int encryptOID;
  342. int keyWrapOID;
  343. int keyAgreeOID;
  344. byte* cert;
  345. size_t certSz;
  346. byte* privateKey;
  347. word32 privateKeySz;
  348. } pkcs7EnvelopedVector;
  349. #ifndef NO_PKCS7_ENCRYPTED_DATA
  350. typedef struct {
  351. const byte* content;
  352. word32 contentSz;
  353. int contentOID;
  354. int encryptOID;
  355. byte* encryptionKey;
  356. word32 encryptionKeySz;
  357. } pkcs7EncryptedVector;
  358. #endif
  359. #endif /* HAVE_PKCS7 */
  360. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  361. typedef int (*ssl_cb)(WOLFSSL* ssl);
  362. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  363. typedef struct test_ssl_cbf {
  364. method_provider method;
  365. ctx_cb ctx_ready;
  366. ssl_cb ssl_ready;
  367. ssl_cb on_result;
  368. ssl_cb on_cleanup;
  369. WOLFSSL_CTX* ctx;
  370. const char* caPemFile;
  371. const char* certPemFile;
  372. const char* keyPemFile;
  373. const char* crlPemFile;
  374. #ifdef WOLFSSL_STATIC_MEMORY
  375. byte* mem;
  376. word32 memSz;
  377. wolfSSL_method_func method_ex;
  378. #endif
  379. int devId;
  380. int return_code;
  381. int last_err;
  382. unsigned char isSharedCtx:1;
  383. unsigned char loadToSSL:1;
  384. unsigned char ticNoInit:1;
  385. unsigned char doUdp:1;
  386. } test_ssl_cbf;
  387. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  388. typedef struct test_ssl_memio_ctx {
  389. WOLFSSL_CTX* s_ctx;
  390. WOLFSSL_CTX* c_ctx;
  391. WOLFSSL* s_ssl;
  392. WOLFSSL* c_ssl;
  393. const char* c_ciphers;
  394. const char* s_ciphers;
  395. char* c_msg;
  396. int c_msglen;
  397. char* s_msg;
  398. int s_msglen;
  399. test_ssl_cbf s_cb;
  400. test_ssl_cbf c_cb;
  401. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  402. int c_len;
  403. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  404. int s_len;
  405. } test_ssl_memio_ctx;
  406. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  407. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  408. /*----------------------------------------------------------------------------*
  409. | Constants
  410. *----------------------------------------------------------------------------*/
  411. /* Test result constants and macros. */
  412. /* Test succeeded. */
  413. #define TEST_SUCCESS (1)
  414. /* Test failed. */
  415. #define TEST_FAIL (0)
  416. /* Test skipped - not run. */
  417. #define TEST_SKIPPED (-7777)
  418. /* Returns the result based on whether check is true.
  419. *
  420. * @param [in] check Condition for success.
  421. * @return When condition is true: TEST_SUCCESS.
  422. * @return When condition is false: TEST_FAIL.
  423. */
  424. #ifdef DEBUG_WOLFSSL_VERBOSE
  425. #define XSTRINGIFY(s) STRINGIFY(s)
  426. #define STRINGIFY(s) #s
  427. #define TEST_RES_CHECK(check) ({ \
  428. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  429. if (_ret == TEST_FAIL) { \
  430. fprintf(stderr, " check \"%s\" at %d ", \
  431. XSTRINGIFY(check), __LINE__); \
  432. } \
  433. _ret; })
  434. #else
  435. #define TEST_RES_CHECK(check) \
  436. ((check) ? TEST_SUCCESS : TEST_FAIL)
  437. #endif /* DEBUG_WOLFSSL_VERBOSE */
  438. #define TEST_STRING "Everyone gets Friday off."
  439. #define TEST_STRING_SZ 25
  440. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  441. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  442. #define TEST_RSA_BITS 1024
  443. #else
  444. #define TEST_RSA_BITS 2048
  445. #endif
  446. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  447. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  448. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  449. static const char* bogusFile =
  450. #ifdef _WIN32
  451. "NUL"
  452. #else
  453. "/dev/null"
  454. #endif
  455. ;
  456. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  457. enum {
  458. TESTING_RSA = 1,
  459. TESTING_ECC = 2
  460. };
  461. #ifdef WOLFSSL_QNX_CAAM
  462. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  463. static int testDevId = WOLFSSL_CAAM_DEVID;
  464. #else
  465. static int testDevId = INVALID_DEVID;
  466. #endif
  467. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  468. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  469. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  470. #define HAVE_IO_TESTS_DEPENDENCIES
  471. #endif
  472. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  473. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  474. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  475. #endif
  476. /*----------------------------------------------------------------------------*
  477. | BIO with fixed read/write size
  478. *----------------------------------------------------------------------------*/
  479. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  480. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  481. int len)
  482. {
  483. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  484. len = 0;
  485. }
  486. else {
  487. if (bio->wrSz - bio->wrIdx < len) {
  488. len = bio->wrSz - bio->wrIdx;
  489. }
  490. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  491. bio->wrIdx += len;
  492. }
  493. return len;
  494. }
  495. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  496. {
  497. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  498. len = 0;
  499. }
  500. else {
  501. if (bio->wrSz - bio->rdIdx < len) {
  502. len = bio->wrSz - bio->rdIdx;
  503. }
  504. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  505. bio->rdIdx += len;
  506. }
  507. return len;
  508. }
  509. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  510. {
  511. static WOLFSSL_BIO_METHOD meth;
  512. meth.type = WOLFSSL_BIO_BIO;
  513. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  514. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  515. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  516. return &meth;
  517. }
  518. #endif
  519. /*----------------------------------------------------------------------------*
  520. | Setup
  521. *----------------------------------------------------------------------------*/
  522. static int test_wolfSSL_Init(void)
  523. {
  524. int result;
  525. result = wolfSSL_Init();
  526. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  527. return result;
  528. }
  529. static int test_wolfSSL_Cleanup(void)
  530. {
  531. int result;
  532. result = wolfSSL_Cleanup();
  533. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  534. return result;
  535. }
  536. /* Initialize the wolfCrypt state.
  537. * POST: 0 success.
  538. */
  539. static int test_wolfCrypt_Init(void)
  540. {
  541. int result;
  542. result = wolfCrypt_Init();
  543. result = TEST_RES_CHECK(result == 0);
  544. return result;
  545. } /* END test_wolfCrypt_Init */
  546. static int test_wolfCrypt_Cleanup(void)
  547. {
  548. int result;
  549. result = wolfCrypt_Cleanup();
  550. result = TEST_RES_CHECK(result == 0);
  551. return result;
  552. }
  553. /*----------------------------------------------------------------------------*
  554. | Platform dependent function test
  555. *----------------------------------------------------------------------------*/
  556. static int test_fileAccess(void)
  557. {
  558. int res = TEST_SKIPPED;
  559. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  560. EXPECT_DECLS;
  561. const char *fname[] = {
  562. svrCertFile, svrKeyFile, caCertFile,
  563. eccCertFile, eccKeyFile, eccRsaCertFile,
  564. cliCertFile, cliCertDerFile, cliKeyFile,
  565. dhParamFile,
  566. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  567. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  568. NULL
  569. };
  570. const char derfile[] = "./certs/server-cert.der";
  571. XFILE f = XBADFILE;
  572. size_t sz;
  573. byte *buff = NULL;
  574. int i;
  575. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  576. for (i=0; fname[i] != NULL ; i++) {
  577. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  578. XFCLOSE(f);
  579. }
  580. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  581. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  582. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  583. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  584. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  585. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  586. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  587. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  588. XFCLOSE(f);
  589. res = EXPECT_RESULT();
  590. #endif
  591. return res;
  592. }
  593. /*----------------------------------------------------------------------------*
  594. | Method Allocators
  595. *----------------------------------------------------------------------------*/
  596. static int test_wolfSSL_Method_Allocators(void)
  597. {
  598. EXPECT_DECLS;
  599. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  600. do { \
  601. WOLFSSL_METHOD *method = NULL; \
  602. condition(method = allocator()); \
  603. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  604. } while (0)
  605. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  606. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  607. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  608. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  609. #ifndef NO_OLD_TLS
  610. #ifdef WOLFSSL_ALLOW_SSLV3
  611. #ifndef NO_WOLFSSL_SERVER
  612. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  613. #endif
  614. #ifndef NO_WOLFSSL_CLIENT
  615. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  616. #endif
  617. #endif
  618. #ifdef WOLFSSL_ALLOW_TLSV10
  619. #ifndef NO_WOLFSSL_SERVER
  620. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  621. #endif
  622. #ifndef NO_WOLFSSL_CLIENT
  623. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  624. #endif
  625. #endif
  626. #ifndef NO_WOLFSSL_SERVER
  627. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  628. #endif
  629. #ifndef NO_WOLFSSL_CLIENT
  630. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  631. #endif
  632. #endif /* !NO_OLD_TLS */
  633. #ifndef WOLFSSL_NO_TLS12
  634. #ifndef NO_WOLFSSL_SERVER
  635. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  636. #endif
  637. #ifndef NO_WOLFSSL_CLIENT
  638. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  639. #endif
  640. #endif /* !WOLFSSL_NO_TLS12 */
  641. #ifdef WOLFSSL_TLS13
  642. #ifndef NO_WOLFSSL_SERVER
  643. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  644. #endif
  645. #ifndef NO_WOLFSSL_CLIENT
  646. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  647. #endif
  648. #endif /* WOLFSSL_TLS13 */
  649. #ifndef NO_WOLFSSL_SERVER
  650. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  651. #endif
  652. #ifndef NO_WOLFSSL_CLIENT
  653. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  654. #endif
  655. #ifdef WOLFSSL_DTLS
  656. #ifndef NO_OLD_TLS
  657. #ifndef NO_WOLFSSL_SERVER
  658. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  659. #endif
  660. #ifndef NO_WOLFSSL_CLIENT
  661. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  662. #endif
  663. #endif
  664. #ifndef WOLFSSL_NO_TLS12
  665. #ifndef NO_WOLFSSL_SERVER
  666. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  667. #endif
  668. #ifndef NO_WOLFSSL_CLIENT
  669. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  670. #endif
  671. #endif
  672. #endif /* WOLFSSL_DTLS */
  673. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  674. /* Stubs */
  675. #ifndef NO_WOLFSSL_SERVER
  676. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  677. #endif
  678. #ifndef NO_WOLFSSL_CLIENT
  679. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  680. #endif
  681. #endif
  682. /* Test Either Method (client or server) */
  683. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  684. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  685. #ifndef NO_OLD_TLS
  686. #ifdef WOLFSSL_ALLOW_TLSV10
  687. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  688. #endif
  689. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  690. #endif /* !NO_OLD_TLS */
  691. #ifndef WOLFSSL_NO_TLS12
  692. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  693. #endif /* !WOLFSSL_NO_TLS12 */
  694. #ifdef WOLFSSL_TLS13
  695. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  696. #endif /* WOLFSSL_TLS13 */
  697. #ifdef WOLFSSL_DTLS
  698. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  699. #ifndef NO_OLD_TLS
  700. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  701. #endif /* !NO_OLD_TLS */
  702. #ifndef WOLFSSL_NO_TLS12
  703. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  704. #endif /* !WOLFSSL_NO_TLS12 */
  705. #endif /* WOLFSSL_DTLS */
  706. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  707. return EXPECT_RESULT();
  708. }
  709. /*----------------------------------------------------------------------------*
  710. | Context
  711. *----------------------------------------------------------------------------*/
  712. #ifndef NO_WOLFSSL_SERVER
  713. static int test_wolfSSL_CTX_new(void)
  714. {
  715. EXPECT_DECLS;
  716. WOLFSSL_CTX *ctx;
  717. WOLFSSL_METHOD* method;
  718. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  719. ExpectNotNull(method = wolfSSLv23_server_method());
  720. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  721. wolfSSL_CTX_free(ctx);
  722. return EXPECT_RESULT();
  723. }
  724. #endif
  725. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  726. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  727. static int test_for_double_Free(void)
  728. {
  729. EXPECT_DECLS;
  730. WOLFSSL_CTX* ctx = NULL;
  731. WOLFSSL* ssl = NULL;
  732. int skipTest = 0;
  733. const char* testCertFile;
  734. const char* testKeyFile;
  735. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  736. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  737. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  738. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  739. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  740. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  741. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  742. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  743. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  744. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  745. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  746. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  747. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  748. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  749. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  750. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  751. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  752. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  753. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  754. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  755. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  756. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  757. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  758. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  759. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  760. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  761. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  762. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  763. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  764. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  765. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  766. #ifdef OPENSSL_EXTRA
  767. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  768. "!SRP:!kRSA:!aNULL:!eNULL";
  769. #endif
  770. #ifndef NO_RSA
  771. testCertFile = svrCertFile;
  772. testKeyFile = svrKeyFile;
  773. #elif defined(HAVE_ECC)
  774. testCertFile = eccCertFile;
  775. testKeyFile = eccKeyFile;
  776. #else
  777. skipTest = 1;
  778. #endif
  779. if (skipTest != 1) {
  780. #ifndef NO_WOLFSSL_SERVER
  781. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  782. #else
  783. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  784. #endif
  785. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  786. WOLFSSL_FILETYPE_PEM));
  787. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  788. WOLFSSL_FILETYPE_PEM));
  789. ExpectNotNull(ssl = wolfSSL_new(ctx));
  790. /* First test freeing SSL, then CTX */
  791. wolfSSL_free(ssl);
  792. ssl = NULL;
  793. wolfSSL_CTX_free(ctx);
  794. ctx = NULL;
  795. #ifndef NO_WOLFSSL_CLIENT
  796. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  797. #else
  798. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  799. #endif
  800. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  801. WOLFSSL_FILETYPE_PEM));
  802. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  803. WOLFSSL_FILETYPE_PEM));
  804. ExpectNotNull(ssl = wolfSSL_new(ctx));
  805. /* Next test freeing CTX then SSL */
  806. wolfSSL_CTX_free(ctx);
  807. ctx = NULL;
  808. wolfSSL_free(ssl);
  809. ssl = NULL;
  810. #ifndef NO_WOLFSSL_SERVER
  811. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  812. #else
  813. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  814. #endif
  815. /* Test setting ciphers at ctx level */
  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. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  821. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  822. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  823. /* only update TLSv13 suites */
  824. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  825. #endif
  826. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  827. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  828. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  829. /* only update pre-TLSv13 suites */
  830. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  831. "ECDHE-RSA-AES128-GCM-SHA256"));
  832. #endif
  833. #ifdef OPENSSL_EXTRA
  834. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  835. #endif
  836. ExpectNotNull(ssl = wolfSSL_new(ctx));
  837. wolfSSL_CTX_free(ctx);
  838. ctx = NULL;
  839. wolfSSL_free(ssl);
  840. ssl = NULL;
  841. #ifndef NO_WOLFSSL_CLIENT
  842. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  843. #else
  844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  845. #endif
  846. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  847. WOLFSSL_FILETYPE_PEM));
  848. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  849. WOLFSSL_FILETYPE_PEM));
  850. ExpectNotNull(ssl = wolfSSL_new(ctx));
  851. /* test setting ciphers at SSL level */
  852. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  853. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  854. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  855. /* only update TLSv13 suites */
  856. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  857. #endif
  858. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  859. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  860. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  861. /* only update pre-TLSv13 suites */
  862. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  863. #endif
  864. wolfSSL_CTX_free(ctx);
  865. ctx = NULL;
  866. wolfSSL_free(ssl);
  867. ssl = NULL;
  868. }
  869. return EXPECT_RESULT();
  870. }
  871. #endif
  872. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  873. {
  874. int res = TEST_SKIPPED;
  875. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  876. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  877. (!defined(NO_RSA) || defined(HAVE_ECC))
  878. EXPECT_DECLS;
  879. const char* testCertFile;
  880. const char* testKeyFile;
  881. WOLFSSL_CTX* ctx = NULL;
  882. WOLFSSL* ssl = NULL;
  883. const byte cipherList[] =
  884. {
  885. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  886. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  887. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  888. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  889. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  890. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  891. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  892. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  893. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  894. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  895. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  896. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  897. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  898. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  899. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  900. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  901. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  902. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  903. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  904. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  905. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  906. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  907. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  908. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  909. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  910. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  911. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  912. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  913. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  914. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  915. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  916. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  917. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  918. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  919. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  920. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  921. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  922. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  923. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  924. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  925. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  926. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  927. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  928. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  929. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  930. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  931. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  932. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  933. /* SHA256 */
  934. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  935. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  936. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  937. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  938. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  939. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  940. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  941. /* SHA384 */
  942. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  943. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  944. /* AES-GCM */
  945. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  946. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  947. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  948. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  949. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  950. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  951. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  952. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  953. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  954. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  955. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  956. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  957. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  958. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  959. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  960. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  961. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  962. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  963. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  964. * also, in some of the other AES-CCM suites
  965. * there will be second byte number conflicts
  966. * with non-ECC AES-GCM */
  967. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  968. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  969. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  970. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  971. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  972. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  973. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  974. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  975. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  976. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  977. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  978. /* Camellia */
  979. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  980. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  981. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  982. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  983. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  984. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  985. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  986. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  987. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  988. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  989. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  990. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  991. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  992. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  993. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  994. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  995. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  996. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  997. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  998. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  999. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1000. /* TLS v1.3 cipher suites */
  1001. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1002. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1003. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1004. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1005. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1006. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1007. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1008. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1009. };
  1010. #ifndef NO_RSA
  1011. testCertFile = svrCertFile;
  1012. testKeyFile = svrKeyFile;
  1013. #elif defined(HAVE_ECC)
  1014. testCertFile = eccCertFile;
  1015. testKeyFile = eccKeyFile;
  1016. #endif
  1017. #ifndef NO_WOLFSSL_SERVER
  1018. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1019. #else
  1020. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1021. #endif
  1022. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1023. sizeof(cipherList)));
  1024. wolfSSL_CTX_free(ctx);
  1025. ctx = NULL;
  1026. #ifndef NO_WOLFSSL_SERVER
  1027. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1028. #else
  1029. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1030. #endif
  1031. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1032. WOLFSSL_FILETYPE_PEM));
  1033. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1034. WOLFSSL_FILETYPE_PEM));
  1035. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1036. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1037. sizeof(cipherList)));
  1038. wolfSSL_free(ssl);
  1039. wolfSSL_CTX_free(ctx);
  1040. res = EXPECT_RESULT();
  1041. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1042. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1043. return res;
  1044. }
  1045. static int test_wolfSSL_CTX_use_certificate_file(void)
  1046. {
  1047. int res = TEST_SKIPPED;
  1048. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1049. EXPECT_DECLS;
  1050. WOLFSSL_CTX *ctx = NULL;
  1051. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1052. /* invalid context */
  1053. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1054. WOLFSSL_FILETYPE_PEM));
  1055. /* invalid cert file */
  1056. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1057. WOLFSSL_FILETYPE_PEM));
  1058. /* invalid cert type */
  1059. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1060. #ifdef NO_RSA
  1061. /* rsa needed */
  1062. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1063. WOLFSSL_FILETYPE_PEM));
  1064. #else
  1065. /* success */
  1066. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1067. WOLFSSL_FILETYPE_PEM));
  1068. #endif
  1069. wolfSSL_CTX_free(ctx);
  1070. res = EXPECT_RESULT();
  1071. #endif
  1072. return res;
  1073. }
  1074. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1075. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1076. {
  1077. int res = TEST_SKIPPED;
  1078. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1079. WOLFSSL_CTX* ctx;
  1080. int ret;
  1081. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1082. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1083. server_cert_der_2048);
  1084. wolfSSL_CTX_free(ctx);
  1085. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1086. #endif
  1087. return res;
  1088. }
  1089. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1090. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1091. * context using buffer.
  1092. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1093. * --enable-testcert flag.
  1094. */
  1095. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1096. {
  1097. int res = TEST_SKIPPED;
  1098. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1099. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1100. EXPECT_DECLS;
  1101. WOLFSSL_CTX* ctx = NULL;
  1102. int ret;
  1103. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1104. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1105. server_cert_der_2048, sizeof_server_cert_der_2048,
  1106. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1107. wolfSSL_CTX_free(ctx);
  1108. res = EXPECT_RESULT();
  1109. #endif
  1110. return res;
  1111. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  1112. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1113. {
  1114. int res = TEST_SKIPPED;
  1115. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1116. EXPECT_DECLS;
  1117. WOLFSSL_CTX *ctx = NULL;
  1118. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1119. /* invalid context */
  1120. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1121. WOLFSSL_FILETYPE_PEM));
  1122. /* invalid key file */
  1123. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1124. WOLFSSL_FILETYPE_PEM));
  1125. /* invalid key type */
  1126. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1127. /* success */
  1128. #ifdef NO_RSA
  1129. /* rsa needed */
  1130. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1131. WOLFSSL_FILETYPE_PEM));
  1132. #else
  1133. /* success */
  1134. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1135. WOLFSSL_FILETYPE_PEM));
  1136. #endif
  1137. wolfSSL_CTX_free(ctx);
  1138. res = EXPECT_RESULT();
  1139. #endif
  1140. return res;
  1141. }
  1142. /* test both file and buffer versions along with unloading trusted peer certs */
  1143. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1144. {
  1145. int res = TEST_SKIPPED;
  1146. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1147. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1148. EXPECT_DECLS;
  1149. WOLFSSL_CTX *ctx = NULL;
  1150. WOLFSSL* ssl = NULL;
  1151. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1152. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1153. #if !defined(NO_FILESYSTEM)
  1154. /* invalid file */
  1155. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1156. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1157. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1158. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1159. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1160. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1161. /* success */
  1162. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1163. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1164. /* unload cert */
  1165. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1166. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1167. /* invalid file */
  1168. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1169. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1170. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1171. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1172. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1173. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1174. /* success */
  1175. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1176. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1177. #ifdef WOLFSSL_LOCAL_X509_STORE
  1178. /* unload cert */
  1179. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1180. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1181. #endif
  1182. #endif
  1183. /* Test of loading certs from buffers */
  1184. /* invalid buffer */
  1185. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1186. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1187. /* success */
  1188. #ifdef USE_CERT_BUFFERS_1024
  1189. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1190. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1191. #endif
  1192. #ifdef USE_CERT_BUFFERS_2048
  1193. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1194. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1195. #endif
  1196. /* unload cert */
  1197. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1198. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1199. wolfSSL_free(ssl);
  1200. wolfSSL_CTX_free(ctx);
  1201. res = EXPECT_RESULT();
  1202. #endif
  1203. return res;
  1204. }
  1205. static int test_wolfSSL_CTX_load_verify_locations(void)
  1206. {
  1207. int res = TEST_SKIPPED;
  1208. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1209. EXPECT_DECLS;
  1210. WOLFSSL_CTX *ctx = NULL;
  1211. #ifndef NO_RSA
  1212. WOLFSSL_CERT_MANAGER* cm = NULL;
  1213. #ifdef PERSIST_CERT_CACHE
  1214. int cacheSz;
  1215. #endif
  1216. #endif
  1217. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1218. const char* load_certs_path = "./certs/external";
  1219. const char* load_no_certs_path = "./examples";
  1220. const char* load_expired_path = "./certs/test/expired";
  1221. #endif
  1222. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1223. /* invalid arguments */
  1224. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1225. WOLFSSL_FAILURE);
  1226. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1227. WOLFSSL_FAILURE);
  1228. /* invalid ca file */
  1229. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1230. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1231. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1232. (defined(WOLFSSL_QT) && \
  1233. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1234. /* invalid path */
  1235. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1236. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1237. #endif
  1238. /* load ca cert */
  1239. #ifdef NO_RSA
  1240. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1241. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1242. #else /* Skip the following test without RSA certs. */
  1243. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1244. WOLFSSL_SUCCESS);
  1245. #ifdef PERSIST_CERT_CACHE
  1246. /* Get cert cache size */
  1247. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1248. #endif
  1249. /* Test unloading CA's */
  1250. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1251. #ifdef PERSIST_CERT_CACHE
  1252. /* Verify no certs (result is less than cacheSz) */
  1253. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1254. #endif
  1255. /* load ca cert again */
  1256. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1257. WOLFSSL_SUCCESS);
  1258. /* Test getting CERT_MANAGER */
  1259. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1260. /* Test unloading CA's using CM */
  1261. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1262. #ifdef PERSIST_CERT_CACHE
  1263. /* Verify no certs (result is less than cacheSz) */
  1264. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1265. #endif
  1266. #endif
  1267. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1268. /* Test loading CA certificates using a path */
  1269. #ifdef NO_RSA
  1270. /* failure here okay since certs in external directory are RSA */
  1271. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1272. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1273. #else
  1274. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1275. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1276. #endif
  1277. /* Test loading path with no files */
  1278. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1279. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1280. /* Test loading expired CA certificates */
  1281. #ifdef NO_RSA
  1282. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1283. load_expired_path,
  1284. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1285. WOLFSSL_SUCCESS);
  1286. #else
  1287. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1288. load_expired_path,
  1289. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1290. WOLFSSL_SUCCESS);
  1291. #endif
  1292. /* Test loading CA certificates and ignoring all errors */
  1293. #ifdef NO_RSA
  1294. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1295. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1296. #else
  1297. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1298. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1299. #endif
  1300. #endif
  1301. wolfSSL_CTX_free(ctx);
  1302. res = EXPECT_RESULT();
  1303. #endif
  1304. return res;
  1305. }
  1306. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1307. {
  1308. int res = TEST_SKIPPED;
  1309. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1310. (!defined(NO_RSA) || defined(HAVE_ECC))
  1311. WOLFSSL_CTX* ctx;
  1312. byte dirValid = 0;
  1313. int ret = 0;
  1314. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1315. if (ctx == NULL) {
  1316. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1317. ret = -1;
  1318. }
  1319. if (ret == 0) {
  1320. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1321. dirValid = 1;
  1322. #else
  1323. word32 numDirs;
  1324. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1325. if (caDirs == NULL || numDirs == 0) {
  1326. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1327. ret = -1;
  1328. }
  1329. else {
  1330. ReadDirCtx dirCtx;
  1331. word32 i;
  1332. for (i = 0; i < numDirs; ++i) {
  1333. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1334. /* Directory isn't empty. */
  1335. dirValid = 1;
  1336. wc_ReadDirClose(&dirCtx);
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. #endif
  1342. }
  1343. /*
  1344. * If the directory isn't empty, we should be able to load CA
  1345. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1346. * usable.
  1347. */
  1348. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1349. WOLFSSL_SUCCESS) {
  1350. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1351. ret = -1;
  1352. }
  1353. #ifdef OPENSSL_EXTRA
  1354. if (ret == 0 &&
  1355. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1356. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1357. ret = -1;
  1358. }
  1359. #endif /* OPENSSL_EXTRA */
  1360. wolfSSL_CTX_free(ctx);
  1361. res = TEST_RES_CHECK(ret == 0);
  1362. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1363. return res;
  1364. }
  1365. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1366. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1367. {
  1368. int ret;
  1369. WOLFSSL_CERT_MANAGER* cm;
  1370. cm = wolfSSL_CertManagerNew();
  1371. if (cm == NULL) {
  1372. fprintf(stderr, "test_cm_load_ca failed\n");
  1373. return -1;
  1374. }
  1375. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1376. wolfSSL_CertManagerFree(cm);
  1377. return ret;
  1378. }
  1379. static int test_cm_load_ca_file(const char* ca_cert_file)
  1380. {
  1381. int ret = 0;
  1382. byte* cert_buf = NULL;
  1383. size_t cert_sz = 0;
  1384. #if defined(WOLFSSL_PEM_TO_DER)
  1385. DerBuffer* pDer = NULL;
  1386. #endif
  1387. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1388. if (ret == 0) {
  1389. /* normal test */
  1390. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1391. if (ret == WOLFSSL_SUCCESS) {
  1392. /* test including null terminator in length */
  1393. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1394. if (tmp == NULL) {
  1395. ret = MEMORY_E;
  1396. }
  1397. else {
  1398. cert_buf = tmp;
  1399. cert_buf[cert_sz] = '\0';
  1400. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1401. WOLFSSL_FILETYPE_PEM);
  1402. }
  1403. }
  1404. #if defined(WOLFSSL_PEM_TO_DER)
  1405. if (ret == WOLFSSL_SUCCESS) {
  1406. /* test loading DER */
  1407. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1408. if (ret == 0 && pDer != NULL) {
  1409. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1410. WOLFSSL_FILETYPE_ASN1);
  1411. wc_FreeDer(&pDer);
  1412. }
  1413. }
  1414. #endif
  1415. }
  1416. free(cert_buf);
  1417. return ret;
  1418. }
  1419. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1420. int file_type, word32 flags)
  1421. {
  1422. int ret;
  1423. WOLFSSL_CERT_MANAGER* cm;
  1424. cm = wolfSSL_CertManagerNew();
  1425. if (cm == NULL) {
  1426. fprintf(stderr, "test_cm_load_ca failed\n");
  1427. return -1;
  1428. }
  1429. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1430. 0, flags);
  1431. wolfSSL_CertManagerFree(cm);
  1432. return ret;
  1433. }
  1434. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1435. {
  1436. int ret = 0;
  1437. byte* cert_buf = NULL;
  1438. size_t cert_sz = 0;
  1439. #if defined(WOLFSSL_PEM_TO_DER)
  1440. DerBuffer* pDer = NULL;
  1441. #endif
  1442. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1443. if (ret == 0) {
  1444. /* normal test */
  1445. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1446. WOLFSSL_FILETYPE_PEM, flags);
  1447. if (ret == WOLFSSL_SUCCESS) {
  1448. /* test including null terminator in length */
  1449. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1450. if (tmp == NULL) {
  1451. ret = MEMORY_E;
  1452. }
  1453. else {
  1454. cert_buf = tmp;
  1455. cert_buf[cert_sz] = '\0';
  1456. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1457. WOLFSSL_FILETYPE_PEM, flags);
  1458. }
  1459. }
  1460. #if defined(WOLFSSL_PEM_TO_DER)
  1461. if (ret == WOLFSSL_SUCCESS) {
  1462. /* test loading DER */
  1463. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1464. if (ret == 0 && pDer != NULL) {
  1465. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1466. WOLFSSL_FILETYPE_ASN1, flags);
  1467. wc_FreeDer(&pDer);
  1468. }
  1469. }
  1470. #endif
  1471. }
  1472. free(cert_buf);
  1473. return ret;
  1474. }
  1475. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1476. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1477. {
  1478. int res = TEST_SKIPPED;
  1479. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1480. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1481. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1482. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1483. defined(HAVE_LIGHTY)
  1484. EXPECT_DECLS;
  1485. WOLFSSL_CERT_MANAGER* cm = NULL;
  1486. /* Raw OCSP response bytes captured using the following setup:
  1487. * - Run responder with
  1488. * openssl ocsp -port 9999 -ndays 9999
  1489. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1490. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1491. * -rkey certs/ocsp/ocsp-responder-key.pem
  1492. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1493. * - Run client with
  1494. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1495. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1496. * -cert certs/ocsp/server1-cert.pem
  1497. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1498. * - Copy raw response from Wireshark.
  1499. */
  1500. byte response[] = {
  1501. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1502. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1503. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1504. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1505. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1506. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1507. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1508. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1509. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1510. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1511. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1512. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1513. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1514. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1515. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1516. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1517. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1518. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1519. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1520. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1521. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1522. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1523. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1524. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1525. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1526. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1527. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1528. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1529. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1530. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1531. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1532. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1533. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1534. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1535. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1536. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1537. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1538. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1539. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1540. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1541. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1542. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1543. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1544. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1545. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1546. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1547. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1548. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1549. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1550. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1551. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1552. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1553. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1554. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1555. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1556. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1557. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1558. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1559. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1560. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1561. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1562. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1563. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1564. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1565. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1566. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1567. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1568. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1569. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1570. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1571. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1572. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1573. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1574. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1575. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1576. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1577. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1578. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1579. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1580. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1581. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1582. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1583. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1584. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1585. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1586. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1587. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1588. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1589. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1590. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1591. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1592. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1593. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1594. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1595. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1596. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1597. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1598. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1599. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1600. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1601. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1602. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1603. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1604. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1605. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1606. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1607. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1608. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1609. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1610. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1611. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1612. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1613. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1614. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1615. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1616. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1617. 0x59, 0x59, 0xa0, 0x07
  1618. };
  1619. OcspEntry entry[1];
  1620. CertStatus status[1];
  1621. OcspRequest* request = NULL;
  1622. byte serial[] = {0x05};
  1623. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1624. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1625. XMEMSET(entry, 0, sizeof(OcspEntry));
  1626. XMEMSET(status, 0, sizeof(CertStatus));
  1627. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1628. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1629. DYNAMIC_TYPE_OCSP_REQUEST));
  1630. if ((request != NULL) && (request->serial != NULL)) {
  1631. request->serialSz = sizeof(serial);
  1632. XMEMCPY(request->serial, serial, sizeof(serial));
  1633. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1634. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1635. }
  1636. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1637. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1638. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1639. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1640. /* Response should be valid. */
  1641. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1642. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1643. /* Flip a byte in the request serial number, response should be invalid
  1644. * now. */
  1645. if ((request != NULL) && (request->serial != NULL))
  1646. request->serial[0] ^= request->serial[0];
  1647. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1648. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1649. wolfSSL_OCSP_REQUEST_free(request);
  1650. wolfSSL_CertManagerFree(cm);
  1651. res = EXPECT_RESULT();
  1652. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1653. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1654. #endif /* HAVE_OCSP */
  1655. return res;
  1656. }
  1657. static int test_wolfSSL_CheckOCSPResponse(void)
  1658. {
  1659. int result = TEST_SKIPPED;
  1660. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1661. EXPECT_DECLS;
  1662. const char* responseFile = "./certs/ocsp/test-response.der";
  1663. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1664. const char* responseNoInternFile =
  1665. "./certs/ocsp/test-response-nointern.der";
  1666. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1667. OcspResponse* res = NULL;
  1668. byte data[4096];
  1669. const unsigned char* pt;
  1670. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  1671. * gcc sanitizer with --enable-heapmath.
  1672. */
  1673. XFILE f = XBADFILE;
  1674. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  1675. WOLFSSL_X509_STORE* st = NULL;
  1676. WOLFSSL_X509* issuer = NULL;
  1677. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  1678. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  1679. XFCLOSE(f);
  1680. f = XBADFILE;
  1681. pt = data;
  1682. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  1683. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  1684. SSL_FILETYPE_PEM));
  1685. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  1686. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1687. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  1688. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1689. wolfSSL_OCSP_BASICRESP_free(bs);
  1690. bs = NULL;
  1691. wolfSSL_OCSP_RESPONSE_free(res);
  1692. res = NULL;
  1693. wolfSSL_X509_STORE_free(st);
  1694. st = NULL;
  1695. wolfSSL_X509_free(issuer);
  1696. issuer = NULL;
  1697. /* check loading a response with optional certs */
  1698. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  1699. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  1700. if (f != XBADFILE)
  1701. XFCLOSE(f);
  1702. f = XBADFILE;
  1703. pt = data;
  1704. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  1705. wolfSSL_OCSP_RESPONSE_free(res);
  1706. res = NULL;
  1707. /* check loading a response with multiple certs */
  1708. {
  1709. WOLFSSL_CERT_MANAGER* cm = NULL;
  1710. OcspEntry *entry = NULL;
  1711. CertStatus* status = NULL;
  1712. OcspRequest* request = NULL;
  1713. byte serial1[] = {0x01};
  1714. byte serial[] = {0x02};
  1715. byte issuerHash[] = {
  1716. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1717. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1718. 0x37, 0x15, 0x8A, 0x88
  1719. };
  1720. byte issuerKeyHash[] = {
  1721. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1722. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1723. 0x7E, 0x72, 0x15, 0x21
  1724. };
  1725. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1726. DYNAMIC_TYPE_OPENSSL));
  1727. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1728. DYNAMIC_TYPE_OPENSSL));
  1729. if (entry != NULL)
  1730. XMEMSET(entry, 0, sizeof(OcspEntry));
  1731. if (status != NULL)
  1732. XMEMSET(status, 0, sizeof(CertStatus));
  1733. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1734. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1735. DYNAMIC_TYPE_OCSP_REQUEST));
  1736. if (request != NULL && request->serial != NULL) {
  1737. request->serialSz = sizeof(serial);
  1738. XMEMCPY(request->serial, serial, sizeof(serial));
  1739. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1740. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  1741. sizeof(issuerKeyHash));
  1742. }
  1743. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1744. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1745. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1746. WOLFSSL_SUCCESS);
  1747. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  1748. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  1749. if (f != XBADFILE)
  1750. XFCLOSE(f);
  1751. f = XBADFILE;
  1752. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1753. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1754. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1755. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1756. ExpectNotNull(entry->status);
  1757. if (request != NULL && request->serial != NULL)
  1758. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1759. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1760. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1761. /* store both status's in the entry to check that "next" is not
  1762. * overwritten */
  1763. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  1764. status->next = entry->status;
  1765. entry->status = status;
  1766. }
  1767. if (request != NULL && request->serial != NULL)
  1768. XMEMCPY(request->serial, serial, sizeof(serial));
  1769. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1770. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1771. ExpectNotNull(entry->status->next);
  1772. /* compare the status found */
  1773. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  1774. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1775. status->serialSz), 0);
  1776. if (status != NULL && entry != NULL && entry->status != status) {
  1777. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  1778. }
  1779. wolfSSL_OCSP_CERTID_free(entry);
  1780. wolfSSL_OCSP_REQUEST_free(request);
  1781. wolfSSL_CertManagerFree(cm);
  1782. }
  1783. #if defined(WC_RSA_PSS)
  1784. {
  1785. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1786. /* check loading a response with RSA-PSS signature */
  1787. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  1788. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  1789. if (f != XBADFILE)
  1790. XFCLOSE(f);
  1791. pt = data;
  1792. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  1793. /* try to verify the response */
  1794. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  1795. SSL_FILETYPE_PEM));
  1796. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  1797. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1798. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  1799. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  1800. WOLFSSL_SUCCESS);
  1801. wolfSSL_OCSP_BASICRESP_free(bs);
  1802. wolfSSL_OCSP_RESPONSE_free(res);
  1803. wolfSSL_X509_STORE_free(st);
  1804. wolfSSL_X509_free(issuer);
  1805. }
  1806. #endif
  1807. result = EXPECT_RESULT();
  1808. #endif /* HAVE_OCSP */
  1809. return result;
  1810. }
  1811. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1812. {
  1813. int res = TEST_SKIPPED;
  1814. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1815. EXPECT_DECLS;
  1816. const char* ca_cert = "./certs/ca-cert.pem";
  1817. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1818. int ret;
  1819. ret = test_cm_load_ca_file(ca_cert);
  1820. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1821. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1822. #elif defined(NO_RSA)
  1823. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1824. #else
  1825. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1826. #endif
  1827. ret = test_cm_load_ca_file(ca_expired_cert);
  1828. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1829. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1830. #elif defined(NO_RSA)
  1831. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1832. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1833. !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  1834. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1835. #else
  1836. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1837. #endif
  1838. res = EXPECT_RESULT();
  1839. #endif
  1840. return res;
  1841. }
  1842. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1843. {
  1844. int res = TEST_SKIPPED;
  1845. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1846. EXPECT_DECLS;
  1847. const char* ca_cert = "./certs/ca-cert.pem";
  1848. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1849. int ret;
  1850. ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE);
  1851. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1852. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1853. #elif defined(NO_RSA)
  1854. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1855. #else
  1856. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1857. #endif
  1858. ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1859. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
  1860. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1861. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1862. #elif defined(NO_RSA)
  1863. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1864. #else
  1865. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1866. #endif
  1867. res = EXPECT_RESULT();
  1868. #endif
  1869. return res;
  1870. }
  1871. static int test_wolfSSL_CertManagerGetCerts(void)
  1872. {
  1873. int res = TEST_SKIPPED;
  1874. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1875. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1876. defined(WOLFSSL_SIGNER_DER_CERT)
  1877. EXPECT_DECLS;
  1878. WOLFSSL_CERT_MANAGER* cm = NULL;
  1879. WOLFSSL_STACK* sk = NULL;
  1880. X509* x509 = NULL;
  1881. X509* cert1 = NULL;
  1882. FILE* file1 = NULL;
  1883. #ifdef DEBUG_WOLFSSL_VERBOSE
  1884. WOLFSSL_BIO* bio = NULL;
  1885. #endif
  1886. int i = 0;
  1887. int ret = 0;
  1888. const byte* der;
  1889. int derSz = 0;
  1890. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1891. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1892. if (file1 != NULL) {
  1893. fclose(file1);
  1894. }
  1895. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1896. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1897. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1898. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1899. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1900. * and full OpenSSL compatibility */
  1901. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1902. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1903. #else
  1904. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1905. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1906. #endif
  1907. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1908. "./certs/ca-cert.pem", NULL));
  1909. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1910. for (i = 0; i < sk_X509_num(sk); i++) {
  1911. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1912. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1913. #ifdef DEBUG_WOLFSSL_VERBOSE
  1914. bio = BIO_new(wolfSSL_BIO_s_file());
  1915. if (bio != NULL) {
  1916. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1917. X509_print(bio, x509);
  1918. BIO_free(bio);
  1919. }
  1920. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1921. }
  1922. wolfSSL_X509_free(cert1);
  1923. sk_X509_pop_free(sk, NULL);
  1924. wolfSSL_CertManagerFree(cm);
  1925. res = EXPECT_RESULT();
  1926. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1927. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1928. defined(WOLFSSL_SIGNER_DER_CERT) */
  1929. return res;
  1930. }
  1931. static int test_wolfSSL_CertManagerSetVerify(void)
  1932. {
  1933. int res = TEST_SKIPPED;
  1934. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1935. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1936. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1937. EXPECT_DECLS;
  1938. int ret = 0;
  1939. WOLFSSL_CERT_MANAGER* cm = NULL;
  1940. int tmp = myVerifyAction;
  1941. const char* ca_cert = "./certs/ca-cert.pem";
  1942. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1943. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1944. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1945. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1946. ExpectIntEQ(ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1947. #else
  1948. ExpectIntEQ(ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1949. WOLFSSL_SUCCESS);
  1950. #endif
  1951. /* Use the test CB that always accepts certs */
  1952. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1953. ExpectIntEQ(ret = wolfSSL_CertManagerVerify(cm, expiredCert,
  1954. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1955. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1956. {
  1957. const char* verifyCert = "./certs/server-cert.pem";
  1958. /* Use the test CB that always fails certs */
  1959. myVerifyAction = VERIFY_FORCE_FAIL;
  1960. ExpectIntEQ(ret = wolfSSL_CertManagerVerify(cm, verifyCert,
  1961. WOLFSSL_FILETYPE_PEM), VERIFY_CERT_ERROR);
  1962. }
  1963. #endif
  1964. wolfSSL_CertManagerFree(cm);
  1965. myVerifyAction = tmp;
  1966. res = EXPECT_RESULT();
  1967. #endif
  1968. return res;
  1969. }
  1970. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1971. defined(DEBUG_UNIT_TEST_CERTS)
  1972. /* Used when debugging name constraint tests. Not static to allow use in
  1973. * multiple locations with complex define guards. */
  1974. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1975. {
  1976. BIO* out = BIO_new_file(fileName, "wb");
  1977. if (out != NULL) {
  1978. PEM_write_bio_X509(out, x509);
  1979. BIO_free(out);
  1980. }
  1981. }
  1982. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1983. {
  1984. BIO* out = BIO_new_file(fileName, "wb");
  1985. if (out != NULL) {
  1986. BIO_write(out, der, derSz);
  1987. BIO_free(out);
  1988. }
  1989. }
  1990. #else
  1991. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1992. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1993. #endif
  1994. static int test_wolfSSL_CertManagerNameConstraint(void)
  1995. {
  1996. int res = TEST_SKIPPED;
  1997. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1998. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1999. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2000. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2001. !defined(NO_SHA256)
  2002. EXPECT_DECLS;
  2003. WOLFSSL_CERT_MANAGER* cm = NULL;
  2004. WOLFSSL_EVP_PKEY *priv = NULL;
  2005. WOLFSSL_X509_NAME* name = NULL;
  2006. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2007. const char* server_cert = "./certs/test/server-goodcn.pem";
  2008. int i = 0;
  2009. static const byte extNameConsOid[] = {85, 29, 30};
  2010. RsaKey key;
  2011. WC_RNG rng;
  2012. byte *der = NULL;
  2013. int derSz = 0;
  2014. word32 idx = 0;
  2015. byte *pt;
  2016. WOLFSSL_X509 *x509 = NULL;
  2017. WOLFSSL_X509 *ca = NULL;
  2018. wc_InitRng(&rng);
  2019. /* load in CA private key for signing */
  2020. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2021. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2022. sizeof_server_key_der_2048), 0);
  2023. /* get ca certificate then alter it */
  2024. ExpectNotNull(der =
  2025. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2026. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2027. WOLFSSL_FILETYPE_ASN1));
  2028. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2029. if (EXPECT_SUCCESS() && (der != NULL)) {
  2030. XMEMCPY(der, pt, derSz);
  2031. /* find the name constraint extension and alter it */
  2032. pt = der;
  2033. for (i = 0; i < derSz - 3; i++) {
  2034. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2035. pt += 3;
  2036. break;
  2037. }
  2038. pt++;
  2039. }
  2040. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2041. /* go to the length value and set it to 0 */
  2042. while (i < derSz && *pt != 0x81) {
  2043. pt++;
  2044. i++;
  2045. }
  2046. ExpectIntNE(i, derSz); /* did not place to alter */
  2047. pt++;
  2048. *pt = 0x00;
  2049. }
  2050. /* resign the altered certificate */
  2051. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2052. FOURK_BUF, &key, NULL, &rng)), 0);
  2053. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2054. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2055. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2056. wolfSSL_CertManagerFree(cm);
  2057. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2058. wolfSSL_X509_free(x509);
  2059. wc_FreeRsaKey(&key);
  2060. wc_FreeRng(&rng);
  2061. /* add email alt name to satisfy constraint */
  2062. pt = (byte*)server_key_der_2048;
  2063. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2064. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2065. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2066. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2067. WOLFSSL_FILETYPE_ASN1));
  2068. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2069. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2070. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2071. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2072. /* Good cert test with proper alt email name */
  2073. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2074. WOLFSSL_FILETYPE_PEM));
  2075. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2076. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2077. name = NULL;
  2078. ExpectNotNull(name = X509_NAME_new());
  2079. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2080. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2081. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2082. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2083. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2084. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2085. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2086. X509_NAME_free(name);
  2087. name = NULL;
  2088. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2089. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2090. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2091. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2092. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2093. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2094. wolfSSL_X509_free(x509);
  2095. x509 = NULL;
  2096. /* Cert with bad alt name list */
  2097. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2098. WOLFSSL_FILETYPE_PEM));
  2099. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2100. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2101. name = NULL;
  2102. ExpectNotNull(name = X509_NAME_new());
  2103. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2104. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2105. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2106. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2107. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2108. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2109. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2110. X509_NAME_free(name);
  2111. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2112. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2113. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2114. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2115. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2116. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2117. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2118. wolfSSL_CertManagerFree(cm);
  2119. wolfSSL_X509_free(x509);
  2120. wolfSSL_X509_free(ca);
  2121. wolfSSL_EVP_PKEY_free(priv);
  2122. res = EXPECT_RESULT();
  2123. #endif
  2124. return res;
  2125. }
  2126. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2127. {
  2128. int res = TEST_SKIPPED;
  2129. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2130. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2131. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2132. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2133. EXPECT_DECLS;
  2134. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2135. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2136. const char* server_cert = "./certs/server-cert.pem";
  2137. WOLFSSL_CERT_MANAGER* cm = NULL;
  2138. WOLFSSL_X509 *x509 = NULL;
  2139. WOLFSSL_X509 *ca = NULL;
  2140. const unsigned char *der = NULL;
  2141. const unsigned char *pt;
  2142. WOLFSSL_EVP_PKEY *priv = NULL;
  2143. WOLFSSL_X509_NAME* name = NULL;
  2144. int derSz = 0;
  2145. /* C=US*/
  2146. char altName[] = {
  2147. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2148. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2149. };
  2150. /* C=ID */
  2151. char altNameFail[] = {
  2152. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2153. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2154. };
  2155. /* C=US ST=California*/
  2156. char altNameExc[] = {
  2157. 0x30, 0x22,
  2158. 0x31, 0x0B,
  2159. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2160. 0x31, 0x13,
  2161. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2162. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2163. };
  2164. /* load in CA private key for signing */
  2165. pt = ca_key_der_2048;
  2166. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2167. sizeof_ca_key_der_2048));
  2168. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2169. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2170. WOLFSSL_FILETYPE_ASN1));
  2171. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2172. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2173. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2174. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2175. WOLFSSL_FILETYPE_PEM));
  2176. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2177. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2178. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2179. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2180. #else
  2181. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2182. #endif
  2183. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2184. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2185. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2186. /* add in matching DIR alt name and resign */
  2187. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2188. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2189. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2190. #else
  2191. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2192. #endif
  2193. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2194. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2195. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2196. wolfSSL_X509_free(x509);
  2197. x509 = NULL;
  2198. /* check verify fail */
  2199. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2200. WOLFSSL_FILETYPE_PEM));
  2201. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2202. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2203. /* add in miss matching DIR alt name and resign */
  2204. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2205. ASN_DIR_TYPE);
  2206. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2207. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2208. #else
  2209. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2210. #endif
  2211. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2212. #ifndef WOLFSSL_NO_ASN_STRICT
  2213. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2214. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2215. #else
  2216. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2217. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2218. #endif
  2219. /* check that it still fails if one bad altname and one good altname is in
  2220. * the certificate */
  2221. wolfSSL_X509_free(x509);
  2222. x509 = NULL;
  2223. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2224. WOLFSSL_FILETYPE_PEM));
  2225. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2226. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2227. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2228. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2229. ASN_DIR_TYPE);
  2230. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2231. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2232. #else
  2233. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2234. #endif
  2235. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2236. #ifndef WOLFSSL_NO_ASN_STRICT
  2237. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2238. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2239. #else
  2240. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2241. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2242. #endif
  2243. /* check it fails with switching position of bad altname */
  2244. wolfSSL_X509_free(x509);
  2245. x509 = NULL;
  2246. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2247. WOLFSSL_FILETYPE_PEM));
  2248. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2249. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2250. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2251. ASN_DIR_TYPE);
  2252. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2253. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2254. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2255. #else
  2256. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2257. #endif
  2258. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2259. #ifndef WOLFSSL_NO_ASN_STRICT
  2260. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2261. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2262. #else
  2263. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2264. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2265. #endif
  2266. wolfSSL_CertManagerFree(cm);
  2267. wolfSSL_X509_free(x509);
  2268. x509 = NULL;
  2269. wolfSSL_X509_free(ca);
  2270. ca = NULL;
  2271. /* now test with excluded name constraint */
  2272. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2273. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2274. WOLFSSL_FILETYPE_ASN1));
  2275. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2276. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2277. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2278. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2279. WOLFSSL_FILETYPE_PEM));
  2280. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2281. ASN_DIR_TYPE);
  2282. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2283. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2284. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2285. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2286. #else
  2287. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2288. #endif
  2289. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2290. #ifndef WOLFSSL_NO_ASN_STRICT
  2291. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2292. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2293. #else
  2294. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2295. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2296. #endif
  2297. wolfSSL_CertManagerFree(cm);
  2298. wolfSSL_X509_free(x509);
  2299. wolfSSL_X509_free(ca);
  2300. wolfSSL_EVP_PKEY_free(priv);
  2301. res = EXPECT_RESULT();
  2302. #endif
  2303. return res;
  2304. }
  2305. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2306. {
  2307. int res = TEST_SKIPPED;
  2308. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2309. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2310. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2311. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2312. !defined(NO_SHA256)
  2313. EXPECT_DECLS;
  2314. WOLFSSL_CERT_MANAGER* cm = NULL;
  2315. WOLFSSL_EVP_PKEY *priv = NULL;
  2316. WOLFSSL_X509_NAME* name = NULL;
  2317. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2318. const char* server_cert = "./certs/test/server-goodcn.pem";
  2319. byte *der = NULL;
  2320. int derSz = 0;
  2321. byte *pt;
  2322. WOLFSSL_X509 *x509 = NULL;
  2323. WOLFSSL_X509 *ca = NULL;
  2324. pt = (byte*)server_key_der_2048;
  2325. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2326. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2327. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2328. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2329. WOLFSSL_FILETYPE_ASN1));
  2330. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2331. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2332. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2333. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2334. /* check satisfying .wolfssl.com constraint passes */
  2335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2336. WOLFSSL_FILETYPE_PEM));
  2337. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2338. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2339. name = NULL;
  2340. ExpectNotNull(name = X509_NAME_new());
  2341. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2342. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2343. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2344. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2345. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2346. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2347. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2348. X509_NAME_free(name);
  2349. name = NULL;
  2350. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2351. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2352. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2353. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2354. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2355. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2356. wolfSSL_X509_free(x509);
  2357. x509 = NULL;
  2358. /* check satisfying .random.com constraint passes */
  2359. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2360. WOLFSSL_FILETYPE_PEM));
  2361. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2362. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2363. name = NULL;
  2364. ExpectNotNull(name = X509_NAME_new());
  2365. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2366. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2367. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2368. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2369. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2370. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2371. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2372. X509_NAME_free(name);
  2373. name = NULL;
  2374. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2375. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2376. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2377. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2378. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2379. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2380. wolfSSL_X509_free(x509);
  2381. x509 = NULL;
  2382. /* check fail case when neither constraint is matched */
  2383. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2384. WOLFSSL_FILETYPE_PEM));
  2385. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2386. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2387. name = NULL;
  2388. ExpectNotNull(name = X509_NAME_new());
  2389. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2390. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2391. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2392. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2393. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2394. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2395. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2396. X509_NAME_free(name);
  2397. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2398. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2399. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2400. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2401. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2402. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2403. wolfSSL_CertManagerFree(cm);
  2404. wolfSSL_X509_free(x509);
  2405. wolfSSL_X509_free(ca);
  2406. wolfSSL_EVP_PKEY_free(priv);
  2407. res = EXPECT_RESULT();
  2408. #endif
  2409. return res;
  2410. }
  2411. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2412. {
  2413. int res = TEST_SKIPPED;
  2414. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2415. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2416. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2417. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2418. !defined(NO_SHA256)
  2419. EXPECT_DECLS;
  2420. WOLFSSL_CERT_MANAGER* cm = NULL;
  2421. WOLFSSL_EVP_PKEY *priv = NULL;
  2422. WOLFSSL_X509_NAME* name = NULL;
  2423. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2424. const char* server_cert = "./certs/test/server-goodcn.pem";
  2425. byte *der = NULL;
  2426. int derSz;
  2427. byte *pt;
  2428. WOLFSSL_X509 *x509 = NULL;
  2429. WOLFSSL_X509 *ca = NULL;
  2430. pt = (byte*)server_key_der_2048;
  2431. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2432. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2433. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2434. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2435. WOLFSSL_FILETYPE_ASN1));
  2436. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2437. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2438. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2439. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2440. /* check satisfying wolfssl.com constraint passes */
  2441. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2442. WOLFSSL_FILETYPE_PEM));
  2443. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2444. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2445. name = NULL;
  2446. ExpectNotNull(name = X509_NAME_new());
  2447. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2448. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2449. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2450. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2451. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2452. X509_NAME_free(name);
  2453. name = NULL;
  2454. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2455. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2456. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2457. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2458. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2459. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2460. wolfSSL_X509_free(x509);
  2461. x509 = NULL;
  2462. /* check satisfying example.com constraint passes */
  2463. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2464. WOLFSSL_FILETYPE_PEM));
  2465. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2466. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2467. name = NULL;
  2468. ExpectNotNull(name = X509_NAME_new());
  2469. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2470. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2471. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2472. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2473. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2474. X509_NAME_free(name);
  2475. name = NULL;
  2476. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2477. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2478. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2479. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2480. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2481. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2482. wolfSSL_X509_free(x509);
  2483. x509 = NULL;
  2484. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2485. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2486. WOLFSSL_FILETYPE_PEM));
  2487. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2488. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2489. name = NULL;
  2490. ExpectNotNull(name = X509_NAME_new());
  2491. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2492. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2493. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2494. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2495. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2496. X509_NAME_free(name);
  2497. name = NULL;
  2498. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2499. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2500. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2501. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2502. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2503. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2504. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2505. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2506. wolfSSL_X509_free(x509);
  2507. x509 = NULL;
  2508. /* check fail when one DNS in the list is bad */
  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*)"wolfssl.com", 11, -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, "www.wolfssl.com", ASN_DNS_TYPE);
  2523. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2524. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2525. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2526. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2527. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2528. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2529. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2530. wolfSSL_X509_free(x509);
  2531. x509 = NULL;
  2532. /* check fail case when neither constraint is matched */
  2533. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2534. WOLFSSL_FILETYPE_PEM));
  2535. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2536. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2537. name = NULL;
  2538. ExpectNotNull(name = X509_NAME_new());
  2539. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2540. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2541. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2542. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2543. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2544. X509_NAME_free(name);
  2545. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2546. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2547. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2548. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2549. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2550. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2551. wolfSSL_CertManagerFree(cm);
  2552. wolfSSL_X509_free(x509);
  2553. wolfSSL_X509_free(ca);
  2554. wolfSSL_EVP_PKEY_free(priv);
  2555. res = EXPECT_RESULT();
  2556. #endif
  2557. return res;
  2558. }
  2559. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2560. {
  2561. int res = TEST_SKIPPED;
  2562. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2563. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2564. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2565. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2566. !defined(NO_SHA256)
  2567. EXPECT_DECLS;
  2568. WOLFSSL_CERT_MANAGER* cm = NULL;
  2569. WOLFSSL_EVP_PKEY *priv = NULL;
  2570. WOLFSSL_X509_NAME* name = NULL;
  2571. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2572. const char* server_cert = "./certs/test/server-goodcn.pem";
  2573. byte *der = NULL;
  2574. int derSz;
  2575. byte *pt;
  2576. WOLFSSL_X509 *x509 = NULL;
  2577. WOLFSSL_X509 *ca = NULL;
  2578. pt = (byte*)server_key_der_2048;
  2579. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2580. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2581. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2582. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2583. WOLFSSL_FILETYPE_ASN1));
  2584. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2585. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2586. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2587. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2588. /* check satisfying wolfssl.com constraint passes */
  2589. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2590. WOLFSSL_FILETYPE_PEM));
  2591. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2592. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2593. name = NULL;
  2594. ExpectNotNull(name = X509_NAME_new());
  2595. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2596. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2597. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2598. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2599. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2600. X509_NAME_free(name);
  2601. name = NULL;
  2602. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2603. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2604. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2605. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2606. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2607. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2608. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2609. wolfSSL_X509_free(x509);
  2610. x509 = NULL;
  2611. /* fail with DNS check because of common name */
  2612. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2613. WOLFSSL_FILETYPE_PEM));
  2614. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2615. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2616. name = NULL;
  2617. ExpectNotNull(name = X509_NAME_new());
  2618. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2619. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2620. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2621. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2622. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2623. X509_NAME_free(name);
  2624. name = NULL;
  2625. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2626. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2627. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2628. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2629. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2630. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2631. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2632. wolfSSL_X509_free(x509);
  2633. x509 = NULL;
  2634. /* fail on permitted DNS name constraint */
  2635. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2636. WOLFSSL_FILETYPE_PEM));
  2637. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2638. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2639. name = NULL;
  2640. ExpectNotNull(name = X509_NAME_new());
  2641. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2642. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2643. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2644. X509_NAME_free(name);
  2645. name = NULL;
  2646. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2647. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2648. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2649. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2650. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2651. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2652. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2653. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2654. wolfSSL_X509_free(x509);
  2655. x509 = NULL;
  2656. /* fail on permitted email name constraint */
  2657. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2658. WOLFSSL_FILETYPE_PEM));
  2659. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2660. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2661. name = NULL;
  2662. ExpectNotNull(name = X509_NAME_new());
  2663. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2664. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2665. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2666. X509_NAME_free(name);
  2667. name = NULL;
  2668. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2669. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2670. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2671. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2672. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2673. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2674. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2675. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2676. wolfSSL_X509_free(x509);
  2677. x509 = NULL;
  2678. /* success with empty email name */
  2679. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2680. WOLFSSL_FILETYPE_PEM));
  2681. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2682. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2683. name = NULL;
  2684. ExpectNotNull(name = X509_NAME_new());
  2685. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2686. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2687. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2688. X509_NAME_free(name);
  2689. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2690. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2691. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2692. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2693. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2694. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2695. wolfSSL_X509_free(x509);
  2696. wolfSSL_CertManagerFree(cm);
  2697. wolfSSL_X509_free(ca);
  2698. wolfSSL_EVP_PKEY_free(priv);
  2699. res = TEST_RES_CHECK(1);
  2700. #endif
  2701. return res;
  2702. }
  2703. static int test_wolfSSL_FPKI(void)
  2704. {
  2705. int res = TEST_SKIPPED;
  2706. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2707. EXPECT_DECLS;
  2708. XFILE f = XBADFILE;
  2709. const char* fpkiCert = "./certs/fpki-cert.der";
  2710. DecodedCert cert;
  2711. byte buf[4096];
  2712. byte* uuid = NULL;
  2713. byte* fascn = NULL;
  2714. word32 fascnSz;
  2715. word32 uuidSz;
  2716. int bytes = 0;
  2717. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  2718. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  2719. if (f != XBADFILE)
  2720. XFCLOSE(f);
  2721. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2722. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2723. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2724. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  2725. DYNAMIC_TYPE_TMP_BUFFER));
  2726. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2727. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2728. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2729. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  2730. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2731. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2732. wc_FreeDecodedCert(&cert);
  2733. res = EXPECT_RESULT();
  2734. #endif
  2735. return res;
  2736. }
  2737. /* use RID in confuncture with other names to test parsing of unknown other
  2738. * names */
  2739. static int test_wolfSSL_OtherName(void)
  2740. {
  2741. int res = TEST_SKIPPED;
  2742. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2743. EXPECT_DECLS;
  2744. XFILE f = XBADFILE;
  2745. const char* ridCert = "./certs/rid-cert.der";
  2746. DecodedCert cert;
  2747. byte buf[4096];
  2748. int bytes = 0;
  2749. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  2750. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  2751. if (f != XBADFILE)
  2752. XFCLOSE(f);
  2753. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2754. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2755. wc_FreeDecodedCert(&cert);
  2756. res = EXPECT_RESULT();
  2757. #endif
  2758. return res;
  2759. }
  2760. static int test_wolfSSL_CertRsaPss(void)
  2761. {
  2762. int res = TEST_SKIPPED;
  2763. /* FIPS v2 and below don't support long salts. */
  2764. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2765. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2766. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2767. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2768. EXPECT_DECLS;
  2769. XFILE f = XBADFILE;
  2770. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2771. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2772. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2773. RSA_MAX_SIZE >= 3072
  2774. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2775. #endif
  2776. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2777. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2778. #endif
  2779. DecodedCert cert;
  2780. byte buf[4096];
  2781. int bytes = 0;
  2782. WOLFSSL_CERT_MANAGER* cm = NULL;
  2783. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2784. ExpectIntEQ(WOLFSSL_SUCCESS,
  2785. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2786. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2787. ExpectIntEQ(WOLFSSL_SUCCESS,
  2788. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2789. #endif
  2790. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  2791. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  2792. if (f != XBADFILE) {
  2793. XFCLOSE(f);
  2794. f = XBADFILE;
  2795. }
  2796. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2797. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2798. wc_FreeDecodedCert(&cert);
  2799. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2800. RSA_MAX_SIZE >= 3072
  2801. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  2802. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  2803. if (f != XBADFILE)
  2804. XFCLOSE(f);
  2805. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2806. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2807. wc_FreeDecodedCert(&cert);
  2808. #endif
  2809. wolfSSL_CertManagerFree(cm);
  2810. res = EXPECT_RESULT();
  2811. #endif
  2812. return res;
  2813. }
  2814. static int test_wolfSSL_CertManagerCRL(void)
  2815. {
  2816. int res = TEST_SKIPPED;
  2817. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2818. !defined(NO_RSA)
  2819. EXPECT_DECLS;
  2820. const char* ca_cert = "./certs/ca-cert.pem";
  2821. const char* crl1 = "./certs/crl/crl.pem";
  2822. const char* crl2 = "./certs/crl/crl2.pem";
  2823. WOLFSSL_CERT_MANAGER* cm = NULL;
  2824. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2825. ExpectIntEQ(WOLFSSL_SUCCESS,
  2826. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2827. ExpectIntEQ(WOLFSSL_SUCCESS,
  2828. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2829. ExpectIntEQ(WOLFSSL_SUCCESS,
  2830. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2831. wolfSSL_CertManagerFreeCRL(cm);
  2832. ExpectIntEQ(WOLFSSL_SUCCESS,
  2833. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2834. ExpectIntEQ(WOLFSSL_SUCCESS,
  2835. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2836. wolfSSL_CertManagerFree(cm);
  2837. res = EXPECT_RESULT();
  2838. #endif
  2839. return res;
  2840. }
  2841. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2842. {
  2843. int res = TEST_SKIPPED;
  2844. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2845. !defined(NO_WOLFSSL_CLIENT)
  2846. EXPECT_DECLS;
  2847. WOLFSSL_CTX* ctx = NULL;
  2848. const char* ca_cert = "./certs/ca-cert.pem";
  2849. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2850. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2851. /* test good CA */
  2852. ExpectTrue(WOLFSSL_SUCCESS ==
  2853. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2854. WOLFSSL_LOAD_FLAG_NONE));
  2855. /* test expired CA */
  2856. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2857. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2858. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2859. #else
  2860. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2861. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2862. #endif
  2863. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2864. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2865. wolfSSL_CTX_free(ctx);
  2866. res = EXPECT_RESULT();
  2867. #endif
  2868. return res;
  2869. }
  2870. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2871. {
  2872. int res = TEST_SKIPPED;
  2873. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2874. defined(USE_CERT_BUFFERS_2048)
  2875. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2876. EXPECT_DECLS;
  2877. WOLFSSL_CTX* ctx;
  2878. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2879. byte ca_expired_cert[TWOK_BUF];
  2880. word32 sizeof_ca_expired_cert;
  2881. XFILE fp = XBADFILE;
  2882. #ifndef NO_WOLFSSL_CLIENT
  2883. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2884. #else
  2885. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2886. #endif
  2887. ExpectNotNull(ctx);
  2888. /* test good CA */
  2889. ExpectTrue(WOLFSSL_SUCCESS ==
  2890. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2891. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2892. WOLFSSL_LOAD_FLAG_NONE));
  2893. /* load expired CA */
  2894. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2895. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  2896. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2897. sizeof(ca_expired_cert), fp), 0);
  2898. if (fp != XBADFILE)
  2899. XFCLOSE(fp);
  2900. /* test expired CA failure */
  2901. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2902. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2903. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2904. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2905. #else
  2906. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2907. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2908. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2909. #endif
  2910. /* test expired CA success */
  2911. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2912. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2913. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2914. wolfSSL_CTX_free(ctx);
  2915. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2916. res = EXPECT_RESULT();
  2917. #endif
  2918. return res;
  2919. }
  2920. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2921. {
  2922. int res = TEST_SKIPPED;
  2923. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2924. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2925. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2926. EXPECT_DECLS;
  2927. WOLFSSL_CTX* ctx = NULL;
  2928. #ifndef NO_WOLFSSL_CLIENT
  2929. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2930. #else
  2931. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2932. #endif
  2933. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2934. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2935. WOLFSSL_FILETYPE_ASN1));
  2936. wolfSSL_CTX_free(ctx);
  2937. res = EXPECT_RESULT();
  2938. #endif
  2939. return res;
  2940. }
  2941. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2942. {
  2943. int res = TEST_SKIPPED;
  2944. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2945. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2946. EXPECT_DECLS;
  2947. WOLFSSL_CTX* ctx;
  2948. WOLFSSL* ssl = NULL;
  2949. const char *certChain[] = {
  2950. "./certs/intermediate/client-int-cert.pem",
  2951. "./certs/intermediate/ca-int2-cert.pem",
  2952. "./certs/intermediate/ca-int-cert.pem",
  2953. "./certs/ca-cert.pem",
  2954. NULL
  2955. };
  2956. const char** cert;
  2957. WOLFSSL_X509* x509 = NULL;
  2958. WOLF_STACK_OF(X509)* chain = NULL;
  2959. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2960. ExpectNotNull(ssl = wolfSSL_new(ctx));
  2961. for (cert = certChain; *cert != NULL; cert++) {
  2962. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  2963. WOLFSSL_FILETYPE_PEM));
  2964. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2965. X509_free(x509);
  2966. x509 = NULL;
  2967. }
  2968. for (cert = certChain; *cert != NULL; cert++) {
  2969. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  2970. WOLFSSL_FILETYPE_PEM));
  2971. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2972. X509_free(x509);
  2973. x509 = NULL;
  2974. }
  2975. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2976. ExpectIntEQ(sk_X509_num(chain), 3);
  2977. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2978. ExpectIntEQ(sk_X509_num(chain), 3);
  2979. SSL_free(ssl);
  2980. SSL_CTX_free(ctx);
  2981. res = EXPECT_RESULT();
  2982. #endif
  2983. return res;
  2984. }
  2985. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2986. {
  2987. int res = TEST_SKIPPED;
  2988. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2989. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2990. EXPECT_DECLS;
  2991. const char* server_chain_der = "./certs/server-cert-chain.der";
  2992. const char* client_single_pem = "./certs/client-cert.pem";
  2993. WOLFSSL_CTX* ctx;
  2994. (void)server_chain_der;
  2995. (void)client_single_pem;
  2996. (void)ctx;
  2997. #ifndef NO_WOLFSSL_CLIENT
  2998. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2999. #else
  3000. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3001. #endif
  3002. ExpectNotNull(ctx);
  3003. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3004. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3005. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3006. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3007. wolfSSL_CTX_free(ctx);
  3008. res = EXPECT_RESULT();
  3009. #endif
  3010. return res;
  3011. }
  3012. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3013. {
  3014. int res = TEST_SKIPPED;
  3015. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3016. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3017. EXPECT_DECLS;
  3018. WOLFSSL_CTX *ctx = NULL;
  3019. (void)ctx;
  3020. #ifndef NO_WOLFSSL_CLIENT
  3021. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3022. #else
  3023. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3024. #endif
  3025. /* invalid context */
  3026. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3027. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3028. /* invalid dhParamFile file */
  3029. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3030. NULL, WOLFSSL_FILETYPE_PEM));
  3031. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3032. bogusFile, WOLFSSL_FILETYPE_PEM));
  3033. /* success */
  3034. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3035. WOLFSSL_FILETYPE_PEM));
  3036. wolfSSL_CTX_free(ctx);
  3037. res = EXPECT_RESULT();
  3038. #endif
  3039. return res;
  3040. }
  3041. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3042. {
  3043. int res = TEST_SKIPPED;
  3044. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3045. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3046. EXPECT_DECLS;
  3047. WOLFSSL_CTX *ctx = NULL;
  3048. #ifndef NO_WOLFSSL_CLIENT
  3049. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3050. #else
  3051. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3052. #endif
  3053. /* invalid context */
  3054. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3055. dh_key_der_2048, sizeof_dh_key_der_2048,
  3056. WOLFSSL_FILETYPE_ASN1));
  3057. /* invalid dhParamFile file */
  3058. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3059. 0, WOLFSSL_FILETYPE_ASN1));
  3060. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3061. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3062. WOLFSSL_FILETYPE_ASN1));
  3063. /* success */
  3064. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3065. dh_key_der_2048, sizeof_dh_key_der_2048,
  3066. WOLFSSL_FILETYPE_ASN1));
  3067. wolfSSL_CTX_free(ctx);
  3068. res = EXPECT_RESULT();
  3069. #endif
  3070. return res;
  3071. }
  3072. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3073. {
  3074. int res = TEST_SKIPPED;
  3075. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3076. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3077. EXPECT_DECLS;
  3078. WOLFSSL_CTX *ctx;
  3079. (void)ctx;
  3080. #ifndef NO_WOLFSSL_CLIENT
  3081. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3082. #else
  3083. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3084. #endif
  3085. ExpectNotNull(ctx);
  3086. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3087. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3088. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3089. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3090. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3091. dh_key_der_2048, sizeof_dh_key_der_2048,
  3092. WOLFSSL_FILETYPE_ASN1));
  3093. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3094. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3095. dh_key_der_2048, sizeof_dh_key_der_2048,
  3096. WOLFSSL_FILETYPE_ASN1));
  3097. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3098. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3099. dh_key_der_2048, sizeof_dh_key_der_2048,
  3100. WOLFSSL_FILETYPE_ASN1));
  3101. wolfSSL_CTX_free(ctx);
  3102. res = EXPECT_RESULT();
  3103. #endif
  3104. return res;
  3105. }
  3106. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3107. {
  3108. int res = TEST_SKIPPED;
  3109. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3110. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3111. EXPECT_DECLS;
  3112. WOLFSSL_CTX* ctx = NULL;
  3113. const char* derCert = "./certs/server-cert.der";
  3114. const char* nullPath = NULL;
  3115. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3116. const char* emptyPath = "";
  3117. /* der load Case 1 ctx NULL */
  3118. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3119. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3120. #ifndef NO_WOLFSSL_CLIENT
  3121. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3122. #else
  3123. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3124. #endif
  3125. /* Case 2 filePath NULL */
  3126. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3127. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3128. /* Case 3 invalid format */
  3129. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3130. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3131. /* Case 4 filePath not valid */
  3132. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3133. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3134. /* Case 5 filePath empty */
  3135. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3136. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3137. #ifndef NO_RSA
  3138. /* Case 6 success case */
  3139. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3140. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3141. #endif
  3142. wolfSSL_CTX_free(ctx);
  3143. res = EXPECT_RESULT();
  3144. #endif
  3145. return res;
  3146. }
  3147. static int test_wolfSSL_CTX_enable_disable(void)
  3148. {
  3149. int res = TEST_SKIPPED;
  3150. #ifndef NO_CERTS
  3151. EXPECT_DECLS;
  3152. WOLFSSL_CTX* ctx = NULL;
  3153. #ifdef HAVE_CRL
  3154. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3155. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3156. #endif
  3157. #ifdef HAVE_OCSP
  3158. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3159. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3160. #endif
  3161. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3162. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3163. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3164. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3165. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3166. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3167. #endif
  3168. #ifndef NO_WOLFSSL_CLIENT
  3169. #ifdef HAVE_EXTENDED_MASTER
  3170. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3171. #endif
  3172. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3173. #ifdef HAVE_EXTENDED_MASTER
  3174. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3175. #endif
  3176. #elif !defined(NO_WOLFSSL_SERVER)
  3177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3178. #endif
  3179. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3180. #ifdef HAVE_CRL
  3181. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3182. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3183. #endif
  3184. #ifdef HAVE_OCSP
  3185. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3186. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3187. WOLFSSL_SUCCESS);
  3188. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3189. WOLFSSL_SUCCESS);
  3190. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3191. WOLFSSL_SUCCESS);
  3192. #endif
  3193. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3194. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3195. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3196. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3197. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3198. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3199. #endif
  3200. wolfSSL_CTX_free(ctx);
  3201. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3202. res = EXPECT_RESULT();
  3203. #endif /* NO_CERTS */
  3204. return res;
  3205. }
  3206. static int test_wolfSSL_CTX_ticket_API(void)
  3207. {
  3208. int res = TEST_SKIPPED;
  3209. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3210. EXPECT_DECLS;
  3211. WOLFSSL_CTX* ctx = NULL;
  3212. void *userCtx = (void*)"this is my ctx";
  3213. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3214. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3215. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3216. wolfSSL_CTX_free(ctx);
  3217. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3218. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3219. res = EXPECT_RESULT();
  3220. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3221. return res;
  3222. }
  3223. static int test_wolfSSL_set_minmax_proto_version(void)
  3224. {
  3225. int res = TEST_SKIPPED;
  3226. #ifdef OPENSSL_EXTRA
  3227. EXPECT_DECLS;
  3228. WOLFSSL_CTX *ctx = NULL;
  3229. WOLFSSL *ssl = NULL;
  3230. (void)ssl;
  3231. #ifndef NO_WOLFSSL_CLIENT
  3232. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3233. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3234. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3235. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3236. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3237. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3238. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3239. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3240. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3241. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3242. wolfSSL_free(ssl);
  3243. wolfSSL_CTX_free(ctx);
  3244. ctx = NULL;
  3245. #endif
  3246. #ifndef NO_WOLFSSL_SERVER
  3247. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3248. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3249. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3250. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3251. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3252. wolfSSL_CTX_free(ctx);
  3253. #endif
  3254. res = EXPECT_RESULT();
  3255. #endif
  3256. return res;
  3257. }
  3258. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3259. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3260. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3261. {
  3262. EXPECT_DECLS;
  3263. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3264. return EXPECT_RESULT();
  3265. }
  3266. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3267. {
  3268. EXPECT_DECLS;
  3269. /* Set TLS 1.2 */
  3270. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3271. WOLFSSL_SUCCESS);
  3272. return EXPECT_RESULT();
  3273. }
  3274. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3275. * what was set at ctx creation. */
  3276. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3277. {
  3278. EXPECT_DECLS;
  3279. test_ssl_cbf client_cbs;
  3280. test_ssl_cbf server_cbs;
  3281. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3282. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3283. client_cbs.method = wolfTLS_client_method;
  3284. server_cbs.method = wolfTLS_server_method;
  3285. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3286. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3287. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3288. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3289. &server_cbs, NULL), TEST_SUCCESS);
  3290. return EXPECT_RESULT();
  3291. }
  3292. #else
  3293. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3294. {
  3295. return TEST_SKIPPED;
  3296. }
  3297. #endif
  3298. /*----------------------------------------------------------------------------*
  3299. | SSL
  3300. *----------------------------------------------------------------------------*/
  3301. static int test_server_wolfSSL_new(void)
  3302. {
  3303. int res = TEST_SKIPPED;
  3304. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3305. !defined(NO_WOLFSSL_SERVER)
  3306. EXPECT_DECLS;
  3307. WOLFSSL_CTX *ctx = NULL;
  3308. WOLFSSL_CTX *ctx_nocert = NULL;
  3309. WOLFSSL *ssl = NULL;
  3310. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3311. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3312. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3313. WOLFSSL_FILETYPE_PEM));
  3314. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3315. WOLFSSL_FILETYPE_PEM));
  3316. /* invalid context */
  3317. ExpectNull(ssl = wolfSSL_new(NULL));
  3318. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3319. !defined(OPENSSL_EXTRA)
  3320. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3321. #endif
  3322. /* success */
  3323. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3324. wolfSSL_free(ssl);
  3325. wolfSSL_CTX_free(ctx);
  3326. wolfSSL_CTX_free(ctx_nocert);
  3327. res = EXPECT_RESULT();
  3328. #endif
  3329. return res;
  3330. }
  3331. static int test_client_wolfSSL_new(void)
  3332. {
  3333. int res = TEST_SKIPPED;
  3334. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3335. !defined(NO_WOLFSSL_CLIENT)
  3336. EXPECT_DECLS;
  3337. WOLFSSL_CTX *ctx = NULL;
  3338. WOLFSSL_CTX *ctx_nocert = NULL;
  3339. WOLFSSL *ssl = NULL;
  3340. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3341. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3342. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3343. /* invalid context */
  3344. ExpectNull(ssl = wolfSSL_new(NULL));
  3345. /* success */
  3346. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3347. wolfSSL_free(ssl);
  3348. ssl = NULL;
  3349. /* success */
  3350. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3351. wolfSSL_free(ssl);
  3352. wolfSSL_CTX_free(ctx);
  3353. wolfSSL_CTX_free(ctx_nocert);
  3354. res = EXPECT_RESULT();
  3355. #endif
  3356. return res;
  3357. }
  3358. static int test_wolfSSL_SetTmpDH_file(void)
  3359. {
  3360. int res = TEST_SKIPPED;
  3361. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3362. !defined(NO_WOLFSSL_SERVER)
  3363. EXPECT_DECLS;
  3364. WOLFSSL_CTX *ctx = NULL;
  3365. WOLFSSL *ssl = NULL;
  3366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3367. #ifndef NO_RSA
  3368. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3369. WOLFSSL_FILETYPE_PEM));
  3370. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3371. WOLFSSL_FILETYPE_PEM));
  3372. #elif defined(HAVE_ECC)
  3373. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3374. WOLFSSL_FILETYPE_PEM));
  3375. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3376. WOLFSSL_FILETYPE_PEM));
  3377. #elif defined(HAVE_ED25519)
  3378. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3379. WOLFSSL_FILETYPE_PEM));
  3380. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3381. WOLFSSL_FILETYPE_PEM));
  3382. #elif defined(HAVE_ED448)
  3383. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3384. WOLFSSL_FILETYPE_PEM));
  3385. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3386. WOLFSSL_FILETYPE_PEM));
  3387. #endif
  3388. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3389. /* invalid ssl */
  3390. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3391. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3392. /* invalid dhParamFile file */
  3393. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3394. NULL, WOLFSSL_FILETYPE_PEM));
  3395. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3396. bogusFile, WOLFSSL_FILETYPE_PEM));
  3397. /* success */
  3398. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3399. WOLFSSL_FILETYPE_PEM));
  3400. wolfSSL_free(ssl);
  3401. wolfSSL_CTX_free(ctx);
  3402. res = EXPECT_RESULT();
  3403. #endif
  3404. return res;
  3405. }
  3406. static int test_wolfSSL_SetTmpDH_buffer(void)
  3407. {
  3408. int res = TEST_SKIPPED;
  3409. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3410. EXPECT_DECLS;
  3411. WOLFSSL_CTX *ctx = NULL;
  3412. WOLFSSL *ssl = NULL;
  3413. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3414. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3415. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3416. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3417. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3418. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3419. /* invalid ssl */
  3420. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3421. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3422. /* invalid dhParamFile file */
  3423. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3424. 0, WOLFSSL_FILETYPE_ASN1));
  3425. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3426. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3427. /* success */
  3428. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3429. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3430. wolfSSL_free(ssl);
  3431. wolfSSL_CTX_free(ctx);
  3432. res = EXPECT_RESULT();
  3433. #endif
  3434. return res;
  3435. }
  3436. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3437. {
  3438. int res = TEST_SKIPPED;
  3439. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3440. EXPECT_DECLS;
  3441. WOLFSSL_CTX *ctx = NULL;
  3442. WOLFSSL_CTX *ctx2 = NULL;
  3443. WOLFSSL *ssl = NULL;
  3444. WOLFSSL *ssl2 = NULL;
  3445. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3446. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3447. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3448. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3449. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3450. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3451. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3452. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3453. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3454. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3455. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3456. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3457. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3458. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3459. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3460. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3461. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3462. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3463. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3464. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3465. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3466. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3467. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3468. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3469. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3470. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3471. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3472. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3473. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3474. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3475. wolfSSL_free(ssl2);
  3476. wolfSSL_CTX_free(ctx2);
  3477. wolfSSL_free(ssl);
  3478. wolfSSL_CTX_free(ctx);
  3479. res = EXPECT_RESULT();
  3480. #endif
  3481. return res;
  3482. }
  3483. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3484. * allowed.
  3485. * POST: return 1 on success.
  3486. */
  3487. static int test_wolfSSL_SetMinVersion(void)
  3488. {
  3489. int res = TEST_SKIPPED;
  3490. #ifndef NO_WOLFSSL_CLIENT
  3491. int failFlag = WOLFSSL_SUCCESS;
  3492. WOLFSSL_CTX* ctx = NULL;
  3493. WOLFSSL* ssl = NULL;
  3494. int itr;
  3495. #ifndef NO_OLD_TLS
  3496. const int versions[] = {
  3497. #ifdef WOLFSSL_ALLOW_TLSV10
  3498. WOLFSSL_TLSV1,
  3499. #endif
  3500. WOLFSSL_TLSV1_1,
  3501. WOLFSSL_TLSV1_2};
  3502. #elif !defined(WOLFSSL_NO_TLS12)
  3503. const int versions[] = { WOLFSSL_TLSV1_2 };
  3504. #else
  3505. const int versions[] = { WOLFSSL_TLSV1_3 };
  3506. #endif
  3507. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3508. ssl = wolfSSL_new(ctx);
  3509. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3510. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3511. failFlag = WOLFSSL_FAILURE;
  3512. }
  3513. }
  3514. wolfSSL_free(ssl);
  3515. wolfSSL_CTX_free(ctx);
  3516. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3517. #endif
  3518. return res;
  3519. } /* END test_wolfSSL_SetMinVersion */
  3520. #ifdef OPENSSL_EXTRA
  3521. static int test_ED25519(void)
  3522. {
  3523. int res = TEST_SKIPPED;
  3524. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3525. defined(WOLFSSL_KEY_GEN)
  3526. byte priv[ED25519_PRV_KEY_SIZE];
  3527. unsigned int privSz = (unsigned int)sizeof(priv);
  3528. byte pub[ED25519_PUB_KEY_SIZE];
  3529. unsigned int pubSz = (unsigned int)sizeof(pub);
  3530. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3531. const char* msg = TEST_STRING;
  3532. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3533. byte sig[ED25519_SIG_SIZE];
  3534. unsigned int sigSz = (unsigned int)sizeof(sig);
  3535. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3536. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3537. WOLFSSL_SUCCESS);
  3538. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3539. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3540. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3541. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3542. &sigSz), WOLFSSL_SUCCESS);
  3543. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3544. #ifdef HAVE_ED25519_VERIFY
  3545. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3546. sigSz), WOLFSSL_SUCCESS);
  3547. #endif /* HAVE_ED25519_VERIFY */
  3548. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3549. res = TEST_RES_CHECK(1);
  3550. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3551. return res;
  3552. }
  3553. static int test_ED448(void)
  3554. {
  3555. int res = TEST_SKIPPED;
  3556. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3557. defined(WOLFSSL_KEY_GEN)
  3558. byte priv[ED448_PRV_KEY_SIZE];
  3559. unsigned int privSz = (unsigned int)sizeof(priv);
  3560. byte pub[ED448_PUB_KEY_SIZE];
  3561. unsigned int pubSz = (unsigned int)sizeof(pub);
  3562. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3563. const char* msg = TEST_STRING;
  3564. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3565. byte sig[ED448_SIG_SIZE];
  3566. unsigned int sigSz = (unsigned int)sizeof(sig);
  3567. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3568. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3569. WOLFSSL_SUCCESS);
  3570. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3571. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3572. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3573. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3574. &sigSz), WOLFSSL_SUCCESS);
  3575. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3576. #ifdef HAVE_ED448_VERIFY
  3577. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3578. sigSz), WOLFSSL_SUCCESS);
  3579. #endif /* HAVE_ED448_VERIFY */
  3580. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3581. res = TEST_RES_CHECK(1);
  3582. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3583. return res;
  3584. }
  3585. #endif /* OPENSSL_EXTRA */
  3586. #include <wolfssl/openssl/pem.h>
  3587. /*----------------------------------------------------------------------------*
  3588. | EVP
  3589. *----------------------------------------------------------------------------*/
  3590. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3591. {
  3592. int res = TEST_SKIPPED;
  3593. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3594. EXPECT_DECLS;
  3595. WOLFSSL_BIO* rbio = NULL;
  3596. WOLFSSL_BIO* wbio = NULL;
  3597. WOLFSSL_EVP_PKEY* pkey = NULL;
  3598. char line[256] = { 0 };
  3599. char line1[256] = { 0 };
  3600. int i;
  3601. /* test error cases */
  3602. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3603. /*
  3604. * test RSA public key print
  3605. * in this test, pass '3' for indent
  3606. */
  3607. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3608. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3609. sizeof_client_keypub_der_1024));
  3610. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3611. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3612. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3613. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3614. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3615. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  3616. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3617. strcpy(line1, " Modulus:\n");
  3618. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3619. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3620. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3621. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3622. /* skip to the end of modulus element*/
  3623. for (i = 0; i < 8 ;i++) {
  3624. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3625. }
  3626. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3627. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3628. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3629. /* should reach EOF */
  3630. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  3631. EVP_PKEY_free(pkey);
  3632. pkey = NULL;
  3633. BIO_free(rbio);
  3634. BIO_free(wbio);
  3635. rbio = NULL;
  3636. wbio = NULL;
  3637. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3638. /*
  3639. * test DSA public key print
  3640. */
  3641. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3642. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3643. sizeof_dsa_pub_key_der_2048));
  3644. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3645. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3646. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3647. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3648. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3649. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3650. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3651. strcpy(line1, "pub:\n");
  3652. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3653. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3654. strcpy(line1,
  3655. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3656. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3657. /* skip to the end of pub element*/
  3658. for (i = 0; i < 17 ;i++) {
  3659. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3660. }
  3661. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3662. strcpy(line1, "P:\n");
  3663. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3664. /* skip to the end of P element*/
  3665. for (i = 0; i < 18 ;i++) {
  3666. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3667. }
  3668. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3669. strcpy(line1, "Q:\n");
  3670. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3671. /* skip to the end of Q element*/
  3672. for (i = 0; i < 3 ;i++) {
  3673. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3674. }
  3675. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3676. strcpy(line1, "G:\n");
  3677. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3678. /* skip to the end of G element*/
  3679. for (i = 0; i < 18 ;i++) {
  3680. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3681. }
  3682. /* should reach EOF */
  3683. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  3684. EVP_PKEY_free(pkey);
  3685. pkey = NULL;
  3686. BIO_free(rbio);
  3687. BIO_free(wbio);
  3688. rbio = NULL;
  3689. wbio = NULL;
  3690. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3691. /*
  3692. * test ECC public key print
  3693. */
  3694. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3695. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3696. sizeof_ecc_clikeypub_der_256));
  3697. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3698. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3699. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3700. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3701. strcpy(line1, "Public-Key: (256 bit)\n");
  3702. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3703. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3704. strcpy(line1, "pub:\n");
  3705. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3706. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3707. strcpy(line1,
  3708. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3709. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3710. /* skip to the end of pub element*/
  3711. for (i = 0; i < 4 ;i++) {
  3712. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3713. }
  3714. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3715. strcpy(line1, "ASN1 OID: prime256v1\n");
  3716. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3717. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3718. strcpy(line1, "NIST CURVE: P-256\n");
  3719. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3720. /* should reach EOF */
  3721. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  3722. EVP_PKEY_free(pkey);
  3723. pkey = NULL;
  3724. BIO_free(rbio);
  3725. BIO_free(wbio);
  3726. rbio = NULL;
  3727. wbio = NULL;
  3728. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3729. /*
  3730. * test DH public key print
  3731. */
  3732. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3733. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3734. sizeof_dh_pub_key_der_2048));
  3735. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3736. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3737. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  3738. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3739. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3740. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3741. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3742. strcpy(line1, "public-key:\n");
  3743. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3744. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3745. strcpy(line1,
  3746. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3747. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3748. /* skip to the end of public-key element*/
  3749. for (i = 0; i < 17 ;i++) {
  3750. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3751. }
  3752. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3753. strcpy(line1, "prime:\n");
  3754. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3755. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3756. strcpy(line1,
  3757. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3758. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3759. /* skip to the end of prime element*/
  3760. for (i = 0; i < 17 ;i++) {
  3761. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3762. }
  3763. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3764. strcpy(line1, "generator: 2 (0x02)\n");
  3765. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3766. /* should reach EOF */
  3767. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  3768. EVP_PKEY_free(pkey);
  3769. pkey = NULL;
  3770. BIO_free(rbio);
  3771. BIO_free(wbio);
  3772. rbio = NULL;
  3773. wbio = NULL;
  3774. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3775. /* to prevent "unused variable" warning */
  3776. (void)pkey;
  3777. (void)wbio;
  3778. (void)rbio;
  3779. (void)line;
  3780. (void)line1;
  3781. (void)i;
  3782. res = EXPECT_RESULT();
  3783. #endif /* OPENSSL_EXTRA */
  3784. return res;
  3785. }
  3786. /* Test functions for base64 encode/decode */
  3787. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3788. {
  3789. int res = TEST_SKIPPED;
  3790. #if defined(OPENSSL_EXTRA) && \
  3791. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3792. EXPECT_DECLS;
  3793. EVP_ENCODE_CTX* ctx = NULL;
  3794. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  3795. ExpectIntEQ(ctx->remaining,0);
  3796. ExpectIntEQ(ctx->data[0],0);
  3797. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  3798. EVP_ENCODE_CTX_free(ctx);
  3799. res = EXPECT_RESULT();
  3800. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3801. return res;
  3802. }
  3803. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3804. {
  3805. int res = TEST_SKIPPED;
  3806. #if defined(OPENSSL_EXTRA) && \
  3807. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3808. EXPECT_DECLS;
  3809. EVP_ENCODE_CTX* ctx = NULL;
  3810. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  3811. EVP_ENCODE_CTX_free(ctx);
  3812. res = EXPECT_RESULT();
  3813. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3814. return res;
  3815. }
  3816. static int test_wolfSSL_EVP_EncodeInit(void)
  3817. {
  3818. int res = TEST_SKIPPED;
  3819. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3820. EXPECT_DECLS;
  3821. EVP_ENCODE_CTX* ctx = NULL;
  3822. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  3823. ExpectIntEQ(ctx->remaining, 0);
  3824. ExpectIntEQ(ctx->data[0], 0);
  3825. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  3826. if (ctx != NULL) {
  3827. /* make ctx dirty */
  3828. ctx->remaining = 10;
  3829. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  3830. }
  3831. EVP_EncodeInit(ctx);
  3832. ExpectIntEQ(ctx->remaining, 0);
  3833. ExpectIntEQ(ctx->data[0], 0);
  3834. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  3835. EVP_ENCODE_CTX_free(ctx);
  3836. res = EXPECT_RESULT();
  3837. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3838. return res;
  3839. }
  3840. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3841. {
  3842. int res = TEST_SKIPPED;
  3843. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3844. EXPECT_DECLS;
  3845. int outl;
  3846. int total;
  3847. const unsigned char plain0[] = {"Th"};
  3848. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3849. const unsigned char plain2[] = {"This is additional data."};
  3850. const unsigned char enc0[] = {"VGg=\n"};
  3851. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3852. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3853. const unsigned char enc2[] =
  3854. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3855. unsigned char encOutBuff[300];
  3856. EVP_ENCODE_CTX* ctx = NULL;
  3857. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  3858. EVP_EncodeInit(ctx);
  3859. /* illegal parameter test */
  3860. ExpectIntEQ(
  3861. EVP_EncodeUpdate(
  3862. NULL, /* pass NULL as ctx */
  3863. encOutBuff,
  3864. &outl,
  3865. plain1,
  3866. sizeof(plain1)-1),
  3867. 0 /* expected result code 0: fail */
  3868. );
  3869. ExpectIntEQ(
  3870. EVP_EncodeUpdate(
  3871. ctx,
  3872. NULL, /* pass NULL as out buff */
  3873. &outl,
  3874. plain1,
  3875. sizeof(plain1)-1),
  3876. 0 /* expected result code 0: fail */
  3877. );
  3878. ExpectIntEQ(
  3879. EVP_EncodeUpdate(
  3880. ctx,
  3881. encOutBuff,
  3882. NULL, /* pass NULL as outl */
  3883. plain1,
  3884. sizeof(plain1)-1),
  3885. 0 /* expected result code 0: fail */
  3886. );
  3887. ExpectIntEQ(
  3888. EVP_EncodeUpdate(
  3889. ctx,
  3890. encOutBuff,
  3891. &outl,
  3892. NULL, /* pass NULL as in */
  3893. sizeof(plain1)-1),
  3894. 0 /* expected result code 0: fail */
  3895. );
  3896. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3897. /* meaningless parameter test */
  3898. ExpectIntEQ(
  3899. EVP_EncodeUpdate(
  3900. ctx,
  3901. encOutBuff,
  3902. &outl,
  3903. plain1,
  3904. 0), /* pass zero input */
  3905. 1 /* expected result code 1: success */
  3906. );
  3907. /* very small data encoding test */
  3908. EVP_EncodeInit(ctx);
  3909. ExpectIntEQ(
  3910. EVP_EncodeUpdate(
  3911. ctx,
  3912. encOutBuff,
  3913. &outl,
  3914. plain0,
  3915. sizeof(plain0)-1),
  3916. 1 /* expected result code 1: success */
  3917. );
  3918. ExpectIntEQ(outl,0);
  3919. if (EXPECT_SUCCESS()) {
  3920. EVP_EncodeFinal(
  3921. ctx,
  3922. encOutBuff + outl,
  3923. &outl);
  3924. }
  3925. ExpectIntEQ( outl, sizeof(enc0)-1);
  3926. ExpectIntEQ(
  3927. XSTRNCMP(
  3928. (const char*)encOutBuff,
  3929. (const char*)enc0,sizeof(enc0) ),
  3930. 0);
  3931. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3932. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3933. sizeof(enc0)-1);
  3934. ExpectIntEQ(
  3935. XSTRNCMP(
  3936. (const char*)encOutBuff,
  3937. (const char*)enc0,sizeof(enc0) ),
  3938. 0);
  3939. /* pass small size( < 48bytes ) input, then make sure they are not
  3940. * encoded and just stored in ctx
  3941. */
  3942. EVP_EncodeInit(ctx);
  3943. total = 0;
  3944. outl = 0;
  3945. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3946. ExpectIntEQ(
  3947. EVP_EncodeUpdate(
  3948. ctx,
  3949. encOutBuff, /* buffer for output */
  3950. &outl, /* size of output */
  3951. plain1, /* input */
  3952. sizeof(plain1)-1), /* size of input */
  3953. 1); /* expected result code 1:success */
  3954. total += outl;
  3955. ExpectIntEQ(outl, 0); /* no output expected */
  3956. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  3957. ExpectTrue(
  3958. XSTRNCMP((const char*)(ctx->data),
  3959. (const char*)plain1,
  3960. ctx->remaining) ==0 );
  3961. ExpectTrue(encOutBuff[0] == 0);
  3962. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3963. * the stored data and the new input together
  3964. */
  3965. ExpectIntEQ(
  3966. EVP_EncodeUpdate(
  3967. ctx,
  3968. encOutBuff + outl, /* buffer for output */
  3969. &outl, /* size of output */
  3970. plain2, /* additional input */
  3971. sizeof(plain2) -1), /* size of additional input */
  3972. 1); /* expected result code 1:success */
  3973. total += outl;
  3974. ExpectIntNE(outl, 0); /* some output is expected this time*/
  3975. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3976. ExpectIntEQ(
  3977. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3978. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3979. EVP_EncodeFinal(
  3980. ctx,
  3981. encOutBuff + outl,
  3982. &outl);
  3983. total += outl;
  3984. ExpectIntNE(total,0);
  3985. ExpectIntNE(outl,0);
  3986. ExpectIntEQ(XSTRNCMP(
  3987. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3988. /* test with illeagal parameters */
  3989. outl = 1;
  3990. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3991. ExpectIntEQ(outl, 0);
  3992. outl = 1;
  3993. EVP_EncodeFinal(ctx, NULL, &outl);
  3994. ExpectIntEQ(outl, 0);
  3995. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3996. EVP_EncodeFinal(NULL, NULL, NULL);
  3997. EVP_ENCODE_CTX_free(ctx);
  3998. res = EXPECT_RESULT();
  3999. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4000. return res;
  4001. }
  4002. static int test_wolfSSL_EVP_EncodeFinal(void)
  4003. {
  4004. int res = TEST_SKIPPED;
  4005. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4006. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4007. * test_wolfSSL_EVP_EncodeUpdate
  4008. */
  4009. res = TEST_SUCCESS;
  4010. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4011. return res;
  4012. }
  4013. static int test_wolfSSL_EVP_DecodeInit(void)
  4014. {
  4015. int res = TEST_SKIPPED;
  4016. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4017. EXPECT_DECLS;
  4018. EVP_ENCODE_CTX* ctx = NULL;
  4019. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4020. ExpectIntEQ( ctx->remaining,0);
  4021. ExpectIntEQ( ctx->data[0],0);
  4022. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4023. if (ctx != NULL) {
  4024. /* make ctx dirty */
  4025. ctx->remaining = 10;
  4026. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4027. }
  4028. EVP_DecodeInit(ctx);
  4029. ExpectIntEQ( ctx->remaining,0);
  4030. ExpectIntEQ( ctx->data[0],0);
  4031. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4032. EVP_ENCODE_CTX_free(ctx);
  4033. res = EXPECT_RESULT();
  4034. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4035. return res;
  4036. }
  4037. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4038. {
  4039. int res = TEST_SKIPPED;
  4040. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4041. EXPECT_DECLS;
  4042. int outl;
  4043. unsigned char decOutBuff[300];
  4044. EVP_ENCODE_CTX* ctx = NULL;
  4045. static const unsigned char enc1[] =
  4046. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4047. /* const unsigned char plain1[] =
  4048. {"This is a base64 decoding test."} */
  4049. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4050. EVP_DecodeInit(ctx);
  4051. /* illegal parameter tests */
  4052. /* pass NULL as ctx */
  4053. ExpectIntEQ(
  4054. EVP_DecodeUpdate(
  4055. NULL, /* pass NULL as ctx */
  4056. decOutBuff,
  4057. &outl,
  4058. enc1,
  4059. sizeof(enc1)-1),
  4060. -1 /* expected result code -1: fail */
  4061. );
  4062. ExpectIntEQ( outl, 0);
  4063. /* pass NULL as output */
  4064. ExpectIntEQ(
  4065. EVP_DecodeUpdate(
  4066. ctx,
  4067. NULL, /* pass NULL as out buff */
  4068. &outl,
  4069. enc1,
  4070. sizeof(enc1)-1),
  4071. -1 /* expected result code -1: fail */
  4072. );
  4073. ExpectIntEQ( outl, 0);
  4074. /* pass NULL as outl */
  4075. ExpectIntEQ(
  4076. EVP_DecodeUpdate(
  4077. ctx,
  4078. decOutBuff,
  4079. NULL, /* pass NULL as outl */
  4080. enc1,
  4081. sizeof(enc1)-1),
  4082. -1 /* expected result code -1: fail */
  4083. );
  4084. /* pass NULL as input */
  4085. ExpectIntEQ(
  4086. EVP_DecodeUpdate(
  4087. ctx,
  4088. decOutBuff,
  4089. &outl,
  4090. NULL, /* pass NULL as in */
  4091. sizeof(enc1)-1),
  4092. -1 /* expected result code -1: fail */
  4093. );
  4094. ExpectIntEQ( outl, 0);
  4095. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4096. /* pass zero length input */
  4097. ExpectIntEQ(
  4098. EVP_DecodeUpdate(
  4099. ctx,
  4100. decOutBuff,
  4101. &outl,
  4102. enc1,
  4103. 0), /* pass zero as input len */
  4104. 1 /* expected result code 1: success */
  4105. );
  4106. /* decode correct base64 string */
  4107. {
  4108. static const unsigned char enc2[] =
  4109. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4110. static const unsigned char plain2[] =
  4111. {"This is a base64 decoding test."};
  4112. EVP_EncodeInit(ctx);
  4113. ExpectIntEQ(
  4114. EVP_DecodeUpdate(
  4115. ctx,
  4116. decOutBuff,
  4117. &outl,
  4118. enc2,
  4119. sizeof(enc2)-1),
  4120. 0 /* expected result code 0: success */
  4121. );
  4122. ExpectIntEQ(outl,sizeof(plain2) -1);
  4123. ExpectIntEQ(
  4124. EVP_DecodeFinal(
  4125. ctx,
  4126. decOutBuff + outl,
  4127. &outl),
  4128. 1 /* expected result code 1: success */
  4129. );
  4130. ExpectIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  4131. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4132. sizeof(plain2) -1 ),0);
  4133. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4134. sizeof(plain2)-1);
  4135. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4136. sizeof(plain2) -1 ),0);
  4137. }
  4138. /* decode correct base64 string which does not have '\n' in its last*/
  4139. {
  4140. static const unsigned char enc3[] =
  4141. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4142. static const unsigned char plain3[] =
  4143. {"This is a base64 decoding test."}; /* 31 chars */
  4144. EVP_EncodeInit(ctx);
  4145. ExpectIntEQ(
  4146. EVP_DecodeUpdate(
  4147. ctx,
  4148. decOutBuff,
  4149. &outl,
  4150. enc3,
  4151. sizeof(enc3)-1),
  4152. 0 /* expected result code 0: success */
  4153. );
  4154. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4155. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4156. sizeof(plain3) -1 ),0);
  4157. ExpectIntEQ(
  4158. EVP_DecodeFinal(
  4159. ctx,
  4160. decOutBuff + outl,
  4161. &outl),
  4162. 1 /* expected result code 1: success */
  4163. );
  4164. ExpectIntEQ(outl,0 );
  4165. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4166. sizeof(plain3)-1);
  4167. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4168. sizeof(plain3) -1 ),0);
  4169. }
  4170. /* decode string which has a padding char ('=') in the illegal position*/
  4171. {
  4172. static const unsigned char enc4[] =
  4173. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4174. EVP_EncodeInit(ctx);
  4175. ExpectIntEQ(
  4176. EVP_DecodeUpdate(
  4177. ctx,
  4178. decOutBuff,
  4179. &outl,
  4180. enc4,
  4181. sizeof(enc4)-1),
  4182. -1 /* expected result code -1: error */
  4183. );
  4184. ExpectIntEQ(outl,0);
  4185. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4186. }
  4187. /* small data decode test */
  4188. {
  4189. static const unsigned char enc00[] = {"VG"};
  4190. static const unsigned char enc01[] = {"g=\n"};
  4191. static const unsigned char plain4[] = {"Th"};
  4192. EVP_EncodeInit(ctx);
  4193. ExpectIntEQ(
  4194. EVP_DecodeUpdate(
  4195. ctx,
  4196. decOutBuff,
  4197. &outl,
  4198. enc00,
  4199. sizeof(enc00)-1),
  4200. 1 /* expected result code 1: success */
  4201. );
  4202. ExpectIntEQ(outl,0);
  4203. ExpectIntEQ(
  4204. EVP_DecodeUpdate(
  4205. ctx,
  4206. decOutBuff + outl,
  4207. &outl,
  4208. enc01,
  4209. sizeof(enc01)-1),
  4210. 0 /* expected result code 0: success */
  4211. );
  4212. ExpectIntEQ(outl,sizeof(plain4)-1);
  4213. /* test with illegal parameters */
  4214. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4215. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4216. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4217. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4218. if (EXPECT_SUCCESS()) {
  4219. EVP_DecodeFinal(
  4220. ctx,
  4221. decOutBuff + outl,
  4222. &outl);
  4223. }
  4224. ExpectIntEQ( outl, 0);
  4225. ExpectIntEQ(
  4226. XSTRNCMP(
  4227. (const char*)decOutBuff,
  4228. (const char*)plain4,sizeof(plain4)-1 ),
  4229. 0);
  4230. }
  4231. EVP_ENCODE_CTX_free(ctx);
  4232. res = EXPECT_RESULT();
  4233. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4234. return res;
  4235. }
  4236. static int test_wolfSSL_EVP_DecodeFinal(void)
  4237. {
  4238. int res = TEST_SKIPPED;
  4239. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4240. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4241. * test_wolfSSL_EVP_DecodeUpdate
  4242. */
  4243. res = TEST_SUCCESS;
  4244. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4245. return res;
  4246. }
  4247. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4248. */
  4249. #ifdef OPENSSL_EXTRA
  4250. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4251. {
  4252. #ifndef NO_AES
  4253. const WOLFSSL_EVP_CIPHER* c;
  4254. c = wolfSSL_EVP_get_cipherbynid(419);
  4255. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4256. defined(WOLFSSL_AES_128)
  4257. AssertNotNull(c);
  4258. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4259. #else
  4260. AssertNull(c);
  4261. #endif
  4262. c = wolfSSL_EVP_get_cipherbynid(423);
  4263. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4264. defined(WOLFSSL_AES_192)
  4265. AssertNotNull(c);
  4266. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4267. #else
  4268. AssertNull(c);
  4269. #endif
  4270. c = wolfSSL_EVP_get_cipherbynid(427);
  4271. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4272. defined(WOLFSSL_AES_256)
  4273. AssertNotNull(c);
  4274. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4275. #else
  4276. AssertNull(c);
  4277. #endif
  4278. c = wolfSSL_EVP_get_cipherbynid(904);
  4279. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4280. AssertNotNull(c);
  4281. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4282. #else
  4283. AssertNull(c);
  4284. #endif
  4285. c = wolfSSL_EVP_get_cipherbynid(905);
  4286. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4287. AssertNotNull(c);
  4288. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4289. #else
  4290. AssertNull(c);
  4291. #endif
  4292. c = wolfSSL_EVP_get_cipherbynid(906);
  4293. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4294. AssertNotNull(c);
  4295. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4296. #else
  4297. AssertNull(c);
  4298. #endif
  4299. c = wolfSSL_EVP_get_cipherbynid(418);
  4300. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4301. AssertNotNull(c);
  4302. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4303. #else
  4304. AssertNull(c);
  4305. #endif
  4306. c = wolfSSL_EVP_get_cipherbynid(422);
  4307. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4308. AssertNotNull(c);
  4309. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4310. #else
  4311. AssertNull(c);
  4312. #endif
  4313. c = wolfSSL_EVP_get_cipherbynid(426);
  4314. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4315. AssertNotNull(c);
  4316. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4317. #else
  4318. AssertNull(c);
  4319. #endif
  4320. #endif /* !NO_AES */
  4321. #ifndef NO_DES3
  4322. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4323. #ifdef WOLFSSL_DES_ECB
  4324. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4325. #endif
  4326. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4327. #ifdef WOLFSSL_DES_ECB
  4328. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4329. #endif
  4330. #endif /* !NO_DES3 */
  4331. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4332. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4333. #endif
  4334. /* test for nid is out of range */
  4335. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4336. return TEST_RES_CHECK(1);
  4337. }
  4338. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4339. {
  4340. int res = TEST_SKIPPED;
  4341. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4342. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4343. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4344. const EVP_CIPHER *test;
  4345. byte key[AES_BLOCK_SIZE] = {0};
  4346. byte iv[AES_BLOCK_SIZE] = {0};
  4347. AssertNotNull(ctx);
  4348. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4349. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4350. test = EVP_CIPHER_CTX_cipher(ctx);
  4351. AssertTrue(init == test);
  4352. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4353. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4354. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4355. EVP_CIPHER_CTX_free(ctx);
  4356. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4357. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4358. res = TEST_RES_CHECK(1);
  4359. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4360. return res;
  4361. }
  4362. #endif /* OPENSSL_EXTRA */
  4363. /*----------------------------------------------------------------------------*
  4364. | IO
  4365. *----------------------------------------------------------------------------*/
  4366. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4367. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4368. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4369. #ifdef WC_SHA512_DIGEST_SIZE
  4370. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4371. #else
  4372. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4373. #endif
  4374. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4375. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4376. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4377. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4378. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4379. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4380. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4381. defined(HAVE_AES_CBC)
  4382. typedef struct openssl_key_ctx {
  4383. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4384. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4385. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4386. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4387. } openssl_key_ctx;
  4388. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4389. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4390. static WC_INLINE int OpenSSLTicketInit(void)
  4391. {
  4392. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4393. if (ret != 0) return ret;
  4394. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4395. sizeof(myOpenSSLKey_ctx.name));
  4396. if (ret != 0) return ret;
  4397. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4398. sizeof(myOpenSSLKey_ctx.key));
  4399. if (ret != 0) return ret;
  4400. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4401. sizeof(myOpenSSLKey_ctx.hmacKey));
  4402. if (ret != 0) return ret;
  4403. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4404. sizeof(myOpenSSLKey_ctx.iv));
  4405. if (ret != 0) return ret;
  4406. return 0;
  4407. }
  4408. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4409. byte name[WOLFSSL_TICKET_NAME_SZ],
  4410. byte iv[WOLFSSL_TICKET_IV_SZ],
  4411. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4412. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4413. (void)ssl;
  4414. if (enc) {
  4415. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4416. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4417. }
  4418. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4419. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4420. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4421. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4422. return 0;
  4423. }
  4424. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4425. if (enc)
  4426. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4427. else
  4428. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4429. return 1;
  4430. }
  4431. static WC_INLINE void OpenSSLTicketCleanup(void)
  4432. {
  4433. wc_FreeRng(&myOpenSSLKey_rng);
  4434. }
  4435. #endif
  4436. #endif
  4437. /* helper functions */
  4438. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4439. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4440. void *ctx)
  4441. {
  4442. struct test_ssl_memio_ctx *test_ctx;
  4443. byte *buf;
  4444. int *len;
  4445. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4446. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4447. buf = test_ctx->c_buff;
  4448. len = &test_ctx->c_len;
  4449. }
  4450. else {
  4451. buf = test_ctx->s_buff;
  4452. len = &test_ctx->s_len;
  4453. }
  4454. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4455. return WOLFSSL_CBIO_ERR_WANT_READ;
  4456. XMEMCPY(buf + *len, data, sz);
  4457. *len += sz;
  4458. return sz;
  4459. }
  4460. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4461. void *ctx)
  4462. {
  4463. struct test_ssl_memio_ctx *test_ctx;
  4464. int read_sz;
  4465. byte *buf;
  4466. int *len;
  4467. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4468. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4469. buf = test_ctx->s_buff;
  4470. len = &test_ctx->s_len;
  4471. }
  4472. else {
  4473. buf = test_ctx->c_buff;
  4474. len = &test_ctx->c_len;
  4475. }
  4476. if (*len == 0)
  4477. return WOLFSSL_CBIO_ERR_WANT_READ;
  4478. read_sz = sz < *len ? sz : *len;
  4479. XMEMCPY(data, buf, read_sz);
  4480. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4481. *len -= read_sz;
  4482. return read_sz;
  4483. }
  4484. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4485. {
  4486. EXPECT_DECLS;
  4487. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4488. int c_sharedCtx = 0;
  4489. int s_sharedCtx = 0;
  4490. #endif
  4491. const char* certFile = svrCertFile;
  4492. const char* keyFile = svrKeyFile;
  4493. /********************************
  4494. * Create WOLFSSL_CTX for client.
  4495. ********************************/
  4496. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4497. if (ctx->c_ctx != NULL) {
  4498. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4499. }
  4500. else
  4501. #endif
  4502. {
  4503. WOLFSSL_METHOD* method = NULL;
  4504. if (ctx->c_cb.method != NULL) {
  4505. method = ctx->c_cb.method();
  4506. }
  4507. else {
  4508. method = wolfSSLv23_client_method();
  4509. }
  4510. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4511. }
  4512. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4513. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4514. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4515. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4516. #endif
  4517. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx, caCertFile, 0),
  4518. WOLFSSL_SUCCESS);
  4519. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4520. if (!c_sharedCtx)
  4521. #endif
  4522. {
  4523. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx->c_ctx, cliCertFile,
  4524. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4525. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  4526. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4527. }
  4528. #ifdef HAVE_CRL
  4529. if (ctx->c_cb.crlPemFile != NULL) {
  4530. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  4531. WOLFSSL_SUCCESS);
  4532. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  4533. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4534. }
  4535. #endif
  4536. if (ctx->c_ciphers != NULL) {
  4537. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  4538. WOLFSSL_SUCCESS);
  4539. }
  4540. if (ctx->c_cb.ctx_ready != NULL) {
  4541. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  4542. }
  4543. /********************************
  4544. * Create WOLFSSL_CTX for server.
  4545. ********************************/
  4546. if (ctx->s_ctx != NULL) {
  4547. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4548. s_sharedCtx = 1;
  4549. #endif
  4550. ctx->s_cb.isSharedCtx = 1;
  4551. }
  4552. else
  4553. {
  4554. WOLFSSL_METHOD* method = NULL;
  4555. if (ctx->s_cb.method != NULL) {
  4556. method = ctx->s_cb.method();
  4557. }
  4558. else {
  4559. method = wolfSSLv23_server_method();
  4560. }
  4561. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  4562. if (EXPECT_FAIL()) {
  4563. XFREE(method, NULL, DYNAMIC_TYPE_METHOD);
  4564. }
  4565. ctx->s_cb.isSharedCtx = 0;
  4566. }
  4567. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  4568. #if defined(HAVE_SESSION_TICKET) && \
  4569. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4570. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4571. OpenSSLTicketInit();
  4572. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  4573. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4574. TicketInit();
  4575. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  4576. #endif
  4577. #endif
  4578. }
  4579. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  4580. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  4581. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  4582. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4583. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx, cliCertFile, 0),
  4584. WOLFSSL_SUCCESS);
  4585. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4586. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  4587. #endif
  4588. if (ctx->s_cb.certPemFile != NULL) {
  4589. certFile = ctx->s_cb.certPemFile;
  4590. }
  4591. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4592. if (!s_sharedCtx)
  4593. #endif
  4594. {
  4595. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx->s_ctx, certFile,
  4596. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4597. }
  4598. if (ctx->s_cb.keyPemFile != NULL) {
  4599. keyFile = ctx->s_cb.keyPemFile;
  4600. }
  4601. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4602. if (!s_sharedCtx)
  4603. #endif
  4604. {
  4605. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  4606. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4607. }
  4608. if (ctx->s_ciphers != NULL) {
  4609. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  4610. WOLFSSL_SUCCESS);
  4611. }
  4612. if (ctx->s_cb.ctx_ready != NULL) {
  4613. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  4614. }
  4615. /****************************
  4616. * Create WOLFSSL for client.
  4617. ****************************/
  4618. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  4619. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  4620. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  4621. if (0
  4622. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4623. || c_sharedCtx
  4624. #endif
  4625. )
  4626. {
  4627. ExpectIntEQ(wolfSSL_use_certificate_file(ctx->c_ssl, cliCertFile,
  4628. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4629. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  4630. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4631. }
  4632. if (ctx->c_cb.ssl_ready != NULL) {
  4633. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  4634. }
  4635. /****************************
  4636. * Create WOLFSSL for server.
  4637. ****************************/
  4638. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  4639. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  4640. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  4641. if (0
  4642. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4643. || s_sharedCtx
  4644. #endif
  4645. )
  4646. {
  4647. ExpectIntEQ(wolfSSL_use_certificate_file(ctx->s_ssl, certFile,
  4648. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4649. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  4650. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4651. }
  4652. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4653. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4654. #elif !defined(NO_DH)
  4655. /* will repick suites with DHE, higher priority than PSK */
  4656. SetDH(ctx->s_ssl);
  4657. #endif
  4658. if (ctx->s_cb.ssl_ready != NULL) {
  4659. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  4660. }
  4661. return EXPECT_RESULT();
  4662. }
  4663. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  4664. int* rounds)
  4665. {
  4666. int handshake_complete = 0;
  4667. int hs_c = 0;
  4668. int hs_s = 0;
  4669. int failing_s = 0;
  4670. int failing_c = 0;
  4671. int ret;
  4672. int err;
  4673. if (rounds != NULL) {
  4674. *rounds = 0;
  4675. }
  4676. while ((!handshake_complete) && (max_rounds > 0)) {
  4677. if (!hs_c) {
  4678. ret = wolfSSL_connect(ctx->c_ssl);
  4679. if (ret == WOLFSSL_SUCCESS) {
  4680. hs_c = 1;
  4681. }
  4682. else {
  4683. err = wolfSSL_get_error(ctx->c_ssl, ret);
  4684. if (err != WOLFSSL_ERROR_WANT_READ &&
  4685. err != WOLFSSL_ERROR_WANT_WRITE) {
  4686. failing_c = 1;
  4687. hs_c = 1;
  4688. if (failing_c && failing_s) {
  4689. break;
  4690. }
  4691. }
  4692. }
  4693. }
  4694. if (!hs_s) {
  4695. ret = wolfSSL_accept(ctx->s_ssl);
  4696. if (ret == WOLFSSL_SUCCESS) {
  4697. hs_s = 1;
  4698. }
  4699. else {
  4700. err = wolfSSL_get_error(ctx->s_ssl, ret);
  4701. if (err != WOLFSSL_ERROR_WANT_READ &&
  4702. err != WOLFSSL_ERROR_WANT_WRITE) {
  4703. failing_s = 1;
  4704. hs_s = 1;
  4705. if (failing_c && failing_s) {
  4706. break;
  4707. }
  4708. }
  4709. }
  4710. }
  4711. handshake_complete = hs_c && hs_s;
  4712. max_rounds--;
  4713. if (rounds != NULL) {
  4714. *rounds += 1;
  4715. }
  4716. }
  4717. if (!handshake_complete) {
  4718. return TEST_FAIL;
  4719. }
  4720. return TEST_SUCCESS;
  4721. }
  4722. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  4723. {
  4724. EXPECT_DECLS;
  4725. char input[1024];
  4726. int idx = 0;
  4727. const char* msg_c = "hello wolfssl!";
  4728. int msglen_c = (int)XSTRLEN(msg_c);
  4729. const char* msg_s = "I hear you fa shizzle!";
  4730. int msglen_s = (int)XSTRLEN(msg_s);
  4731. if (ctx->c_msg != NULL) {
  4732. msg_c = ctx->c_msg;
  4733. msglen_c = ctx->c_msglen;
  4734. }
  4735. if (ctx->s_msg != NULL) {
  4736. msg_s = ctx->s_msg;
  4737. msglen_s = ctx->s_msglen;
  4738. }
  4739. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  4740. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  4741. if (idx >= 0) {
  4742. input[idx] = '\0';
  4743. }
  4744. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  4745. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  4746. ctx->s_cb.return_code = EXPECT_RESULT();
  4747. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  4748. if (idx >= 0) {
  4749. input[idx] = '\0';
  4750. }
  4751. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  4752. ctx->c_cb.return_code = EXPECT_RESULT();
  4753. if (ctx->c_cb.on_result != NULL) {
  4754. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  4755. }
  4756. if (ctx->s_cb.on_result != NULL) {
  4757. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  4758. }
  4759. return EXPECT_RESULT();
  4760. }
  4761. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  4762. {
  4763. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  4764. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  4765. if (ctx->c_cb.on_cleanup != NULL) {
  4766. ctx->c_cb.on_cleanup(ctx->c_ssl);
  4767. }
  4768. if (ctx->s_cb.on_cleanup != NULL) {
  4769. ctx->s_cb.on_cleanup(ctx->s_ssl);
  4770. }
  4771. wolfSSL_shutdown(ctx->s_ssl);
  4772. wolfSSL_shutdown(ctx->c_ssl);
  4773. wolfSSL_free(ctx->s_ssl);
  4774. wolfSSL_free(ctx->c_ssl);
  4775. if (!ctx->s_cb.isSharedCtx) {
  4776. wolfSSL_CTX_free(ctx->s_ctx);
  4777. ctx->s_ctx = NULL;
  4778. }
  4779. if (!ctx->c_cb.isSharedCtx) {
  4780. wolfSSL_CTX_free(ctx->c_ctx);
  4781. ctx->c_ctx = NULL;
  4782. }
  4783. if (!ctx->s_cb.ticNoInit) {
  4784. #if defined(HAVE_SESSION_TICKET) && \
  4785. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4786. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4787. OpenSSLTicketCleanup();
  4788. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4789. TicketCleanup();
  4790. #endif
  4791. #endif
  4792. }
  4793. }
  4794. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  4795. test_ssl_cbf* server_cb, cbType client_on_handshake)
  4796. {
  4797. EXPECT_DECLS;
  4798. struct test_ssl_memio_ctx test_ctx;
  4799. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4800. size_t msg_len;
  4801. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4802. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  4803. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  4804. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  4805. test_ctx.c_ctx = client_cb->ctx;
  4806. test_ctx.s_ctx = server_cb->ctx;
  4807. test_ctx.c_cb.return_code = TEST_FAIL;
  4808. test_ctx.s_cb.return_code = TEST_FAIL;
  4809. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  4810. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  4811. if (client_on_handshake != NULL) {
  4812. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  4813. TEST_SUCCESS);
  4814. }
  4815. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4816. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4817. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  4818. MD_MAX_SIZE);
  4819. ExpectIntGE(msg_len, 0);
  4820. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4821. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  4822. MD_MAX_SIZE);
  4823. ExpectIntGE(msg_len, 0);
  4824. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4825. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  4826. test_ssl_memio_cleanup(&test_ctx);
  4827. client_cb->return_code = test_ctx.c_cb.return_code;
  4828. server_cb->return_code = test_ctx.s_cb.return_code;
  4829. return EXPECT_RESULT();
  4830. }
  4831. #endif
  4832. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4833. #ifdef WOLFSSL_SESSION_EXPORT
  4834. #ifdef WOLFSSL_DTLS
  4835. /* set up function for sending session information */
  4836. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4837. {
  4838. WOLFSSL_CTX* ctx = NULL;
  4839. WOLFSSL* ssl = NULL;
  4840. AssertNotNull(inSsl);
  4841. AssertNotNull(buf);
  4842. AssertIntNE(0, sz);
  4843. /* Set ctx to DTLS 1.2 */
  4844. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4845. AssertNotNull(ctx);
  4846. ssl = wolfSSL_new(ctx);
  4847. AssertNotNull(ssl);
  4848. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4849. wolfSSL_free(ssl);
  4850. wolfSSL_CTX_free(ctx);
  4851. (void)userCtx;
  4852. return 0;
  4853. }
  4854. #endif
  4855. /* returns negative value on fail and positive (including 0) on success */
  4856. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4857. {
  4858. int ret, err, loop_count, count, timeout = 10;
  4859. char msg[] = "I hear you fa shizzle!";
  4860. char input[1024];
  4861. loop_count = ((func_args*)args)->argc;
  4862. #ifdef WOLFSSL_ASYNC_CRYPT
  4863. err = 0; /* Reset error */
  4864. #endif
  4865. do {
  4866. #ifdef WOLFSSL_ASYNC_CRYPT
  4867. if (err == WC_PENDING_E) {
  4868. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4869. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4870. }
  4871. #endif
  4872. ret = wolfSSL_accept(ssl);
  4873. err = wolfSSL_get_error(ssl, 0);
  4874. if (err == WOLFSSL_ERROR_WANT_READ ||
  4875. err == WOLFSSL_ERROR_WANT_WRITE) {
  4876. int select_ret;
  4877. err = WC_PENDING_E;
  4878. select_ret = tcp_select(*sockfd, timeout);
  4879. if (select_ret == TEST_TIMEOUT) {
  4880. return WOLFSSL_FATAL_ERROR;
  4881. }
  4882. }
  4883. } while (err == WC_PENDING_E);
  4884. if (ret != WOLFSSL_SUCCESS) {
  4885. char buff[WOLFSSL_MAX_ERROR_SZ];
  4886. fprintf(stderr, "error = %d, %s\n", err,
  4887. wolfSSL_ERR_error_string(err, buff));
  4888. return ret;
  4889. }
  4890. for (count = 0; count < loop_count; count++) {
  4891. int select_ret;
  4892. select_ret = tcp_select(*sockfd, timeout);
  4893. if (select_ret == TEST_TIMEOUT) {
  4894. ret = WOLFSSL_FATAL_ERROR;
  4895. break;
  4896. }
  4897. do {
  4898. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4899. if (ret > 0) {
  4900. input[ret] = '\0';
  4901. fprintf(stderr, "Client message: %s\n", input);
  4902. }
  4903. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4904. do {
  4905. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4906. return WOLFSSL_FATAL_ERROR;
  4907. }
  4908. err = wolfSSL_get_error(ssl, ret);
  4909. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4910. }
  4911. return ret;
  4912. }
  4913. #endif /* WOLFSSL_SESSION_EXPORT */
  4914. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4915. {
  4916. SOCKET_T sockfd = 0;
  4917. SOCKET_T clientfd = 0;
  4918. word16 port;
  4919. callback_functions* cbf;
  4920. WOLFSSL_CTX* ctx = 0;
  4921. WOLFSSL* ssl = 0;
  4922. func_args* opts = (func_args*)args;
  4923. char msg[] = "I hear you fa shizzle!";
  4924. char input[1024];
  4925. int idx;
  4926. int ret, err = 0;
  4927. int sharedCtx = 0;
  4928. int doUdp = 0;
  4929. SOCKADDR_IN_T cliAddr;
  4930. socklen_t cliLen;
  4931. const char* certFile = svrCertFile;
  4932. const char* keyFile = svrKeyFile;
  4933. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4934. size_t msg_len = 0;
  4935. #endif
  4936. #ifdef WOLFSSL_TIRTOS
  4937. fdOpenSession(Task_self());
  4938. #endif
  4939. opts->return_code = TEST_FAIL;
  4940. cbf = opts->callbacks;
  4941. if (cbf != NULL && cbf->ctx) {
  4942. ctx = cbf->ctx;
  4943. sharedCtx = 1;
  4944. }
  4945. else
  4946. {
  4947. WOLFSSL_METHOD* method = NULL;
  4948. if (cbf != NULL && cbf->method != NULL) {
  4949. method = cbf->method();
  4950. }
  4951. else {
  4952. method = wolfSSLv23_server_method();
  4953. }
  4954. ctx = wolfSSL_CTX_new(method);
  4955. }
  4956. if (ctx == NULL) {
  4957. /* Release the wait for TCP ready. */
  4958. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  4959. opts->signal->ready = 1;
  4960. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  4961. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  4962. goto done;
  4963. }
  4964. if (cbf == NULL || !cbf->ticNoInit) {
  4965. #if defined(HAVE_SESSION_TICKET) && \
  4966. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4967. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4968. OpenSSLTicketInit();
  4969. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4970. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4971. TicketInit();
  4972. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4973. #endif
  4974. #endif
  4975. }
  4976. #if defined(USE_WINDOWS_API)
  4977. port = opts->signal->port;
  4978. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4979. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4980. /* Let tcp_listen assign port */
  4981. port = 0;
  4982. #else
  4983. /* Use default port */
  4984. port = wolfSSLPort;
  4985. #endif
  4986. if (cbf != NULL)
  4987. doUdp = cbf->doUdp;
  4988. /* do it here to detect failure */
  4989. tcp_accept(
  4990. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4991. if (doUdp) {
  4992. cliLen = sizeof(cliAddr);
  4993. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4994. (struct sockaddr*)&cliAddr, &cliLen);
  4995. AssertIntGT(idx, 0);
  4996. }
  4997. else {
  4998. CloseSocket(sockfd);
  4999. }
  5000. wolfSSL_CTX_set_verify(ctx,
  5001. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5002. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5003. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5004. #endif
  5005. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5006. != WOLFSSL_SUCCESS) {
  5007. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5008. goto done;
  5009. }
  5010. if (cbf != NULL && cbf->certPemFile != NULL)
  5011. certFile = cbf->certPemFile;
  5012. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5013. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5014. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5015. #else
  5016. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5017. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5018. #endif
  5019. /*err_sys("can't load server cert chain file, "
  5020. "Please run from wolfSSL home dir");*/
  5021. goto done;
  5022. }
  5023. if (cbf != NULL && cbf->keyPemFile != NULL)
  5024. keyFile = cbf->keyPemFile;
  5025. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5026. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5027. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5028. #else
  5029. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5030. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5031. #endif
  5032. /*err_sys("can't load server key file, "
  5033. "Please run from wolfSSL home dir");*/
  5034. goto done;
  5035. }
  5036. #ifdef HAVE_CRL
  5037. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5038. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5039. goto done;
  5040. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5041. != WOLFSSL_SUCCESS)
  5042. goto done;
  5043. }
  5044. #endif
  5045. /* call ctx setup callback */
  5046. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5047. cbf->ctx_ready(ctx);
  5048. }
  5049. ssl = wolfSSL_new(ctx);
  5050. if (ssl == NULL) {
  5051. goto done;
  5052. }
  5053. if (doUdp) {
  5054. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5055. if (err != WOLFSSL_SUCCESS)
  5056. goto done;
  5057. }
  5058. #ifdef WOLFSSL_SESSION_EXPORT
  5059. /* only add in more complex nonblocking case with session export tests */
  5060. if (args && opts->argc > 0) {
  5061. /* set as nonblock and time out for waiting on read/write */
  5062. tcp_set_nonblocking(&clientfd);
  5063. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5064. }
  5065. #endif
  5066. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5067. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5068. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5069. #else
  5070. if (wolfSSL_use_certificate_file(ssl, certFile,
  5071. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5072. #endif
  5073. /*err_sys("can't load server cert chain file, "
  5074. "Please run from wolfSSL home dir");*/
  5075. goto done;
  5076. }
  5077. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5078. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5079. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5080. #else
  5081. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5082. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5083. #endif
  5084. /*err_sys("can't load server key file, "
  5085. "Please run from wolfSSL home dir");*/
  5086. goto done;
  5087. }
  5088. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5089. /*err_sys("SSL_set_fd failed");*/
  5090. goto done;
  5091. }
  5092. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5093. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5094. #elif !defined(NO_DH)
  5095. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5096. #endif
  5097. /* call ssl setup callback */
  5098. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5099. cbf->ssl_ready(ssl);
  5100. }
  5101. #ifdef WOLFSSL_SESSION_EXPORT
  5102. /* only add in more complex nonblocking case with session export tests */
  5103. if (opts->argc > 0) {
  5104. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5105. if (ret >= 0) {
  5106. opts->return_code = TEST_SUCCESS;
  5107. }
  5108. #ifdef WOLFSSL_TIRTOS
  5109. Task_yield();
  5110. #endif
  5111. goto done;
  5112. }
  5113. #endif
  5114. #ifdef WOLFSSL_ASYNC_CRYPT
  5115. err = 0; /* Reset error */
  5116. #endif
  5117. do {
  5118. #ifdef WOLFSSL_ASYNC_CRYPT
  5119. if (err == WC_PENDING_E) {
  5120. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5121. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5122. }
  5123. #endif
  5124. ret = wolfSSL_accept(ssl);
  5125. err = wolfSSL_get_error(ssl, 0);
  5126. } while (err == WC_PENDING_E);
  5127. if (ret != WOLFSSL_SUCCESS) {
  5128. char buff[WOLFSSL_MAX_ERROR_SZ];
  5129. fprintf(stderr, "error = %d, %s\n", err,
  5130. wolfSSL_ERR_error_string(err, buff));
  5131. /*err_sys("SSL_accept failed");*/
  5132. goto done;
  5133. }
  5134. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5135. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5136. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5137. AssertIntGE(msg_len, 0);
  5138. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5139. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5140. AssertIntGE(msg_len, 0);
  5141. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5142. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5143. if (idx > 0) {
  5144. input[idx] = '\0';
  5145. fprintf(stderr, "Client message: %s\n", input);
  5146. }
  5147. else if (idx < 0) {
  5148. goto done;
  5149. }
  5150. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5151. /*err_sys("SSL_write failed");*/
  5152. goto done;
  5153. }
  5154. if (cbf != NULL && cbf->on_result != NULL)
  5155. cbf->on_result(ssl);
  5156. #ifdef WOLFSSL_TIRTOS
  5157. Task_yield();
  5158. #endif
  5159. opts->return_code = TEST_SUCCESS;
  5160. done:
  5161. if (cbf != NULL)
  5162. cbf->last_err = err;
  5163. if (cbf != NULL && cbf->on_cleanup != NULL)
  5164. cbf->on_cleanup(ssl);
  5165. wolfSSL_shutdown(ssl);
  5166. wolfSSL_free(ssl);
  5167. if (!sharedCtx)
  5168. wolfSSL_CTX_free(ctx);
  5169. CloseSocket(clientfd);
  5170. #ifdef WOLFSSL_TIRTOS
  5171. fdCloseSession(Task_self());
  5172. #endif
  5173. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5174. && defined(HAVE_THREAD_LS)
  5175. wc_ecc_fp_free(); /* free per thread cache */
  5176. #endif
  5177. if (cbf == NULL || !cbf->ticNoInit) {
  5178. #if defined(HAVE_SESSION_TICKET) && \
  5179. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5180. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5181. OpenSSLTicketCleanup();
  5182. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5183. TicketCleanup();
  5184. #endif
  5185. #endif
  5186. }
  5187. #ifndef WOLFSSL_TIRTOS
  5188. return 0;
  5189. #endif
  5190. }
  5191. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5192. !defined(WOLFSSL_NO_TLS12)
  5193. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5194. {
  5195. SOCKET_T sockfd = 0;
  5196. SOCKET_T clientfd = 0;
  5197. word16 port;
  5198. callback_functions* cbf;
  5199. WOLFSSL_CTX* ctx = 0;
  5200. WOLFSSL* ssl = 0;
  5201. char msg[] = "I hear you fa shizzle!";
  5202. char input[1024];
  5203. int idx;
  5204. int ret, err = 0;
  5205. int sharedCtx = 0;
  5206. func_args* opts = (func_args*)args;
  5207. int loop_count = opts->argc;
  5208. int count = 0;
  5209. #ifdef WOLFSSL_TIRTOS
  5210. fdOpenSession(Task_self());
  5211. #endif
  5212. opts->return_code = TEST_FAIL;
  5213. cbf = opts->callbacks;
  5214. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5215. if (cbf != NULL && cbf->ctx) {
  5216. ctx = cbf->ctx;
  5217. sharedCtx = 1;
  5218. }
  5219. else
  5220. #endif
  5221. {
  5222. WOLFSSL_METHOD* method = NULL;
  5223. if (cbf != NULL && cbf->method != NULL) {
  5224. method = cbf->method();
  5225. }
  5226. else {
  5227. method = wolfSSLv23_server_method();
  5228. }
  5229. ctx = wolfSSL_CTX_new(method);
  5230. }
  5231. #if defined(USE_WINDOWS_API)
  5232. port = opts->signal->port;
  5233. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5234. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5235. /* Let tcp_listen assign port */
  5236. port = 0;
  5237. #else
  5238. /* Use default port */
  5239. port = wolfSSLPort;
  5240. #endif
  5241. wolfSSL_CTX_set_verify(ctx,
  5242. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5243. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5244. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5245. #endif
  5246. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5247. != WOLFSSL_SUCCESS) {
  5248. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5249. /* Release the wait for TCP ready. */
  5250. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5251. opts->signal->ready = 1;
  5252. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5253. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5254. goto done;
  5255. }
  5256. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5257. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5258. /*err_sys("can't load server cert chain file, "
  5259. "Please run from wolfSSL home dir");*/
  5260. /* Release the wait for TCP ready. */
  5261. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5262. opts->signal->ready = 1;
  5263. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5264. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5265. goto done;
  5266. }
  5267. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5268. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5269. /*err_sys("can't load server key file, "
  5270. "Please run from wolfSSL home dir");*/
  5271. /* Release the wait for TCP ready. */
  5272. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5273. opts->signal->ready = 1;
  5274. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5275. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5276. goto done;
  5277. }
  5278. /* call ctx setup callback */
  5279. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5280. cbf->ctx_ready(ctx);
  5281. }
  5282. while (count != loop_count) {
  5283. ssl = wolfSSL_new(ctx);
  5284. if (ssl == NULL) {
  5285. /* Release the wait for TCP ready. */
  5286. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5287. opts->signal->ready = 1;
  5288. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5289. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5290. goto done;
  5291. }
  5292. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5293. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5294. /*err_sys("can't load server cert chain file, "
  5295. "Please run from wolfSSL home dir");*/
  5296. /* Release the wait for TCP ready. */
  5297. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5298. opts->signal->ready = 1;
  5299. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5300. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5301. goto done;
  5302. }
  5303. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5304. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5305. /*err_sys("can't load server key file, "
  5306. "Please run from wolfSSL home dir");*/
  5307. /* Release the wait for TCP ready. */
  5308. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5309. opts->signal->ready = 1;
  5310. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5311. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5312. goto done;
  5313. }
  5314. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5315. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5316. #elif !defined(NO_DH)
  5317. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5318. #endif
  5319. /* call ssl setup callback */
  5320. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5321. cbf->ssl_ready(ssl);
  5322. }
  5323. /* do it here to detect failure */
  5324. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5325. CloseSocket(sockfd);
  5326. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5327. /*err_sys("SSL_set_fd failed");*/
  5328. goto done;
  5329. }
  5330. #ifdef WOLFSSL_ASYNC_CRYPT
  5331. err = 0; /* Reset error */
  5332. #endif
  5333. do {
  5334. #ifdef WOLFSSL_ASYNC_CRYPT
  5335. if (err == WC_PENDING_E) {
  5336. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5337. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5338. }
  5339. #endif
  5340. ret = wolfSSL_accept(ssl);
  5341. err = wolfSSL_get_error(ssl, 0);
  5342. } while (err == WC_PENDING_E);
  5343. if (ret != WOLFSSL_SUCCESS) {
  5344. char buff[WOLFSSL_MAX_ERROR_SZ];
  5345. fprintf(stderr, "error = %d, %s\n", err,
  5346. wolfSSL_ERR_error_string(err, buff));
  5347. /*err_sys("SSL_accept failed");*/
  5348. goto done;
  5349. }
  5350. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5351. if (idx > 0) {
  5352. input[idx] = '\0';
  5353. fprintf(stderr, "Client message: %s\n", input);
  5354. }
  5355. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5356. /*err_sys("SSL_write failed");*/
  5357. goto done;
  5358. }
  5359. /* free ssl for this connection */
  5360. wolfSSL_shutdown(ssl);
  5361. wolfSSL_free(ssl); ssl = NULL;
  5362. CloseSocket(clientfd);
  5363. count++;
  5364. }
  5365. #ifdef WOLFSSL_TIRTOS
  5366. Task_yield();
  5367. #endif
  5368. opts->return_code = TEST_SUCCESS;
  5369. done:
  5370. if (ssl != NULL) {
  5371. wolfSSL_shutdown(ssl);
  5372. wolfSSL_free(ssl);
  5373. }
  5374. if (!sharedCtx)
  5375. wolfSSL_CTX_free(ctx);
  5376. CloseSocket(clientfd);
  5377. #ifdef WOLFSSL_TIRTOS
  5378. fdCloseSession(Task_self());
  5379. #endif
  5380. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5381. && defined(HAVE_THREAD_LS)
  5382. wc_ecc_fp_free(); /* free per thread cache */
  5383. #endif
  5384. #ifndef WOLFSSL_TIRTOS
  5385. return 0;
  5386. #endif
  5387. }
  5388. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5389. static int test_client_nofail(void* args, cbType cb)
  5390. {
  5391. #if !defined(NO_WOLFSSL_CLIENT)
  5392. SOCKET_T sockfd = 0;
  5393. callback_functions* cbf;
  5394. WOLFSSL_CTX* ctx = 0;
  5395. WOLFSSL* ssl = 0;
  5396. WOLFSSL_CIPHER* cipher;
  5397. char msg[64] = "hello wolfssl!";
  5398. char reply[1024];
  5399. int input;
  5400. int msgSz = (int)XSTRLEN(msg);
  5401. int ret, err = 0;
  5402. int cipherSuite;
  5403. int sharedCtx = 0;
  5404. int doUdp = 0;
  5405. const char* cipherName1, *cipherName2;
  5406. #ifdef WOLFSSL_TIRTOS
  5407. fdOpenSession(Task_self());
  5408. #endif
  5409. ((func_args*)args)->return_code = TEST_FAIL;
  5410. cbf = ((func_args*)args)->callbacks;
  5411. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5412. if (cbf != NULL && cbf->ctx) {
  5413. ctx = cbf->ctx;
  5414. sharedCtx = cbf->isSharedCtx;
  5415. }
  5416. else
  5417. #endif
  5418. {
  5419. WOLFSSL_METHOD* method = NULL;
  5420. if (cbf != NULL && cbf->method != NULL) {
  5421. method = cbf->method();
  5422. }
  5423. else {
  5424. method = wolfSSLv23_client_method();
  5425. }
  5426. ctx = wolfSSL_CTX_new(method);
  5427. }
  5428. if (cbf != NULL)
  5429. doUdp = cbf->doUdp;
  5430. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5431. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5432. #endif
  5433. /* Do connect here so server detects failures */
  5434. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5435. doUdp, 0, NULL);
  5436. /* Connect the socket so that we don't have to set the peer later on */
  5437. if (doUdp)
  5438. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5439. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5440. {
  5441. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5442. goto done;
  5443. }
  5444. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5445. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5446. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5447. #else
  5448. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5449. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5450. #endif
  5451. /*err_sys("can't load client cert file, "
  5452. "Please run from wolfSSL home dir");*/
  5453. goto done;
  5454. }
  5455. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5456. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5457. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5458. #else
  5459. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5460. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5461. #endif
  5462. /*err_sys("can't load client key file, "
  5463. "Please run from wolfSSL home dir");*/
  5464. goto done;
  5465. }
  5466. #ifdef HAVE_CRL
  5467. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5468. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5469. goto done;
  5470. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5471. != WOLFSSL_SUCCESS)
  5472. goto done;
  5473. }
  5474. #endif
  5475. /* call ctx setup callback */
  5476. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5477. cbf->ctx_ready(ctx);
  5478. }
  5479. ssl = wolfSSL_new(ctx);
  5480. if (ssl == NULL) {
  5481. goto done;
  5482. }
  5483. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5484. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5485. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5486. #else
  5487. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5488. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5489. #endif
  5490. /*err_sys("can't load client cert file, "
  5491. "Please run from wolfSSL home dir");*/
  5492. goto done;
  5493. }
  5494. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5495. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5496. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5497. #else
  5498. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5499. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5500. #endif
  5501. /*err_sys("can't load client key file, "
  5502. "Please run from wolfSSL home dir");*/
  5503. goto done;
  5504. }
  5505. if (!doUdp) {
  5506. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5507. /*err_sys("SSL_set_fd failed");*/
  5508. goto done;
  5509. }
  5510. }
  5511. else {
  5512. #ifdef WOLFSSL_DTLS
  5513. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5514. /*err_sys("SSL_set_fd failed");*/
  5515. goto done;
  5516. }
  5517. #else
  5518. goto done;
  5519. #endif
  5520. }
  5521. /* call ssl setup callback */
  5522. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5523. cbf->ssl_ready(ssl);
  5524. }
  5525. #ifdef WOLFSSL_ASYNC_CRYPT
  5526. err = 0; /* Reset error */
  5527. #endif
  5528. do {
  5529. #ifdef WOLFSSL_ASYNC_CRYPT
  5530. if (err == WC_PENDING_E) {
  5531. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5532. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5533. }
  5534. #endif
  5535. ret = wolfSSL_connect(ssl);
  5536. err = wolfSSL_get_error(ssl, 0);
  5537. } while (err == WC_PENDING_E);
  5538. if (ret != WOLFSSL_SUCCESS) {
  5539. char buff[WOLFSSL_MAX_ERROR_SZ];
  5540. fprintf(stderr, "error = %d, %s\n", err,
  5541. wolfSSL_ERR_error_string(err, buff));
  5542. /*err_sys("SSL_connect failed");*/
  5543. goto done;
  5544. }
  5545. /* test the various get cipher methods */
  5546. /* Internal cipher suite names */
  5547. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5548. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5549. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5550. (cipherSuite >> 8), cipherSuite & 0xFF);
  5551. AssertStrEQ(cipherName1, cipherName2);
  5552. /* IANA Cipher Suites Names */
  5553. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5554. then it's the internal cipher suite name */
  5555. cipher = wolfSSL_get_current_cipher(ssl);
  5556. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  5557. cipherName2 = wolfSSL_get_cipher(ssl);
  5558. AssertStrEQ(cipherName1, cipherName2);
  5559. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  5560. !defined(WOLFSSL_QT)
  5561. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  5562. (cipherSuite >> 8), cipherSuite & 0xFF);
  5563. AssertStrEQ(cipherName1, cipherName2);
  5564. #endif
  5565. if (cb != NULL)
  5566. (cb)(ctx, ssl);
  5567. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5568. /*err_sys("SSL_write failed");*/
  5569. goto done;
  5570. }
  5571. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5572. if (input > 0) {
  5573. reply[input] = '\0';
  5574. fprintf(stderr, "Server response: %s\n", reply);
  5575. }
  5576. if (cbf != NULL && cbf->on_result != NULL)
  5577. cbf->on_result(ssl);
  5578. ((func_args*)args)->return_code = TEST_SUCCESS;
  5579. done:
  5580. if (cbf != NULL)
  5581. cbf->last_err = err;
  5582. if (cbf != NULL && cbf->on_cleanup != NULL)
  5583. cbf->on_cleanup(ssl);
  5584. wolfSSL_free(ssl);
  5585. if (!sharedCtx)
  5586. wolfSSL_CTX_free(ctx);
  5587. CloseSocket(sockfd);
  5588. #ifdef WOLFSSL_TIRTOS
  5589. fdCloseSession(Task_self());
  5590. #endif
  5591. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5592. && defined(HAVE_THREAD_LS)
  5593. wc_ecc_fp_free(); /* free per thread cache */
  5594. #endif
  5595. #else
  5596. (void)args;
  5597. (void)cb;
  5598. #endif /* !NO_WOLFSSL_CLIENT */
  5599. return 0;
  5600. }
  5601. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  5602. callback_functions* server_cb, cbType client_on_handshake)
  5603. {
  5604. func_args client_args;
  5605. func_args server_args;
  5606. tcp_ready ready;
  5607. THREAD_TYPE serverThread;
  5608. XMEMSET(&client_args, 0, sizeof(func_args));
  5609. XMEMSET(&server_args, 0, sizeof(func_args));
  5610. #ifdef WOLFSSL_TIRTOS
  5611. fdOpenSession(Task_self());
  5612. #endif
  5613. StartTCP();
  5614. InitTcpReady(&ready);
  5615. #if defined(USE_WINDOWS_API)
  5616. /* use RNG to get random port if using windows */
  5617. ready.port = GetRandomPort();
  5618. #endif
  5619. server_args.signal = &ready;
  5620. server_args.callbacks = server_cb;
  5621. client_args.signal = &ready;
  5622. client_args.callbacks = client_cb;
  5623. start_thread(test_server_nofail, &server_args, &serverThread);
  5624. wait_tcp_ready(&server_args);
  5625. test_client_nofail(&client_args, client_on_handshake);
  5626. join_thread(serverThread);
  5627. client_cb->return_code = client_args.return_code;
  5628. server_cb->return_code = server_args.return_code;
  5629. FreeTcpReady(&ready);
  5630. #ifdef WOLFSSL_TIRTOS
  5631. fdOpenSession(Task_self());
  5632. #endif
  5633. }
  5634. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  5635. callback_functions* server_cb)
  5636. {
  5637. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  5638. }
  5639. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5640. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  5641. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  5642. void* server_args)
  5643. {
  5644. SOCKET_T sockfd = 0;
  5645. callback_functions* cbf;
  5646. WOLFSSL_CTX* ctx = 0;
  5647. WOLFSSL* ssl = 0;
  5648. WOLFSSL_SESSION* session = NULL;
  5649. char msg[64] = "hello wolfssl!";
  5650. char reply[1024];
  5651. int input;
  5652. int msgSz = (int)XSTRLEN(msg);
  5653. int ret, err = 0;
  5654. int sharedCtx = 0;
  5655. #ifdef WOLFSSL_TIRTOS
  5656. fdOpenSession(Task_self());
  5657. #endif
  5658. ((func_args*)args)->return_code = TEST_FAIL;
  5659. cbf = ((func_args*)args)->callbacks;
  5660. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5661. if (cbf != NULL && cbf->ctx) {
  5662. ctx = cbf->ctx;
  5663. sharedCtx = 1;
  5664. }
  5665. else
  5666. #endif
  5667. {
  5668. WOLFSSL_METHOD* method = NULL;
  5669. if (cbf != NULL && cbf->method != NULL) {
  5670. method = cbf->method();
  5671. }
  5672. else {
  5673. method = wolfSSLv23_client_method();
  5674. }
  5675. ctx = wolfSSL_CTX_new(method);
  5676. }
  5677. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5678. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5679. #endif
  5680. /* Do connect here so server detects failures */
  5681. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5682. 0, 0, NULL);
  5683. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5684. {
  5685. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5686. goto done;
  5687. }
  5688. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5689. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5690. /*err_sys("can't load client cert file, "
  5691. "Please run from wolfSSL home dir");*/
  5692. goto done;
  5693. }
  5694. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5695. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5696. /*err_sys("can't load client key file, "
  5697. "Please run from wolfSSL home dir");*/
  5698. goto done;
  5699. }
  5700. /* call ctx setup callback */
  5701. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5702. cbf->ctx_ready(ctx);
  5703. }
  5704. ssl = wolfSSL_new(ctx);
  5705. if (ssl == NULL) {
  5706. goto done;
  5707. }
  5708. /* keep handshakre resources for re-using WOLFSSL obj */
  5709. wolfSSL_KeepArrays(ssl);
  5710. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5711. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5712. goto done;
  5713. }
  5714. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5715. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5716. /*err_sys("can't load client cert file, "
  5717. "Please run from wolfSSL home dir");*/
  5718. goto done;
  5719. }
  5720. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5721. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5722. /*err_sys("can't load client key file, "
  5723. "Please run from wolfSSL home dir");*/
  5724. goto done;
  5725. }
  5726. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5727. /*err_sys("SSL_set_fd failed");*/
  5728. goto done;
  5729. }
  5730. /* call ssl setup callback */
  5731. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5732. cbf->ssl_ready(ssl);
  5733. }
  5734. #ifdef WOLFSSL_ASYNC_CRYPT
  5735. err = 0; /* Reset error */
  5736. #endif
  5737. do {
  5738. #ifdef WOLFSSL_ASYNC_CRYPT
  5739. if (err == WC_PENDING_E) {
  5740. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5741. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5742. }
  5743. #endif
  5744. ret = wolfSSL_connect(ssl);
  5745. err = wolfSSL_get_error(ssl, 0);
  5746. } while (err == WC_PENDING_E);
  5747. if (ret != WOLFSSL_SUCCESS) {
  5748. char buff[WOLFSSL_MAX_ERROR_SZ];
  5749. fprintf(stderr, "error = %d, %s\n", err,
  5750. wolfSSL_ERR_error_string(err, buff));
  5751. /*err_sys("SSL_connect failed");*/
  5752. goto done;
  5753. }
  5754. /* Build first session */
  5755. if (cb != NULL)
  5756. cb(ctx, ssl);
  5757. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5758. /*err_sys("SSL_write failed");*/
  5759. goto done;
  5760. }
  5761. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5762. if (input > 0) {
  5763. reply[input] = '\0';
  5764. fprintf(stderr, "Server response: %s\n", reply);
  5765. }
  5766. /* Session Resumption by re-using WOLFSSL object */
  5767. wolfSSL_set_quiet_shutdown(ssl, 1);
  5768. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5769. /* err_sys ("SSL shutdown failed"); */
  5770. goto done;
  5771. }
  5772. session = wolfSSL_get1_session(ssl);
  5773. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5774. wolfSSL_SESSION_free(session);
  5775. /* err_sys ("SSL_clear failed"); */
  5776. goto done;
  5777. }
  5778. wolfSSL_set_session(ssl, session);
  5779. wolfSSL_SESSION_free(session);
  5780. session = NULL;
  5781. /* close socket once */
  5782. CloseSocket(sockfd);
  5783. sockfd = 0;
  5784. /* wait until server ready */
  5785. wait_tcp_ready((func_args*)server_args);
  5786. fprintf(stderr, "session resumption\n");
  5787. /* Do re-connect */
  5788. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5789. 0, 0, NULL);
  5790. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5791. /*err_sys("SSL_set_fd failed");*/
  5792. goto done;
  5793. }
  5794. #ifdef WOLFSSL_ASYNC_CRYPT
  5795. err = 0; /* Reset error */
  5796. #endif
  5797. do {
  5798. #ifdef WOLFSSL_ASYNC_CRYPT
  5799. if (err == WC_PENDING_E) {
  5800. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5801. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5802. }
  5803. #endif
  5804. ret = wolfSSL_connect(ssl);
  5805. err = wolfSSL_get_error(ssl, 0);
  5806. } while (err == WC_PENDING_E);
  5807. if (ret != WOLFSSL_SUCCESS) {
  5808. char buff[WOLFSSL_MAX_ERROR_SZ];
  5809. fprintf(stderr, "error = %d, %s\n", err,
  5810. wolfSSL_ERR_error_string(err, buff));
  5811. /*err_sys("SSL_connect failed");*/
  5812. goto done;
  5813. }
  5814. /* Build first session */
  5815. if (cb != NULL)
  5816. cb(ctx, ssl);
  5817. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5818. /*err_sys("SSL_write failed");*/
  5819. goto done;
  5820. }
  5821. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5822. if (input > 0) {
  5823. reply[input] = '\0';
  5824. fprintf(stderr, "Server response: %s\n", reply);
  5825. }
  5826. ((func_args*)args)->return_code = TEST_SUCCESS;
  5827. done:
  5828. wolfSSL_free(ssl);
  5829. if (!sharedCtx)
  5830. wolfSSL_CTX_free(ctx);
  5831. CloseSocket(sockfd);
  5832. #ifdef WOLFSSL_TIRTOS
  5833. fdCloseSession(Task_self());
  5834. #endif
  5835. return;
  5836. }
  5837. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5838. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5839. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5840. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5841. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5842. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5843. #define HAVE_ALPN_PROTOS_SUPPORT
  5844. #endif
  5845. /* Generic TLS client / server with callbacks for API unit tests
  5846. * Used by SNI / ALPN / crypto callback helper functions */
  5847. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5848. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5849. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5850. #define ENABLE_TLS_CALLBACK_TEST
  5851. #endif
  5852. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5853. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5854. /* TLS server for API unit testing - generic */
  5855. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5856. {
  5857. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5858. WOLFSSL_CTX* ctx = NULL;
  5859. WOLFSSL* ssl = NULL;
  5860. SOCKET_T sfd = 0;
  5861. SOCKET_T cfd = 0;
  5862. word16 port;
  5863. char msg[] = "I hear you fa shizzle!";
  5864. int len = (int) XSTRLEN(msg);
  5865. char input[1024];
  5866. int idx;
  5867. int ret, err = 0;
  5868. ((func_args*)args)->return_code = TEST_FAIL;
  5869. #if defined(USE_WINDOWS_API)
  5870. port = ((func_args*)args)->signal->port;
  5871. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5872. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5873. /* Let tcp_listen assign port */
  5874. port = 0;
  5875. #else
  5876. /* Use default port */
  5877. port = wolfSSLPort;
  5878. #endif
  5879. #ifdef WOLFSSL_DTLS
  5880. if (callbacks->method == wolfDTLS_server_method
  5881. #ifdef WOLFSSL_STATIC_MEMORY
  5882. || callbacks->method_ex == wolfDTLS_server_method_ex
  5883. #endif
  5884. #ifndef NO_OLD_TLS
  5885. || callbacks->method == wolfDTLSv1_server_method
  5886. #ifdef WOLFSSL_STATIC_MEMORY
  5887. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  5888. #endif
  5889. #endif
  5890. #ifndef WOLFSSL_NO_TLS12
  5891. || callbacks->method == wolfDTLSv1_2_server_method
  5892. #ifdef WOLFSSL_STATIC_MEMORY
  5893. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  5894. #endif
  5895. #endif
  5896. #ifdef WOLFSSL_DTLS13
  5897. || callbacks->method == wolfDTLSv1_3_server_method
  5898. #ifdef WOLFSSL_STATIC_MEMORY
  5899. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  5900. #endif
  5901. #endif
  5902. ) {
  5903. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5904. }
  5905. else
  5906. #endif
  5907. {
  5908. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5909. }
  5910. #ifdef WOLFSSL_STATIC_MEMORY
  5911. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5912. callbacks->memSz > 0) {
  5913. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5914. callbacks->mem, callbacks->memSz, 0, 1);
  5915. if (ret != WOLFSSL_SUCCESS) {
  5916. fprintf(stderr, "CTX static new failed %d\n", ret);
  5917. goto cleanup;
  5918. }
  5919. }
  5920. #else
  5921. ctx = wolfSSL_CTX_new(callbacks->method());
  5922. if (ctx == NULL) {
  5923. fprintf(stderr, "CTX new failed\n");
  5924. goto cleanup;
  5925. }
  5926. #endif
  5927. /* set defaults */
  5928. if (callbacks->caPemFile == NULL)
  5929. callbacks->caPemFile = cliCertFile;
  5930. if (callbacks->certPemFile == NULL)
  5931. callbacks->certPemFile = svrCertFile;
  5932. if (callbacks->keyPemFile == NULL)
  5933. callbacks->keyPemFile = svrKeyFile;
  5934. #ifdef WOLFSSL_TIRTOS
  5935. fdOpenSession(Task_self());
  5936. #endif
  5937. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5938. wolfSSL_CTX_set_verify(ctx,
  5939. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5940. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5941. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5942. #endif
  5943. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5944. if (callbacks->method == wolfDTLSv1_2_server_method) {
  5945. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  5946. goto cleanup;
  5947. }
  5948. #endif
  5949. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  5950. WOLFSSL_SUCCESS) {
  5951. goto cleanup;
  5952. }
  5953. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5954. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5955. goto cleanup;
  5956. }
  5957. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5958. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5959. goto cleanup;
  5960. }
  5961. #ifdef HAVE_CRL
  5962. if (callbacks->crlPemFile != NULL) {
  5963. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5964. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5965. goto cleanup;
  5966. }
  5967. }
  5968. #endif
  5969. if (callbacks->ctx_ready)
  5970. callbacks->ctx_ready(ctx);
  5971. ssl = wolfSSL_new(ctx);
  5972. if (ssl == NULL) {
  5973. fprintf(stderr, "SSL new failed\n");
  5974. goto cleanup;
  5975. }
  5976. if (wolfSSL_dtls(ssl)) {
  5977. SOCKADDR_IN_T cliAddr;
  5978. socklen_t cliLen;
  5979. cliLen = sizeof(cliAddr);
  5980. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5981. (struct sockaddr*)&cliAddr, &cliLen);
  5982. if (idx <= 0) {
  5983. goto cleanup;
  5984. }
  5985. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5986. }
  5987. else {
  5988. CloseSocket(sfd);
  5989. }
  5990. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  5991. goto cleanup;
  5992. }
  5993. if (callbacks->loadToSSL) {
  5994. wolfSSL_SetDevId(ssl, callbacks->devId);
  5995. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5996. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5997. goto cleanup;
  5998. }
  5999. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6000. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6001. goto cleanup;
  6002. }
  6003. }
  6004. #ifdef NO_PSK
  6005. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6006. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6007. #elif !defined(NO_DH)
  6008. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6009. #endif
  6010. #endif
  6011. if (callbacks->ssl_ready)
  6012. callbacks->ssl_ready(ssl);
  6013. #ifdef WOLFSSL_ASYNC_CRYPT
  6014. err = 0; /* Reset error */
  6015. #endif
  6016. do {
  6017. #ifdef WOLFSSL_ASYNC_CRYPT
  6018. if (err == WC_PENDING_E) {
  6019. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6020. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6021. }
  6022. #endif
  6023. ret = wolfSSL_accept(ssl);
  6024. err = wolfSSL_get_error(ssl, ret);
  6025. } while (err == WC_PENDING_E);
  6026. if (ret != WOLFSSL_SUCCESS) {
  6027. char buff[WOLFSSL_MAX_ERROR_SZ];
  6028. fprintf(stderr, "accept error = %d, %s\n", err,
  6029. wolfSSL_ERR_error_string(err, buff));
  6030. /*err_sys("SSL_accept failed");*/
  6031. }
  6032. else {
  6033. #ifdef WOLFSSL_ASYNC_CRYPT
  6034. err = 0; /* Reset error */
  6035. #endif
  6036. do {
  6037. #ifdef WOLFSSL_ASYNC_CRYPT
  6038. if (err == WC_PENDING_E) {
  6039. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6040. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6041. }
  6042. #endif
  6043. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6044. err = wolfSSL_get_error(ssl, idx);
  6045. } while (err == WC_PENDING_E);
  6046. if (idx > 0) {
  6047. input[idx] = 0;
  6048. fprintf(stderr, "Client message: %s\n", input);
  6049. }
  6050. #ifdef WOLFSSL_ASYNC_CRYPT
  6051. err = 0; /* Reset error */
  6052. #endif
  6053. do {
  6054. #ifdef WOLFSSL_ASYNC_CRYPT
  6055. if (err == WC_PENDING_E) {
  6056. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6057. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6058. }
  6059. #endif
  6060. ret = wolfSSL_write(ssl, msg, len);
  6061. err = wolfSSL_get_error(ssl, ret);
  6062. } while (err == WC_PENDING_E);
  6063. if (len != ret) {
  6064. goto cleanup;
  6065. }
  6066. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6067. defined(WOLFSSL_DTLS)
  6068. if (wolfSSL_dtls(ssl)) {
  6069. byte* import;
  6070. word32 sz;
  6071. wolfSSL_dtls_export(ssl, NULL, &sz);
  6072. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6073. if (import == NULL) {
  6074. goto cleanup;
  6075. }
  6076. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6077. if (idx < 0) {
  6078. goto cleanup;
  6079. }
  6080. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6081. goto cleanup;
  6082. }
  6083. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6084. }
  6085. #endif
  6086. #ifdef WOLFSSL_TIRTOS
  6087. Task_yield();
  6088. #endif
  6089. ((func_args*)args)->return_code = TEST_SUCCESS;
  6090. }
  6091. if (callbacks->on_result)
  6092. callbacks->on_result(ssl);
  6093. wolfSSL_shutdown(ssl);
  6094. cleanup:
  6095. wolfSSL_free(ssl);
  6096. wolfSSL_CTX_free(ctx);
  6097. CloseSocket(cfd);
  6098. #ifdef WOLFSSL_TIRTOS
  6099. fdCloseSession(Task_self());
  6100. #endif
  6101. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6102. && defined(HAVE_THREAD_LS)
  6103. wc_ecc_fp_free(); /* free per thread cache */
  6104. #endif
  6105. #ifndef WOLFSSL_TIRTOS
  6106. return 0;
  6107. #endif
  6108. }
  6109. /* TLS Client for API unit testing - generic */
  6110. static void run_wolfssl_client(void* args)
  6111. {
  6112. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6113. WOLFSSL_CTX* ctx = NULL;
  6114. WOLFSSL* ssl = NULL;
  6115. SOCKET_T sfd = 0;
  6116. char msg[] = "hello wolfssl server!";
  6117. int len = (int) XSTRLEN(msg);
  6118. char input[1024];
  6119. int ret, err = 0;
  6120. ((func_args*)args)->return_code = TEST_FAIL;
  6121. /* set defaults */
  6122. if (callbacks->caPemFile == NULL)
  6123. callbacks->caPemFile = caCertFile;
  6124. if (callbacks->certPemFile == NULL)
  6125. callbacks->certPemFile = cliCertFile;
  6126. if (callbacks->keyPemFile == NULL)
  6127. callbacks->keyPemFile = cliKeyFile;
  6128. #ifdef WOLFSSL_STATIC_MEMORY
  6129. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6130. callbacks->memSz > 0) {
  6131. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6132. callbacks->mem, callbacks->memSz, 0, 1);
  6133. if (ret != WOLFSSL_SUCCESS) {
  6134. fprintf(stderr, "CTX static new failed %d\n", ret);
  6135. goto cleanup;
  6136. }
  6137. }
  6138. #else
  6139. if (ctx == NULL) {
  6140. ctx = wolfSSL_CTX_new(callbacks->method());
  6141. }
  6142. if (ctx == NULL) {
  6143. fprintf(stderr, "CTX new failed\n");
  6144. goto cleanup;
  6145. }
  6146. #endif
  6147. #ifdef WOLFSSL_TIRTOS
  6148. fdOpenSession(Task_self());
  6149. #endif
  6150. if (!callbacks->loadToSSL) {
  6151. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6152. }
  6153. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6154. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6155. #endif
  6156. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6157. WOLFSSL_SUCCESS) {
  6158. goto cleanup;
  6159. }
  6160. if (!callbacks->loadToSSL) {
  6161. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6162. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6163. goto cleanup;
  6164. }
  6165. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6166. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6167. goto cleanup;
  6168. }
  6169. }
  6170. #ifdef HAVE_CRL
  6171. if (callbacks->crlPemFile != NULL) {
  6172. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6173. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6174. goto cleanup;
  6175. }
  6176. }
  6177. #endif
  6178. if (callbacks->ctx_ready)
  6179. callbacks->ctx_ready(ctx);
  6180. ssl = wolfSSL_new(ctx);
  6181. if (wolfSSL_dtls(ssl)) {
  6182. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6183. 1, 0, ssl);
  6184. }
  6185. else {
  6186. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6187. 0, 0, ssl);
  6188. }
  6189. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6190. goto cleanup;
  6191. }
  6192. if (callbacks->loadToSSL) {
  6193. wolfSSL_SetDevId(ssl, callbacks->devId);
  6194. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6195. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6196. goto cleanup;
  6197. }
  6198. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6199. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6200. goto cleanup;
  6201. }
  6202. }
  6203. if (callbacks->ssl_ready)
  6204. callbacks->ssl_ready(ssl);
  6205. #ifdef WOLFSSL_ASYNC_CRYPT
  6206. err = 0; /* Reset error */
  6207. #endif
  6208. do {
  6209. #ifdef WOLFSSL_ASYNC_CRYPT
  6210. if (err == WC_PENDING_E) {
  6211. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6212. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6213. }
  6214. #endif
  6215. ret = wolfSSL_connect(ssl);
  6216. err = wolfSSL_get_error(ssl, ret);
  6217. } while (err == WC_PENDING_E);
  6218. if (ret != WOLFSSL_SUCCESS) {
  6219. char buff[WOLFSSL_MAX_ERROR_SZ];
  6220. fprintf(stderr, "error = %d, %s\n", err,
  6221. wolfSSL_ERR_error_string(err, buff));
  6222. /*err_sys("SSL_connect failed");*/
  6223. }
  6224. else {
  6225. #ifdef WOLFSSL_ASYNC_CRYPT
  6226. err = 0; /* Reset error */
  6227. #endif
  6228. do {
  6229. #ifdef WOLFSSL_ASYNC_CRYPT
  6230. if (err == WC_PENDING_E) {
  6231. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6232. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6233. }
  6234. #endif
  6235. ret = wolfSSL_write(ssl, msg, len);
  6236. err = wolfSSL_get_error(ssl, ret);
  6237. } while (err == WC_PENDING_E);
  6238. if (len != ret)
  6239. goto cleanup;
  6240. #ifdef WOLFSSL_ASYNC_CRYPT
  6241. err = 0; /* Reset error */
  6242. #endif
  6243. do {
  6244. #ifdef WOLFSSL_ASYNC_CRYPT
  6245. if (err == WC_PENDING_E) {
  6246. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6247. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6248. }
  6249. #endif
  6250. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6251. err = wolfSSL_get_error(ssl, ret);
  6252. } while (err == WC_PENDING_E);
  6253. if (ret > 0) {
  6254. input[ret] = '\0'; /* null term */
  6255. fprintf(stderr, "Server response: %s\n", input);
  6256. }
  6257. ((func_args*)args)->return_code = TEST_SUCCESS;
  6258. }
  6259. if (callbacks->on_result)
  6260. callbacks->on_result(ssl);
  6261. cleanup:
  6262. wolfSSL_free(ssl);
  6263. wolfSSL_CTX_free(ctx);
  6264. CloseSocket(sfd);
  6265. #ifdef WOLFSSL_TIRTOS
  6266. fdCloseSession(Task_self());
  6267. #endif
  6268. }
  6269. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6270. static int test_wolfSSL_read_write(void)
  6271. {
  6272. /* The unit testing for read and write shall happen simultaneously, since
  6273. * one can't do anything with one without the other. (Except for a failure
  6274. * test case.) This function will call all the others that will set up,
  6275. * execute, and report their test findings.
  6276. *
  6277. * Set up the success case first. This function will become the template
  6278. * for the other tests. This should eventually be renamed
  6279. *
  6280. * The success case isn't interesting, how can this fail?
  6281. * - Do not give the client context a CA certificate. The connect should
  6282. * fail. Do not need server for this?
  6283. * - Using NULL for the ssl object on server. Do not need client for this.
  6284. * - Using NULL for the ssl object on client. Do not need server for this.
  6285. * - Good ssl objects for client and server. Client write() without server
  6286. * read().
  6287. * - Good ssl objects for client and server. Server write() without client
  6288. * read().
  6289. * - Forgetting the password callback?
  6290. */
  6291. tcp_ready ready;
  6292. func_args client_args;
  6293. func_args server_args;
  6294. THREAD_TYPE serverThread;
  6295. EXPECT_DECLS;
  6296. XMEMSET(&client_args, 0, sizeof(func_args));
  6297. XMEMSET(&server_args, 0, sizeof(func_args));
  6298. #ifdef WOLFSSL_TIRTOS
  6299. fdOpenSession(Task_self());
  6300. #endif
  6301. StartTCP();
  6302. InitTcpReady(&ready);
  6303. #if defined(USE_WINDOWS_API)
  6304. /* use RNG to get random port if using windows */
  6305. ready.port = GetRandomPort();
  6306. #endif
  6307. server_args.signal = &ready;
  6308. client_args.signal = &ready;
  6309. start_thread(test_server_nofail, &server_args, &serverThread);
  6310. wait_tcp_ready(&server_args);
  6311. test_client_nofail(&client_args, NULL);
  6312. join_thread(serverThread);
  6313. ExpectTrue(client_args.return_code);
  6314. ExpectTrue(server_args.return_code);
  6315. FreeTcpReady(&ready);
  6316. #ifdef WOLFSSL_TIRTOS
  6317. fdOpenSession(Task_self());
  6318. #endif
  6319. return EXPECT_RESULT();
  6320. }
  6321. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6322. {
  6323. int res = TEST_SKIPPED;
  6324. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6325. !defined(WOLFSSL_NO_TLS12)
  6326. /* The unit test for session resumption by re-using WOLFSSL object.
  6327. * WOLFSSL object is not cleared after first session. It re-use the obeject
  6328. * for second connection.
  6329. */
  6330. tcp_ready ready;
  6331. func_args client_args;
  6332. func_args server_args;
  6333. THREAD_TYPE serverThread;
  6334. callback_functions client_cbf;
  6335. callback_functions server_cbf;
  6336. EXPECT_DECLS;
  6337. XMEMSET(&client_args, 0, sizeof(func_args));
  6338. XMEMSET(&server_args, 0, sizeof(func_args));
  6339. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6340. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6341. #ifdef WOLFSSL_TIRTOS
  6342. fdOpenSession(Task_self());
  6343. #endif
  6344. StartTCP();
  6345. InitTcpReady(&ready);
  6346. #if defined(USE_WINDOWS_API)
  6347. /* use RNG to get random port if using windows */
  6348. ready.port = GetRandomPort();
  6349. #endif
  6350. client_cbf.method = wolfTLSv1_2_client_method;
  6351. server_cbf.method = wolfTLSv1_2_server_method;
  6352. client_args.callbacks = &client_cbf;
  6353. server_args.callbacks = &server_cbf;
  6354. server_args.signal = &ready;
  6355. client_args.signal = &ready;
  6356. /* the var is used for loop number */
  6357. server_args.argc = 2;
  6358. start_thread(test_server_loop, &server_args, &serverThread);
  6359. wait_tcp_ready(&server_args);
  6360. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6361. join_thread(serverThread);
  6362. ExpectTrue(client_args.return_code);
  6363. ExpectTrue(server_args.return_code);
  6364. FreeTcpReady(&ready);
  6365. #ifdef WOLFSSL_TIRTOS
  6366. fdOpenSession(Task_self());
  6367. #endif
  6368. res = EXPECT_RESULT();
  6369. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6370. return res;
  6371. }
  6372. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6373. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6374. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6375. WOLFSSL_CTX* ctx)
  6376. {
  6377. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6378. myVerifyAction = VERIFY_USE_PREVERFIY;
  6379. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6380. return TEST_SUCCESS;
  6381. }
  6382. #endif
  6383. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6384. {
  6385. int res = TEST_SKIPPED;
  6386. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6387. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6388. EXPECT_DECLS;
  6389. test_ssl_cbf client_cbf;
  6390. test_ssl_cbf server_cbf;
  6391. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6392. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6393. #ifdef WOLFSSL_TLS13
  6394. client_cbf.method = wolfTLSv1_3_client_method;
  6395. #endif /* WOLFSSL_TLS13 */
  6396. client_cbf.ctx_ready =
  6397. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6398. /* test case 1 verify depth is equal to peer chain */
  6399. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6400. &server_cbf, NULL), TEST_SUCCESS);
  6401. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6402. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6403. res = EXPECT_RESULT();
  6404. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6405. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6406. return res;
  6407. }
  6408. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6409. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6410. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6411. WOLFSSL_CTX* ctx)
  6412. {
  6413. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6414. myVerifyAction = VERIFY_USE_PREVERFIY;
  6415. wolfSSL_CTX_set_verify_depth(ctx, 1);
  6416. return TEST_SUCCESS;
  6417. }
  6418. #endif
  6419. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6420. {
  6421. int res = TEST_SKIPPED;
  6422. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6423. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6424. EXPECT_DECLS;
  6425. test_ssl_cbf client_cbf;
  6426. test_ssl_cbf server_cbf;
  6427. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6428. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6429. #ifdef WOLFSSL_TLS13
  6430. client_cbf.method = wolfTLSv1_3_client_method;
  6431. #endif /* WOLFSSL_TLS13 */
  6432. client_cbf.ctx_ready =
  6433. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6434. /* test case 2
  6435. * verify depth is zero, number of peer's chain is 2.
  6436. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6437. * callback.
  6438. */
  6439. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6440. &server_cbf, NULL), TEST_SUCCESS);
  6441. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6442. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6443. res = EXPECT_RESULT();
  6444. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6445. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6446. return res;
  6447. }
  6448. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6449. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6450. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6451. WOLFSSL_CTX* ctx)
  6452. {
  6453. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6454. myVerifyAction = VERIFY_USE_PREVERFIY;
  6455. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6456. return TEST_SUCCESS;
  6457. }
  6458. #endif
  6459. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6460. {
  6461. int res = TEST_SKIPPED;
  6462. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6463. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6464. EXPECT_DECLS;
  6465. test_ssl_cbf client_cbf;
  6466. test_ssl_cbf server_cbf;
  6467. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6468. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6469. #ifdef WOLFSSL_TLS13
  6470. client_cbf.method = wolfTLSv1_3_client_method;
  6471. #endif /* WOLFSSL_TLS13 */
  6472. client_cbf.ctx_ready =
  6473. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6474. /* test case 3
  6475. * verify depth is zero, number of peer's chain is 2
  6476. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6477. * therefore, handshake becomes failure.
  6478. */
  6479. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6480. &server_cbf, NULL), TEST_SUCCESS);
  6481. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6482. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6483. res = EXPECT_RESULT();
  6484. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6485. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6486. return res;
  6487. }
  6488. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6489. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6490. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6491. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6492. {
  6493. EXPECT_DECLS;
  6494. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6495. return EXPECT_RESULT();
  6496. }
  6497. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6498. {
  6499. EXPECT_DECLS;
  6500. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6501. return EXPECT_RESULT();
  6502. }
  6503. #endif
  6504. static int test_wolfSSL_CTX_set_cipher_list(void)
  6505. {
  6506. int res = TEST_SKIPPED;
  6507. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6508. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6509. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6510. EXPECT_DECLS;
  6511. WOLFSSL_CTX* ctxClient = NULL;
  6512. WOLFSSL* sslClient = NULL;
  6513. test_ssl_cbf client_cbf;
  6514. test_ssl_cbf server_cbf;
  6515. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6516. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6517. server_cbf.method = wolfTLSv1_2_server_method;
  6518. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6519. client_cbf.method = wolfTLSv1_2_client_method;
  6520. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6521. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6522. &server_cbf, NULL), TEST_SUCCESS);
  6523. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6524. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6525. /* check with cipher string that has '+' */
  6526. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6527. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6528. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6529. /* check for the existance of an ECDHE ECDSA cipher suite */
  6530. if (EXPECT_SUCCESS()) {
  6531. int i = 0;
  6532. int found = 0;
  6533. const char* suite;
  6534. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6535. WOLFSSL_CIPHER* current;
  6536. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6537. do {
  6538. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6539. if (current) {
  6540. suite = wolfSSL_CIPHER_get_name(current);
  6541. if (suite && XSTRSTR(suite, "ECDSA")) {
  6542. found = 1;
  6543. break;
  6544. }
  6545. }
  6546. } while (current);
  6547. ExpectIntEQ(found, 1);
  6548. }
  6549. wolfSSL_free(sslClient);
  6550. wolfSSL_CTX_free(ctxClient);
  6551. res = EXPECT_RESULT();
  6552. #endif
  6553. return res;
  6554. }
  6555. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6556. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6557. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  6558. WOLFSSL* ssl)
  6559. {
  6560. EXPECT_DECLS;
  6561. size_t msg_len;
  6562. (void)ctx;
  6563. /* get_finished test */
  6564. /* 1. get own sent message */
  6565. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6566. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6567. ExpectIntGE(msg_len, 0);
  6568. /* 2. get peer message */
  6569. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6570. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6571. ExpectIntGE(msg_len, 0);
  6572. return EXPECT_RESULT();
  6573. }
  6574. #endif
  6575. static int test_wolfSSL_get_finished(void)
  6576. {
  6577. int res = TEST_SKIPPED;
  6578. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6579. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6580. EXPECT_DECLS;
  6581. test_ssl_cbf client_cbf;
  6582. test_ssl_cbf server_cbf;
  6583. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6584. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6585. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6586. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  6587. TEST_SUCCESS);
  6588. /* test received msg vs sent msg */
  6589. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6590. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6591. res = EXPECT_RESULT();
  6592. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  6593. return res;
  6594. }
  6595. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6596. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6597. !defined(NO_SESSION_CACHE)
  6598. /* Sessions to restore/store */
  6599. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6600. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6601. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6602. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6603. {
  6604. /* Don't store sessions. Lookup is still enabled. */
  6605. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6606. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6607. #ifdef OPENSSL_EXTRA
  6608. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6609. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6610. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6611. #endif
  6612. /* Require both peers to provide certs */
  6613. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6614. }
  6615. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6616. {
  6617. WOLFSSL_SESSION** sess;
  6618. if (wolfSSL_is_server(ssl))
  6619. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6620. else
  6621. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6622. if (*sess == NULL) {
  6623. #ifdef NO_SESSION_CACHE_REF
  6624. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6625. #else
  6626. /* Test for backwards compatibility */
  6627. if (wolfSSL_is_server(ssl)) {
  6628. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6629. }
  6630. else {
  6631. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6632. }
  6633. #endif
  6634. /* Now save the session in the internal store to make it available
  6635. * for lookup. For TLS 1.3, we can't save the session without
  6636. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6637. * session from cache. */
  6638. if (wolfSSL_is_server(ssl)
  6639. #ifndef WOLFSSL_TICKET_HAVE_ID
  6640. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6641. #endif
  6642. )
  6643. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6644. *sess), WOLFSSL_SUCCESS);
  6645. }
  6646. else {
  6647. /* If we have a session retrieved then remaining connections should be
  6648. * resuming on that session */
  6649. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6650. }
  6651. /* Save CTX to be able to decrypt tickets */
  6652. if (wolfSSL_is_server(ssl) &&
  6653. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6654. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6655. = wolfSSL_get_SSL_CTX(ssl));
  6656. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6657. WOLFSSL_SUCCESS);
  6658. }
  6659. #ifdef SESSION_CERTS
  6660. #ifndef WOLFSSL_TICKET_HAVE_ID
  6661. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6662. wolfSSL_session_reused(ssl))
  6663. #endif
  6664. {
  6665. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6666. * for all connections. TLS 1.3 only has tickets so if we don't
  6667. * include the session id in the ticket then the certificates
  6668. * will not be available on resumption. */
  6669. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6670. AssertNotNull(peer);
  6671. wolfSSL_X509_free(peer);
  6672. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6673. #ifdef OPENSSL_EXTRA
  6674. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6675. #endif
  6676. }
  6677. #endif /* SESSION_CERTS */
  6678. }
  6679. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6680. {
  6681. /* Set the session to reuse for the client */
  6682. AssertIntEQ(wolfSSL_set_session(ssl,
  6683. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6684. }
  6685. #endif
  6686. static int test_wolfSSL_CTX_add_session(void)
  6687. {
  6688. int res = TEST_SKIPPED;
  6689. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6690. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6691. !defined(NO_SESSION_CACHE)
  6692. EXPECT_DECLS;
  6693. tcp_ready ready;
  6694. func_args client_args;
  6695. func_args server_args;
  6696. THREAD_TYPE serverThread;
  6697. callback_functions client_cb;
  6698. callback_functions server_cb;
  6699. method_provider methods[][2] = {
  6700. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6701. !defined(NO_DES3))
  6702. /* Without AES there are almost no ciphersuites available. This leads
  6703. * to no ciphersuites being available and an error. */
  6704. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6705. #endif
  6706. #ifndef WOLFSSL_NO_TLS12
  6707. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6708. #endif
  6709. /* Needs the default ticket callback since it is tied to the
  6710. * connection context and this makes it easy to carry over the ticket
  6711. * crypto context between connections */
  6712. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6713. defined(HAVE_SESSION_TICKET)
  6714. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6715. #endif
  6716. };
  6717. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6718. size_t i, j;
  6719. for (i = 0; i < methodsLen; i++) {
  6720. /* First run creates a connection while the second+ run will attempt
  6721. * to resume the connection. The trick is that the internal cache
  6722. * is turned off. wolfSSL_CTX_add_session should put the session in
  6723. * the cache anyway. */
  6724. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6725. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6726. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6727. #ifdef NO_SESSION_CACHE_REF
  6728. for (j = 0; j < 4; j++) {
  6729. #else
  6730. /* The session may be overwritten in this case. Do only one resumption
  6731. * to stop this test from failing intermittently. */
  6732. for (j = 0; j < 2; j++) {
  6733. #endif
  6734. #ifdef WOLFSSL_TIRTOS
  6735. fdOpenSession(Task_self());
  6736. #endif
  6737. StartTCP();
  6738. InitTcpReady(&ready);
  6739. XMEMSET(&client_args, 0, sizeof(func_args));
  6740. XMEMSET(&server_args, 0, sizeof(func_args));
  6741. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6742. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6743. client_cb.method = methods[i][0];
  6744. server_cb.method = methods[i][1];
  6745. server_args.signal = &ready;
  6746. server_args.callbacks = &server_cb;
  6747. client_args.signal = &ready;
  6748. client_args.callbacks = &client_cb;
  6749. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6750. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6751. server_cb.isSharedCtx = 1;
  6752. }
  6753. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6754. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6755. if (j != 0)
  6756. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6757. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6758. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6759. server_cb.ticNoInit = 1; /* Use default builtin */
  6760. start_thread(test_server_nofail, &server_args, &serverThread);
  6761. wait_tcp_ready(&server_args);
  6762. test_client_nofail(&client_args, NULL);
  6763. join_thread(serverThread);
  6764. ExpectTrue(client_args.return_code);
  6765. ExpectTrue(server_args.return_code);
  6766. FreeTcpReady(&ready);
  6767. if (EXPECT_FAIL())
  6768. break;
  6769. }
  6770. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6771. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6772. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6773. if (EXPECT_FAIL())
  6774. break;
  6775. }
  6776. res = EXPECT_RESULT();
  6777. #endif
  6778. return res;
  6779. }
  6780. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6781. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  6782. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  6783. defined(HAVE_SESSION_TICKET) && \
  6784. !defined(TITAN_SESSION_CACHE) && \
  6785. !defined(HUGE_SESSION_CACHE) && \
  6786. !defined(BIG_SESSION_CACHE) && \
  6787. !defined(MEDIUM_SESSION_CACHE)
  6788. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  6789. /* Sessions to restore/store */
  6790. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  6791. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  6792. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  6793. static int twcase_new_session_called = 0;
  6794. static int twcase_remove_session_called = 0;
  6795. static int twcase_get_session_called = 0;
  6796. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  6797. #define SESSION_CACHE_SIZE 33
  6798. typedef struct {
  6799. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  6800. WOLFSSL_SESSION* value;
  6801. } hashTable_entry;
  6802. typedef struct {
  6803. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  6804. size_t capacity; /* size of entries */
  6805. size_t length; /* number of items in the hash table */
  6806. wolfSSL_Mutex htLock; /* lock */
  6807. }hashTable;
  6808. static hashTable server_sessionCache;
  6809. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  6810. {
  6811. int i;
  6812. (void)ssl;
  6813. /*
  6814. * This example uses a hash table.
  6815. * Steps you should take for a non-demo code:
  6816. * - acquire a lock for the file named according to the session id
  6817. * - open the file
  6818. * - encrypt and write the SSL_SESSION object to the file
  6819. * - release the lock
  6820. *
  6821. * Return:
  6822. * 0: The callback does not wish to hold a reference of the sess
  6823. * 1: The callback wants to hold a reference of the sess. The callback is
  6824. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  6825. */
  6826. if (sess == NULL)
  6827. return 0;
  6828. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6829. return 0;
  6830. }
  6831. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6832. if (server_sessionCache.entries[i].value == NULL) {
  6833. if (sess->haveAltSessionID == 1)
  6834. server_sessionCache.entries[i].key = sess->altSessionID;
  6835. else
  6836. server_sessionCache.entries[i].key = sess->sessionID;
  6837. server_sessionCache.entries[i].value = sess;
  6838. server_sessionCache.length++;
  6839. break;
  6840. }
  6841. }
  6842. ++twcase_new_session_called;
  6843. wc_UnLockMutex(&server_sessionCache.htLock);
  6844. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  6845. twcase_new_session_called);
  6846. return 1;
  6847. }
  6848. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  6849. {
  6850. int i;
  6851. (void)ctx;
  6852. (void)sess;
  6853. if (sess == NULL)
  6854. return;
  6855. /*
  6856. * This example uses a hash table.
  6857. * Steps you should take for a non-demo code:
  6858. * - acquire a lock for the file named according to the session id
  6859. * - remove the file
  6860. * - release the lock
  6861. */
  6862. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6863. return;
  6864. }
  6865. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6866. if (server_sessionCache.entries[i].key != NULL &&
  6867. XMEMCMP(server_sessionCache.entries[i].key,
  6868. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6869. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6870. server_sessionCache.entries[i].value = NULL;
  6871. server_sessionCache.entries[i].key = NULL;
  6872. server_sessionCache.length--;
  6873. break;
  6874. }
  6875. }
  6876. ++twcase_remove_session_called;
  6877. wc_UnLockMutex(&server_sessionCache.htLock);
  6878. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  6879. twcase_remove_session_called);
  6880. }
  6881. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  6882. const unsigned char *id, int len, int *ref)
  6883. {
  6884. int i;
  6885. (void)ssl;
  6886. (void)id;
  6887. (void)len;
  6888. /*
  6889. * This example uses a hash table.
  6890. * Steps you should take for a non-demo code:
  6891. * - acquire a lock for the file named according to the session id in the
  6892. * 2nd arg
  6893. * - read and decrypt contents of file and create a new SSL_SESSION
  6894. * - object release the lock
  6895. * - return the new session object
  6896. */
  6897. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  6898. ++twcase_get_session_called);
  6899. /* This callback want to retain a copy of the object. If we want wolfSSL to
  6900. * be responsible for the pointer then set to 0. */
  6901. *ref = 1;
  6902. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6903. if (server_sessionCache.entries[i].key != NULL &&
  6904. XMEMCMP(server_sessionCache.entries[i].key, id,
  6905. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6906. return server_sessionCache.entries[i].value;
  6907. }
  6908. }
  6909. return NULL;
  6910. }
  6911. static int twcase_get_sessionCb_cleanup(void)
  6912. {
  6913. int i;
  6914. int cnt = 0;
  6915. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  6916. * for freeing sessions */
  6917. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6918. if (server_sessionCache.entries[i].value != NULL) {
  6919. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6920. cnt++;
  6921. }
  6922. }
  6923. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  6924. cnt);
  6925. return TEST_SUCCESS;
  6926. }
  6927. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  6928. {
  6929. EXPECT_DECLS;
  6930. /* off - Disable internal cache */
  6931. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6932. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6933. #ifdef OPENSSL_EXTRA
  6934. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6935. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6936. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6937. #endif
  6938. /* off - Donot setup external cache */
  6939. /* Require both peers to provide certs */
  6940. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6941. return EXPECT_RESULT();
  6942. }
  6943. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  6944. {
  6945. EXPECT_DECLS;
  6946. /* on - internal cache is on by default*/
  6947. /* off - Donot setup external cache */
  6948. /* Require both peers to provide certs */
  6949. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6950. return EXPECT_RESULT();
  6951. }
  6952. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  6953. {
  6954. EXPECT_DECLS;
  6955. /* off - Disable internal cache */
  6956. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6957. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6958. #ifdef OPENSSL_EXTRA
  6959. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6960. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6961. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6962. #endif
  6963. /* on - Enable external cache */
  6964. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6965. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6966. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6967. /* Require both peers to provide certs */
  6968. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6969. return EXPECT_RESULT();
  6970. }
  6971. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  6972. {
  6973. EXPECT_DECLS;
  6974. /* on - internal cache is on by default */
  6975. /* on - Enable external cache */
  6976. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6977. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6978. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6979. /* Require both peers to provide certs */
  6980. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6981. return EXPECT_RESULT();
  6982. }
  6983. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  6984. {
  6985. EXPECT_DECLS;
  6986. /* on - internal cache is on by default */
  6987. /* on - Enable external cache */
  6988. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6989. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6990. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6991. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  6992. /* Require both peers to provide certs */
  6993. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6994. return EXPECT_RESULT();
  6995. }
  6996. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  6997. {
  6998. EXPECT_DECLS;
  6999. WOLFSSL_SESSION** sess;
  7000. if (wolfSSL_is_server(ssl))
  7001. sess = &twcase_server_first_session_ptr;
  7002. else
  7003. return TEST_SUCCESS;
  7004. if (*sess == NULL) {
  7005. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7006. /* Now save the session in the internal store to make it available
  7007. * for lookup. For TLS 1.3, we can't save the session without
  7008. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7009. * session from cache. */
  7010. if (wolfSSL_is_server(ssl)
  7011. #ifndef WOLFSSL_TICKET_HAVE_ID
  7012. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7013. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7014. #endif
  7015. ) {
  7016. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7017. *sess), WOLFSSL_SUCCESS);
  7018. }
  7019. }
  7020. /* Save CTX to be able to decrypt tickets */
  7021. if (twcase_server_current_ctx_ptr == NULL) {
  7022. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7023. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7024. WOLFSSL_SUCCESS);
  7025. }
  7026. #ifdef SESSION_CERTS
  7027. #ifndef WOLFSSL_TICKET_HAVE_ID
  7028. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7029. wolfSSL_session_reused(ssl))
  7030. #endif
  7031. {
  7032. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7033. * for all connections. TLS 1.3 only has tickets so if we don't
  7034. * include the session id in the ticket then the certificates
  7035. * will not be available on resumption. */
  7036. WOLFSSL_X509* peer = NULL;
  7037. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7038. wolfSSL_X509_free(peer);
  7039. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7040. }
  7041. #endif
  7042. return EXPECT_RESULT();
  7043. }
  7044. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7045. {
  7046. EXPECT_DECLS;
  7047. WOLFSSL_SESSION** sess;
  7048. sess = &twcase_client_first_session_ptr;
  7049. if (*sess == NULL) {
  7050. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7051. }
  7052. else {
  7053. /* If we have a session retrieved then remaining connections should be
  7054. * resuming on that session */
  7055. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7056. }
  7057. #ifdef SESSION_CERTS
  7058. #ifndef WOLFSSL_TICKET_HAVE_ID
  7059. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7060. wolfSSL_session_reused(ssl))
  7061. #endif
  7062. {
  7063. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7064. ExpectNotNull(peer);
  7065. wolfSSL_X509_free(peer);
  7066. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7067. #ifdef OPENSSL_EXTRA
  7068. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7069. #endif
  7070. }
  7071. #endif
  7072. return EXPECT_RESULT();
  7073. }
  7074. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7075. {
  7076. EXPECT_DECLS;
  7077. /* Set the session to reuse for the client */
  7078. ExpectNotNull(ssl);
  7079. ExpectNotNull(twcase_client_first_session_ptr);
  7080. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7081. WOLFSSL_SUCCESS);
  7082. return EXPECT_RESULT();
  7083. }
  7084. #endif
  7085. static int test_wolfSSL_CTX_add_session_ext(void)
  7086. {
  7087. int res = TEST_SKIPPED;
  7088. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7089. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7090. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7091. defined(HAVE_SESSION_TICKET) && \
  7092. !defined(TITAN_SESSION_CACHE) && \
  7093. !defined(HUGE_SESSION_CACHE) && \
  7094. !defined(BIG_SESSION_CACHE) && \
  7095. !defined(MEDIUM_SESSION_CACHE)
  7096. EXPECT_DECLS;
  7097. /* Test the default 33 sessions */
  7098. struct test_params {
  7099. method_provider client_meth;
  7100. method_provider server_meth;
  7101. const char* tls_version;
  7102. } params[] = {
  7103. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7104. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7105. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  7106. #ifdef WOLFSSL_DTLS13
  7107. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  7108. #endif
  7109. #endif
  7110. #ifndef WOLFSSL_NO_TLS12
  7111. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  7112. #ifdef WOLFSSL_DTLS
  7113. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  7114. #endif
  7115. #endif
  7116. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7117. !defined(NO_DES3))
  7118. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  7119. #ifdef WOLFSSL_DTLS
  7120. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  7121. #endif
  7122. #endif
  7123. };
  7124. const int paramsLen = sizeof(params)/sizeof(*params);
  7125. int i, j;
  7126. /* Clear cache before starting */
  7127. wolfSSL_CTX_flush_sessions(NULL, -1);
  7128. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7129. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7130. return BAD_MUTEX_E;
  7131. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7132. for (i = 0; i < paramsLen; i++) {
  7133. fprintf(stderr, "\tBegin %s\n", params[i].tls_version);
  7134. for (j = 0; j < 5; j++) {
  7135. int tls13 = XSTRSTR(params[i].tls_version, "TLSv1_3") != NULL;
  7136. int dtls = XSTRSTR(params[i].tls_version, "DTLS") != NULL;
  7137. test_ssl_cbf client_cb;
  7138. test_ssl_cbf server_cb;
  7139. (void)dtls;
  7140. /* Test five cache configurations */
  7141. twcase_client_first_session_ptr = NULL;
  7142. twcase_server_first_session_ptr = NULL;
  7143. twcase_server_current_ctx_ptr = NULL;
  7144. twcase_new_session_called = 0;
  7145. twcase_remove_session_called = 0;
  7146. twcase_get_session_called = 0;
  7147. /* connection 1 - first connection */
  7148. fprintf(stderr, "\tconnect: %s: j=%d, methodsLen=%d\n",
  7149. params[i].tls_version, j, paramsLen);
  7150. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7151. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7152. client_cb.method = params[i].client_meth;
  7153. server_cb.method = params[i].server_meth;
  7154. if (dtls)
  7155. client_cb.doUdp = server_cb.doUdp = 1;
  7156. /* Setup internal and external cache */
  7157. switch (j) {
  7158. case 0:
  7159. /* SSL_OP_NO_TICKET stateful ticket case */
  7160. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7161. break;
  7162. case 1:
  7163. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7164. break;
  7165. case 2:
  7166. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7167. break;
  7168. case 3:
  7169. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7170. break;
  7171. case 4:
  7172. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7173. break;
  7174. }
  7175. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7176. /* Add session to internal cache and save SSL session for testing */
  7177. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7178. /* Save client SSL session for testing */
  7179. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7180. server_cb.ticNoInit = 1; /* Use default builtin */
  7181. /* Don't free/release ctx */
  7182. server_cb.ctx = twcase_server_current_ctx_ptr;
  7183. server_cb.isSharedCtx = 1;
  7184. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7185. &server_cb, NULL), TEST_SUCCESS);
  7186. ExpectIntEQ(twcase_get_session_called, 0);
  7187. if (EXPECT_FAIL()) {
  7188. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7189. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7190. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7191. break;
  7192. }
  7193. switch (j) {
  7194. case 0:
  7195. case 1:
  7196. case 2:
  7197. /* cache cannot be searched with out a connection */
  7198. /* Add a new session */
  7199. ExpectIntEQ(twcase_new_session_called, 1);
  7200. /* In twcase_server_sess_ctx_pre_shutdown
  7201. * wolfSSL_CTX_add_session which evicts the existing session
  7202. * in cache and adds it back in */
  7203. ExpectIntLE(twcase_remove_session_called, 1);
  7204. break;
  7205. case 3:
  7206. case 4:
  7207. /* no external cache */
  7208. ExpectIntEQ(twcase_new_session_called, 0);
  7209. ExpectIntEQ(twcase_remove_session_called, 0);
  7210. break;
  7211. }
  7212. /* connection 2 - session resume */
  7213. fprintf(stderr, "\tresume: %s: j=%d, methodsLen=%d\n",
  7214. params[i].tls_version, j, paramsLen);
  7215. twcase_new_session_called = 0;
  7216. twcase_remove_session_called = 0;
  7217. twcase_get_session_called = 0;
  7218. server_cb.on_result = 0;
  7219. client_cb.on_result = 0;
  7220. server_cb.ticNoInit = 1; /* Use default builtin */
  7221. server_cb.ctx = twcase_server_current_ctx_ptr;
  7222. /* try session resumption */
  7223. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7224. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7225. &server_cb, NULL), TEST_SUCCESS);
  7226. /* Clear cache before checking */
  7227. wolfSSL_CTX_flush_sessions(NULL, -1);
  7228. switch (j) {
  7229. case 0:
  7230. if (tls13) {
  7231. /* (D)TLSv1.3 stateful case */
  7232. /* cache hit */
  7233. /* DTLS accesses cache once for stateless parsing and
  7234. * once for stateful parsing */
  7235. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7236. /* (D)TLSv1.3 creates a new ticket,
  7237. * updates both internal and external cache */
  7238. ExpectIntEQ(twcase_new_session_called, 1);
  7239. ExpectIntEQ(twcase_remove_session_called, 1);
  7240. }
  7241. else {
  7242. /* non (D)TLSv1.3 case, no update */
  7243. /* DTLS accesses cache once for stateless parsing and
  7244. * once for stateful parsing */
  7245. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7246. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7247. #else
  7248. ExpectIntEQ(twcase_get_session_called, 1);
  7249. #endif
  7250. ExpectIntEQ(twcase_new_session_called, 0);
  7251. /* Called on session added in
  7252. * twcase_server_sess_ctx_pre_shutdown */
  7253. ExpectIntEQ(twcase_remove_session_called, 1);
  7254. }
  7255. break;
  7256. case 1:
  7257. if (tls13) {
  7258. /* (D)TLSv1.3 case */
  7259. /* cache hit */
  7260. ExpectIntEQ(twcase_get_session_called, 1);
  7261. /* (D)TLSv1.3 creates a new ticket,
  7262. * updates both internal and external cache */
  7263. ExpectIntEQ(twcase_new_session_called, 1);
  7264. /* Called on session added in
  7265. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7266. ExpectIntEQ(twcase_remove_session_called, 1);
  7267. }
  7268. else {
  7269. /* non (D)TLSv1.3 case */
  7270. /* cache hit */
  7271. /* DTLS accesses cache once for stateless parsing and
  7272. * once for stateful parsing */
  7273. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7274. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7275. #else
  7276. ExpectIntEQ(twcase_get_session_called, 1);
  7277. #endif
  7278. ExpectIntEQ(twcase_new_session_called, 0);
  7279. /* Called on session added in
  7280. * twcase_server_sess_ctx_pre_shutdown */
  7281. ExpectIntEQ(twcase_remove_session_called, 1);
  7282. }
  7283. break;
  7284. case 2:
  7285. if (tls13) {
  7286. /* (D)TLSv1.3 case */
  7287. /* cache hit */
  7288. ExpectIntEQ(twcase_get_session_called, 1);
  7289. /* (D)TLSv1.3 creates a new ticket,
  7290. * updates both internal and external cache */
  7291. ExpectIntEQ(twcase_new_session_called, 1);
  7292. /* Called on session added in
  7293. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7294. ExpectIntEQ(twcase_remove_session_called, 1);
  7295. }
  7296. else {
  7297. /* non (D)TLSv1.3 case */
  7298. /* cache hit */
  7299. /* DTLS accesses cache once for stateless parsing and
  7300. * once for stateful parsing */
  7301. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7302. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7303. #else
  7304. ExpectIntEQ(twcase_get_session_called, 1);
  7305. #endif
  7306. ExpectIntEQ(twcase_new_session_called, 0);
  7307. /* Called on session added in
  7308. * twcase_server_sess_ctx_pre_shutdown */
  7309. ExpectIntEQ(twcase_remove_session_called, 1);
  7310. }
  7311. break;
  7312. case 3:
  7313. case 4:
  7314. /* no external cache */
  7315. ExpectIntEQ(twcase_get_session_called, 0);
  7316. ExpectIntEQ(twcase_new_session_called, 0);
  7317. ExpectIntEQ(twcase_remove_session_called, 0);
  7318. break;
  7319. }
  7320. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7321. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7322. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7323. if (EXPECT_FAIL())
  7324. break;
  7325. }
  7326. twcase_get_sessionCb_cleanup();
  7327. XMEMSET(&server_sessionCache.entries, 0,
  7328. sizeof(server_sessionCache.entries));
  7329. fprintf(stderr, "\tEnd %s\n", params[i].tls_version);
  7330. if (EXPECT_FAIL())
  7331. break;
  7332. }
  7333. wc_FreeMutex(&server_sessionCache.htLock);
  7334. res = EXPECT_RESULT();
  7335. #endif
  7336. return res;
  7337. }
  7338. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7339. /* canned export of a session using older version 3 */
  7340. static unsigned char version_3[] = {
  7341. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7342. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7343. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7344. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7345. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7346. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7347. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7348. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7349. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7352. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7354. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7355. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7356. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7357. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7358. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7359. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7369. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7373. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7374. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7375. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7376. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7377. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7378. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7379. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7380. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7381. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7382. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7383. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7384. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7385. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7386. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7387. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7388. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7389. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7390. 0x2E, 0x31, 0xED, 0x4F
  7391. };
  7392. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7393. static int test_wolfSSL_dtls_export(void)
  7394. {
  7395. int res = TEST_SKIPPED;
  7396. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7397. EXPECT_DECLS;
  7398. tcp_ready ready;
  7399. func_args client_args;
  7400. func_args server_args;
  7401. THREAD_TYPE serverThread;
  7402. callback_functions server_cbf;
  7403. callback_functions client_cbf;
  7404. #ifdef WOLFSSL_TIRTOS
  7405. fdOpenSession(Task_self());
  7406. #endif
  7407. InitTcpReady(&ready);
  7408. #if defined(USE_WINDOWS_API)
  7409. /* use RNG to get random port if using windows */
  7410. ready.port = GetRandomPort();
  7411. #endif
  7412. /* set using dtls */
  7413. XMEMSET(&client_args, 0, sizeof(func_args));
  7414. XMEMSET(&server_args, 0, sizeof(func_args));
  7415. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7416. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7417. server_cbf.method = wolfDTLSv1_2_server_method;
  7418. client_cbf.method = wolfDTLSv1_2_client_method;
  7419. server_args.callbacks = &server_cbf;
  7420. client_args.callbacks = &client_cbf;
  7421. server_args.signal = &ready;
  7422. client_args.signal = &ready;
  7423. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7424. wait_tcp_ready(&server_args);
  7425. run_wolfssl_client(&client_args);
  7426. join_thread(serverThread);
  7427. ExpectTrue(client_args.return_code);
  7428. ExpectTrue(server_args.return_code);
  7429. FreeTcpReady(&ready);
  7430. #ifdef WOLFSSL_TIRTOS
  7431. fdOpenSession(Task_self());
  7432. #endif
  7433. if (EXPECT_SUCCESS()) {
  7434. SOCKET_T sockfd = 0;
  7435. WOLFSSL_CTX* ctx = NULL;
  7436. WOLFSSL* ssl = NULL;
  7437. char msg[64] = "hello wolfssl!";
  7438. char reply[1024];
  7439. int msgSz = (int)XSTRLEN(msg);
  7440. byte *session, *window;
  7441. unsigned int sessionSz = 0;
  7442. unsigned int windowSz = 0;
  7443. #ifndef TEST_IPV6
  7444. struct sockaddr_in peerAddr;
  7445. #else
  7446. struct sockaddr_in6 peerAddr;
  7447. #endif /* TEST_IPV6 */
  7448. int i;
  7449. /* Set ctx to DTLS 1.2 */
  7450. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7451. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7452. /* test importing version 3 */
  7453. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7454. /* test importing bad length and bad version */
  7455. version_3[2] += 1;
  7456. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7457. version_3[2] -= 1; version_3[1] = 0XA0;
  7458. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7459. wolfSSL_free(ssl);
  7460. wolfSSL_CTX_free(ctx);
  7461. /* check storing client state after connection and storing window only */
  7462. #ifdef WOLFSSL_TIRTOS
  7463. fdOpenSession(Task_self());
  7464. #endif
  7465. InitTcpReady(&ready);
  7466. #if defined(USE_WINDOWS_API)
  7467. /* use RNG to get random port if using windows */
  7468. ready.port = GetRandomPort();
  7469. #endif
  7470. /* set using dtls */
  7471. XMEMSET(&server_args, 0, sizeof(func_args));
  7472. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7473. server_cbf.method = wolfDTLSv1_2_server_method;
  7474. server_cbf.doUdp = 1;
  7475. server_args.callbacks = &server_cbf;
  7476. server_args.argc = 3; /* set loop_count to 3 */
  7477. server_args.signal = &ready;
  7478. start_thread(test_server_nofail, &server_args, &serverThread);
  7479. wait_tcp_ready(&server_args);
  7480. /* create and connect with client */
  7481. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  7482. ExpectIntEQ(WOLFSSL_SUCCESS,
  7483. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  7484. ExpectIntEQ(WOLFSSL_SUCCESS,
  7485. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  7486. ExpectIntEQ(WOLFSSL_SUCCESS,
  7487. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  7488. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  7489. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7490. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7491. /* store server information connected too */
  7492. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  7493. #ifndef TEST_IPV6
  7494. peerAddr.sin_family = AF_INET;
  7495. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  7496. peerAddr.sin_port = XHTONS(server_args.signal->port);
  7497. #else
  7498. peerAddr.sin6_family = AF_INET6;
  7499. ExpectIntEQ(
  7500. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  7501. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  7502. #endif
  7503. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7504. WOLFSSL_SUCCESS);
  7505. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  7506. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  7507. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7508. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  7509. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7510. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7511. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  7512. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7513. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7514. wolfSSL_free(ssl);
  7515. for (i = 1; i < server_args.argc; i++) {
  7516. /* restore state */
  7517. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7518. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  7519. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  7520. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7521. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7522. WOLFSSL_SUCCESS);
  7523. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7524. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7525. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7526. wolfSSL_free(ssl);
  7527. }
  7528. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7529. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7530. wolfSSL_CTX_free(ctx);
  7531. fprintf(stderr, "done and waiting for server\n");
  7532. join_thread(serverThread);
  7533. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  7534. FreeTcpReady(&ready);
  7535. #ifdef WOLFSSL_TIRTOS
  7536. fdOpenSession(Task_self());
  7537. #endif
  7538. }
  7539. res = EXPECT_RESULT();
  7540. #endif
  7541. return res;
  7542. }
  7543. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7544. #ifdef WOLFSSL_TLS13
  7545. static const byte canned_client_tls13_session[] = {
  7546. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  7547. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7548. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  7549. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7550. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7551. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7552. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7553. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  7554. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7555. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7556. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7557. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  7558. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7559. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7560. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7561. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7562. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7563. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7564. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7565. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7566. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7567. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7568. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7569. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7570. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7571. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7572. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7573. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7574. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7575. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  7576. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  7577. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  7578. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7579. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7580. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7581. 0x00, 0x03
  7582. };
  7583. static const byte canned_server_tls13_session[] = {
  7584. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  7585. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7586. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  7587. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7588. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7589. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7590. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7591. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  7592. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7593. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7594. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7595. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  7596. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7597. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7598. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7599. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7600. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7601. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7602. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7603. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7604. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7605. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7606. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7607. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7608. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7609. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7610. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7611. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7612. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7613. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  7614. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  7615. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  7616. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7617. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7618. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7619. 0x00, 0x04
  7620. };
  7621. #endif /* WOLFSSL_TLS13 */
  7622. static const byte canned_client_session[] = {
  7623. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7624. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7625. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  7626. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7627. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7628. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7629. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7630. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  7631. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7632. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7633. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7634. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  7635. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7636. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7637. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7638. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7639. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7640. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7641. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7642. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7643. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7644. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7645. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7646. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7647. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7648. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7649. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7650. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7651. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7652. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7654. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7655. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7656. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7657. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7658. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7659. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7660. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7661. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7662. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7663. 0x00, 0x03
  7664. };
  7665. static const byte canned_server_session[] = {
  7666. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7667. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7668. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7669. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7670. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7671. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7672. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7673. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  7674. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7675. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7676. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7677. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  7678. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7679. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7680. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7681. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7682. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7683. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7684. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7685. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7686. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7687. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7688. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7689. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7690. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7691. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7692. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7693. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7694. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7695. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7698. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7699. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7700. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7701. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7702. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7703. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7704. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7705. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7706. 0x00, 0x04
  7707. };
  7708. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  7709. {
  7710. SOCKET_T sockfd = 0;
  7711. SOCKET_T clientfd = 0;
  7712. word16 port;
  7713. callback_functions* cbf;
  7714. WOLFSSL_CTX* ctx = 0;
  7715. WOLFSSL* ssl = 0;
  7716. char msg[] = "I hear you fa shizzle!";
  7717. char input[1024];
  7718. int idx;
  7719. #ifdef WOLFSSL_TIRTOS
  7720. fdOpenSession(Task_self());
  7721. #endif
  7722. ((func_args*)args)->return_code = TEST_FAIL;
  7723. cbf = ((func_args*)args)->callbacks;
  7724. #if defined(USE_WINDOWS_API)
  7725. port = ((func_args*)args)->signal->port;
  7726. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7727. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7728. /* Let tcp_listen assign port */
  7729. port = 0;
  7730. #else
  7731. /* Use default port */
  7732. port = wolfSSLPort;
  7733. #endif
  7734. /* do it here to detect failure */
  7735. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7736. CloseSocket(sockfd);
  7737. {
  7738. WOLFSSL_METHOD* method = NULL;
  7739. if (cbf != NULL && cbf->method != NULL) {
  7740. method = cbf->method();
  7741. }
  7742. else {
  7743. method = wolfTLSv1_2_server_method();
  7744. }
  7745. ctx = wolfSSL_CTX_new(method);
  7746. }
  7747. if (ctx == NULL) {
  7748. goto done;
  7749. }
  7750. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7751. /* call ctx setup callback */
  7752. if (cbf != NULL && cbf->ctx_ready != NULL) {
  7753. cbf->ctx_ready(ctx);
  7754. }
  7755. ssl = wolfSSL_new(ctx);
  7756. if (ssl == NULL) {
  7757. goto done;
  7758. }
  7759. wolfSSL_set_fd(ssl, clientfd);
  7760. /* call ssl setup callback */
  7761. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7762. cbf->ssl_ready(ssl);
  7763. }
  7764. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7765. if (idx > 0) {
  7766. input[idx] = '\0';
  7767. fprintf(stderr, "Client message export/import: %s\n", input);
  7768. }
  7769. else {
  7770. fprintf(stderr, "ret = %d error = %d\n", idx,
  7771. wolfSSL_get_error(ssl, idx));
  7772. goto done;
  7773. }
  7774. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  7775. /*err_sys("SSL_write failed");*/
  7776. #ifdef WOLFSSL_TIRTOS
  7777. return;
  7778. #else
  7779. return 0;
  7780. #endif
  7781. }
  7782. #ifdef WOLFSSL_TIRTOS
  7783. Task_yield();
  7784. #endif
  7785. ((func_args*)args)->return_code = TEST_SUCCESS;
  7786. done:
  7787. wolfSSL_shutdown(ssl);
  7788. wolfSSL_free(ssl);
  7789. wolfSSL_CTX_free(ctx);
  7790. CloseSocket(clientfd);
  7791. #ifdef WOLFSSL_TIRTOS
  7792. fdCloseSession(Task_self());
  7793. #endif
  7794. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7795. && defined(HAVE_THREAD_LS)
  7796. wc_ecc_fp_free(); /* free per thread cache */
  7797. #endif
  7798. #if defined(HAVE_SESSION_TICKET) && \
  7799. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  7800. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  7801. OpenSSLTicketCleanup();
  7802. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  7803. TicketCleanup();
  7804. #endif
  7805. #endif
  7806. #ifndef WOLFSSL_TIRTOS
  7807. return 0;
  7808. #endif
  7809. }
  7810. static void load_tls12_canned_server(WOLFSSL* ssl)
  7811. {
  7812. int clientfd = wolfSSL_get_fd(ssl);
  7813. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  7814. sizeof(canned_server_session)), sizeof(canned_server_session));
  7815. wolfSSL_set_fd(ssl, clientfd);
  7816. }
  7817. #ifdef WOLFSSL_TLS13
  7818. static void load_tls13_canned_server(WOLFSSL* ssl)
  7819. {
  7820. int clientfd = wolfSSL_get_fd(ssl);
  7821. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  7822. sizeof(canned_server_tls13_session)),
  7823. sizeof(canned_server_tls13_session));
  7824. wolfSSL_set_fd(ssl, clientfd);
  7825. }
  7826. #endif
  7827. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  7828. static int test_wolfSSL_tls_export_run(int v)
  7829. {
  7830. EXPECT_DECLS;
  7831. SOCKET_T sockfd = 0;
  7832. WOLFSSL_CTX* ctx = 0;
  7833. WOLFSSL* ssl = 0;
  7834. char msg[64] = "hello wolfssl!";
  7835. char reply[1024];
  7836. word32 replySz;
  7837. int msgSz = (int)XSTRLEN(msg);
  7838. const byte* clientSession = NULL;
  7839. int clientSessionSz = 0;
  7840. tcp_ready ready;
  7841. func_args server_args;
  7842. THREAD_TYPE serverThread;
  7843. callback_functions server_cbf;
  7844. #ifdef WOLFSSL_TIRTOS
  7845. fdOpenSession(Task_self());
  7846. #endif
  7847. InitTcpReady(&ready);
  7848. #if defined(USE_WINDOWS_API)
  7849. /* use RNG to get random port if using windows */
  7850. ready.port = GetRandomPort();
  7851. #endif
  7852. XMEMSET(&server_args, 0, sizeof(func_args));
  7853. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7854. switch (v) {
  7855. case WOLFSSL_TLSV1_2:
  7856. server_cbf.method = wolfTLSv1_2_server_method;
  7857. server_cbf.ssl_ready = load_tls12_canned_server;
  7858. /* setup the client side */
  7859. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7860. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7861. clientSession = canned_client_session;
  7862. clientSessionSz = sizeof(canned_client_session);
  7863. break;
  7864. #ifdef WOLFSSL_TLS13
  7865. case WOLFSSL_TLSV1_3:
  7866. server_cbf.method = wolfTLSv1_3_server_method;
  7867. server_cbf.ssl_ready = load_tls13_canned_server;
  7868. /* setup the client side */
  7869. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7870. clientSession = canned_client_tls13_session;
  7871. clientSessionSz = sizeof(canned_client_tls13_session);
  7872. break;
  7873. #endif
  7874. }
  7875. server_args.callbacks = &server_cbf;
  7876. server_args.signal = &ready;
  7877. start_thread(tls_export_server, &server_args, &serverThread);
  7878. wait_tcp_ready(&server_args);
  7879. #ifdef WOLFSSL_TIRTOS
  7880. fdOpenSession(Task_self());
  7881. #endif
  7882. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7883. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  7884. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  7885. clientSessionSz);
  7886. replySz = sizeof(reply);
  7887. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  7888. #if !defined(NO_PSK) && defined(HAVE_ANON)
  7889. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  7890. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  7891. #endif
  7892. wolfSSL_set_fd(ssl, sockfd);
  7893. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7894. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  7895. wolfSSL_free(ssl);
  7896. wolfSSL_CTX_free(ctx);
  7897. CloseSocket(sockfd);
  7898. #ifdef WOLFSSL_TIRTOS
  7899. fdCloseSession(Task_self());
  7900. #endif
  7901. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7902. && defined(HAVE_THREAD_LS)
  7903. wc_ecc_fp_free(); /* free per thread cache */
  7904. #endif
  7905. join_thread(serverThread);
  7906. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  7907. FreeTcpReady(&ready);
  7908. #ifdef WOLFSSL_TIRTOS
  7909. fdOpenSession(Task_self());
  7910. #endif
  7911. return EXPECT_RESULT();
  7912. }
  7913. #endif
  7914. static int test_wolfSSL_tls_export(void)
  7915. {
  7916. int res = TEST_SKIPPED;
  7917. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7918. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  7919. #ifdef WOLFSSL_TLS13
  7920. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  7921. #endif
  7922. res = TEST_RES_CHECK(1);
  7923. #endif
  7924. return res;
  7925. }
  7926. /*----------------------------------------------------------------------------*
  7927. | TLS extensions tests
  7928. *----------------------------------------------------------------------------*/
  7929. #ifdef ENABLE_TLS_CALLBACK_TEST
  7930. /* Connection test runner - generic */
  7931. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  7932. callback_functions* server_callbacks)
  7933. {
  7934. tcp_ready ready;
  7935. func_args client_args;
  7936. func_args server_args;
  7937. THREAD_TYPE serverThread;
  7938. XMEMSET(&client_args, 0, sizeof(func_args));
  7939. XMEMSET(&server_args, 0, sizeof(func_args));
  7940. StartTCP();
  7941. client_args.callbacks = client_callbacks;
  7942. server_args.callbacks = server_callbacks;
  7943. #ifdef WOLFSSL_TIRTOS
  7944. fdOpenSession(Task_self());
  7945. #endif
  7946. /* RUN Server side */
  7947. InitTcpReady(&ready);
  7948. #if defined(USE_WINDOWS_API)
  7949. /* use RNG to get random port if using windows */
  7950. ready.port = GetRandomPort();
  7951. #endif
  7952. server_args.signal = &ready;
  7953. client_args.signal = &ready;
  7954. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7955. wait_tcp_ready(&server_args);
  7956. /* RUN Client side */
  7957. run_wolfssl_client(&client_args);
  7958. join_thread(serverThread);
  7959. FreeTcpReady(&ready);
  7960. #ifdef WOLFSSL_TIRTOS
  7961. fdCloseSession(Task_self());
  7962. #endif
  7963. client_callbacks->return_code = client_args.return_code;
  7964. server_callbacks->return_code = server_args.return_code;
  7965. }
  7966. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7967. #ifdef HAVE_SNI
  7968. static int test_wolfSSL_UseSNI_params(void)
  7969. {
  7970. int res = TEST_SKIPPED;
  7971. #if !defined(NO_WOLFSSL_CLIENT)
  7972. EXPECT_DECLS;
  7973. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7974. WOLFSSL *ssl = wolfSSL_new(ctx);
  7975. ExpectNotNull(ctx);
  7976. ExpectNotNull(ssl);
  7977. /* invalid [ctx|ssl] */
  7978. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  7979. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  7980. /* invalid type */
  7981. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  7982. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  7983. /* invalid data */
  7984. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  7985. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  7986. /* success case */
  7987. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  7988. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  7989. wolfSSL_free(ssl);
  7990. wolfSSL_CTX_free(ctx);
  7991. res = EXPECT_RESULT();
  7992. #endif /* !NO_WOLFSSL_CLIENT */
  7993. return res;
  7994. }
  7995. /* BEGIN of connection tests callbacks */
  7996. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7997. {
  7998. AssertIntEQ(WOLFSSL_SUCCESS,
  7999. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8000. }
  8001. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8002. {
  8003. AssertIntEQ(WOLFSSL_SUCCESS,
  8004. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8005. }
  8006. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8007. {
  8008. AssertIntEQ(WOLFSSL_SUCCESS,
  8009. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8010. }
  8011. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8012. {
  8013. use_SNI_at_ssl(ssl);
  8014. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8015. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8016. }
  8017. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8018. {
  8019. use_SNI_at_ssl(ssl);
  8020. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8021. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8022. }
  8023. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8024. {
  8025. use_SNI_at_ctx(ctx);
  8026. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8027. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8028. }
  8029. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8030. {
  8031. use_SNI_at_ssl(ssl);
  8032. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8033. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8034. }
  8035. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8036. {
  8037. use_SNI_at_ctx(ctx);
  8038. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8039. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8040. }
  8041. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8042. {
  8043. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8044. }
  8045. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8046. {
  8047. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8048. }
  8049. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8050. {
  8051. byte type = WOLFSSL_SNI_HOST_NAME;
  8052. void* request = (void*) &type; /* to be overwritten */
  8053. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8054. AssertNotNull(request);
  8055. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8056. AssertNull(request);
  8057. }
  8058. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8059. {
  8060. byte type = WOLFSSL_SNI_HOST_NAME;
  8061. void* request = NULL;
  8062. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8063. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8064. AssertNotNull(request);
  8065. AssertStrEQ("www.wolfssl.com", (char*)request);
  8066. }
  8067. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8068. {
  8069. byte type = WOLFSSL_SNI_HOST_NAME;
  8070. void* request = NULL;
  8071. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8072. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8073. AssertNotNull(request);
  8074. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8075. }
  8076. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8077. {
  8078. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8079. }
  8080. /* END of connection tests callbacks */
  8081. static int test_wolfSSL_UseSNI_connection(void)
  8082. {
  8083. int res = TEST_SKIPPED;
  8084. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8085. callback_functions client_cb;
  8086. callback_functions server_cb;
  8087. size_t i;
  8088. struct {
  8089. method_provider client_meth;
  8090. method_provider server_meth;
  8091. } methods[] = {
  8092. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8093. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  8094. #endif
  8095. #ifndef WOLFSSL_NO_TLS12
  8096. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  8097. #endif
  8098. #ifdef WOLFSSL_TLS13
  8099. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  8100. #endif
  8101. };
  8102. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  8103. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8104. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8105. client_cb.method = methods[i].client_meth;
  8106. server_cb.method = methods[i].server_meth;
  8107. client_cb.devId = testDevId;
  8108. server_cb.devId = testDevId;
  8109. /* success case at ctx */
  8110. printf("success case at ctx\n");
  8111. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8112. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8113. test_wolfSSL_client_server(&client_cb, &server_cb);
  8114. /* success case at ssl */
  8115. printf("success case at ssl\n");
  8116. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8117. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8118. test_wolfSSL_client_server(&client_cb, &server_cb);
  8119. /* default mismatch behavior */
  8120. printf("default mismatch behavior\n");
  8121. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8122. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8123. test_wolfSSL_client_server(&client_cb, &server_cb);
  8124. /* continue on mismatch */
  8125. printf("continue on mismatch\n");
  8126. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8127. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8128. test_wolfSSL_client_server(&client_cb, &server_cb);
  8129. /* fake answer on mismatch */
  8130. printf("fake answer on mismatch\n");
  8131. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8132. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8133. test_wolfSSL_client_server(&client_cb, &server_cb);
  8134. /* sni abort - success */
  8135. printf("sni abort - success\n");
  8136. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8137. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8138. test_wolfSSL_client_server(&client_cb, &server_cb);
  8139. /* sni abort - abort when absent (ctx) */
  8140. printf("sni abort - abort when absent (ctx)\n");
  8141. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8142. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8143. test_wolfSSL_client_server(&client_cb, &server_cb);
  8144. /* sni abort - abort when absent (ssl) */
  8145. printf("sni abort - abort when absent (ssl)\n");
  8146. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8147. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8148. test_wolfSSL_client_server(&client_cb, &server_cb);
  8149. /* sni abort - success when overwritten */
  8150. printf("sni abort - success when overwritten\n");
  8151. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8152. 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;
  8153. test_wolfSSL_client_server(&client_cb, &server_cb);
  8154. /* sni abort - success when allowing mismatches */
  8155. printf("sni abort - success when allowing mismatches\n");
  8156. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8157. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8158. test_wolfSSL_client_server(&client_cb, &server_cb);
  8159. }
  8160. res = TEST_RES_CHECK(1);
  8161. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8162. return res;
  8163. }
  8164. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8165. {
  8166. EXPECT_DECLS;
  8167. byte buff[] = { /* www.paypal.com */
  8168. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8169. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8170. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8171. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8172. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8173. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8174. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8175. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8176. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8177. };
  8178. byte buff2[] = { /* api.textmate.org */
  8179. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8180. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8181. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8182. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8183. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8184. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8185. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8186. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8187. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8188. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8189. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8190. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8191. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8192. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8193. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8194. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8195. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8196. };
  8197. byte buff3[] = { /* no sni extension */
  8198. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8199. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8200. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8201. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8202. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8203. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8204. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8205. };
  8206. byte buff4[] = { /* last extension has zero size */
  8207. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8208. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8209. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8210. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8211. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8212. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8213. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8214. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8215. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8216. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8217. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8218. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8219. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8220. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8221. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8222. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8223. 0x12, 0x00, 0x00
  8224. };
  8225. byte buff5[] = { /* SSL v2.0 client hello */
  8226. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8227. /* dummy bytes bellow, just to pass size check */
  8228. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8229. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8230. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8231. };
  8232. byte result[32] = {0};
  8233. word32 length = 32;
  8234. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8235. 0, result, &length));
  8236. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8237. 0, result, &length));
  8238. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8239. 1, result, &length));
  8240. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8241. 0, result, &length));
  8242. buff[0] = 0x16;
  8243. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8244. 0, result, &length));
  8245. buff[1] = 0x03;
  8246. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8247. sizeof(buff), 0, result, &length));
  8248. buff[2] = 0x03;
  8249. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8250. sizeof(buff), 0, result, &length));
  8251. buff[4] = 0x64;
  8252. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8253. 0, result, &length));
  8254. if (EXPECT_SUCCESS())
  8255. result[length] = 0;
  8256. ExpectStrEQ("www.paypal.com", (const char*) result);
  8257. length = 32;
  8258. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8259. 0, result, &length));
  8260. if (EXPECT_SUCCESS())
  8261. result[length] = 0;
  8262. ExpectStrEQ("api.textmate.org", (const char*) result);
  8263. /* SSL v2.0 tests */
  8264. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8265. sizeof(buff5), 0, result, &length));
  8266. buff5[2] = 0x02;
  8267. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8268. sizeof(buff5), 0, result, &length));
  8269. buff5[2] = 0x01; buff5[6] = 0x08;
  8270. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8271. sizeof(buff5), 0, result, &length));
  8272. buff5[6] = 0x09; buff5[8] = 0x01;
  8273. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8274. sizeof(buff5), 0, result, &length));
  8275. return EXPECT_RESULT();
  8276. }
  8277. #endif /* HAVE_SNI */
  8278. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8279. static int test_wolfSSL_UseTrustedCA(void)
  8280. {
  8281. int res = TEST_SKIPPED;
  8282. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8283. && !defined(NO_RSA)
  8284. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8285. EXPECT_DECLS;
  8286. WOLFSSL_CTX *ctx = NULL;
  8287. WOLFSSL *ssl = NULL;
  8288. byte id[20];
  8289. #ifndef NO_WOLFSSL_SERVER
  8290. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8291. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8292. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8293. #else
  8294. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8295. #endif
  8296. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8297. XMEMSET(id, 0, sizeof(id));
  8298. /* error cases */
  8299. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8300. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8301. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8302. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8303. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8304. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8305. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8306. #ifdef NO_SHA
  8307. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8308. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8309. #endif
  8310. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8311. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8312. /* success cases */
  8313. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8314. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8315. #ifndef NO_SHA
  8316. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8317. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8318. #endif
  8319. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8320. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8321. wolfSSL_free(ssl);
  8322. wolfSSL_CTX_free(ctx);
  8323. res = EXPECT_RESULT();
  8324. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8325. #endif /* HAVE_TRUSTED_CA */
  8326. return res;
  8327. }
  8328. static int test_wolfSSL_UseMaxFragment(void)
  8329. {
  8330. int res = TEST_SKIPPED;
  8331. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8332. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8333. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8334. EXPECT_DECLS;
  8335. #ifndef NO_WOLFSSL_SERVER
  8336. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8337. #else
  8338. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8339. #endif
  8340. WOLFSSL *ssl = NULL;
  8341. #ifdef OPENSSL_EXTRA
  8342. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8343. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8344. #else
  8345. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8346. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8347. #endif
  8348. #ifndef NO_WOLFSSL_SERVER
  8349. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8350. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8351. #endif
  8352. ExpectNotNull(ctx);
  8353. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8354. #ifdef OPENSSL_EXTRA
  8355. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8356. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8357. #else
  8358. UseMaxFragment = wolfSSL_UseMaxFragment;
  8359. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8360. #endif
  8361. /* error cases */
  8362. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8363. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8364. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8365. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8366. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8367. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8368. /* success case */
  8369. #ifdef OPENSSL_EXTRA
  8370. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8371. #else
  8372. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8373. #endif
  8374. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8375. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8376. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8377. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8378. #ifdef OPENSSL_EXTRA
  8379. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8380. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8381. #else
  8382. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8383. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8384. #endif
  8385. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8386. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8387. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8388. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8389. #ifdef OPENSSL_EXTRA
  8390. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8391. #else
  8392. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8393. #endif
  8394. wolfSSL_free(ssl);
  8395. wolfSSL_CTX_free(ctx);
  8396. res = EXPECT_RESULT();
  8397. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8398. #endif
  8399. return res;
  8400. }
  8401. static int test_wolfSSL_UseTruncatedHMAC(void)
  8402. {
  8403. int res = TEST_SKIPPED;
  8404. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8405. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8406. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8407. EXPECT_DECLS;
  8408. #ifndef NO_WOLFSSL_SERVER
  8409. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8410. #else
  8411. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8412. #endif
  8413. WOLFSSL *ssl = NULL;
  8414. ExpectNotNull(ctx);
  8415. #ifndef NO_WOLFSSL_SERVER
  8416. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8417. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8418. #endif
  8419. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8420. /* error cases */
  8421. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8422. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8423. /* success case */
  8424. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8425. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8426. wolfSSL_free(ssl);
  8427. wolfSSL_CTX_free(ctx);
  8428. res = EXPECT_RESULT();
  8429. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8430. #endif
  8431. return res;
  8432. }
  8433. static int test_wolfSSL_UseSupportedCurve(void)
  8434. {
  8435. int res = TEST_SKIPPED;
  8436. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8437. !defined(NO_TLS)
  8438. EXPECT_DECLS;
  8439. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8440. WOLFSSL *ssl = wolfSSL_new(ctx);
  8441. ExpectNotNull(ctx);
  8442. ExpectNotNull(ssl);
  8443. /* error cases */
  8444. ExpectIntNE(WOLFSSL_SUCCESS,
  8445. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8446. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8447. ExpectIntNE(WOLFSSL_SUCCESS,
  8448. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8449. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8450. /* success case */
  8451. ExpectIntEQ(WOLFSSL_SUCCESS,
  8452. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8453. ExpectIntEQ(WOLFSSL_SUCCESS,
  8454. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8455. wolfSSL_free(ssl);
  8456. wolfSSL_CTX_free(ctx);
  8457. res = EXPECT_RESULT();
  8458. #endif
  8459. return res;
  8460. }
  8461. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  8462. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8463. {
  8464. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  8465. }
  8466. static void use_ALPN_all(WOLFSSL* ssl)
  8467. {
  8468. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8469. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8470. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8471. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8472. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8473. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8474. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8475. }
  8476. static void use_ALPN_all_continue(WOLFSSL* ssl)
  8477. {
  8478. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8479. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8480. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8481. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8482. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8483. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8484. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8485. }
  8486. static void use_ALPN_one(WOLFSSL* ssl)
  8487. {
  8488. /* spdy/2 */
  8489. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8490. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8491. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8492. }
  8493. static void use_ALPN_unknown(WOLFSSL* ssl)
  8494. {
  8495. /* http/2.0 */
  8496. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8497. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8498. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8499. }
  8500. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  8501. {
  8502. /* http/2.0 */
  8503. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8504. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8505. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8506. }
  8507. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  8508. {
  8509. /* spdy/3 */
  8510. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8511. char *proto = NULL;
  8512. word16 protoSz = 0;
  8513. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8514. /* check value */
  8515. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  8516. if (proto) {
  8517. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  8518. }
  8519. }
  8520. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  8521. {
  8522. char *proto = NULL;
  8523. word16 protoSz = 0;
  8524. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  8525. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8526. /* check value */
  8527. AssertIntEQ(1, (0 == protoSz));
  8528. AssertIntEQ(1, (NULL == proto));
  8529. }
  8530. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  8531. {
  8532. /* http/1.1 */
  8533. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8534. char *proto;
  8535. word16 protoSz = 0;
  8536. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8537. /* check value */
  8538. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8539. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8540. }
  8541. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  8542. {
  8543. /* spdy/2 */
  8544. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8545. char *proto;
  8546. word16 protoSz = 0;
  8547. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8548. /* check value */
  8549. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8550. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8551. }
  8552. static void verify_ALPN_client_list(WOLFSSL* ssl)
  8553. {
  8554. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8555. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8556. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8557. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8558. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8559. char *clist = NULL;
  8560. word16 clistSz = 0;
  8561. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  8562. &clistSz));
  8563. /* check value */
  8564. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  8565. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  8566. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  8567. }
  8568. static int test_wolfSSL_UseALPN_connection(void)
  8569. {
  8570. int res = TEST_SKIPPED;
  8571. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8572. callback_functions client_cb;
  8573. callback_functions server_cb;
  8574. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8575. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8576. client_cb.method = wolfSSLv23_client_method;
  8577. server_cb.method = wolfSSLv23_server_method;
  8578. client_cb.devId = testDevId;
  8579. server_cb.devId = testDevId;
  8580. /* success case same list */
  8581. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8582. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  8583. test_wolfSSL_client_server(&client_cb, &server_cb);
  8584. /* success case only one for server */
  8585. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8586. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  8587. test_wolfSSL_client_server(&client_cb, &server_cb);
  8588. /* success case only one for client */
  8589. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  8590. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  8591. test_wolfSSL_client_server(&client_cb, &server_cb);
  8592. /* success case none for client */
  8593. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8594. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  8595. test_wolfSSL_client_server(&client_cb, &server_cb);
  8596. /* success case mismatch behavior but option 'continue' set */
  8597. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  8598. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  8599. test_wolfSSL_client_server(&client_cb, &server_cb);
  8600. /* success case read protocol send by client */
  8601. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8602. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  8603. test_wolfSSL_client_server(&client_cb, &server_cb);
  8604. /* mismatch behavior with same list
  8605. * the first and only this one must be taken */
  8606. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8607. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  8608. test_wolfSSL_client_server(&client_cb, &server_cb);
  8609. /* default mismatch behavior */
  8610. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8611. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  8612. test_wolfSSL_client_server(&client_cb, &server_cb);
  8613. res = TEST_RES_CHECK(1);
  8614. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8615. return res;
  8616. }
  8617. static int test_wolfSSL_UseALPN_params(void)
  8618. {
  8619. int res = TEST_SKIPPED;
  8620. #ifndef NO_WOLFSSL_CLIENT
  8621. EXPECT_DECLS;
  8622. /* "http/1.1" */
  8623. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8624. /* "spdy/1" */
  8625. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  8626. /* "spdy/2" */
  8627. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8628. /* "spdy/3" */
  8629. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8630. char buff[256];
  8631. word32 idx;
  8632. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8633. WOLFSSL *ssl = wolfSSL_new(ctx);
  8634. ExpectNotNull(ctx);
  8635. ExpectNotNull(ssl);
  8636. /* error cases */
  8637. ExpectIntNE(WOLFSSL_SUCCESS,
  8638. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  8639. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8640. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  8641. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8642. /* success case */
  8643. /* http1 only */
  8644. ExpectIntEQ(WOLFSSL_SUCCESS,
  8645. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  8646. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8647. /* http1, spdy1 */
  8648. XMEMCPY(buff, http1, sizeof(http1));
  8649. idx = sizeof(http1);
  8650. buff[idx++] = ',';
  8651. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8652. idx += sizeof(spdy1);
  8653. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8654. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8655. /* http1, spdy2, spdy1 */
  8656. XMEMCPY(buff, http1, sizeof(http1));
  8657. idx = sizeof(http1);
  8658. buff[idx++] = ',';
  8659. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8660. idx += sizeof(spdy2);
  8661. buff[idx++] = ',';
  8662. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8663. idx += sizeof(spdy1);
  8664. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8665. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8666. /* spdy3, http1, spdy2, spdy1 */
  8667. XMEMCPY(buff, spdy3, sizeof(spdy3));
  8668. idx = sizeof(spdy3);
  8669. buff[idx++] = ',';
  8670. XMEMCPY(buff+idx, http1, sizeof(http1));
  8671. idx += sizeof(http1);
  8672. buff[idx++] = ',';
  8673. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8674. idx += sizeof(spdy2);
  8675. buff[idx++] = ',';
  8676. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8677. idx += sizeof(spdy1);
  8678. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8679. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8680. wolfSSL_free(ssl);
  8681. wolfSSL_CTX_free(ctx);
  8682. res = EXPECT_RESULT();
  8683. #endif
  8684. return res;
  8685. }
  8686. #endif /* HAVE_ALPN */
  8687. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  8688. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  8689. {
  8690. unsigned char p[] = {
  8691. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8692. 6, 's', 'p', 'd', 'y', '/', '2',
  8693. 6, 's', 'p', 'd', 'y', '/', '1',
  8694. };
  8695. unsigned char p_len = sizeof(p);
  8696. int ret;
  8697. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  8698. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8699. AssertIntEQ(ret, 0);
  8700. #else
  8701. AssertIntEQ(ret, SSL_SUCCESS);
  8702. #endif
  8703. }
  8704. static void set_alpn_protos(SSL* ssl)
  8705. {
  8706. unsigned char p[] = {
  8707. 6, 's', 'p', 'd', 'y', '/', '3',
  8708. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8709. 6, 's', 'p', 'd', 'y', '/', '2',
  8710. 6, 's', 'p', 'd', 'y', '/', '1',
  8711. };
  8712. unsigned char p_len = sizeof(p);
  8713. int ret;
  8714. ret = SSL_set_alpn_protos(ssl, p, p_len);
  8715. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8716. AssertIntEQ(ret, 0);
  8717. #else
  8718. AssertIntEQ(ret, SSL_SUCCESS);
  8719. #endif
  8720. }
  8721. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  8722. {
  8723. /* "spdy/3" */
  8724. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8725. const unsigned char *proto;
  8726. unsigned int protoSz = 0;
  8727. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8728. /* check value */
  8729. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8730. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8731. }
  8732. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  8733. {
  8734. /* "http/1.1" */
  8735. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8736. const unsigned char *proto;
  8737. unsigned int protoSz = 0;
  8738. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8739. /* check value */
  8740. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8741. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8742. }
  8743. static int test_wolfSSL_set_alpn_protos(void)
  8744. {
  8745. int res = TEST_SKIPPED;
  8746. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8747. callback_functions client_cb;
  8748. callback_functions server_cb;
  8749. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8750. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8751. client_cb.method = wolfSSLv23_client_method;
  8752. server_cb.method = wolfSSLv23_server_method;
  8753. client_cb.devId = testDevId;
  8754. server_cb.devId = testDevId;
  8755. /* use CTX_alpn_protos */
  8756. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8757. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  8758. test_wolfSSL_client_server(&client_cb, &server_cb);
  8759. /* use set_alpn_protos */
  8760. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  8761. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  8762. test_wolfSSL_client_server(&client_cb, &server_cb);
  8763. res = TEST_SUCCESS;
  8764. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8765. return res;
  8766. }
  8767. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  8768. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  8769. {
  8770. int res = TEST_SKIPPED;
  8771. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  8772. EXPECT_DECLS;
  8773. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8774. WOLFSSL *ssl = wolfSSL_new(ctx);
  8775. ExpectNotNull(ctx);
  8776. ExpectNotNull(ssl);
  8777. /* error cases */
  8778. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  8779. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  8780. /* success cases */
  8781. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  8782. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  8783. wolfSSL_free(ssl);
  8784. wolfSSL_CTX_free(ctx);
  8785. res = EXPECT_RESULT();
  8786. #endif
  8787. return res;
  8788. }
  8789. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  8790. {
  8791. int res = TEST_SKIPPED;
  8792. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  8793. EXPECT_DECLS;
  8794. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8795. WOLFSSL *ssl = wolfSSL_new(ctx);
  8796. ExpectNotNull(ctx);
  8797. ExpectNotNull(ssl);
  8798. /* error cases */
  8799. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  8800. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  8801. /* success cases */
  8802. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  8803. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  8804. wolfSSL_free(ssl);
  8805. wolfSSL_CTX_free(ctx);
  8806. res = EXPECT_RESULT();
  8807. #endif
  8808. return res;
  8809. }
  8810. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  8811. static int test_wolfSSL_SCR_Reconnect(void)
  8812. {
  8813. int res = TEST_SKIPPED;
  8814. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  8815. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  8816. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
  8817. EXPECT_DECLS;
  8818. struct test_memio_ctx test_ctx;
  8819. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  8820. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  8821. byte data;
  8822. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  8823. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  8824. test_ctx.s_ciphers =
  8825. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  8826. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8827. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8828. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  8829. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  8830. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  8831. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  8832. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8833. /* WOLFSSL_FATAL_ERROR since it will block */
  8834. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  8835. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8836. WOLFSSL_ERROR_WANT_READ);
  8837. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  8838. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8839. WOLFSSL_ERROR_WANT_READ);
  8840. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8841. wolfSSL_free(ssl_c);
  8842. ssl_c = NULL;
  8843. wolfSSL_free(ssl_s);
  8844. ssl_s = NULL;
  8845. wolfSSL_CTX_free(ctx_c);
  8846. ctx_c = NULL;
  8847. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  8848. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8849. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8850. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8851. wolfSSL_free(ssl_s);
  8852. wolfSSL_free(ssl_c);
  8853. wolfSSL_CTX_free(ctx_s);
  8854. wolfSSL_CTX_free(ctx_c);
  8855. res = EXPECT_RESULT();
  8856. #endif
  8857. return res;
  8858. }
  8859. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  8860. (!defined(NO_RSA) || defined(HAVE_ECC))
  8861. /* Called when writing. */
  8862. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8863. {
  8864. (void)ssl;
  8865. (void)buf;
  8866. (void)sz;
  8867. (void)ctx;
  8868. /* Force error return from wolfSSL_accept_TLSv13(). */
  8869. return WANT_WRITE;
  8870. }
  8871. /* Called when reading. */
  8872. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8873. {
  8874. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  8875. int len = (int)msg->length;
  8876. (void)ssl;
  8877. (void)sz;
  8878. /* Pass back as much of message as will fit in buffer. */
  8879. if (len > sz)
  8880. len = sz;
  8881. XMEMCPY(buf, msg->buffer, len);
  8882. /* Move over returned data. */
  8883. msg->buffer += len;
  8884. msg->length -= len;
  8885. /* Amount actually copied. */
  8886. return len;
  8887. }
  8888. #endif
  8889. /* Test the detection of duplicate known TLS extensions.
  8890. * Specifically in a ClientHello.
  8891. */
  8892. static int test_tls_ext_duplicate(void)
  8893. {
  8894. int res = TEST_SKIPPED;
  8895. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  8896. !defined(NO_FILESYSTEM)
  8897. EXPECT_DECLS;
  8898. const unsigned char clientHelloDupTlsExt[] = {
  8899. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  8900. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  8901. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  8902. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  8903. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  8904. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  8905. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  8906. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  8907. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8908. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  8909. 0x00, 0x9e, 0x01, 0x00,
  8910. /* Extensions - duplicate signature algorithms. */
  8911. 0x00, 0x19, 0x00, 0x0d,
  8912. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  8913. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  8914. /* Supported Versions extension for TLS 1.3. */
  8915. 0x00, 0x2b,
  8916. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  8917. };
  8918. WOLFSSL_BUFFER_INFO msg;
  8919. const char* testCertFile;
  8920. const char* testKeyFile;
  8921. WOLFSSL_CTX *ctx = NULL;
  8922. WOLFSSL *ssl = NULL;
  8923. #ifndef NO_RSA
  8924. testCertFile = svrCertFile;
  8925. testKeyFile = svrKeyFile;
  8926. #elif defined(HAVE_ECC)
  8927. testCertFile = eccCertFile;
  8928. testKeyFile = eccKeyFile;
  8929. #endif
  8930. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8931. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  8932. WOLFSSL_FILETYPE_PEM));
  8933. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  8934. WOLFSSL_FILETYPE_PEM));
  8935. /* Read from 'msg'. */
  8936. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  8937. /* No where to send to - dummy sender. */
  8938. wolfSSL_SetIOSend(ctx, DummySend);
  8939. ssl = wolfSSL_new(ctx);
  8940. ExpectNotNull(ssl);
  8941. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  8942. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  8943. wolfSSL_SetIOReadCtx(ssl, &msg);
  8944. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  8945. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  8946. wolfSSL_free(ssl);
  8947. wolfSSL_CTX_free(ctx);
  8948. res = EXPECT_RESULT();
  8949. #endif
  8950. return res;
  8951. }
  8952. /*----------------------------------------------------------------------------*
  8953. | X509 Tests
  8954. *----------------------------------------------------------------------------*/
  8955. static int test_wolfSSL_X509_NAME_get_entry(void)
  8956. {
  8957. int res = TEST_SKIPPED;
  8958. #if !defined(NO_CERTS) && !defined(NO_RSA)
  8959. #if defined(OPENSSL_ALL) || \
  8960. (defined(OPENSSL_EXTRA) && \
  8961. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  8962. EXPECT_DECLS;
  8963. /* use openssl like name to test mapping */
  8964. X509_NAME_ENTRY* ne;
  8965. X509_NAME* name;
  8966. X509* x509 = NULL;
  8967. #ifndef NO_FILESYSTEM
  8968. ASN1_STRING* asn;
  8969. char* subCN = NULL;
  8970. #endif
  8971. int idx;
  8972. ASN1_OBJECT *object = NULL;
  8973. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8974. defined(WOLFSSL_NGINX)
  8975. #ifndef NO_BIO
  8976. BIO* bio = NULL;
  8977. #endif
  8978. #endif
  8979. #ifndef NO_FILESYSTEM
  8980. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8981. WOLFSSL_FILETYPE_PEM));
  8982. ExpectNotNull(name = X509_get_subject_name(x509));
  8983. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  8984. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  8985. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  8986. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  8987. wolfSSL_FreeX509(x509);
  8988. x509 = NULL;
  8989. #endif
  8990. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8991. WOLFSSL_FILETYPE_PEM));
  8992. ExpectNotNull(name = X509_get_subject_name(x509));
  8993. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  8994. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8995. defined(WOLFSSL_NGINX)
  8996. #ifndef NO_BIO
  8997. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  8998. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  8999. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9000. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9001. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9002. BIO_free(bio);
  9003. #endif
  9004. #endif
  9005. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9006. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9007. wolfSSL_FreeX509(x509);
  9008. res = EXPECT_RESULT();
  9009. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9010. #endif /* !NO_CERTS && !NO_RSA */
  9011. return res;
  9012. }
  9013. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9014. static int test_wolfSSL_PKCS12(void)
  9015. {
  9016. int res = TEST_SKIPPED;
  9017. /* .p12 file is encrypted with DES3 */
  9018. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9019. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9020. * Password Key
  9021. */
  9022. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9023. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9024. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9025. EXPECT_DECLS;
  9026. byte buf[6000];
  9027. char file[] = "./certs/test-servercert.p12";
  9028. char order[] = "./certs/ecc-rsa-server.p12";
  9029. #ifdef WC_RC2
  9030. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9031. #endif
  9032. char pass[] = "a password";
  9033. const char goodPsw[] = "wolfSSL test";
  9034. const char badPsw[] = "bad";
  9035. #ifdef HAVE_ECC
  9036. WOLFSSL_X509_NAME *subject = NULL;
  9037. WOLFSSL_X509 *x509 = NULL;
  9038. #endif
  9039. XFILE f = XBADFILE;
  9040. int bytes, ret, goodPswLen, badPswLen;
  9041. WOLFSSL_BIO *bio = NULL;
  9042. WOLFSSL_EVP_PKEY *pkey = NULL;
  9043. WC_PKCS12 *pkcs12 = NULL;
  9044. WC_PKCS12 *pkcs12_2 = NULL;
  9045. WOLFSSL_X509 *cert = NULL;
  9046. WOLFSSL_X509 *tmp = NULL;
  9047. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9048. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9049. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9050. WOLFSSL_CTX *ctx = NULL;
  9051. WOLFSSL *ssl = NULL;
  9052. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9053. #endif
  9054. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9055. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9056. if (f != XBADFILE) {
  9057. XFCLOSE(f);
  9058. f = XBADFILE;
  9059. }
  9060. goodPswLen = (int)XSTRLEN(goodPsw);
  9061. badPswLen = (int)XSTRLEN(badPsw);
  9062. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9063. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9064. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9065. ExpectNotNull(pkcs12);
  9066. BIO_free(bio);
  9067. bio = NULL;
  9068. /* check verify MAC directly */
  9069. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9070. /* check verify MAC fail case directly */
  9071. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9072. /* check verify MAC fail case */
  9073. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9074. ExpectNull(pkey);
  9075. ExpectNull(cert);
  9076. /* check parse with no extra certs kept */
  9077. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9078. 1);
  9079. ExpectNotNull(pkey);
  9080. ExpectNotNull(cert);
  9081. wolfSSL_EVP_PKEY_free(pkey);
  9082. pkey = NULL;
  9083. wolfSSL_X509_free(cert);
  9084. cert = NULL;
  9085. /* check parse with extra certs kept */
  9086. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9087. 1);
  9088. ExpectNotNull(pkey);
  9089. ExpectNotNull(cert);
  9090. ExpectNotNull(ca);
  9091. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9092. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9093. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9094. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9095. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9096. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9097. #else
  9098. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9099. #endif
  9100. /* Copy stack structure */
  9101. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9102. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9103. /* CTX now owns the tmp_ca stack structure */
  9104. tmp_ca = NULL;
  9105. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9106. ExpectNotNull(tmp_ca);
  9107. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9108. /* Check that the main cert is also set */
  9109. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9110. ExpectNotNull(ssl = SSL_new(ctx));
  9111. ExpectNotNull(SSL_get_certificate(ssl));
  9112. SSL_free(ssl);
  9113. SSL_CTX_free(ctx);
  9114. ctx = NULL;
  9115. #endif
  9116. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9117. /* should be 2 other certs on stack */
  9118. ExpectNotNull(tmp = sk_X509_pop(ca));
  9119. X509_free(tmp);
  9120. ExpectNotNull(tmp = sk_X509_pop(ca));
  9121. X509_free(tmp);
  9122. ExpectNull(sk_X509_pop(ca));
  9123. EVP_PKEY_free(pkey);
  9124. pkey = NULL;
  9125. X509_free(cert);
  9126. cert = NULL;
  9127. sk_X509_pop_free(ca, X509_free);
  9128. ca = NULL;
  9129. /* check PKCS12_create */
  9130. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9131. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9132. SSL_SUCCESS);
  9133. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9134. -1, -1, 100, -1, 0)));
  9135. EVP_PKEY_free(pkey);
  9136. pkey = NULL;
  9137. X509_free(cert);
  9138. cert = NULL;
  9139. sk_X509_pop_free(ca, NULL);
  9140. ca = NULL;
  9141. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9142. SSL_SUCCESS);
  9143. PKCS12_free(pkcs12_2);
  9144. pkcs12_2 = NULL;
  9145. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9146. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9147. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9148. 2000, 1, 0)));
  9149. EVP_PKEY_free(pkey);
  9150. pkey = NULL;
  9151. X509_free(cert);
  9152. cert = NULL;
  9153. sk_X509_pop_free(ca, NULL);
  9154. ca = NULL;
  9155. /* convert to DER then back and parse */
  9156. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9157. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9158. PKCS12_free(pkcs12_2);
  9159. pkcs12_2 = NULL;
  9160. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9161. BIO_free(bio);
  9162. bio = NULL;
  9163. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9164. SSL_SUCCESS);
  9165. /* should be 2 other certs on stack */
  9166. ExpectNotNull(tmp = sk_X509_pop(ca));
  9167. X509_free(tmp);
  9168. ExpectNotNull(tmp = sk_X509_pop(ca));
  9169. X509_free(tmp);
  9170. ExpectNull(sk_X509_pop(ca));
  9171. #ifndef NO_RC4
  9172. PKCS12_free(pkcs12_2);
  9173. pkcs12_2 = NULL;
  9174. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9175. NID_pbe_WithSHA1And128BitRC4,
  9176. NID_pbe_WithSHA1And128BitRC4,
  9177. 2000, 1, 0)));
  9178. EVP_PKEY_free(pkey);
  9179. pkey = NULL;
  9180. X509_free(cert);
  9181. cert = NULL;
  9182. sk_X509_pop_free(ca, NULL);
  9183. ca = NULL;
  9184. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9185. SSL_SUCCESS);
  9186. #endif /* NO_RC4 */
  9187. EVP_PKEY_free(pkey);
  9188. pkey = NULL;
  9189. X509_free(cert);
  9190. cert = NULL;
  9191. PKCS12_free(pkcs12);
  9192. pkcs12 = NULL;
  9193. PKCS12_free(pkcs12_2);
  9194. pkcs12_2 = NULL;
  9195. sk_X509_pop_free(ca, NULL);
  9196. ca = NULL;
  9197. #ifdef HAVE_ECC
  9198. /* test order of parsing */
  9199. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9200. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9201. if (f != XBADFILE) {
  9202. XFCLOSE(f);
  9203. f = XBADFILE;
  9204. }
  9205. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9206. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9207. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9208. WOLFSSL_SUCCESS);
  9209. /* check use of pkey after parse */
  9210. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9211. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9212. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9213. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9214. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9215. #else
  9216. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9217. #endif
  9218. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9219. SSL_CTX_free(ctx);
  9220. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9221. #endif
  9222. ExpectNotNull(pkey);
  9223. ExpectNotNull(cert);
  9224. ExpectNotNull(ca);
  9225. /* compare subject lines of certificates */
  9226. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9227. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9228. SSL_FILETYPE_PEM));
  9229. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9230. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9231. X509_free(x509);
  9232. x509 = NULL;
  9233. /* test expected fail case */
  9234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9235. SSL_FILETYPE_PEM));
  9236. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9237. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9238. X509_free(x509);
  9239. x509 = NULL;
  9240. X509_free(cert);
  9241. cert = NULL;
  9242. /* get subject line from ca stack */
  9243. ExpectNotNull(cert = sk_X509_pop(ca));
  9244. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9245. /* compare subject from certificate in ca to expected */
  9246. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9247. SSL_FILETYPE_PEM));
  9248. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9249. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9250. EVP_PKEY_free(pkey);
  9251. pkey = NULL;
  9252. X509_free(x509);
  9253. x509 = NULL;
  9254. X509_free(cert);
  9255. cert = NULL;
  9256. BIO_free(bio);
  9257. bio = NULL;
  9258. PKCS12_free(pkcs12);
  9259. pkcs12 = NULL;
  9260. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9261. ca = NULL;
  9262. /* test order of parsing */
  9263. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9264. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9265. if (f != XBADFILE) {
  9266. XFCLOSE(f);
  9267. f = XBADFILE;
  9268. }
  9269. /* check verify MAC fail case */
  9270. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9271. ExpectNull(pkey);
  9272. ExpectNull(cert);
  9273. /* check parse with no extra certs kept */
  9274. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9275. 1);
  9276. ExpectNotNull(pkey);
  9277. ExpectNotNull(cert);
  9278. wolfSSL_EVP_PKEY_free(pkey);
  9279. pkey = NULL;
  9280. wolfSSL_X509_free(cert);
  9281. cert = NULL;
  9282. /* check parse with extra certs kept */
  9283. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9284. 1);
  9285. ExpectNotNull(pkey);
  9286. ExpectNotNull(cert);
  9287. ExpectNotNull(ca);
  9288. wolfSSL_EVP_PKEY_free(pkey);
  9289. pkey = NULL;
  9290. wolfSSL_X509_free(cert);
  9291. cert = NULL;
  9292. sk_X509_pop_free(ca, NULL);
  9293. ca = NULL;
  9294. PKCS12_free(pkcs12);
  9295. pkcs12 = NULL;
  9296. #endif /* HAVE_ECC */
  9297. #ifdef WC_RC2
  9298. /* test PKCS#12 with RC2 encryption */
  9299. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9300. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9301. if (f != XBADFILE) {
  9302. XFCLOSE(f);
  9303. f = XBADFILE;
  9304. }
  9305. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9306. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9307. /* check verify MAC fail case */
  9308. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9309. ExpectNull(pkey);
  9310. ExpectNull(cert);
  9311. /* check parse iwth not extra certs kept */
  9312. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9313. WOLFSSL_SUCCESS);
  9314. ExpectNotNull(pkey);
  9315. ExpectNotNull(cert);
  9316. wolfSSL_EVP_PKEY_free(pkey);
  9317. pkey = NULL;
  9318. wolfSSL_X509_free(cert);
  9319. cert = NULL;
  9320. /* check parse with extra certs kept */
  9321. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9322. WOLFSSL_SUCCESS);;
  9323. ExpectNotNull(pkey);
  9324. ExpectNotNull(cert);
  9325. ExpectNotNull(ca);
  9326. wolfSSL_EVP_PKEY_free(pkey);
  9327. wolfSSL_X509_free(cert);
  9328. sk_X509_pop_free(ca, NULL);
  9329. BIO_free(bio);
  9330. bio = NULL;
  9331. PKCS12_free(pkcs12);
  9332. pkcs12 = NULL;
  9333. #endif /* WC_RC2 */
  9334. /* Test i2d_PKCS12_bio */
  9335. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9336. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9337. if (f != XBADFILE)
  9338. XFCLOSE(f);
  9339. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9340. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9341. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9342. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9343. PKCS12_free(pkcs12);
  9344. BIO_free(bio);
  9345. (void)order;
  9346. res = EXPECT_RESULT();
  9347. #endif /* OPENSSL_EXTRA */
  9348. #endif /* HAVE_FIPS */
  9349. return res;
  9350. }
  9351. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9352. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9353. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9354. #define TEST_PKCS8_ENC
  9355. #endif
  9356. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9357. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9358. /* used to keep track if FailTestCallback was called */
  9359. static int failTestCallbackCalled = 0;
  9360. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9361. {
  9362. (void)passwd;
  9363. (void)sz;
  9364. (void)rw;
  9365. (void)userdata;
  9366. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9367. failTestCallbackCalled = 1;
  9368. return -1;
  9369. }
  9370. #endif
  9371. static int test_wolfSSL_no_password_cb(void)
  9372. {
  9373. int res = TEST_SKIPPED;
  9374. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9375. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9376. EXPECT_DECLS;
  9377. WOLFSSL_CTX* ctx = NULL;
  9378. byte buff[FOURK_BUF];
  9379. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9380. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9381. XFILE f = XBADFILE;
  9382. int bytes;
  9383. #ifndef NO_WOLFSSL_CLIENT
  9384. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9385. #else
  9386. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9387. #endif
  9388. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9389. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9390. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9391. if (f != XBADFILE) {
  9392. XFCLOSE(f);
  9393. f = XBADFILE;
  9394. }
  9395. ExpectIntLE(bytes, sizeof(buff));
  9396. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9397. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9398. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9399. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9400. if (f != XBADFILE)
  9401. XFCLOSE(f);
  9402. ExpectIntLE(bytes, sizeof(buff));
  9403. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9404. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9405. wolfSSL_CTX_free(ctx);
  9406. /* Password callback should not be called by default */
  9407. ExpectIntEQ(failTestCallbackCalled, 0);
  9408. res = EXPECT_RESULT();
  9409. #endif
  9410. return res;
  9411. }
  9412. #ifdef TEST_PKCS8_ENC
  9413. /* for PKCS8 test case */
  9414. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9415. {
  9416. int flag = 0;
  9417. (void)rw;
  9418. if (userdata != NULL) {
  9419. flag = *((int*)userdata); /* user set data */
  9420. }
  9421. switch (flag) {
  9422. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9423. * can be anything the user wishes to be passed to the callback
  9424. * associated with the WOLFSSL_CTX */
  9425. XSTRNCPY(passwd, "yassl123", sz);
  9426. return 8;
  9427. default:
  9428. return BAD_FUNC_ARG;
  9429. }
  9430. }
  9431. #endif /* TEST_PKCS8_ENC */
  9432. /* Testing functions dealing with PKCS8 */
  9433. static int test_wolfSSL_PKCS8(void)
  9434. {
  9435. int res = TEST_SKIPPED;
  9436. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9437. !defined(WOLFCRYPT_ONLY)
  9438. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9439. EXPECT_DECLS;
  9440. byte buff[FOURK_BUF];
  9441. byte der[FOURK_BUF];
  9442. #ifndef NO_RSA
  9443. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9444. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9445. #endif
  9446. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9447. #ifdef HAVE_ECC
  9448. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9449. #endif
  9450. XFILE f = XBADFILE;
  9451. int bytes = 0;
  9452. WOLFSSL_CTX* ctx = NULL;
  9453. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9454. int ret;
  9455. ecc_key key;
  9456. word32 x = 0;
  9457. #endif
  9458. #ifdef TEST_PKCS8_ENC
  9459. #if !defined(NO_RSA) && !defined(NO_SHA)
  9460. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9461. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9462. #endif
  9463. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9464. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9465. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9466. #endif
  9467. int flag;
  9468. #endif
  9469. (void)der;
  9470. #ifndef NO_WOLFSSL_CLIENT
  9471. #ifndef WOLFSSL_NO_TLS12
  9472. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9473. #else
  9474. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9475. #endif
  9476. #else
  9477. #ifndef WOLFSSL_NO_TLS12
  9478. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  9479. #else
  9480. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  9481. #endif
  9482. #endif
  9483. #ifdef TEST_PKCS8_ENC
  9484. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  9485. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  9486. flag = 1; /* used by password callback as return code */
  9487. #if !defined(NO_RSA) && !defined(NO_SHA)
  9488. /* test loading PEM PKCS8 encrypted file */
  9489. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  9490. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9491. if (f != XBADFILE) {
  9492. XFCLOSE(f);
  9493. f = XBADFILE;
  9494. }
  9495. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9496. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9497. /* this next case should fail because of password callback return code */
  9498. flag = 0; /* used by password callback as return code */
  9499. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9500. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9501. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9502. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9503. "yassl123"), 0);
  9504. /* test that error value is returned with a bad password */
  9505. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9506. "bad"), 0);
  9507. /* test loading PEM PKCS8 encrypted file */
  9508. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  9509. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9510. if (f != XBADFILE) {
  9511. XFCLOSE(f);
  9512. f = XBADFILE;
  9513. }
  9514. flag = 1; /* used by password callback as return code */
  9515. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9516. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9517. /* this next case should fail because of password callback return code */
  9518. flag = 0; /* used by password callback as return code */
  9519. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9520. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9521. #endif /* !NO_RSA && !NO_SHA */
  9522. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9523. /* test loading PEM PKCS8 encrypted ECC Key file */
  9524. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  9525. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9526. if (f != XBADFILE) {
  9527. XFCLOSE(f);
  9528. f = XBADFILE;
  9529. }
  9530. flag = 1; /* used by password callback as return code */
  9531. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9532. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9533. /* this next case should fail because of password callback return code */
  9534. flag = 0; /* used by password callback as return code */
  9535. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9536. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9537. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9538. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9539. "yassl123"), 0);
  9540. /* test that error value is returned with a bad password */
  9541. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9542. "bad"), 0);
  9543. /* test loading DER PKCS8 encrypted ECC Key file */
  9544. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  9545. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9546. if (f != XBADFILE) {
  9547. XFCLOSE(f);
  9548. f = XBADFILE;
  9549. }
  9550. flag = 1; /* used by password callback as return code */
  9551. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9552. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9553. /* this next case should fail because of password callback return code */
  9554. flag = 0; /* used by password callback as return code */
  9555. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9556. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9557. /* leave flag as "okay" */
  9558. flag = 1;
  9559. #endif /* HAVE_ECC && !NO_SHA */
  9560. #endif /* TEST_PKCS8_ENC */
  9561. #ifndef NO_RSA
  9562. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  9563. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  9564. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9565. if (f != XBADFILE) {
  9566. XFCLOSE(f);
  9567. f = XBADFILE;
  9568. }
  9569. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9570. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9571. /* test loading PEM PKCS8 private key file (not encrypted) */
  9572. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  9573. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9574. if (f != XBADFILE) {
  9575. XFCLOSE(f);
  9576. f = XBADFILE;
  9577. }
  9578. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9579. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9580. #endif /* !NO_RSA */
  9581. /* Test PKCS8 PEM ECC key no crypt */
  9582. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9583. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9584. if (f != XBADFILE) {
  9585. XFCLOSE(f);
  9586. f = XBADFILE;
  9587. }
  9588. #ifdef HAVE_ECC
  9589. /* Test PKCS8 PEM ECC key no crypt */
  9590. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9591. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9592. #ifndef NO_CODING
  9593. /* decrypt PKCS8 PEM to key in DER format */
  9594. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  9595. (word32)sizeof(der), NULL)), 0);
  9596. ret = wc_ecc_init(&key);
  9597. if (ret == 0) {
  9598. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  9599. wc_ecc_free(&key);
  9600. }
  9601. ExpectIntEQ(ret, 0);
  9602. #endif
  9603. /* Test PKCS8 DER ECC key no crypt */
  9604. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9605. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9606. if (f != XBADFILE)
  9607. XFCLOSE(f);
  9608. /* Test using a PKCS8 ECC PEM */
  9609. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9610. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9611. #else
  9612. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  9613. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  9614. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  9615. #endif /* HAVE_ECC */
  9616. wolfSSL_CTX_free(ctx);
  9617. res = EXPECT_RESULT();
  9618. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9619. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  9620. return res;
  9621. }
  9622. static int test_wolfSSL_PKCS8_ED25519(void)
  9623. {
  9624. int res = TEST_SKIPPED;
  9625. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9626. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  9627. defined(HAVE_ED25519_KEY_IMPORT)
  9628. EXPECT_DECLS;
  9629. const byte encPrivKey[] = \
  9630. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9631. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  9632. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  9633. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  9634. "2L9W4v7swXkV+X+a1ww=\n"
  9635. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9636. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9637. byte der[FOURK_BUF];
  9638. WOLFSSL_CTX* ctx = NULL;
  9639. int bytes;
  9640. XMEMSET(der, 0, sizeof(der));
  9641. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9642. (word32)sizeof(der), password)), 0);
  9643. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9644. #ifndef NO_WOLFSSL_SERVER
  9645. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9646. #else
  9647. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9648. #endif
  9649. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9650. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9651. wolfSSL_CTX_free(ctx);
  9652. res = EXPECT_RESULT();
  9653. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9654. #endif
  9655. return res;
  9656. }
  9657. static int test_wolfSSL_PKCS8_ED448(void)
  9658. {
  9659. int res = TEST_SKIPPED;
  9660. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9661. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  9662. defined(HAVE_ED448_KEY_IMPORT)
  9663. EXPECT_DECLS;
  9664. const byte encPrivKey[] = \
  9665. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9666. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  9667. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  9668. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  9669. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  9670. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9671. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9672. byte der[FOURK_BUF];
  9673. WOLFSSL_CTX* ctx = NULL;
  9674. int bytes;
  9675. XMEMSET(der, 0, sizeof(der));
  9676. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9677. (word32)sizeof(der), password)), 0);
  9678. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9679. #ifndef NO_WOLFSSL_SERVER
  9680. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9681. #else
  9682. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9683. #endif
  9684. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9685. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9686. wolfSSL_CTX_free(ctx);
  9687. res = EXPECT_RESULT();
  9688. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9689. #endif
  9690. return res;
  9691. }
  9692. /* Testing functions dealing with PKCS5 */
  9693. static int test_wolfSSL_PKCS5(void)
  9694. {
  9695. int res = TEST_SKIPPED;
  9696. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  9697. EXPECT_DECLS;
  9698. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  9699. const char* passwd = "myfipsPa$$W0rd";
  9700. #else
  9701. const char *passwd = "pass1234";
  9702. #endif
  9703. const unsigned char *salt = (unsigned char *)"salt1234";
  9704. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  9705. DYNAMIC_TYPE_TMP_BUFFER);
  9706. int ret = 0;
  9707. ExpectNotNull(out);
  9708. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  9709. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  9710. WOLFSSL_SUCCESS);
  9711. #ifdef WOLFSSL_SHA512
  9712. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  9713. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  9714. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  9715. #endif
  9716. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9717. res = EXPECT_RESULT();
  9718. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  9719. return res;
  9720. }
  9721. /* test parsing URI from certificate */
  9722. static int test_wolfSSL_URI(void)
  9723. {
  9724. int res = TEST_SKIPPED;
  9725. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9726. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  9727. defined(OPENSSL_EXTRA))
  9728. EXPECT_DECLS;
  9729. WOLFSSL_X509* x509 = NULL;
  9730. const char uri[] = "./certs/client-uri-cert.pem";
  9731. const char urn[] = "./certs/client-absolute-urn.pem";
  9732. const char badUri[] = "./certs/client-relative-uri.pem";
  9733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  9734. WOLFSSL_FILETYPE_PEM));
  9735. wolfSSL_FreeX509(x509);
  9736. x509 = NULL;
  9737. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  9738. WOLFSSL_FILETYPE_PEM));
  9739. wolfSSL_FreeX509(x509);
  9740. x509 = NULL;
  9741. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  9742. && !defined(WOLFSSL_FPKI)
  9743. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  9744. WOLFSSL_FILETYPE_PEM));
  9745. #else
  9746. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  9747. WOLFSSL_FILETYPE_PEM));
  9748. #endif
  9749. wolfSSL_FreeX509(x509);
  9750. res = EXPECT_RESULT();
  9751. #endif
  9752. return res;
  9753. }
  9754. static int test_wolfSSL_TBS(void)
  9755. {
  9756. int res = TEST_SKIPPED;
  9757. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9758. && defined(OPENSSL_EXTRA)
  9759. EXPECT_DECLS;
  9760. WOLFSSL_X509* x509 = NULL;
  9761. const unsigned char* tbs;
  9762. int tbsSz;
  9763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  9764. WOLFSSL_FILETYPE_PEM));
  9765. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  9766. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  9767. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  9768. ExpectIntEQ(tbsSz, 1003);
  9769. wolfSSL_FreeX509(x509);
  9770. res = EXPECT_RESULT();
  9771. #endif
  9772. return res;
  9773. }
  9774. static int test_wolfSSL_X509_verify(void)
  9775. {
  9776. int res = TEST_SKIPPED;
  9777. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9778. && defined(OPENSSL_EXTRA)
  9779. EXPECT_DECLS;
  9780. WOLFSSL_X509* ca = NULL;
  9781. WOLFSSL_X509* serv = NULL;
  9782. WOLFSSL_EVP_PKEY* pkey = NULL;
  9783. unsigned char buf[2048];
  9784. const unsigned char* pt = NULL;
  9785. int bufSz;
  9786. ExpectNotNull(ca =
  9787. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  9788. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  9789. WOLFSSL_SUCCESS);
  9790. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  9791. WOLFSSL_SUCCESS);
  9792. ExpectIntEQ(bufSz, 294);
  9793. bufSz = 2048;
  9794. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  9795. WOLFSSL_SUCCESS);
  9796. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  9797. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  9798. ExpectNotNull(serv =
  9799. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  9800. /* success case */
  9801. pt = buf;
  9802. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9803. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  9804. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  9805. wolfSSL_EVP_PKEY_free(pkey);
  9806. pkey = NULL;
  9807. /* fail case */
  9808. bufSz = 2048;
  9809. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  9810. WOLFSSL_SUCCESS);
  9811. pt = buf;
  9812. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9813. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  9814. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  9815. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  9816. wolfSSL_EVP_PKEY_free(pkey);
  9817. wolfSSL_FreeX509(ca);
  9818. wolfSSL_FreeX509(serv);
  9819. res = EXPECT_RESULT();
  9820. #endif
  9821. return res;
  9822. }
  9823. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  9824. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  9825. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  9826. /* create certificate with version 2 */
  9827. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  9828. {
  9829. EXPECT_DECLS;
  9830. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  9831. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  9832. unsigned char *der = NULL, *key = NULL, *pt;
  9833. char *header = NULL, *name = NULL;
  9834. int derSz;
  9835. long keySz;
  9836. XFILE fp = XBADFILE;
  9837. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  9838. time_t t;
  9839. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9840. WOLFSSL_FILETYPE_PEM));
  9841. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  9842. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  9843. WOLFSSL_SUCCESS);
  9844. if (fp != XBADFILE)
  9845. XFCLOSE(fp);
  9846. pt = key;
  9847. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  9848. (const unsigned char**)&pt, keySz));
  9849. /* create the version 2 certificate */
  9850. ExpectNotNull(x509v2 = X509_new());
  9851. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  9852. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  9853. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  9854. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  9855. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  9856. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  9857. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  9858. t = time(NULL);
  9859. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  9860. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  9861. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  9862. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  9863. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  9864. derSz = wolfSSL_i2d_X509(x509v2, &der);
  9865. ExpectIntGT(derSz, 0);
  9866. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  9867. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9868. /* TODO: Replace with API call */
  9869. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  9870. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9871. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9872. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9873. wolfSSL_X509_free(x509);
  9874. wolfSSL_X509_free(x509v2);
  9875. wolfSSL_EVP_PKEY_free(priv);
  9876. wolfSSL_EVP_PKEY_free(pub);
  9877. wolfSSL_ASN1_TIME_free(notBefore);
  9878. wolfSSL_ASN1_TIME_free(notAfter);
  9879. return EXPECT_RESULT();
  9880. }
  9881. /* override certificate version error */
  9882. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  9883. {
  9884. EXPECT_DECLS;
  9885. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9886. ExpectIntEQ(store->error, ASN_VERSION_E);
  9887. #else
  9888. ExpectIntEQ(store->error, 0);
  9889. #endif
  9890. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  9891. (void)preverify;
  9892. return EXPECT_RESULT() == TEST_SUCCESS;
  9893. }
  9894. /* set verify callback that will override bad certificate version */
  9895. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  9896. {
  9897. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  9898. return TEST_SUCCESS;
  9899. }
  9900. #endif
  9901. static int test_wolfSSL_X509_TLS_version_test_1(void)
  9902. {
  9903. int res = TEST_SKIPPED;
  9904. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  9905. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  9906. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  9907. EXPECT_DECLS;
  9908. test_ssl_cbf func_cb_client;
  9909. test_ssl_cbf func_cb_server;
  9910. /* test server rejects a client certificate that is not version 3 */
  9911. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  9912. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  9913. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9914. #ifndef WOLFSSL_NO_TLS12
  9915. func_cb_client.method = wolfTLSv1_2_client_method;
  9916. #else
  9917. func_cb_client.method = wolfTLSv1_3_client_method;
  9918. #endif
  9919. #ifndef WOLFSSL_NO_TLS12
  9920. func_cb_server.method = wolfTLSv1_2_server_method;
  9921. #else
  9922. func_cb_server.method = wolfTLSv1_3_server_method;
  9923. #endif
  9924. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9925. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  9926. &func_cb_server, NULL), TEST_FAIL);
  9927. #else
  9928. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  9929. &func_cb_server, NULL), TEST_SUCCESS);
  9930. #endif
  9931. res = EXPECT_RESULT();
  9932. #endif
  9933. return res;
  9934. }
  9935. static int test_wolfSSL_X509_TLS_version_test_2(void)
  9936. {
  9937. int res = TEST_SKIPPED;
  9938. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  9939. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  9940. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  9941. EXPECT_DECLS;
  9942. test_ssl_cbf func_cb_client;
  9943. test_ssl_cbf func_cb_server;
  9944. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  9945. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  9946. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9947. func_cb_server.ctx_ready = &test_set_override_x509;
  9948. #ifndef WOLFSSL_NO_TLS12
  9949. func_cb_client.method = wolfTLSv1_2_client_method;
  9950. #else
  9951. func_cb_client.method = wolfTLSv1_3_client_method;
  9952. #endif
  9953. #ifndef WOLFSSL_NO_TLS12
  9954. func_cb_server.method = wolfTLSv1_2_server_method;
  9955. #else
  9956. func_cb_server.method = wolfTLSv1_3_server_method;
  9957. #endif
  9958. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  9959. &func_cb_server, NULL), TEST_SUCCESS);
  9960. res = EXPECT_RESULT();
  9961. #endif
  9962. return res;
  9963. }
  9964. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  9965. * version allowed.
  9966. * POST: 1 on success.
  9967. */
  9968. static int test_wolfSSL_CTX_SetMinVersion(void)
  9969. {
  9970. int res = TEST_SKIPPED;
  9971. #ifndef NO_WOLFSSL_CLIENT
  9972. int failFlag = WOLFSSL_SUCCESS;
  9973. WOLFSSL_CTX* ctx;
  9974. int itr;
  9975. #ifndef NO_OLD_TLS
  9976. const int versions[] = {
  9977. #ifdef WOLFSSL_ALLOW_TLSV10
  9978. WOLFSSL_TLSV1,
  9979. #endif
  9980. WOLFSSL_TLSV1_1,
  9981. WOLFSSL_TLSV1_2 };
  9982. #elif !defined(WOLFSSL_NO_TLS12)
  9983. const int versions[] = { WOLFSSL_TLSV1_2 };
  9984. #elif defined(WOLFSSL_TLS13)
  9985. const int versions[] = { WOLFSSL_TLSV1_3 };
  9986. #else
  9987. const int versions[0];
  9988. #endif
  9989. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9990. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  9991. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  9992. != WOLFSSL_SUCCESS) {
  9993. failFlag = WOLFSSL_FAILURE;
  9994. }
  9995. }
  9996. wolfSSL_CTX_free(ctx);
  9997. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  9998. #endif
  9999. return res;
  10000. } /* END test_wolfSSL_CTX_SetMinVersion */
  10001. /*----------------------------------------------------------------------------*
  10002. | OCSP Stapling
  10003. *----------------------------------------------------------------------------*/
  10004. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10005. * need to contact the CA, lowering the cost of cert revocation checking.
  10006. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10007. * POST: 1 returned for success.
  10008. */
  10009. static int test_wolfSSL_UseOCSPStapling(void)
  10010. {
  10011. int res = TEST_SKIPPED;
  10012. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10013. !defined(NO_WOLFSSL_CLIENT)
  10014. EXPECT_DECLS;
  10015. WOLFSSL_CTX* ctx = NULL;
  10016. WOLFSSL* ssl = NULL;
  10017. #ifndef NO_WOLFSSL_CLIENT
  10018. #ifndef WOLFSSL_NO_TLS12
  10019. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10020. #else
  10021. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10022. #endif
  10023. #else
  10024. #ifndef WOLFSSL_NO_TLS12
  10025. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10026. #else
  10027. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10028. #endif
  10029. #endif
  10030. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10031. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10032. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10033. #ifndef NO_WOLFSSL_CLIENT
  10034. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10035. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10036. #else
  10037. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10038. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10039. #endif
  10040. wolfSSL_free(ssl);
  10041. wolfSSL_CTX_free(ctx);
  10042. res = EXPECT_RESULT();
  10043. #endif
  10044. return res;
  10045. } /*END test_wolfSSL_UseOCSPStapling */
  10046. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10047. * stapling eliminates the need to contact the CA and lowers cert revocation
  10048. * check.
  10049. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10050. */
  10051. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10052. {
  10053. int res = TEST_SKIPPED;
  10054. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10055. !defined(NO_WOLFSSL_CLIENT)
  10056. EXPECT_DECLS;
  10057. WOLFSSL_CTX* ctx = NULL;
  10058. WOLFSSL* ssl = NULL;
  10059. #ifndef NO_WOLFSSL_CLIENT
  10060. #ifndef WOLFSSL_NO_TLS12
  10061. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10062. #else
  10063. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10064. #endif
  10065. #else
  10066. #ifndef WOLFSSL_NO_TLS12
  10067. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10068. #else
  10069. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10070. #endif
  10071. #endif
  10072. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10073. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10074. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10075. #ifndef NO_WOLFSSL_CLIENT
  10076. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10077. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10078. #else
  10079. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10080. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10081. #endif
  10082. wolfSSL_free(ssl);
  10083. wolfSSL_CTX_free(ctx);
  10084. res = EXPECT_RESULT();
  10085. #endif
  10086. return res;
  10087. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10088. /*----------------------------------------------------------------------------*
  10089. | Multicast Tests
  10090. *----------------------------------------------------------------------------*/
  10091. static int test_wolfSSL_mcast(void)
  10092. {
  10093. int res = TEST_SKIPPED;
  10094. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10095. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10096. EXPECT_DECLS;
  10097. WOLFSSL_CTX* ctx = NULL;
  10098. WOLFSSL* ssl = NULL;
  10099. byte preMasterSecret[512];
  10100. byte clientRandom[32];
  10101. byte serverRandom[32];
  10102. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10103. byte buf[256];
  10104. word16 newId;
  10105. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10106. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10107. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10108. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10109. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10110. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10111. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10112. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10113. WOLFSSL_SUCCESS);
  10114. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10115. ExpectIntLE(newId, 100);
  10116. wolfSSL_free(ssl);
  10117. wolfSSL_CTX_free(ctx);
  10118. res = EXPECT_RESULT();
  10119. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10120. * WOLFSSL_SNIFFER) */
  10121. return res;
  10122. }
  10123. /*----------------------------------------------------------------------------*
  10124. | Wolfcrypt
  10125. *----------------------------------------------------------------------------*/
  10126. /*
  10127. * Unit test for the wc_InitBlake2b()
  10128. */
  10129. static int test_wc_InitBlake2b(void)
  10130. {
  10131. int res = TEST_SKIPPED;
  10132. #ifdef HAVE_BLAKE2
  10133. Blake2b blake;
  10134. int ret = 0;
  10135. /* Test good arg. */
  10136. ret = wc_InitBlake2b(&blake, 64);
  10137. if (ret != 0) {
  10138. ret = WOLFSSL_FATAL_ERROR;
  10139. }
  10140. /* Test bad arg. */
  10141. if (!ret) {
  10142. ret = wc_InitBlake2b(NULL, 64);
  10143. if (ret == 0) {
  10144. ret = WOLFSSL_FATAL_ERROR;
  10145. }
  10146. else {
  10147. ret = 0;
  10148. }
  10149. }
  10150. if (!ret) {
  10151. ret = wc_InitBlake2b(NULL, 128);
  10152. if (ret == 0) {
  10153. ret = WOLFSSL_FATAL_ERROR;
  10154. }
  10155. else {
  10156. ret = 0;
  10157. }
  10158. }
  10159. if (!ret) {
  10160. ret = wc_InitBlake2b(&blake, 128);
  10161. if (ret == 0) {
  10162. ret = WOLFSSL_FATAL_ERROR;
  10163. }
  10164. else {
  10165. ret = 0;
  10166. }
  10167. }
  10168. if (!ret) {
  10169. ret = wc_InitBlake2b(NULL, 0);
  10170. if (ret == 0) {
  10171. ret = WOLFSSL_FATAL_ERROR;
  10172. }
  10173. else {
  10174. ret = 0;
  10175. }
  10176. }
  10177. if (!ret) {
  10178. ret = wc_InitBlake2b(&blake, 0);
  10179. if (ret == 0) {
  10180. ret = WOLFSSL_FATAL_ERROR;
  10181. }
  10182. else {
  10183. ret = 0;
  10184. }
  10185. }
  10186. res = TEST_RES_CHECK(ret == 0);
  10187. #endif
  10188. return res;
  10189. } /*END test_wc_InitBlake2b*/
  10190. /*
  10191. * Unit test for the wc_InitBlake2b_WithKey()
  10192. */
  10193. static int test_wc_InitBlake2b_WithKey(void)
  10194. {
  10195. int res = TEST_SKIPPED;
  10196. #ifdef HAVE_BLAKE2
  10197. Blake2b blake;
  10198. word32 digestSz = BLAKE2B_KEYBYTES;
  10199. byte key[BLAKE2B_KEYBYTES];
  10200. word32 keylen = BLAKE2B_KEYBYTES;
  10201. int ret = 0;
  10202. XMEMSET(key, 0, sizeof(key));
  10203. /* Test good arg. */
  10204. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  10205. if (ret != 0) {
  10206. ret = WOLFSSL_FATAL_ERROR;
  10207. }
  10208. /* Test bad args. */
  10209. if (ret == 0) {
  10210. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  10211. if (ret == BAD_FUNC_ARG) {
  10212. ret = 0;
  10213. }
  10214. }
  10215. if (ret == 0) {
  10216. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  10217. if (ret == BAD_FUNC_ARG) {
  10218. ret = 0;
  10219. }
  10220. }
  10221. if (ret == 0) {
  10222. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  10223. }
  10224. res = TEST_RES_CHECK(ret == 0);
  10225. #endif
  10226. return res;
  10227. } /*END wc_InitBlake2b_WithKey*/
  10228. /*
  10229. * Unit test for the wc_InitBlake2s_WithKey()
  10230. */
  10231. static int test_wc_InitBlake2s_WithKey(void)
  10232. {
  10233. int res = TEST_SKIPPED;
  10234. #ifdef HAVE_BLAKE2S
  10235. Blake2s blake;
  10236. word32 digestSz = BLAKE2S_KEYBYTES;
  10237. byte *key = (byte*)"01234567890123456789012345678901";
  10238. word32 keylen = BLAKE2S_KEYBYTES;
  10239. int ret = 0;
  10240. /* Test good arg. */
  10241. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  10242. if (ret != 0) {
  10243. ret = WOLFSSL_FATAL_ERROR;
  10244. }
  10245. /* Test bad args. */
  10246. if (ret == 0) {
  10247. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  10248. if (ret == BAD_FUNC_ARG) {
  10249. ret = 0;
  10250. }
  10251. }
  10252. if (ret == 0) {
  10253. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  10254. if (ret == BAD_FUNC_ARG) {
  10255. ret = 0;
  10256. }
  10257. }
  10258. if (ret == 0) {
  10259. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  10260. }
  10261. res = TEST_RES_CHECK(ret == 0);
  10262. #endif
  10263. return res;
  10264. } /*END wc_InitBlake2s_WithKey*/
  10265. /*
  10266. * Unit test for the wc_InitMd5()
  10267. */
  10268. static int test_wc_InitMd5(void)
  10269. {
  10270. int res = TEST_SKIPPED;
  10271. #ifndef NO_MD5
  10272. wc_Md5 md5;
  10273. int ret;
  10274. int flag = 0;
  10275. /* Test good arg. */
  10276. ret = wc_InitMd5(&md5);
  10277. if (ret != 0) {
  10278. flag = WOLFSSL_FATAL_ERROR;
  10279. }
  10280. /* Test bad arg. */
  10281. if (!flag) {
  10282. ret = wc_InitMd5(NULL);
  10283. if (ret != BAD_FUNC_ARG) {
  10284. flag = WOLFSSL_FATAL_ERROR;
  10285. }
  10286. }
  10287. wc_Md5Free(&md5);
  10288. res = TEST_RES_CHECK(flag == 0);
  10289. #endif
  10290. return res;
  10291. } /* END test_wc_InitMd5 */
  10292. /*
  10293. * Testing wc_UpdateMd5()
  10294. */
  10295. static int test_wc_Md5Update(void)
  10296. {
  10297. int res = TEST_SKIPPED;
  10298. #ifndef NO_MD5
  10299. wc_Md5 md5;
  10300. byte hash[WC_MD5_DIGEST_SIZE];
  10301. testVector a, b, c;
  10302. int ret;
  10303. int flag = 0;
  10304. ret = wc_InitMd5(&md5);
  10305. if (ret != 0) {
  10306. flag = ret;
  10307. }
  10308. /* Input */
  10309. if (!flag) {
  10310. a.input = "a";
  10311. a.inLen = XSTRLEN(a.input);
  10312. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  10313. if (ret != 0) {
  10314. flag = ret;
  10315. }
  10316. }
  10317. if (!flag) {
  10318. ret = wc_Md5Final(&md5, hash);
  10319. if (ret != 0) {
  10320. flag = ret;
  10321. }
  10322. }
  10323. /* Update input. */
  10324. if (!flag) {
  10325. a.input = "abc";
  10326. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10327. "\x72";
  10328. a.inLen = XSTRLEN(a.input);
  10329. a.outLen = XSTRLEN(a.output);
  10330. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  10331. if (ret != 0) {
  10332. flag = ret;
  10333. }
  10334. }
  10335. if (!flag) {
  10336. ret = wc_Md5Final(&md5, hash);
  10337. if (ret != 0) {
  10338. flag = ret;
  10339. }
  10340. }
  10341. if (!flag) {
  10342. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  10343. flag = WOLFSSL_FATAL_ERROR;
  10344. }
  10345. }
  10346. /*Pass in bad values. */
  10347. if (!flag) {
  10348. b.input = NULL;
  10349. b.inLen = 0;
  10350. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  10351. if (ret != 0) {
  10352. flag = ret;
  10353. }
  10354. }
  10355. if (!flag) {
  10356. c.input = NULL;
  10357. c.inLen = WC_MD5_DIGEST_SIZE;
  10358. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  10359. if (ret != BAD_FUNC_ARG) {
  10360. flag = WOLFSSL_FATAL_ERROR;
  10361. }
  10362. }
  10363. if (!flag) {
  10364. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  10365. if (ret != BAD_FUNC_ARG) {
  10366. flag = WOLFSSL_FATAL_ERROR;
  10367. }
  10368. }
  10369. wc_Md5Free(&md5);
  10370. res = TEST_RES_CHECK(flag == 0);
  10371. #endif
  10372. return res;
  10373. } /* END test_wc_Md5Update() */
  10374. /*
  10375. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10376. */
  10377. static int test_wc_Md5Final(void)
  10378. {
  10379. int res = TEST_SKIPPED;
  10380. #ifndef NO_MD5
  10381. /* Instantiate */
  10382. wc_Md5 md5;
  10383. byte* hash_test[3];
  10384. byte hash1[WC_MD5_DIGEST_SIZE];
  10385. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10386. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10387. int times, i, ret;
  10388. int flag = 0;
  10389. /* Initialize */
  10390. ret = wc_InitMd5(&md5);
  10391. if (ret != 0) {
  10392. flag = ret;
  10393. }
  10394. if (!flag) {
  10395. hash_test[0] = hash1;
  10396. hash_test[1] = hash2;
  10397. hash_test[2] = hash3;
  10398. }
  10399. times = sizeof(hash_test)/sizeof(byte*);
  10400. for (i = 0; i < times; i++) {
  10401. if (!flag) {
  10402. ret = wc_Md5Final(&md5, hash_test[i]);
  10403. if (ret != 0) {
  10404. flag = WOLFSSL_FATAL_ERROR;
  10405. }
  10406. }
  10407. }
  10408. /* Test bad args. */
  10409. if (!flag) {
  10410. ret = wc_Md5Final(NULL, NULL);
  10411. if (ret != BAD_FUNC_ARG) {
  10412. flag = WOLFSSL_FATAL_ERROR;
  10413. }
  10414. }
  10415. if (!flag) {
  10416. ret = wc_Md5Final(NULL, hash1);
  10417. if (ret != BAD_FUNC_ARG) {
  10418. flag = WOLFSSL_FATAL_ERROR;
  10419. }
  10420. }
  10421. if (!flag) {
  10422. ret = wc_Md5Final(&md5, NULL);
  10423. if (ret != BAD_FUNC_ARG) {
  10424. flag = WOLFSSL_FATAL_ERROR;
  10425. }
  10426. }
  10427. wc_Md5Free(&md5);
  10428. res = TEST_RES_CHECK(flag == 0);
  10429. #endif
  10430. return res;
  10431. }
  10432. /*
  10433. * Unit test for the wc_InitSha()
  10434. */
  10435. static int test_wc_InitSha(void)
  10436. {
  10437. int res = TEST_SKIPPED;
  10438. #ifndef NO_SHA
  10439. wc_Sha sha;
  10440. int ret;
  10441. int flag = 0;
  10442. /* Test good arg. */
  10443. ret = wc_InitSha(&sha);
  10444. if (ret != 0) {
  10445. flag = WOLFSSL_FATAL_ERROR;
  10446. }
  10447. /* Test bad arg. */
  10448. if (!flag) {
  10449. ret = wc_InitSha(NULL);
  10450. if (ret != BAD_FUNC_ARG) {
  10451. flag = WOLFSSL_FATAL_ERROR;
  10452. }
  10453. }
  10454. wc_ShaFree(&sha);
  10455. res = TEST_RES_CHECK(flag == 0);
  10456. #endif
  10457. return res;
  10458. } /* END test_wc_InitSha */
  10459. /*
  10460. * Tesing wc_ShaUpdate()
  10461. */
  10462. static int test_wc_ShaUpdate(void)
  10463. {
  10464. int res = TEST_SKIPPED;
  10465. #ifndef NO_SHA
  10466. wc_Sha sha;
  10467. byte hash[WC_SHA_DIGEST_SIZE];
  10468. testVector a, b, c;
  10469. int flag = 0;
  10470. int ret;
  10471. ret = wc_InitSha(&sha);
  10472. if (ret != 0) {
  10473. flag = ret;
  10474. }
  10475. /* Input. */
  10476. if (!flag) {
  10477. a.input = "a";
  10478. a.inLen = XSTRLEN(a.input);
  10479. ret = wc_ShaUpdate(&sha, NULL, 0);
  10480. if (ret != 0) {
  10481. flag = ret;
  10482. }
  10483. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  10484. if (ret != 0) {
  10485. flag = ret;
  10486. }
  10487. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  10488. if (ret != 0) {
  10489. flag = ret;
  10490. }
  10491. }
  10492. if (!flag) {
  10493. ret = wc_ShaFinal(&sha, hash);
  10494. if (ret != 0) {
  10495. flag = ret;
  10496. }
  10497. }
  10498. /* Update input. */
  10499. if (!flag) {
  10500. a.input = "abc";
  10501. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10502. "\x6C\x9C\xD0\xD8\x9D";
  10503. a.inLen = XSTRLEN(a.input);
  10504. a.outLen = XSTRLEN(a.output);
  10505. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  10506. if (ret != 0) {
  10507. flag = ret;
  10508. }
  10509. }
  10510. if (!flag) {
  10511. ret = wc_ShaFinal(&sha, hash);
  10512. if (ret !=0) {
  10513. flag = ret;
  10514. }
  10515. }
  10516. if (!flag) {
  10517. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  10518. flag = WOLFSSL_FATAL_ERROR;
  10519. }
  10520. }
  10521. /* Try passing in bad values. */
  10522. if (!flag) {
  10523. b.input = NULL;
  10524. b.inLen = 0;
  10525. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  10526. if (ret != 0) {
  10527. flag = ret;
  10528. }
  10529. }
  10530. if (!flag) {
  10531. c.input = NULL;
  10532. c.inLen = WC_SHA_DIGEST_SIZE;
  10533. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  10534. if (ret != BAD_FUNC_ARG) {
  10535. flag = WOLFSSL_FATAL_ERROR;
  10536. }
  10537. }
  10538. if (!flag) {
  10539. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10540. if (ret != BAD_FUNC_ARG) {
  10541. flag = WOLFSSL_FATAL_ERROR;
  10542. }
  10543. }
  10544. wc_ShaFree(&sha);
  10545. res = TEST_RES_CHECK(flag == 0);
  10546. #endif
  10547. return res;
  10548. } /* END test_wc_ShaUpdate() */
  10549. /*
  10550. * Unit test on wc_ShaFinal
  10551. */
  10552. static int test_wc_ShaFinal(void)
  10553. {
  10554. int res = TEST_SKIPPED;
  10555. #ifndef NO_SHA
  10556. wc_Sha sha;
  10557. byte* hash_test[3];
  10558. byte hash1[WC_SHA_DIGEST_SIZE];
  10559. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10560. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10561. int times, i, ret;
  10562. int flag = 0;
  10563. /*Initialize*/
  10564. ret = wc_InitSha(&sha);
  10565. if (ret) {
  10566. flag = ret;
  10567. }
  10568. if (!flag) {
  10569. hash_test[0] = hash1;
  10570. hash_test[1] = hash2;
  10571. hash_test[2] = hash3;
  10572. }
  10573. times = sizeof(hash_test)/sizeof(byte*);
  10574. for (i = 0; i < times; i++) {
  10575. if (!flag) {
  10576. ret = wc_ShaFinal(&sha, hash_test[i]);
  10577. if (ret != 0) {
  10578. flag = WOLFSSL_FATAL_ERROR;
  10579. }
  10580. }
  10581. }
  10582. /* Test bad args. */
  10583. if (!flag) {
  10584. ret = wc_ShaFinal(NULL, NULL);
  10585. if (ret != BAD_FUNC_ARG) {
  10586. flag = WOLFSSL_FATAL_ERROR;
  10587. }
  10588. }
  10589. if (!flag) {
  10590. ret = wc_ShaFinal(NULL, hash1);
  10591. if (ret != BAD_FUNC_ARG) {
  10592. flag = WOLFSSL_FATAL_ERROR;
  10593. }
  10594. }
  10595. if (!flag) {
  10596. ret = wc_ShaFinal(&sha, NULL);
  10597. if (ret != BAD_FUNC_ARG) {
  10598. flag = WOLFSSL_FATAL_ERROR;
  10599. }
  10600. }
  10601. wc_ShaFree(&sha);
  10602. res = TEST_RES_CHECK(flag == 0);
  10603. #endif
  10604. return res;
  10605. } /* END test_wc_ShaFinal */
  10606. /*
  10607. * Unit test for wc_InitSha256()
  10608. */
  10609. static int test_wc_InitSha256(void)
  10610. {
  10611. int res = TEST_SKIPPED;
  10612. #ifndef NO_SHA256
  10613. wc_Sha256 sha256;
  10614. int ret;
  10615. int flag = 0;
  10616. /* Test good arg. */
  10617. ret = wc_InitSha256(&sha256);
  10618. if (ret != 0) {
  10619. flag = WOLFSSL_FATAL_ERROR;
  10620. }
  10621. /* Test bad arg. */
  10622. if (!flag) {
  10623. ret = wc_InitSha256(NULL);
  10624. if (ret != BAD_FUNC_ARG) {
  10625. flag = WOLFSSL_FATAL_ERROR;
  10626. }
  10627. }
  10628. wc_Sha256Free(&sha256);
  10629. res = TEST_RES_CHECK(flag == 0);
  10630. #endif
  10631. return res;
  10632. } /* END test_wc_InitSha256 */
  10633. /*
  10634. * Unit test for wc_Sha256Update()
  10635. */
  10636. static int test_wc_Sha256Update(void)
  10637. {
  10638. int res = TEST_SKIPPED;
  10639. #ifndef NO_SHA256
  10640. wc_Sha256 sha256;
  10641. byte hash[WC_SHA256_DIGEST_SIZE];
  10642. testVector a, b, c;
  10643. int ret;
  10644. int flag = 0;
  10645. ret = wc_InitSha256(&sha256);
  10646. if (ret != 0) {
  10647. flag = ret;
  10648. }
  10649. /* Input. */
  10650. if (!flag) {
  10651. a.input = "a";
  10652. a.inLen = XSTRLEN(a.input);
  10653. ret = wc_Sha256Update(&sha256, NULL, 0);
  10654. if (ret != 0) {
  10655. flag = ret;
  10656. }
  10657. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  10658. if (ret != 0) {
  10659. flag = ret;
  10660. }
  10661. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10662. if (ret != 0) {
  10663. flag = ret;
  10664. }
  10665. }
  10666. if (!flag) {
  10667. ret = wc_Sha256Final(&sha256, hash);
  10668. if (ret != 0) {
  10669. flag = ret;
  10670. }
  10671. }
  10672. /* Update input. */
  10673. if (!flag) {
  10674. a.input = "abc";
  10675. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10676. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10677. "\x15\xAD";
  10678. a.inLen = XSTRLEN(a.input);
  10679. a.outLen = XSTRLEN(a.output);
  10680. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10681. if (ret != 0) {
  10682. flag = ret;
  10683. }
  10684. }
  10685. if (!flag) {
  10686. ret = wc_Sha256Final(&sha256, hash);
  10687. if (ret != 0) {
  10688. flag = ret;
  10689. }
  10690. }
  10691. if (!flag) {
  10692. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  10693. flag = WOLFSSL_FATAL_ERROR;
  10694. }
  10695. }
  10696. /* Try passing in bad values */
  10697. if (!flag) {
  10698. b.input = NULL;
  10699. b.inLen = 0;
  10700. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  10701. if (ret != 0) {
  10702. flag = ret;
  10703. }
  10704. }
  10705. if (!flag) {
  10706. c.input = NULL;
  10707. c.inLen = WC_SHA256_DIGEST_SIZE;
  10708. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  10709. if (ret != BAD_FUNC_ARG) {
  10710. flag = WOLFSSL_FATAL_ERROR;
  10711. }
  10712. }
  10713. if (!flag) {
  10714. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10715. if (ret != BAD_FUNC_ARG) {
  10716. flag = WOLFSSL_FATAL_ERROR;
  10717. }
  10718. }
  10719. wc_Sha256Free(&sha256);
  10720. res = TEST_RES_CHECK(flag == 0);
  10721. #endif
  10722. return res;
  10723. } /* END test_wc_Sha256Update */
  10724. /*
  10725. * Unit test function for wc_Sha256Final()
  10726. */
  10727. static int test_wc_Sha256Final(void)
  10728. {
  10729. int res = TEST_SKIPPED;
  10730. #ifndef NO_SHA256
  10731. wc_Sha256 sha256;
  10732. byte* hash_test[3];
  10733. byte hash1[WC_SHA256_DIGEST_SIZE];
  10734. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10735. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10736. int times, i, ret;
  10737. int flag = 0;
  10738. /* Initialize */
  10739. ret = wc_InitSha256(&sha256);
  10740. if (ret != 0) {
  10741. flag = ret;
  10742. }
  10743. if (!flag) {
  10744. hash_test[0] = hash1;
  10745. hash_test[1] = hash2;
  10746. hash_test[2] = hash3;
  10747. }
  10748. times = sizeof(hash_test) / sizeof(byte*);
  10749. for (i = 0; i < times; i++) {
  10750. if (!flag) {
  10751. ret = wc_Sha256Final(&sha256, hash_test[i]);
  10752. if (ret != 0) {
  10753. flag = WOLFSSL_FATAL_ERROR;
  10754. }
  10755. }
  10756. }
  10757. /* Test bad args. */
  10758. if (!flag ) {
  10759. ret = wc_Sha256Final(NULL, NULL);
  10760. if (ret != BAD_FUNC_ARG) {
  10761. flag = WOLFSSL_FATAL_ERROR;
  10762. }
  10763. }
  10764. if (!flag) {
  10765. ret = wc_Sha256Final(NULL, hash1);
  10766. if (ret != BAD_FUNC_ARG) {
  10767. flag = WOLFSSL_FATAL_ERROR;
  10768. }
  10769. }
  10770. if (!flag) {
  10771. ret = wc_Sha256Final(&sha256, NULL);
  10772. if (ret != BAD_FUNC_ARG) {
  10773. flag = WOLFSSL_FATAL_ERROR;
  10774. }
  10775. }
  10776. wc_Sha256Free(&sha256);
  10777. res = TEST_RES_CHECK(flag == 0);
  10778. #endif
  10779. return res;
  10780. } /* END test_wc_Sha256Final */
  10781. /*
  10782. * Unit test function for wc_Sha256FinalRaw()
  10783. */
  10784. static int test_wc_Sha256FinalRaw(void)
  10785. {
  10786. int res = TEST_SKIPPED;
  10787. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10788. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10789. !defined(WOLFSSL_NO_HASH_RAW)
  10790. wc_Sha256 sha256;
  10791. byte* hash_test[3];
  10792. byte hash1[WC_SHA256_DIGEST_SIZE];
  10793. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10794. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10795. int times, i, ret;
  10796. int flag = 0;
  10797. /* Initialize */
  10798. ret = wc_InitSha256(&sha256);
  10799. if (ret != 0) {
  10800. flag = ret;
  10801. }
  10802. if (!flag) {
  10803. hash_test[0] = hash1;
  10804. hash_test[1] = hash2;
  10805. hash_test[2] = hash3;
  10806. }
  10807. times = sizeof(hash_test) / sizeof(byte*);
  10808. for (i = 0; i < times; i++) {
  10809. if (!flag) {
  10810. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  10811. if (ret != 0) {
  10812. flag = WOLFSSL_FATAL_ERROR;
  10813. }
  10814. }
  10815. }
  10816. /* Test bad args. */
  10817. if (!flag) {
  10818. ret = wc_Sha256FinalRaw(NULL, NULL);
  10819. if (ret != BAD_FUNC_ARG) {
  10820. flag = WOLFSSL_FATAL_ERROR;
  10821. }
  10822. }
  10823. if (!flag) {
  10824. ret = wc_Sha256FinalRaw(NULL, hash1);
  10825. if (ret != BAD_FUNC_ARG) {
  10826. flag = WOLFSSL_FATAL_ERROR;
  10827. }
  10828. }
  10829. if (!flag) {
  10830. ret = wc_Sha256FinalRaw(&sha256, NULL);
  10831. if (ret != BAD_FUNC_ARG) {
  10832. flag = WOLFSSL_FATAL_ERROR;
  10833. }
  10834. }
  10835. wc_Sha256Free(&sha256);
  10836. res = TEST_RES_CHECK(flag == 0);
  10837. #endif
  10838. return res;
  10839. } /* END test_wc_Sha256FinalRaw */
  10840. /*
  10841. * Unit test function for wc_Sha256GetFlags()
  10842. */
  10843. static int test_wc_Sha256GetFlags(void)
  10844. {
  10845. int res = TEST_SKIPPED;
  10846. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10847. wc_Sha256 sha256;
  10848. word32 flags = 0;
  10849. int flag = 0;
  10850. /* Initialize */
  10851. flag = wc_InitSha256(&sha256);
  10852. if (flag == 0) {
  10853. flag = wc_Sha256GetFlags(&sha256, &flags);
  10854. }
  10855. if (flag == 0) {
  10856. if (flags & WC_HASH_FLAG_ISCOPY) {
  10857. flag = 0;
  10858. }
  10859. }
  10860. wc_Sha256Free(&sha256);
  10861. res = TEST_RES_CHECK(flag == 0);
  10862. #endif
  10863. return res;
  10864. } /* END test_wc_Sha256GetFlags */
  10865. /*
  10866. * Unit test function for wc_Sha256Free()
  10867. */
  10868. static int test_wc_Sha256Free(void)
  10869. {
  10870. int res = TEST_SKIPPED;
  10871. #ifndef NO_SHA256
  10872. wc_Sha256Free(NULL);
  10873. res = TEST_RES_CHECK(1);
  10874. #endif
  10875. return res;
  10876. } /* END test_wc_Sha256Free */
  10877. /*
  10878. * Unit test function for wc_Sha256GetHash()
  10879. */
  10880. static int test_wc_Sha256GetHash(void)
  10881. {
  10882. int res = TEST_SKIPPED;
  10883. #ifndef NO_SHA256
  10884. wc_Sha256 sha256;
  10885. byte hash1[WC_SHA256_DIGEST_SIZE];
  10886. int flag = 0;
  10887. /* Initialize */
  10888. flag = wc_InitSha256(&sha256);
  10889. if (flag == 0) {
  10890. flag = wc_Sha256GetHash(&sha256, hash1);
  10891. }
  10892. /*test bad arguments*/
  10893. if (flag == 0) {
  10894. flag = wc_Sha256GetHash(NULL, NULL);
  10895. if (flag == BAD_FUNC_ARG) {
  10896. flag = 0;
  10897. }
  10898. }
  10899. if (flag == 0) {
  10900. flag = wc_Sha256GetHash(NULL, hash1);
  10901. if (flag == BAD_FUNC_ARG) {
  10902. flag = 0;
  10903. }
  10904. }
  10905. if (flag == 0) {
  10906. flag = wc_Sha256GetHash(&sha256, NULL);
  10907. if (flag == BAD_FUNC_ARG) {
  10908. flag = 0;
  10909. }
  10910. }
  10911. wc_Sha256Free(&sha256);
  10912. res = TEST_RES_CHECK(flag == 0);
  10913. #endif
  10914. return res;
  10915. } /* END test_wc_Sha256GetHash */
  10916. /*
  10917. * Unit test function for wc_Sha256Copy()
  10918. */
  10919. static int test_wc_Sha256Copy(void)
  10920. {
  10921. int res = TEST_SKIPPED;
  10922. #ifndef NO_SHA256
  10923. wc_Sha256 sha256;
  10924. wc_Sha256 temp;
  10925. int flag = 0;
  10926. /* Initialize */
  10927. flag = wc_InitSha256(&sha256);
  10928. if (flag == 0) {
  10929. flag = wc_InitSha256(&temp);
  10930. }
  10931. if (flag == 0) {
  10932. flag = wc_Sha256Copy(&sha256, &temp);
  10933. }
  10934. /*test bad arguments*/
  10935. if (flag == 0) {
  10936. flag = wc_Sha256Copy(NULL, NULL);
  10937. if (flag == BAD_FUNC_ARG) {
  10938. flag = 0;
  10939. }
  10940. }
  10941. if (flag == 0) {
  10942. flag = wc_Sha256Copy(NULL, &temp);
  10943. if (flag == BAD_FUNC_ARG) {
  10944. flag = 0;
  10945. }
  10946. }
  10947. if (flag == 0) {
  10948. flag = wc_Sha256Copy(&sha256, NULL);
  10949. if (flag == BAD_FUNC_ARG) {
  10950. flag = 0;
  10951. }
  10952. }
  10953. wc_Sha256Free(&sha256);
  10954. wc_Sha256Free(&temp);
  10955. res = TEST_RES_CHECK(flag == 0);
  10956. #endif
  10957. return res;
  10958. } /* END test_wc_Sha256Copy */
  10959. /*
  10960. * Testing wc_InitSha512()
  10961. */
  10962. static int test_wc_InitSha512(void)
  10963. {
  10964. int res = TEST_SKIPPED;
  10965. #ifdef WOLFSSL_SHA512
  10966. wc_Sha512 sha512;
  10967. int ret;
  10968. int flag = 0;
  10969. /* Test good arg. */
  10970. ret = wc_InitSha512(&sha512);
  10971. if (ret != 0) {
  10972. flag = WOLFSSL_FATAL_ERROR;
  10973. }
  10974. /* Test bad arg. */
  10975. if (!flag) {
  10976. ret = wc_InitSha512(NULL);
  10977. if (ret != BAD_FUNC_ARG) {
  10978. flag = WOLFSSL_FATAL_ERROR;
  10979. }
  10980. }
  10981. wc_Sha512Free(&sha512);
  10982. res = TEST_RES_CHECK(flag == 0);
  10983. #endif
  10984. return res;
  10985. } /* END test_wc_InitSha512 */
  10986. /*
  10987. * wc_Sha512Update() test.
  10988. */
  10989. static int test_wc_Sha512Update(void)
  10990. {
  10991. int res = TEST_SKIPPED;
  10992. #ifdef WOLFSSL_SHA512
  10993. wc_Sha512 sha512;
  10994. byte hash[WC_SHA512_DIGEST_SIZE];
  10995. testVector a, b, c;
  10996. int ret;
  10997. int flag = 0;
  10998. ret = wc_InitSha512(&sha512);
  10999. if (ret != 0) {
  11000. flag = ret;
  11001. }
  11002. /* Input. */
  11003. if (!flag) {
  11004. a.input = "a";
  11005. a.inLen = XSTRLEN(a.input);
  11006. ret = wc_Sha512Update(&sha512, NULL, 0);
  11007. if (ret != 0) {
  11008. flag = ret;
  11009. }
  11010. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  11011. if (ret != 0) {
  11012. flag = ret;
  11013. }
  11014. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  11015. if (ret != 0) {
  11016. flag = ret;
  11017. }
  11018. ret = wc_Sha512Final(&sha512, hash);
  11019. if (ret != 0) {
  11020. flag = ret;
  11021. }
  11022. }
  11023. /* Update input. */
  11024. if (!flag) {
  11025. a.input = "abc";
  11026. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11027. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11028. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11029. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11030. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11031. a.inLen = XSTRLEN(a.input);
  11032. a.outLen = XSTRLEN(a.output);
  11033. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  11034. if (ret != 0) {
  11035. flag = ret;
  11036. }
  11037. }
  11038. if (!flag) {
  11039. ret = wc_Sha512Final(&sha512, hash);
  11040. if (ret != 0) {
  11041. flag = ret;
  11042. }
  11043. }
  11044. if (!flag) {
  11045. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  11046. flag = WOLFSSL_FATAL_ERROR;
  11047. }
  11048. }
  11049. /* Try passing in bad values */
  11050. if (!flag) {
  11051. b.input = NULL;
  11052. b.inLen = 0;
  11053. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  11054. if (ret != 0) {
  11055. flag = ret;
  11056. }
  11057. }
  11058. if (!flag) {
  11059. c.input = NULL;
  11060. c.inLen = WC_SHA512_DIGEST_SIZE;
  11061. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11062. if (ret != BAD_FUNC_ARG) {
  11063. flag = WOLFSSL_FATAL_ERROR;
  11064. }
  11065. }
  11066. if (!flag) {
  11067. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  11068. if (ret != BAD_FUNC_ARG) {
  11069. flag = WOLFSSL_FATAL_ERROR;
  11070. }
  11071. }
  11072. wc_Sha512Free(&sha512);
  11073. res = TEST_RES_CHECK(flag == 0);
  11074. #endif
  11075. return res;
  11076. } /* END test_wc_Sha512Update */
  11077. #ifdef WOLFSSL_SHA512
  11078. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11079. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11080. /* Perfoms test for
  11081. * - wc_Sha512Final/wc_Sha512FinalRaw
  11082. * - wc_Sha512_224Final/wc_Sha512_224Final
  11083. * - wc_Sha512_256Final/wc_Sha512_256Final
  11084. * parameter:
  11085. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11086. * WC_HASH_TYPE_SHA512_256
  11087. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11088. *return 0 on success
  11089. */
  11090. static int test_Sha512_Family_Final(int type, int isRaw)
  11091. {
  11092. wc_Sha512 sha512;
  11093. byte* hash_test[3];
  11094. byte hash1[WC_SHA512_DIGEST_SIZE];
  11095. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11096. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11097. int times, i, ret;
  11098. int(*initFp)(wc_Sha512*);
  11099. int(*finalFp)(wc_Sha512*, byte*);
  11100. void(*freeFp)(wc_Sha512*);
  11101. if (type == WC_HASH_TYPE_SHA512) {
  11102. initFp = wc_InitSha512;
  11103. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11104. !defined(WOLFSSL_NO_HASH_RAW)
  11105. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11106. #else
  11107. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11108. #endif
  11109. freeFp = wc_Sha512Free;
  11110. }
  11111. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11112. #if !defined(WOLFSSL_NOSHA512_224)
  11113. else if (type == WC_HASH_TYPE_SHA512_224) {
  11114. initFp = wc_InitSha512_224;
  11115. #if !defined(WOLFSSL_NO_HASH_RAW)
  11116. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11117. #else
  11118. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11119. #endif
  11120. freeFp = wc_Sha512_224Free;
  11121. }
  11122. #endif
  11123. #if !defined(WOLFSSL_NOSHA512_256)
  11124. else if (type == WC_HASH_TYPE_SHA512_256) {
  11125. initFp = wc_InitSha512_256;
  11126. #if !defined(WOLFSSL_NO_HASH_RAW)
  11127. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11128. #else
  11129. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11130. #endif
  11131. freeFp = wc_Sha512_256Free;
  11132. }
  11133. #endif
  11134. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11135. else
  11136. return TEST_FAIL;
  11137. /* Initialize */
  11138. ret = initFp(&sha512);
  11139. if (!ret) {
  11140. hash_test[0] = hash1;
  11141. hash_test[1] = hash2;
  11142. hash_test[2] = hash3;
  11143. }
  11144. times = sizeof(hash_test) / sizeof(byte *);
  11145. /* Good test args. */
  11146. for (i = 0; i < times && ret == 0; i++) {
  11147. ret = finalFp(&sha512, hash_test[i]);
  11148. }
  11149. /* Test bad args. */
  11150. if (!ret) {
  11151. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  11152. ret = WOLFSSL_FATAL_ERROR;
  11153. }
  11154. }
  11155. if (!ret) {
  11156. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  11157. ret = WOLFSSL_FATAL_ERROR;
  11158. }
  11159. }
  11160. if (!ret) {
  11161. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  11162. ret = WOLFSSL_FATAL_ERROR;
  11163. }
  11164. }
  11165. freeFp(&sha512);
  11166. return ret;
  11167. }
  11168. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11169. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11170. #endif /* WOLFSSL_SHA512 */
  11171. /*
  11172. * Unit test function for wc_Sha512Final()
  11173. */
  11174. static int test_wc_Sha512Final(void)
  11175. {
  11176. int res = TEST_SKIPPED;
  11177. #ifdef WOLFSSL_SHA512
  11178. wc_Sha512 sha512;
  11179. byte* hash_test[3];
  11180. byte hash1[WC_SHA512_DIGEST_SIZE];
  11181. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11182. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11183. int times, i, ret;
  11184. int flag = 0;
  11185. /* Initialize */
  11186. ret = wc_InitSha512(&sha512);
  11187. if (ret != 0) {
  11188. flag = ret;
  11189. }
  11190. if (!flag) {
  11191. hash_test[0] = hash1;
  11192. hash_test[1] = hash2;
  11193. hash_test[2] = hash3;
  11194. }
  11195. times = sizeof(hash_test) / sizeof(byte *);
  11196. for (i = 0; i < times; i++) {
  11197. if (!flag) {
  11198. ret = wc_Sha512Final(&sha512, hash_test[i]);
  11199. if (ret != 0) {
  11200. flag = WOLFSSL_FATAL_ERROR;
  11201. }
  11202. }
  11203. }
  11204. /* Test bad args. */
  11205. if (!flag) {
  11206. ret = wc_Sha512Final(NULL, NULL);
  11207. if (ret != BAD_FUNC_ARG) {
  11208. flag = WOLFSSL_FATAL_ERROR;
  11209. }
  11210. }
  11211. if (!flag) {
  11212. ret = wc_Sha512Final(NULL, hash1);
  11213. if (ret != BAD_FUNC_ARG) {
  11214. flag = WOLFSSL_FATAL_ERROR;
  11215. }
  11216. }
  11217. if (!flag) {
  11218. ret = wc_Sha512Final(&sha512, NULL);
  11219. if (ret != BAD_FUNC_ARG) {
  11220. flag = WOLFSSL_FATAL_ERROR;
  11221. }
  11222. }
  11223. wc_Sha512Free(&sha512);
  11224. res = TEST_RES_CHECK(flag == 0);
  11225. #endif
  11226. return res;
  11227. } /* END test_wc_Sha512Final */
  11228. /*
  11229. * Unit test function for wc_Sha512GetFlags()
  11230. */
  11231. static int test_wc_Sha512GetFlags(void)
  11232. {
  11233. int res = TEST_SKIPPED;
  11234. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11235. wc_Sha512 sha512;
  11236. word32 flags = 0;
  11237. int flag = 0;
  11238. /* Initialize */
  11239. flag = wc_InitSha512(&sha512);
  11240. if (flag == 0) {
  11241. flag = wc_Sha512GetFlags(&sha512, &flags);
  11242. }
  11243. if (flag == 0) {
  11244. if (flags & WC_HASH_FLAG_ISCOPY) {
  11245. flag = 0;
  11246. }
  11247. }
  11248. wc_Sha512Free(&sha512);
  11249. res = TEST_RES_CHECK(flag == 0);
  11250. #endif
  11251. return res;
  11252. } /* END test_wc_Sha512GetFlags */
  11253. /*
  11254. * Unit test function for wc_Sha512FinalRaw()
  11255. */
  11256. static int test_wc_Sha512FinalRaw(void)
  11257. {
  11258. int res = TEST_SKIPPED;
  11259. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11260. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11261. !defined(WOLFSSL_NO_HASH_RAW)
  11262. wc_Sha512 sha512;
  11263. byte* hash_test[3];
  11264. byte hash1[WC_SHA512_DIGEST_SIZE];
  11265. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11266. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11267. int times, i, ret;
  11268. int flag = 0;
  11269. /* Initialize */
  11270. ret = wc_InitSha512(&sha512);
  11271. if (ret != 0) {
  11272. flag = ret;
  11273. }
  11274. if (!flag) {
  11275. hash_test[0] = hash1;
  11276. hash_test[1] = hash2;
  11277. hash_test[2] = hash3;
  11278. }
  11279. times = sizeof(hash_test) / sizeof(byte*);
  11280. /* Good test args. */
  11281. for (i = 0; i < times; i++) {
  11282. if (!flag) {
  11283. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  11284. if (ret != 0) {
  11285. flag = WOLFSSL_FATAL_ERROR;
  11286. }
  11287. }
  11288. }
  11289. /* Test bad args. */
  11290. if (!flag ) {
  11291. ret = wc_Sha512FinalRaw(NULL, NULL);
  11292. if (ret != BAD_FUNC_ARG) {
  11293. flag = WOLFSSL_FATAL_ERROR;
  11294. }
  11295. }
  11296. if (!flag) {
  11297. ret = wc_Sha512FinalRaw(NULL, hash1);
  11298. if (ret != BAD_FUNC_ARG) {
  11299. flag = WOLFSSL_FATAL_ERROR;
  11300. }
  11301. }
  11302. if (!flag) {
  11303. ret = wc_Sha512FinalRaw(&sha512, NULL);
  11304. if (ret != BAD_FUNC_ARG) {
  11305. flag = WOLFSSL_FATAL_ERROR;
  11306. }
  11307. }
  11308. wc_Sha512Free(&sha512);
  11309. res = TEST_RES_CHECK(flag == 0);
  11310. #endif
  11311. return res;
  11312. } /* END test_wc_Sha512FinalRaw */
  11313. /*
  11314. * Unit test function for wc_Sha512Free()
  11315. */
  11316. static int test_wc_Sha512Free(void)
  11317. {
  11318. int res = TEST_SKIPPED;
  11319. #ifdef WOLFSSL_SHA512
  11320. wc_Sha512Free(NULL);
  11321. res = TEST_RES_CHECK(1);
  11322. #endif
  11323. return res;
  11324. } /* END test_wc_Sha512Free */
  11325. #ifdef WOLFSSL_SHA512
  11326. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11327. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11328. static int test_Sha512_Family_GetHash(int type )
  11329. {
  11330. int flag = 0;
  11331. int(*initFp)(wc_Sha512*);
  11332. int(*ghashFp)(wc_Sha512*, byte*);
  11333. wc_Sha512 sha512;
  11334. byte hash1[WC_SHA512_DIGEST_SIZE];
  11335. if (type == WC_HASH_TYPE_SHA512) {
  11336. initFp = wc_InitSha512;
  11337. ghashFp = wc_Sha512GetHash;
  11338. }
  11339. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11340. #if !defined(WOLFSSL_NOSHA512_224)
  11341. else if (type == WC_HASH_TYPE_SHA512_224) {
  11342. initFp = wc_InitSha512_224;
  11343. ghashFp = wc_Sha512_224GetHash;
  11344. }
  11345. #endif
  11346. #if !defined(WOLFSSL_NOSHA512_256)
  11347. else if (type == WC_HASH_TYPE_SHA512_256) {
  11348. initFp = wc_InitSha512_256;
  11349. ghashFp = wc_Sha512_256GetHash;
  11350. }
  11351. #endif
  11352. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11353. else {
  11354. initFp = NULL;
  11355. ghashFp = NULL;
  11356. }
  11357. if (initFp == NULL || ghashFp == NULL)
  11358. return TEST_FAIL;
  11359. if (!flag) {
  11360. flag = initFp(&sha512);
  11361. }
  11362. if (!flag) {
  11363. flag = ghashFp(&sha512, hash1);
  11364. }
  11365. /*test bad arguments*/
  11366. if (!flag) {
  11367. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  11368. flag = WOLFSSL_FATAL_ERROR;
  11369. }
  11370. if (!flag) {
  11371. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  11372. flag = WOLFSSL_FATAL_ERROR;
  11373. }
  11374. if (!flag) {
  11375. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  11376. flag = WOLFSSL_FATAL_ERROR;
  11377. }
  11378. wc_Sha512Free(&sha512);
  11379. return flag;
  11380. }
  11381. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11382. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11383. #endif /* WOLFSSL_SHA512 */
  11384. /*
  11385. * Unit test function for wc_Sha512GetHash()
  11386. */
  11387. static int test_wc_Sha512GetHash(void)
  11388. {
  11389. int res = TEST_SKIPPED;
  11390. #ifdef WOLFSSL_SHA512
  11391. wc_Sha512 sha512;
  11392. byte hash1[WC_SHA512_DIGEST_SIZE];
  11393. int flag = 0;
  11394. /* Initialize */
  11395. flag = wc_InitSha512(&sha512);
  11396. if (flag == 0) {
  11397. flag = wc_Sha512GetHash(&sha512, hash1);
  11398. }
  11399. /*test bad arguments*/
  11400. if (flag == 0) {
  11401. flag = wc_Sha512GetHash(NULL, NULL);
  11402. if (flag == BAD_FUNC_ARG) {
  11403. flag = 0;
  11404. }
  11405. }
  11406. if (flag == 0) {
  11407. flag = wc_Sha512GetHash(NULL, hash1);
  11408. if (flag == BAD_FUNC_ARG) {
  11409. flag = 0;
  11410. }
  11411. }
  11412. if (flag == 0) {
  11413. flag = wc_Sha512GetHash(&sha512, NULL);
  11414. if (flag == BAD_FUNC_ARG) {
  11415. flag = 0;
  11416. }
  11417. }
  11418. wc_Sha512Free(&sha512);
  11419. res = TEST_RES_CHECK(flag == 0);
  11420. #endif
  11421. return res;
  11422. } /* END test_wc_Sha512GetHash */
  11423. /*
  11424. * Unit test function for wc_Sha512Copy()
  11425. */
  11426. static int test_wc_Sha512Copy(void)
  11427. {
  11428. int res = TEST_SKIPPED;
  11429. #ifdef WOLFSSL_SHA512
  11430. wc_Sha512 sha512;
  11431. wc_Sha512 temp;
  11432. int flag;
  11433. /* Initialize */
  11434. flag = wc_InitSha512(&sha512);
  11435. if (flag == 0) {
  11436. flag = wc_InitSha512(&temp);
  11437. }
  11438. if (flag == 0) {
  11439. flag = wc_Sha512Copy(&sha512, &temp);
  11440. }
  11441. /*test bad arguments*/
  11442. if (flag == 0) {
  11443. flag = wc_Sha512Copy(NULL, NULL);
  11444. if (flag == BAD_FUNC_ARG) {
  11445. flag = 0;
  11446. }
  11447. }
  11448. if (flag == 0) {
  11449. flag = wc_Sha512Copy(NULL, &temp);
  11450. if (flag == BAD_FUNC_ARG) {
  11451. flag = 0;
  11452. }
  11453. }
  11454. if (flag == 0) {
  11455. flag = wc_Sha512Copy(&sha512, NULL);
  11456. if (flag == BAD_FUNC_ARG) {
  11457. flag = 0;
  11458. }
  11459. }
  11460. wc_Sha512Free(&sha512);
  11461. wc_Sha512Free(&temp);
  11462. res = TEST_RES_CHECK(flag == 0);
  11463. #endif
  11464. return res;
  11465. } /* END test_wc_Sha512Copy */
  11466. static int test_wc_InitSha512_224(void)
  11467. {
  11468. int res = TEST_SKIPPED;
  11469. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11470. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11471. wc_Sha512 sha512;
  11472. int ret;
  11473. int flag = 0;
  11474. /* Test good arg. */
  11475. ret = wc_InitSha512_224(&sha512);
  11476. if (ret != 0) {
  11477. flag = WOLFSSL_FATAL_ERROR;
  11478. }
  11479. /* Test bad arg. */
  11480. if (!flag) {
  11481. ret = wc_InitSha512_224(NULL);
  11482. if (ret != BAD_FUNC_ARG) {
  11483. flag = WOLFSSL_FATAL_ERROR;
  11484. }
  11485. }
  11486. wc_Sha512_224Free(&sha512);
  11487. res = TEST_RES_CHECK(flag == 0);
  11488. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11489. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11490. return res;
  11491. }
  11492. static int test_wc_Sha512_224Update(void)
  11493. {
  11494. int res = TEST_SKIPPED;
  11495. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11496. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11497. wc_Sha512 sha512;
  11498. byte hash[WC_SHA512_DIGEST_SIZE];
  11499. testVector a, c;
  11500. int ret;
  11501. int flag = 0;
  11502. ret = wc_InitSha512_224(&sha512);
  11503. if (ret != 0) {
  11504. flag = ret;
  11505. }
  11506. /* Input. */
  11507. if (!flag) {
  11508. a.input = "a";
  11509. a.inLen = XSTRLEN(a.input);
  11510. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  11511. if (ret != 0) {
  11512. flag = ret;
  11513. }
  11514. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  11515. if (ret != 0) {
  11516. flag = ret;
  11517. }
  11518. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  11519. if (ret != 0) {
  11520. flag = ret;
  11521. }
  11522. ret = wc_Sha512_224Final(&sha512, hash);
  11523. if (ret != 0) {
  11524. flag = ret;
  11525. }
  11526. }
  11527. /* Update input. */
  11528. if (!flag) {
  11529. a.input = "abc";
  11530. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11531. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11532. a.inLen = XSTRLEN(a.input);
  11533. a.outLen = XSTRLEN(a.output);
  11534. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  11535. if (ret != 0) {
  11536. flag = ret;
  11537. }
  11538. }
  11539. if (!flag) {
  11540. ret = wc_Sha512_224Final(&sha512, hash);
  11541. if (ret != 0) {
  11542. flag = ret;
  11543. }
  11544. }
  11545. if (!flag) {
  11546. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  11547. flag = WOLFSSL_FATAL_ERROR;
  11548. }
  11549. }
  11550. if (!flag) {
  11551. c.input = NULL;
  11552. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11553. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11554. if (ret != BAD_FUNC_ARG) {
  11555. flag = WOLFSSL_FATAL_ERROR;
  11556. }
  11557. }
  11558. if (!flag) {
  11559. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11560. if (ret != BAD_FUNC_ARG) {
  11561. flag = WOLFSSL_FATAL_ERROR;
  11562. }
  11563. }
  11564. wc_Sha512_224Free(&sha512);
  11565. res = TEST_RES_CHECK(flag == 0);
  11566. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11567. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11568. return res;
  11569. }
  11570. static int test_wc_Sha512_224Final(void)
  11571. {
  11572. int res = TEST_SKIPPED;
  11573. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11574. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11575. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  11576. res = TEST_RES_CHECK(ret == 0);
  11577. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11578. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11579. return res;
  11580. }
  11581. static int test_wc_Sha512_224GetFlags(void)
  11582. {
  11583. int res = TEST_SKIPPED;
  11584. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11585. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11586. wc_Sha512 sha512, copy;
  11587. word32 flags = 0;
  11588. int flag = 0;
  11589. /* Initialize */
  11590. flag = wc_InitSha512_224(&sha512);
  11591. if (!flag) {
  11592. flag = wc_InitSha512_224(&copy);
  11593. }
  11594. if (!flag) {
  11595. flag = wc_Sha512_224Copy(&sha512, &copy);
  11596. }
  11597. if (!flag) {
  11598. flag = wc_Sha512_224GetFlags(&copy, &flags);
  11599. }
  11600. if (!flag) {
  11601. if (flags & WC_HASH_FLAG_ISCOPY)
  11602. flag = 0;
  11603. else
  11604. flag = WOLFSSL_FATAL_ERROR;
  11605. }
  11606. wc_Sha512_224Free(&copy);
  11607. wc_Sha512_224Free(&sha512);
  11608. res = TEST_RES_CHECK(flag == 0);
  11609. #endif
  11610. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11611. return res;
  11612. }
  11613. static int test_wc_Sha512_224FinalRaw(void)
  11614. {
  11615. int res = TEST_SKIPPED;
  11616. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11617. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11618. !defined(WOLFSSL_NO_HASH_RAW)
  11619. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  11620. res = TEST_RES_CHECK(ret == 0);
  11621. #endif
  11622. return res;
  11623. }
  11624. static int test_wc_Sha512_224Free(void)
  11625. {
  11626. int res = TEST_SKIPPED;
  11627. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11628. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11629. wc_Sha512_224Free(NULL);
  11630. res = TEST_RES_CHECK(1);
  11631. #endif
  11632. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11633. return res;
  11634. }
  11635. static int test_wc_Sha512_224GetHash(void)
  11636. {
  11637. int res = TEST_SKIPPED;
  11638. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11639. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11640. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  11641. res = TEST_RES_CHECK(ret == 0);
  11642. #endif
  11643. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11644. return res;
  11645. }
  11646. static int test_wc_Sha512_224Copy(void)
  11647. {
  11648. int res = TEST_SKIPPED;
  11649. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11650. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11651. wc_Sha512 sha512;
  11652. wc_Sha512 temp;
  11653. int flag = 0;
  11654. /* Initialize */
  11655. flag = wc_InitSha512_224(&sha512);
  11656. if (flag == 0) {
  11657. flag = wc_InitSha512_224(&temp);
  11658. }
  11659. if (flag == 0) {
  11660. flag = wc_Sha512_224Copy(&sha512, &temp);
  11661. }
  11662. /*test bad arguments*/
  11663. if (flag == 0) {
  11664. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  11665. flag = WOLFSSL_FATAL_ERROR;
  11666. }
  11667. if (flag == 0) {
  11668. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  11669. flag = WOLFSSL_FATAL_ERROR;
  11670. }
  11671. if (flag == 0) {
  11672. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11673. flag = WOLFSSL_FATAL_ERROR;
  11674. }
  11675. wc_Sha512_224Free(&sha512);
  11676. wc_Sha512_224Free(&temp);
  11677. res = TEST_RES_CHECK(flag == 0);
  11678. #endif
  11679. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11680. return res;
  11681. }
  11682. static int test_wc_InitSha512_256(void)
  11683. {
  11684. int res = TEST_SKIPPED;
  11685. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11686. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11687. wc_Sha512 sha512;
  11688. int ret;
  11689. int flag = 0;
  11690. /* Test good arg. */
  11691. ret = wc_InitSha512_256(&sha512);
  11692. if (ret != 0) {
  11693. flag = WOLFSSL_FATAL_ERROR;
  11694. }
  11695. /* Test bad arg. */
  11696. if (!flag) {
  11697. ret = wc_InitSha512_256(NULL);
  11698. if (ret != BAD_FUNC_ARG) {
  11699. flag = WOLFSSL_FATAL_ERROR;
  11700. }
  11701. }
  11702. wc_Sha512_256Free(&sha512);
  11703. res = TEST_RES_CHECK(flag == 0);
  11704. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11705. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11706. return res;
  11707. }
  11708. static int test_wc_Sha512_256Update(void)
  11709. {
  11710. int res = TEST_SKIPPED;
  11711. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11712. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11713. wc_Sha512 sha512;
  11714. byte hash[WC_SHA512_DIGEST_SIZE];
  11715. testVector a, c;
  11716. int ret;
  11717. int flag = 0;
  11718. ret = wc_InitSha512_256(&sha512);
  11719. if (ret != 0) {
  11720. flag = ret;
  11721. }
  11722. /* Input. */
  11723. if (!flag) {
  11724. a.input = "a";
  11725. a.inLen = XSTRLEN(a.input);
  11726. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  11727. if (ret != 0) {
  11728. flag = ret;
  11729. }
  11730. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  11731. if (ret != 0) {
  11732. flag = ret;
  11733. }
  11734. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  11735. if (ret != 0) {
  11736. flag = ret;
  11737. }
  11738. ret = wc_Sha512_256Final(&sha512, hash);
  11739. if (ret != 0) {
  11740. flag = ret;
  11741. }
  11742. }
  11743. /* Update input. */
  11744. if (!flag) {
  11745. a.input = "abc";
  11746. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11747. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11748. "\x07\xe7\xaf\x23";
  11749. a.inLen = XSTRLEN(a.input);
  11750. a.outLen = XSTRLEN(a.output);
  11751. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  11752. if (ret != 0) {
  11753. flag = ret;
  11754. }
  11755. }
  11756. if (!flag) {
  11757. ret = wc_Sha512_256Final(&sha512, hash);
  11758. if (ret != 0) {
  11759. flag = ret;
  11760. }
  11761. }
  11762. if (!flag) {
  11763. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  11764. flag = WOLFSSL_FATAL_ERROR;
  11765. }
  11766. }
  11767. if (!flag) {
  11768. c.input = NULL;
  11769. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11770. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11771. if (ret != BAD_FUNC_ARG) {
  11772. flag = WOLFSSL_FATAL_ERROR;
  11773. }
  11774. }
  11775. if (!flag) {
  11776. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  11777. if (ret != BAD_FUNC_ARG) {
  11778. flag = WOLFSSL_FATAL_ERROR;
  11779. }
  11780. }
  11781. wc_Sha512_256Free(&sha512);
  11782. res = TEST_RES_CHECK(flag == 0);
  11783. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11784. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11785. return res;
  11786. }
  11787. static int test_wc_Sha512_256Final(void)
  11788. {
  11789. int res = TEST_SKIPPED;
  11790. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11791. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11792. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  11793. res = TEST_RES_CHECK(ret == 0);
  11794. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11795. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11796. return res;
  11797. }
  11798. static int test_wc_Sha512_256GetFlags(void)
  11799. {
  11800. int res = TEST_SKIPPED;
  11801. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11802. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11803. wc_Sha512 sha512, copy;
  11804. word32 flags = 0;
  11805. int flag = 0;
  11806. /* Initialize */
  11807. flag = wc_InitSha512_256(&sha512);
  11808. if (!flag ) {
  11809. flag = wc_InitSha512_256(&copy);
  11810. }
  11811. if (!flag ) {
  11812. flag = wc_Sha512_256Copy(&sha512, &copy);
  11813. }
  11814. if (!flag ) {
  11815. flag = wc_Sha512_256GetFlags(&copy, &flags);
  11816. }
  11817. if (!flag) {
  11818. if (flags & WC_HASH_FLAG_ISCOPY)
  11819. flag = 0;
  11820. else
  11821. flag = WOLFSSL_FATAL_ERROR;
  11822. }
  11823. wc_Sha512_256Free(&sha512);
  11824. res = TEST_RES_CHECK(flag == 0);
  11825. #endif
  11826. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11827. return res;
  11828. }
  11829. static int test_wc_Sha512_256FinalRaw(void)
  11830. {
  11831. int res = TEST_SKIPPED;
  11832. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11833. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11834. !defined(WOLFSSL_NO_HASH_RAW)
  11835. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  11836. res = TEST_RES_CHECK(ret == 0);
  11837. #endif
  11838. return res;
  11839. }
  11840. static int test_wc_Sha512_256Free(void)
  11841. {
  11842. int res = TEST_SKIPPED;
  11843. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11844. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11845. wc_Sha512_256Free(NULL);
  11846. res = TEST_RES_CHECK(1);
  11847. #endif
  11848. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11849. return res;
  11850. }
  11851. static int test_wc_Sha512_256GetHash(void)
  11852. {
  11853. int res = TEST_SKIPPED;
  11854. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11855. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11856. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  11857. res = TEST_RES_CHECK(ret == 0);
  11858. #endif
  11859. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11860. return res;
  11861. }
  11862. static int test_wc_Sha512_256Copy(void)
  11863. {
  11864. int res = TEST_SKIPPED;
  11865. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11866. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11867. wc_Sha512 sha512;
  11868. wc_Sha512 temp;
  11869. int flag = 0;
  11870. /* Initialize */
  11871. flag = wc_InitSha512_256(&sha512);
  11872. if (flag == 0) {
  11873. flag = wc_InitSha512_256(&temp);
  11874. }
  11875. if (flag == 0) {
  11876. flag = wc_Sha512_256Copy(&sha512, &temp);
  11877. }
  11878. /*test bad arguments*/
  11879. if (flag == 0) {
  11880. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  11881. flag = WOLFSSL_FATAL_ERROR;
  11882. }
  11883. if (flag == 0) {
  11884. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  11885. flag = WOLFSSL_FATAL_ERROR;
  11886. }
  11887. if (flag == 0) {
  11888. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11889. flag = WOLFSSL_FATAL_ERROR;
  11890. }
  11891. wc_Sha512_256Free(&sha512);
  11892. wc_Sha512_256Free(&temp);
  11893. res = TEST_RES_CHECK(flag == 0);
  11894. #endif
  11895. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11896. return res;
  11897. }
  11898. /*
  11899. * Testing wc_InitSha384()
  11900. */
  11901. static int test_wc_InitSha384(void)
  11902. {
  11903. int res = TEST_SKIPPED;
  11904. #ifdef WOLFSSL_SHA384
  11905. wc_Sha384 sha384;
  11906. int ret;
  11907. int flag = 0;
  11908. /* Test good arg. */
  11909. ret = wc_InitSha384(&sha384);
  11910. if (ret != 0) {
  11911. flag = WOLFSSL_FATAL_ERROR;
  11912. }
  11913. /* Test bad arg. */
  11914. if (!flag) {
  11915. ret = wc_InitSha384(NULL);
  11916. if (ret != BAD_FUNC_ARG) {
  11917. flag = WOLFSSL_FATAL_ERROR;
  11918. }
  11919. }
  11920. wc_Sha384Free(&sha384);
  11921. res = TEST_RES_CHECK(flag == 0);
  11922. #endif
  11923. return res;
  11924. } /* END test_wc_InitSha384 */
  11925. /*
  11926. * test wc_Sha384Update()
  11927. */
  11928. static int test_wc_Sha384Update(void)
  11929. {
  11930. int res = TEST_SKIPPED;
  11931. #ifdef WOLFSSL_SHA384
  11932. wc_Sha384 sha384;
  11933. byte hash[WC_SHA384_DIGEST_SIZE];
  11934. testVector a, b, c;
  11935. int ret;
  11936. int flag = 0;
  11937. ret = wc_InitSha384(&sha384);
  11938. if (ret != 0) {
  11939. flag = ret;
  11940. }
  11941. /* Input */
  11942. if (!flag) {
  11943. a.input = "a";
  11944. a.inLen = XSTRLEN(a.input);
  11945. ret = wc_Sha384Update(&sha384, NULL, 0);
  11946. if (ret != 0) {
  11947. flag = ret;
  11948. }
  11949. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  11950. if (ret != 0) {
  11951. flag = ret;
  11952. }
  11953. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11954. if (ret != 0) {
  11955. flag = ret;
  11956. }
  11957. }
  11958. if (!flag) {
  11959. ret = wc_Sha384Final(&sha384, hash);
  11960. if (ret != 0) {
  11961. flag = ret;
  11962. }
  11963. }
  11964. /* Update input. */
  11965. if (!flag) {
  11966. a.input = "abc";
  11967. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11968. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11969. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11970. "\xc8\x25\xa7";
  11971. a.inLen = XSTRLEN(a.input);
  11972. a.outLen = XSTRLEN(a.output);
  11973. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11974. if (ret != 0) {
  11975. flag = ret;
  11976. }
  11977. }
  11978. if (!flag) {
  11979. ret = wc_Sha384Final(&sha384, hash);
  11980. if (ret != 0) {
  11981. flag = ret;
  11982. }
  11983. }
  11984. if (!flag) {
  11985. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  11986. flag = WOLFSSL_FATAL_ERROR;
  11987. }
  11988. }
  11989. /* Pass in bad values. */
  11990. if (!flag) {
  11991. b.input = NULL;
  11992. b.inLen = 0;
  11993. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  11994. if (ret != 0) {
  11995. flag = ret;
  11996. }
  11997. }
  11998. if (!flag) {
  11999. c.input = NULL;
  12000. c.inLen = WC_SHA384_DIGEST_SIZE;
  12001. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  12002. if (ret != BAD_FUNC_ARG) {
  12003. flag = WOLFSSL_FATAL_ERROR;
  12004. }
  12005. }
  12006. if (!flag) {
  12007. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  12008. if (ret != BAD_FUNC_ARG) {
  12009. flag = WOLFSSL_FATAL_ERROR;
  12010. }
  12011. }
  12012. wc_Sha384Free(&sha384);
  12013. res = TEST_RES_CHECK(flag == 0);
  12014. #endif
  12015. return res;
  12016. } /* END test_wc_Sha384Update */
  12017. /*
  12018. * Unit test function for wc_Sha384Final();
  12019. */
  12020. static int test_wc_Sha384Final(void)
  12021. {
  12022. int res = TEST_SKIPPED;
  12023. #ifdef WOLFSSL_SHA384
  12024. wc_Sha384 sha384;
  12025. byte* hash_test[3];
  12026. byte hash1[WC_SHA384_DIGEST_SIZE];
  12027. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12028. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12029. int times, i, ret;
  12030. int flag = 0;
  12031. /* Initialize */
  12032. ret = wc_InitSha384(&sha384);
  12033. if (ret) {
  12034. flag = ret;
  12035. }
  12036. if (!flag) {
  12037. hash_test[0] = hash1;
  12038. hash_test[1] = hash2;
  12039. hash_test[2] = hash3;
  12040. }
  12041. times = sizeof(hash_test) / sizeof(byte*);
  12042. /* Good test args. */
  12043. for (i = 0; i < times; i++) {
  12044. if (!flag) {
  12045. ret = wc_Sha384Final(&sha384, hash_test[i]);
  12046. if (ret != 0) {
  12047. flag = WOLFSSL_FATAL_ERROR;
  12048. }
  12049. }
  12050. }
  12051. /* Test bad args. */
  12052. if (!flag) {
  12053. ret = wc_Sha384Final(NULL, NULL);
  12054. if (ret != BAD_FUNC_ARG) {
  12055. flag = WOLFSSL_FATAL_ERROR;
  12056. }
  12057. }
  12058. if (!flag) {
  12059. ret = wc_Sha384Final(NULL, hash1);
  12060. if (ret != BAD_FUNC_ARG) {
  12061. flag = WOLFSSL_FATAL_ERROR;
  12062. }
  12063. }
  12064. if (!flag) {
  12065. ret = wc_Sha384Final(&sha384, NULL);
  12066. if (ret != BAD_FUNC_ARG) {
  12067. flag = WOLFSSL_FATAL_ERROR;
  12068. }
  12069. }
  12070. wc_Sha384Free(&sha384);
  12071. res = TEST_RES_CHECK(flag == 0);
  12072. #endif
  12073. return res;
  12074. } /* END test_wc_Sha384Final */
  12075. /*
  12076. * Unit test function for wc_Sha384GetFlags()
  12077. */
  12078. static int test_wc_Sha384GetFlags(void)
  12079. {
  12080. int res = TEST_SKIPPED;
  12081. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12082. wc_Sha384 sha384;
  12083. word32 flags = 0;
  12084. int flag = 0;
  12085. /* Initialize */
  12086. flag = wc_InitSha384(&sha384);
  12087. if (flag == 0) {
  12088. flag = wc_Sha384GetFlags(&sha384, &flags);
  12089. }
  12090. if (flag == 0) {
  12091. if (flags & WC_HASH_FLAG_ISCOPY) {
  12092. flag = 0;
  12093. }
  12094. }
  12095. wc_Sha384Free(&sha384);
  12096. res = TEST_RES_CHECK(flag == 0);
  12097. #endif
  12098. return res;
  12099. } /* END test_wc_Sha384GetFlags */
  12100. /*
  12101. * Unit test function for wc_Sha384FinalRaw()
  12102. */
  12103. static int test_wc_Sha384FinalRaw(void)
  12104. {
  12105. int res = TEST_SKIPPED;
  12106. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12107. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12108. !defined(WOLFSSL_NO_HASH_RAW)
  12109. wc_Sha384 sha384;
  12110. byte* hash_test[3];
  12111. byte hash1[WC_SHA384_DIGEST_SIZE];
  12112. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12113. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12114. int times, i, ret;
  12115. int flag = 0;
  12116. /* Initialize */
  12117. ret = wc_InitSha384(&sha384);
  12118. if (ret != 0) {
  12119. flag = ret;
  12120. }
  12121. if (!flag) {
  12122. hash_test[0] = hash1;
  12123. hash_test[1] = hash2;
  12124. hash_test[2] = hash3;
  12125. }
  12126. times = sizeof(hash_test) / sizeof(byte*);
  12127. /* Good test args. */
  12128. for (i = 0; i < times; i++) {
  12129. if (!flag) {
  12130. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  12131. if (ret != 0) {
  12132. flag = WOLFSSL_FATAL_ERROR;
  12133. }
  12134. }
  12135. }
  12136. /* Test bad args. */
  12137. if (!flag ) {
  12138. ret = wc_Sha384FinalRaw(NULL, NULL);
  12139. if (ret != BAD_FUNC_ARG) {
  12140. flag = WOLFSSL_FATAL_ERROR;
  12141. }
  12142. }
  12143. if (!flag) {
  12144. ret = wc_Sha384FinalRaw(NULL, hash1);
  12145. if (ret != BAD_FUNC_ARG) {
  12146. flag = WOLFSSL_FATAL_ERROR;
  12147. }
  12148. }
  12149. if (!flag) {
  12150. ret = wc_Sha384FinalRaw(&sha384, NULL);
  12151. if (ret != BAD_FUNC_ARG) {
  12152. flag = WOLFSSL_FATAL_ERROR;
  12153. }
  12154. }
  12155. wc_Sha384Free(&sha384);
  12156. res = TEST_RES_CHECK(flag == 0);
  12157. #endif
  12158. return res;
  12159. } /* END test_wc_Sha384FinalRaw */
  12160. /*
  12161. * Unit test function for wc_Sha384Free()
  12162. */
  12163. static int test_wc_Sha384Free(void)
  12164. {
  12165. int res = TEST_SKIPPED;
  12166. #ifdef WOLFSSL_SHA384
  12167. wc_Sha384Free(NULL);
  12168. res = TEST_RES_CHECK(1);
  12169. #endif
  12170. return res;
  12171. } /* END test_wc_Sha384Free */
  12172. /*
  12173. * Unit test function for wc_Sha384GetHash()
  12174. */
  12175. static int test_wc_Sha384GetHash(void)
  12176. {
  12177. int res = TEST_SKIPPED;
  12178. #ifdef WOLFSSL_SHA384
  12179. wc_Sha384 sha384;
  12180. byte hash1[WC_SHA384_DIGEST_SIZE];
  12181. int flag = 0;
  12182. /* Initialize */
  12183. flag = wc_InitSha384(&sha384);
  12184. if (flag == 0) {
  12185. flag = wc_Sha384GetHash(&sha384, hash1);
  12186. }
  12187. /*test bad arguments*/
  12188. if (flag == 0) {
  12189. flag = wc_Sha384GetHash(NULL, NULL);
  12190. if (flag == BAD_FUNC_ARG) {
  12191. flag = 0;
  12192. }
  12193. }
  12194. if (flag == 0) {
  12195. flag = wc_Sha384GetHash(NULL, hash1);
  12196. if (flag == BAD_FUNC_ARG) {
  12197. flag = 0;
  12198. }
  12199. }
  12200. if (flag == 0) {
  12201. flag = wc_Sha384GetHash(&sha384, NULL);
  12202. if (flag == BAD_FUNC_ARG) {
  12203. flag = 0;
  12204. }
  12205. }
  12206. wc_Sha384Free(&sha384);
  12207. res = TEST_RES_CHECK(flag == 0);
  12208. #endif
  12209. return res;
  12210. } /* END test_wc_Sha384GetHash */
  12211. /*
  12212. * Unit test function for wc_Sha384Copy()
  12213. */
  12214. static int test_wc_Sha384Copy(void)
  12215. {
  12216. int res = TEST_SKIPPED;
  12217. #ifdef WOLFSSL_SHA384
  12218. wc_Sha384 sha384;
  12219. wc_Sha384 temp;
  12220. int flag = 0;
  12221. /* Initialize */
  12222. flag = wc_InitSha384(&sha384);
  12223. if (flag == 0) {
  12224. flag = wc_InitSha384(&temp);
  12225. }
  12226. if (flag == 0) {
  12227. flag = wc_Sha384Copy(&sha384, &temp);
  12228. }
  12229. /*test bad arguments*/
  12230. if (flag == 0) {
  12231. flag = wc_Sha384Copy(NULL, NULL);
  12232. if (flag == BAD_FUNC_ARG) {
  12233. flag = 0;
  12234. }
  12235. }
  12236. if (flag == 0) {
  12237. flag = wc_Sha384Copy(NULL, &temp);
  12238. if (flag == BAD_FUNC_ARG) {
  12239. flag = 0;
  12240. }
  12241. }
  12242. if (flag == 0) {
  12243. flag = wc_Sha384Copy(&sha384, NULL);
  12244. if (flag == BAD_FUNC_ARG) {
  12245. flag = 0;
  12246. }
  12247. }
  12248. wc_Sha384Free(&sha384);
  12249. wc_Sha384Free(&temp);
  12250. res = TEST_RES_CHECK(flag == 0);
  12251. #endif
  12252. return res;
  12253. } /* END test_wc_Sha384Copy */
  12254. /*
  12255. * Testing wc_InitSha224();
  12256. */
  12257. static int test_wc_InitSha224(void)
  12258. {
  12259. int res = TEST_SKIPPED;
  12260. #ifdef WOLFSSL_SHA224
  12261. wc_Sha224 sha224;
  12262. int ret;
  12263. int flag = 0;
  12264. /* Test good arg. */
  12265. ret = wc_InitSha224(&sha224);
  12266. if (ret != 0) {
  12267. flag = WOLFSSL_FATAL_ERROR;
  12268. }
  12269. /* Test bad arg. */
  12270. if (!flag) {
  12271. ret = wc_InitSha224(NULL);
  12272. if (ret != BAD_FUNC_ARG) {
  12273. flag = WOLFSSL_FATAL_ERROR;
  12274. }
  12275. }
  12276. wc_Sha224Free(&sha224);
  12277. res = TEST_RES_CHECK(flag == 0);
  12278. #endif
  12279. return res;
  12280. } /* END test_wc_InitSha224 */
  12281. /*
  12282. * Unit test on wc_Sha224Update
  12283. */
  12284. static int test_wc_Sha224Update(void)
  12285. {
  12286. int res = TEST_SKIPPED;
  12287. #ifdef WOLFSSL_SHA224
  12288. wc_Sha224 sha224;
  12289. byte hash[WC_SHA224_DIGEST_SIZE];
  12290. testVector a, b, c;
  12291. int ret;
  12292. int flag = 0;
  12293. ret = wc_InitSha224(&sha224);
  12294. if (ret != 0) {
  12295. flag = ret;
  12296. }
  12297. /* Input. */
  12298. if (!flag) {
  12299. a.input = "a";
  12300. a.inLen = XSTRLEN(a.input);
  12301. ret = wc_Sha224Update(&sha224, NULL, 0);
  12302. if (ret != 0) {
  12303. flag = ret;
  12304. }
  12305. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  12306. if (ret != 0) {
  12307. flag = ret;
  12308. }
  12309. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  12310. if (ret != 0) {
  12311. flag = ret;
  12312. }
  12313. }
  12314. if (!flag) {
  12315. ret = wc_Sha224Final(&sha224, hash);
  12316. if (ret != 0) {
  12317. flag = ret;
  12318. }
  12319. }
  12320. /* Update input. */
  12321. if (!flag) {
  12322. a.input = "abc";
  12323. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12324. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12325. a.inLen = XSTRLEN(a.input);
  12326. a.outLen = XSTRLEN(a.output);
  12327. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  12328. if (ret != 0) {
  12329. flag = ret;
  12330. }
  12331. }
  12332. if (!flag) {
  12333. ret = wc_Sha224Final(&sha224, hash);
  12334. if (ret != 0) {
  12335. flag = ret;
  12336. }
  12337. }
  12338. if (!flag) {
  12339. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  12340. flag = WOLFSSL_FATAL_ERROR;
  12341. }
  12342. }
  12343. /* Pass in bad values. */
  12344. if (!flag) {
  12345. b.input = NULL;
  12346. b.inLen = 0;
  12347. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  12348. if (ret != 0) {
  12349. flag = ret;
  12350. }
  12351. }
  12352. if (!flag) {
  12353. c.input = NULL;
  12354. c.inLen = WC_SHA224_DIGEST_SIZE;
  12355. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  12356. if (ret != BAD_FUNC_ARG) {
  12357. flag = WOLFSSL_FATAL_ERROR;
  12358. }
  12359. }
  12360. if (!flag) {
  12361. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  12362. if (ret != BAD_FUNC_ARG) {
  12363. flag = WOLFSSL_FATAL_ERROR;
  12364. }
  12365. }
  12366. wc_Sha224Free(&sha224);
  12367. res = TEST_RES_CHECK(flag == 0);
  12368. #endif
  12369. return res;
  12370. } /* END test_wc_Sha224Update */
  12371. /*
  12372. * Unit test for wc_Sha224Final();
  12373. */
  12374. static int test_wc_Sha224Final(void)
  12375. {
  12376. int res = TEST_SKIPPED;
  12377. #ifdef WOLFSSL_SHA224
  12378. wc_Sha224 sha224;
  12379. byte* hash_test[3];
  12380. byte hash1[WC_SHA224_DIGEST_SIZE];
  12381. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12382. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12383. int times, i, ret;
  12384. int flag = 0;
  12385. /* Initialize */
  12386. ret = wc_InitSha224(&sha224);
  12387. if (ret) {
  12388. flag = ret;
  12389. }
  12390. if (!flag) {
  12391. hash_test[0] = hash1;
  12392. hash_test[1] = hash2;
  12393. hash_test[2] = hash3;
  12394. }
  12395. times = sizeof(hash_test) / sizeof(byte*);
  12396. /* Good test args. */
  12397. /* Testing oversized buffers. */
  12398. for (i = 0; i < times; i++) {
  12399. if (!flag) {
  12400. ret = wc_Sha224Final(&sha224, hash_test[i]);
  12401. if (ret != 0) {
  12402. flag = WOLFSSL_FATAL_ERROR;
  12403. }
  12404. }
  12405. }
  12406. /* Test bad args. */
  12407. if (!flag) {
  12408. ret = wc_Sha224Final(NULL, NULL);
  12409. if (ret != BAD_FUNC_ARG) {
  12410. flag = WOLFSSL_FATAL_ERROR;
  12411. }
  12412. }
  12413. if (!flag) {
  12414. ret = wc_Sha224Final(NULL, hash1);
  12415. if (ret != BAD_FUNC_ARG) {
  12416. flag = WOLFSSL_FATAL_ERROR;
  12417. }
  12418. }
  12419. if (!flag) {
  12420. ret = wc_Sha224Final(&sha224, NULL);
  12421. if (ret != BAD_FUNC_ARG) {
  12422. flag = WOLFSSL_FATAL_ERROR;
  12423. }
  12424. }
  12425. wc_Sha224Free(&sha224);
  12426. res = TEST_RES_CHECK(flag == 0);
  12427. #endif
  12428. return res;
  12429. } /* END test_wc_Sha224Final */
  12430. /*
  12431. * Unit test function for wc_Sha224SetFlags()
  12432. */
  12433. static int test_wc_Sha224SetFlags(void)
  12434. {
  12435. int res = TEST_SKIPPED;
  12436. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12437. wc_Sha224 sha224;
  12438. word32 flags = 0;
  12439. int flag = 0;
  12440. /* Initialize */
  12441. flag = wc_InitSha224(&sha224);
  12442. if (flag == 0) {
  12443. flag = wc_Sha224SetFlags(&sha224, flags);
  12444. }
  12445. if (flag == 0) {
  12446. if (flags & WC_HASH_FLAG_ISCOPY) {
  12447. flag = 0;
  12448. }
  12449. }
  12450. wc_Sha224Free(&sha224);
  12451. res = TEST_RES_CHECK(flag == 0);
  12452. #endif
  12453. return res;
  12454. } /* END test_wc_Sha224SetFlags */
  12455. /*
  12456. * Unit test function for wc_Sha224GetFlags()
  12457. */
  12458. static int test_wc_Sha224GetFlags(void)
  12459. {
  12460. int res = TEST_SKIPPED;
  12461. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12462. wc_Sha224 sha224;
  12463. word32 flags = 0;
  12464. int flag = 0;
  12465. /* Initialize */
  12466. flag = wc_InitSha224(&sha224);
  12467. if (flag == 0) {
  12468. flag = wc_Sha224GetFlags(&sha224, &flags);
  12469. }
  12470. if (flag == 0) {
  12471. if (flags & WC_HASH_FLAG_ISCOPY) {
  12472. flag = 0;
  12473. }
  12474. }
  12475. wc_Sha224Free(&sha224);
  12476. res = TEST_RES_CHECK(flag == 0);
  12477. #endif
  12478. return res;
  12479. } /* END test_wc_Sha224GetFlags */
  12480. /*
  12481. * Unit test function for wc_Sha224Free()
  12482. */
  12483. static int test_wc_Sha224Free(void)
  12484. {
  12485. int res = TEST_SKIPPED;
  12486. #ifdef WOLFSSL_SHA224
  12487. wc_Sha224Free(NULL);
  12488. res = TEST_RES_CHECK(1);
  12489. #endif
  12490. return res;
  12491. } /* END test_wc_Sha224Free */
  12492. /*
  12493. * Unit test function for wc_Sha224GetHash()
  12494. */
  12495. static int test_wc_Sha224GetHash(void)
  12496. {
  12497. int res = TEST_SKIPPED;
  12498. #ifdef WOLFSSL_SHA224
  12499. wc_Sha224 sha224;
  12500. byte hash1[WC_SHA224_DIGEST_SIZE];
  12501. int flag = 0;
  12502. /* Initialize */
  12503. flag = wc_InitSha224(&sha224);
  12504. if (flag == 0) {
  12505. flag = wc_Sha224GetHash(&sha224, hash1);
  12506. }
  12507. /*test bad arguments*/
  12508. if (flag == 0) {
  12509. flag = wc_Sha224GetHash(NULL, NULL);
  12510. if (flag == BAD_FUNC_ARG) {
  12511. flag = 0;
  12512. }
  12513. }
  12514. if (flag == 0) {
  12515. flag = wc_Sha224GetHash(NULL, hash1);
  12516. if (flag == BAD_FUNC_ARG) {
  12517. flag = 0;
  12518. }
  12519. }
  12520. if (flag == 0) {
  12521. flag = wc_Sha224GetHash(&sha224, NULL);
  12522. if (flag == BAD_FUNC_ARG) {
  12523. flag = 0;
  12524. }
  12525. }
  12526. wc_Sha224Free(&sha224);
  12527. res = TEST_RES_CHECK(flag == 0);
  12528. #endif
  12529. return res;
  12530. } /* END test_wc_Sha224GetHash */
  12531. /*
  12532. * Unit test function for wc_Sha224Copy()
  12533. */
  12534. static int test_wc_Sha224Copy(void)
  12535. {
  12536. int res = TEST_SKIPPED;
  12537. #ifdef WOLFSSL_SHA224
  12538. wc_Sha224 sha224;
  12539. wc_Sha224 temp;
  12540. int flag = 0;
  12541. /* Initialize */
  12542. flag = wc_InitSha224(&sha224);
  12543. if (flag == 0) {
  12544. flag = wc_InitSha224(&temp);
  12545. }
  12546. if (flag == 0) {
  12547. flag = wc_Sha224Copy(&sha224, &temp);
  12548. }
  12549. /*test bad arguments*/
  12550. if (flag == 0) {
  12551. flag = wc_Sha224Copy(NULL, NULL);
  12552. if (flag == BAD_FUNC_ARG) {
  12553. flag = 0;
  12554. }
  12555. }
  12556. if (flag == 0) {
  12557. flag = wc_Sha224Copy(NULL, &temp);
  12558. if (flag == BAD_FUNC_ARG) {
  12559. flag = 0;
  12560. }
  12561. }
  12562. if (flag == 0) {
  12563. flag = wc_Sha224Copy(&sha224, NULL);
  12564. if (flag == BAD_FUNC_ARG) {
  12565. flag = 0;
  12566. }
  12567. }
  12568. wc_Sha224Free(&sha224);
  12569. wc_Sha224Free(&temp);
  12570. res = TEST_RES_CHECK(flag == 0);
  12571. #endif
  12572. return res;
  12573. } /* END test_wc_Sha224Copy */
  12574. /*
  12575. * Testing wc_InitRipeMd()
  12576. */
  12577. static int test_wc_InitRipeMd(void)
  12578. {
  12579. int res = TEST_SKIPPED;
  12580. #ifdef WOLFSSL_RIPEMD
  12581. RipeMd ripemd;
  12582. int ret;
  12583. int flag = 0;
  12584. /* Test good arg. */
  12585. ret = wc_InitRipeMd(&ripemd);
  12586. if (ret != 0) {
  12587. flag = WOLFSSL_FATAL_ERROR;
  12588. }
  12589. /* Test bad arg. */
  12590. if (!flag) {
  12591. ret = wc_InitRipeMd(NULL);
  12592. if (ret != BAD_FUNC_ARG) {
  12593. flag = WOLFSSL_FATAL_ERROR;
  12594. }
  12595. }
  12596. res = TEST_RES_CHECK(flag == 0);
  12597. #endif
  12598. return res;
  12599. } /* END test_wc_InitRipeMd */
  12600. /*
  12601. * Testing wc_RipeMdUpdate()
  12602. */
  12603. static int test_wc_RipeMdUpdate(void)
  12604. {
  12605. int res = TEST_SKIPPED;
  12606. #ifdef WOLFSSL_RIPEMD
  12607. RipeMd ripemd;
  12608. byte hash[RIPEMD_DIGEST_SIZE];
  12609. testVector a, b, c;
  12610. int ret;
  12611. int flag = 0;
  12612. ret = wc_InitRipeMd(&ripemd);
  12613. if (ret != 0) {
  12614. flag = ret;
  12615. }
  12616. /* Input */
  12617. if (!flag) {
  12618. a.input = "a";
  12619. a.inLen = XSTRLEN(a.input);
  12620. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12621. if (ret != 0) {
  12622. flag = ret;
  12623. }
  12624. }
  12625. if (!flag) {
  12626. ret = wc_RipeMdFinal(&ripemd, hash);
  12627. if (ret != 0) {
  12628. flag = ret;
  12629. }
  12630. }
  12631. /* Update input. */
  12632. if (!flag) {
  12633. a.input = "abc";
  12634. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12635. "\xb0\x87\xf1\x5a\x0b\xfc";
  12636. a.inLen = XSTRLEN(a.input);
  12637. a.outLen = XSTRLEN(a.output);
  12638. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12639. if (ret != 0) {
  12640. flag = ret;
  12641. }
  12642. }
  12643. if (!flag) {
  12644. ret = wc_RipeMdFinal(&ripemd, hash);
  12645. if (ret != 0) {
  12646. flag = ret;
  12647. }
  12648. }
  12649. if (!flag) {
  12650. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  12651. flag = WOLFSSL_FATAL_ERROR;
  12652. }
  12653. }
  12654. /* Pass in bad values. */
  12655. if (!flag) {
  12656. b.input = NULL;
  12657. b.inLen = 0;
  12658. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  12659. if (ret != 0) {
  12660. flag = ret;
  12661. }
  12662. }
  12663. if (!flag) {
  12664. c.input = NULL;
  12665. c.inLen = RIPEMD_DIGEST_SIZE;
  12666. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  12667. if (ret != BAD_FUNC_ARG) {
  12668. flag = WOLFSSL_FATAL_ERROR;
  12669. }
  12670. }
  12671. if (!flag) {
  12672. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12673. if (ret != BAD_FUNC_ARG) {
  12674. flag = WOLFSSL_FATAL_ERROR;
  12675. }
  12676. }
  12677. res = TEST_RES_CHECK(flag == 0);
  12678. #endif
  12679. return res;
  12680. } /* END test_wc_RipeMdUdpate */
  12681. /*
  12682. * Unit test function for wc_RipeMdFinal()
  12683. */
  12684. static int test_wc_RipeMdFinal(void)
  12685. {
  12686. int res = TEST_SKIPPED;
  12687. #ifdef WOLFSSL_RIPEMD
  12688. RipeMd ripemd;
  12689. byte* hash_test[3];
  12690. byte hash1[RIPEMD_DIGEST_SIZE];
  12691. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12692. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12693. int times, i, ret;
  12694. int flag = 0;
  12695. /* Initialize */
  12696. ret = wc_InitRipeMd(&ripemd);
  12697. if (ret != 0) {
  12698. flag = ret;
  12699. }
  12700. if (!flag) {
  12701. hash_test[0] = hash1;
  12702. hash_test[1] = hash2;
  12703. hash_test[2] = hash3;
  12704. }
  12705. times = sizeof(hash_test) / sizeof(byte*);
  12706. /* Testing oversized buffers. */
  12707. for (i = 0; i < times; i++) {
  12708. if (!flag) {
  12709. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  12710. if (ret != 0) {
  12711. flag = WOLFSSL_FATAL_ERROR;
  12712. }
  12713. }
  12714. }
  12715. /* Test bad args. */
  12716. if (!flag) {
  12717. ret = wc_RipeMdFinal(NULL, NULL);
  12718. if (ret != BAD_FUNC_ARG) {
  12719. flag = WOLFSSL_FATAL_ERROR;
  12720. }
  12721. }
  12722. if (!flag) {
  12723. ret = wc_RipeMdFinal(NULL, hash1);
  12724. if (ret != BAD_FUNC_ARG) {
  12725. flag = WOLFSSL_FATAL_ERROR;
  12726. }
  12727. }
  12728. if (!flag) {
  12729. ret = wc_RipeMdFinal(&ripemd, NULL);
  12730. if (ret != BAD_FUNC_ARG) {
  12731. flag = WOLFSSL_FATAL_ERROR;
  12732. }
  12733. }
  12734. res = TEST_RES_CHECK(flag == 0);
  12735. #endif
  12736. return res;
  12737. } /* END test_wc_RipeMdFinal */
  12738. /*
  12739. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12740. * wc_InitSha3_512
  12741. */
  12742. static int test_wc_InitSha3(void)
  12743. {
  12744. int res = TEST_SKIPPED;
  12745. #if defined(WOLFSSL_SHA3)
  12746. wc_Sha3 sha3;
  12747. int ret = 0;
  12748. (void)sha3;
  12749. #if !defined(WOLFSSL_NOSHA3_224)
  12750. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12751. /* Test bad args. */
  12752. if (ret == 0) {
  12753. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  12754. if (ret == BAD_FUNC_ARG) {
  12755. ret = 0;
  12756. }
  12757. else if (ret == 0) {
  12758. ret = WOLFSSL_FATAL_ERROR;
  12759. }
  12760. }
  12761. wc_Sha3_224_Free(&sha3);
  12762. #endif /* NOSHA3_224 */
  12763. #if !defined(WOLFSSL_NOSHA3_256)
  12764. if (ret == 0) {
  12765. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12766. /* Test bad args. */
  12767. if (ret == 0) {
  12768. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  12769. if (ret == BAD_FUNC_ARG) {
  12770. ret = 0;
  12771. }
  12772. else if (ret == 0) {
  12773. ret = WOLFSSL_FATAL_ERROR;
  12774. }
  12775. }
  12776. wc_Sha3_256_Free(&sha3);
  12777. } /* END sha3_256 */
  12778. #endif /* NOSHA3_256 */
  12779. #if !defined(WOLFSSL_NOSHA3_384)
  12780. if (ret == 0) {
  12781. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12782. /* Test bad args. */
  12783. if (ret == 0) {
  12784. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  12785. if (ret == BAD_FUNC_ARG) {
  12786. ret = 0;
  12787. }
  12788. else if (ret == 0) {
  12789. ret = WOLFSSL_FATAL_ERROR;
  12790. }
  12791. }
  12792. wc_Sha3_384_Free(&sha3);
  12793. } /* END sha3_384 */
  12794. #endif /* NOSHA3_384 */
  12795. #if !defined(WOLFSSL_NOSHA3_512)
  12796. if (ret == 0) {
  12797. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12798. /* Test bad args. */
  12799. if (ret == 0) {
  12800. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  12801. if (ret == BAD_FUNC_ARG) {
  12802. ret = 0;
  12803. }
  12804. else if (ret == 0) {
  12805. ret = WOLFSSL_FATAL_ERROR;
  12806. }
  12807. }
  12808. wc_Sha3_512_Free(&sha3);
  12809. } /* END sha3_512 */
  12810. #endif /* NOSHA3_512 */
  12811. res = TEST_RES_CHECK(ret == 0);
  12812. #endif
  12813. return res;
  12814. } /* END test_wc_InitSha3 */
  12815. /*
  12816. * Testing wc_Sha3_Update()
  12817. */
  12818. static int testing_wc_Sha3_Update(void)
  12819. {
  12820. int res = TEST_SKIPPED;
  12821. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12822. !defined(WOLFSSL_AFALG_XILINX)
  12823. wc_Sha3 sha3;
  12824. byte msg[] = "Everybody's working for the weekend.";
  12825. byte msg2[] = "Everybody gets Friday off.";
  12826. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12827. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12828. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12829. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12830. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12831. word32 msglen = sizeof(msg) - 1;
  12832. word32 msg2len = sizeof(msg2);
  12833. word32 msgCmplen = sizeof(msgCmp);
  12834. int ret = 0;
  12835. #if !defined(WOLFSSL_NOSHA3_224)
  12836. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12837. if (ret != 0) {
  12838. return TEST_FAIL;
  12839. }
  12840. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  12841. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12842. ret = WOLFSSL_FATAL_ERROR;
  12843. }
  12844. if (ret == 0) {
  12845. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12846. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12847. ret = WOLFSSL_FATAL_ERROR;
  12848. }
  12849. }
  12850. /* Pass bad args. */
  12851. if (ret == 0) {
  12852. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  12853. if (ret == BAD_FUNC_ARG) {
  12854. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  12855. }
  12856. if (ret == BAD_FUNC_ARG) {
  12857. wc_Sha3_224_Free(&sha3);
  12858. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  12859. return TEST_FAIL;
  12860. }
  12861. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  12862. if (ret == 0) {
  12863. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12864. }
  12865. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12866. ret = WOLFSSL_FATAL_ERROR;
  12867. }
  12868. }
  12869. }
  12870. wc_Sha3_224_Free(&sha3);
  12871. #endif /* SHA3_224 */
  12872. #if !defined(WOLFSSL_NOSHA3_256)
  12873. if (ret == 0) {
  12874. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12875. if (ret != 0) {
  12876. return TEST_FAIL;
  12877. }
  12878. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  12879. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12880. ret = WOLFSSL_FATAL_ERROR;
  12881. }
  12882. if (ret == 0) {
  12883. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12884. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12885. ret = WOLFSSL_FATAL_ERROR;
  12886. }
  12887. }
  12888. /* Pass bad args. */
  12889. if (ret == 0) {
  12890. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  12891. if (ret == BAD_FUNC_ARG) {
  12892. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  12893. }
  12894. if (ret == BAD_FUNC_ARG) {
  12895. wc_Sha3_256_Free(&sha3);
  12896. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  12897. return TEST_FAIL;
  12898. }
  12899. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  12900. if (ret == 0) {
  12901. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12902. }
  12903. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12904. ret = WOLFSSL_FATAL_ERROR;
  12905. }
  12906. }
  12907. }
  12908. wc_Sha3_256_Free(&sha3);
  12909. }
  12910. #endif /* SHA3_256 */
  12911. #if !defined(WOLFSSL_NOSHA3_384)
  12912. if (ret == 0) {
  12913. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12914. if (ret != 0) {
  12915. return TEST_FAIL;
  12916. }
  12917. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  12918. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12919. ret = WOLFSSL_FATAL_ERROR;
  12920. }
  12921. if (ret == 0) {
  12922. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12923. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12924. ret = WOLFSSL_FATAL_ERROR;
  12925. }
  12926. }
  12927. /* Pass bad args. */
  12928. if (ret == 0) {
  12929. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  12930. if (ret == BAD_FUNC_ARG) {
  12931. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  12932. }
  12933. if (ret == BAD_FUNC_ARG) {
  12934. wc_Sha3_384_Free(&sha3);
  12935. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  12936. return TEST_FAIL;
  12937. }
  12938. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  12939. if (ret == 0) {
  12940. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12941. }
  12942. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12943. ret = WOLFSSL_FATAL_ERROR;
  12944. }
  12945. }
  12946. }
  12947. wc_Sha3_384_Free(&sha3);
  12948. }
  12949. #endif /* SHA3_384 */
  12950. #if !defined(WOLFSSL_NOSHA3_512)
  12951. if (ret == 0) {
  12952. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12953. if (ret != 0) {
  12954. return TEST_FAIL;
  12955. }
  12956. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  12957. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12958. ret = WOLFSSL_FATAL_ERROR;
  12959. }
  12960. if (ret == 0) {
  12961. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12962. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12963. ret = WOLFSSL_FATAL_ERROR;
  12964. }
  12965. }
  12966. /* Pass bad args. */
  12967. if (ret == 0) {
  12968. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  12969. if (ret == BAD_FUNC_ARG) {
  12970. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  12971. }
  12972. if (ret == BAD_FUNC_ARG) {
  12973. wc_Sha3_512_Free(&sha3);
  12974. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  12975. return TEST_FAIL;
  12976. }
  12977. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  12978. if (ret == 0) {
  12979. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12980. }
  12981. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12982. ret = WOLFSSL_FATAL_ERROR;
  12983. }
  12984. }
  12985. }
  12986. wc_Sha3_512_Free(&sha3);
  12987. }
  12988. #endif /* SHA3_512 */
  12989. res = TEST_RES_CHECK(ret == 0);
  12990. #endif /* WOLFSSL_SHA3 */
  12991. return res;
  12992. } /* END testing_wc_Sha3_Update */
  12993. /*
  12994. * Testing wc_Sha3_224_Final()
  12995. */
  12996. static int test_wc_Sha3_224_Final(void)
  12997. {
  12998. int res = TEST_SKIPPED;
  12999. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13000. wc_Sha3 sha3;
  13001. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13002. "nopnopq";
  13003. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13004. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13005. "\x64\xea\xd0\xfc\xce\x33";
  13006. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13007. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13008. int ret = 0;
  13009. /* Init stack variables. */
  13010. XMEMSET(hash, 0, sizeof(hash));
  13011. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  13012. if (ret != 0) {
  13013. return TEST_FAIL;
  13014. }
  13015. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13016. if (ret == 0) {
  13017. ret = wc_Sha3_224_Final(&sha3, hash);
  13018. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  13019. ret = WOLFSSL_FATAL_ERROR;
  13020. }
  13021. }
  13022. /* Test bad args. */
  13023. if (ret == 0) {
  13024. ret = wc_Sha3_224_Final(NULL, hash);
  13025. if (ret == 0) {
  13026. ret = wc_Sha3_224_Final(&sha3, NULL);
  13027. }
  13028. if (ret == BAD_FUNC_ARG) {
  13029. ret = 0;
  13030. }
  13031. else if (ret == 0) {
  13032. ret = WOLFSSL_FATAL_ERROR;
  13033. }
  13034. }
  13035. wc_Sha3_224_Free(&sha3);
  13036. if (ret == 0) {
  13037. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  13038. if (ret != 0) {
  13039. return TEST_FAIL;
  13040. }
  13041. /* Init stack variables. */
  13042. XMEMSET(hash, 0, sizeof(hash));
  13043. XMEMSET(hashRet, 0, sizeof(hashRet));
  13044. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13045. if (ret == 0) {
  13046. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  13047. }
  13048. if (ret == 0) {
  13049. ret = wc_Sha3_224_Final(&sha3, hash);
  13050. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  13051. ret = WOLFSSL_FATAL_ERROR;
  13052. }
  13053. }
  13054. if (ret == 0) {
  13055. /* Test bad args. */
  13056. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  13057. if (ret == BAD_FUNC_ARG) {
  13058. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  13059. }
  13060. if (ret == BAD_FUNC_ARG) {
  13061. ret = 0;
  13062. }
  13063. else if (ret == 0) {
  13064. ret = WOLFSSL_FATAL_ERROR;
  13065. }
  13066. }
  13067. }
  13068. wc_Sha3_224_Free(&sha3);
  13069. res = TEST_RES_CHECK(ret == 0);
  13070. #endif
  13071. return res;
  13072. } /* END test_wc_Sha3_224_Final */
  13073. /*
  13074. * Testing wc_Sha3_256_Final()
  13075. */
  13076. static int test_wc_Sha3_256_Final(void)
  13077. {
  13078. int res = TEST_SKIPPED;
  13079. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13080. wc_Sha3 sha3;
  13081. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13082. "nopnopq";
  13083. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13084. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13085. "\xdd\x97\x49\x6d\x33\x76";
  13086. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13087. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13088. int ret = 0;
  13089. /* Init stack variables. */
  13090. XMEMSET(hash, 0, sizeof(hash));
  13091. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  13092. if (ret != 0) {
  13093. return TEST_FAIL;
  13094. }
  13095. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13096. if (ret == 0) {
  13097. ret = wc_Sha3_256_Final(&sha3, hash);
  13098. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  13099. ret = WOLFSSL_FATAL_ERROR;
  13100. }
  13101. }
  13102. /* Test bad args. */
  13103. if (ret == 0) {
  13104. ret = wc_Sha3_256_Final(NULL, hash);
  13105. if (ret == 0) {
  13106. ret = wc_Sha3_256_Final(&sha3, NULL);
  13107. }
  13108. if (ret == BAD_FUNC_ARG) {
  13109. ret = 0;
  13110. }
  13111. else if (ret == 0) {
  13112. ret = WOLFSSL_FATAL_ERROR;
  13113. }
  13114. }
  13115. wc_Sha3_256_Free(&sha3);
  13116. if (ret == 0) {
  13117. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  13118. if (ret != 0) {
  13119. return TEST_FAIL;
  13120. }
  13121. /* Init stack variables. */
  13122. XMEMSET(hash, 0, sizeof(hash));
  13123. XMEMSET(hashRet, 0, sizeof(hashRet));
  13124. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13125. if (ret == 0) {
  13126. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  13127. }
  13128. if (ret == 0) {
  13129. ret = wc_Sha3_256_Final(&sha3, hash);
  13130. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  13131. ret = WOLFSSL_FATAL_ERROR;
  13132. }
  13133. }
  13134. if (ret == 0) {
  13135. /* Test bad args. */
  13136. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  13137. if (ret == BAD_FUNC_ARG) {
  13138. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  13139. }
  13140. if (ret == BAD_FUNC_ARG) {
  13141. ret = 0;
  13142. }
  13143. else if (ret == 0) {
  13144. ret = WOLFSSL_FATAL_ERROR;
  13145. }
  13146. }
  13147. }
  13148. wc_Sha3_256_Free(&sha3);
  13149. res = TEST_RES_CHECK(ret == 0);
  13150. #endif
  13151. return res;
  13152. } /* END test_wc_Sha3_256_Final */
  13153. /*
  13154. * Testing wc_Sha3_384_Final()
  13155. */
  13156. static int test_wc_Sha3_384_Final(void)
  13157. {
  13158. int res = TEST_SKIPPED;
  13159. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13160. wc_Sha3 sha3;
  13161. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13162. "nopnopq";
  13163. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13164. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13165. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13166. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13167. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13168. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13169. int ret = 0;
  13170. /* Init stack variables. */
  13171. XMEMSET(hash, 0, sizeof(hash));
  13172. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  13173. if (ret != 0) {
  13174. return TEST_FAIL;
  13175. }
  13176. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13177. if (ret == 0) {
  13178. ret = wc_Sha3_384_Final(&sha3, hash);
  13179. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  13180. ret = WOLFSSL_FATAL_ERROR;
  13181. }
  13182. }
  13183. /* Test bad args. */
  13184. if (ret == 0) {
  13185. ret = wc_Sha3_384_Final(NULL, hash);
  13186. if (ret == 0) {
  13187. ret = wc_Sha3_384_Final(&sha3, NULL);
  13188. }
  13189. if (ret == BAD_FUNC_ARG) {
  13190. ret = 0;
  13191. }
  13192. else if (ret == 0) {
  13193. ret = WOLFSSL_FATAL_ERROR;
  13194. }
  13195. }
  13196. wc_Sha3_384_Free(&sha3);
  13197. if (ret == 0) {
  13198. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  13199. if (ret != 0) {
  13200. return TEST_FAIL;
  13201. }
  13202. /* Init stack variables. */
  13203. XMEMSET(hash, 0, sizeof(hash));
  13204. XMEMSET(hashRet, 0, sizeof(hashRet));
  13205. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13206. if (ret == 0) {
  13207. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  13208. }
  13209. if (ret == 0) {
  13210. ret = wc_Sha3_384_Final(&sha3, hash);
  13211. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  13212. ret = WOLFSSL_FATAL_ERROR;
  13213. }
  13214. }
  13215. if (ret == 0) {
  13216. /* Test bad args. */
  13217. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  13218. if (ret == BAD_FUNC_ARG) {
  13219. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  13220. }
  13221. if (ret == BAD_FUNC_ARG) {
  13222. ret = 0;
  13223. }
  13224. else if (ret == 0) {
  13225. ret = WOLFSSL_FATAL_ERROR;
  13226. }
  13227. }
  13228. }
  13229. wc_Sha3_384_Free(&sha3);
  13230. res = TEST_RES_CHECK(ret == 0);
  13231. #endif
  13232. return res;
  13233. } /* END test_wc_Sha3_384_Final */
  13234. /*
  13235. * Testing wc_Sha3_512_Final()
  13236. */
  13237. static int test_wc_Sha3_512_Final(void)
  13238. {
  13239. int res = TEST_SKIPPED;
  13240. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13241. !defined(WOLFSSL_NOSHA3_384)
  13242. wc_Sha3 sha3;
  13243. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13244. "nopnopq";
  13245. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13246. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13247. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13248. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13249. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13250. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13251. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13252. int ret = 0;
  13253. /* Init stack variables. */
  13254. XMEMSET(hash, 0, sizeof(hash));
  13255. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  13256. if (ret != 0) {
  13257. return TEST_FAIL;
  13258. }
  13259. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13260. if (ret == 0) {
  13261. ret = wc_Sha3_512_Final(&sha3, hash);
  13262. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  13263. ret = WOLFSSL_FATAL_ERROR;
  13264. }
  13265. }
  13266. /* Test bad args. */
  13267. if (ret == 0) {
  13268. ret = wc_Sha3_512_Final(NULL, hash);
  13269. if (ret == 0) {
  13270. ret = wc_Sha3_384_Final(&sha3, NULL);
  13271. }
  13272. if (ret == BAD_FUNC_ARG) {
  13273. ret = 0;
  13274. }
  13275. else if (ret == 0) {
  13276. ret = WOLFSSL_FATAL_ERROR;
  13277. }
  13278. }
  13279. wc_Sha3_512_Free(&sha3);
  13280. if (ret == 0) {
  13281. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  13282. if (ret != 0) {
  13283. return TEST_FAIL;
  13284. }
  13285. /* Init stack variables. */
  13286. XMEMSET(hash, 0, sizeof(hash));
  13287. XMEMSET(hashRet, 0, sizeof(hashRet));
  13288. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  13289. if (ret == 0) {
  13290. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  13291. }
  13292. if (ret == 0) {
  13293. ret = wc_Sha3_512_Final(&sha3, hash);
  13294. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  13295. ret = WOLFSSL_FATAL_ERROR;
  13296. }
  13297. }
  13298. if (ret == 0) {
  13299. /* Test bad args. */
  13300. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  13301. if (ret == BAD_FUNC_ARG) {
  13302. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  13303. }
  13304. if (ret == BAD_FUNC_ARG) {
  13305. ret = 0;
  13306. }
  13307. else if (ret == 0) {
  13308. ret = WOLFSSL_FATAL_ERROR;
  13309. }
  13310. }
  13311. }
  13312. wc_Sha3_512_Free(&sha3);
  13313. res = TEST_RES_CHECK(ret == 0);
  13314. #endif
  13315. return res;
  13316. } /* END test_wc_Sha3_512_Final */
  13317. /*
  13318. * Testing wc_Sha3_224_Copy()
  13319. */
  13320. static int test_wc_Sha3_224_Copy(void)
  13321. {
  13322. int res = TEST_SKIPPED;
  13323. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13324. wc_Sha3 sha3, sha3Cpy;
  13325. const char* msg = TEST_STRING;
  13326. word32 msglen = (word32)TEST_STRING_SZ;
  13327. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13328. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13329. int ret = 0;
  13330. XMEMSET(hash, 0, sizeof(hash));
  13331. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13332. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  13333. if (ret != 0) {
  13334. return TEST_FAIL;
  13335. }
  13336. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  13337. if (ret != 0) {
  13338. wc_Sha3_224_Free(&sha3);
  13339. return TEST_FAIL;
  13340. }
  13341. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  13342. if (ret == 0) {
  13343. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  13344. if (ret == 0) {
  13345. ret = wc_Sha3_224_Final(&sha3, hash);
  13346. if (ret == 0) {
  13347. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  13348. }
  13349. }
  13350. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13351. ret = WOLFSSL_FATAL_ERROR;
  13352. }
  13353. }
  13354. /* Test bad args. */
  13355. if (ret == 0) {
  13356. ret = wc_Sha3_224_Copy(NULL, &sha3);
  13357. if (ret == BAD_FUNC_ARG) {
  13358. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  13359. }
  13360. if (ret == BAD_FUNC_ARG) {
  13361. ret = 0;
  13362. }
  13363. else if (ret == 0) {
  13364. ret = WOLFSSL_FATAL_ERROR;
  13365. }
  13366. }
  13367. res = TEST_RES_CHECK(ret == 0);
  13368. #endif
  13369. return res;
  13370. } /* END test_wc_Sha3_224_Copy */
  13371. /*
  13372. * Testing wc_Sha3_256_Copy()
  13373. */
  13374. static int test_wc_Sha3_256_Copy(void)
  13375. {
  13376. int res = TEST_SKIPPED;
  13377. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13378. wc_Sha3 sha3, sha3Cpy;
  13379. const char* msg = TEST_STRING;
  13380. word32 msglen = (word32)TEST_STRING_SZ;
  13381. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13382. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13383. int ret = 0;
  13384. XMEMSET(hash, 0, sizeof(hash));
  13385. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13386. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  13387. if (ret != 0) {
  13388. return TEST_FAIL;
  13389. }
  13390. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  13391. if (ret != 0) {
  13392. wc_Sha3_256_Free(&sha3);
  13393. return TEST_FAIL;
  13394. }
  13395. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  13396. if (ret == 0) {
  13397. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  13398. if (ret == 0) {
  13399. ret = wc_Sha3_256_Final(&sha3, hash);
  13400. if (ret == 0) {
  13401. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  13402. }
  13403. }
  13404. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13405. ret = WOLFSSL_FATAL_ERROR;
  13406. }
  13407. }
  13408. /* Test bad args. */
  13409. if (ret == 0) {
  13410. ret = wc_Sha3_256_Copy(NULL, &sha3);
  13411. if (ret == BAD_FUNC_ARG) {
  13412. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  13413. }
  13414. if (ret == BAD_FUNC_ARG) {
  13415. ret = 0;
  13416. }
  13417. else if (ret == 0) {
  13418. ret = WOLFSSL_FATAL_ERROR;
  13419. }
  13420. }
  13421. res = TEST_RES_CHECK(ret == 0);
  13422. #endif
  13423. return res;
  13424. } /* END test_wc_Sha3_256_Copy */
  13425. /*
  13426. * Testing wc_Sha3_384_Copy()
  13427. */
  13428. static int test_wc_Sha3_384_Copy(void)
  13429. {
  13430. int res = TEST_SKIPPED;
  13431. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13432. wc_Sha3 sha3, sha3Cpy;
  13433. const char* msg = TEST_STRING;
  13434. word32 msglen = (word32)TEST_STRING_SZ;
  13435. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13436. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13437. int ret = 0;
  13438. XMEMSET(hash, 0, sizeof(hash));
  13439. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13440. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  13441. if (ret != 0) {
  13442. return TEST_FAIL;
  13443. }
  13444. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  13445. if (ret != 0) {
  13446. wc_Sha3_384_Free(&sha3);
  13447. return TEST_FAIL;
  13448. }
  13449. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  13450. if (ret == 0) {
  13451. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  13452. if (ret == 0) {
  13453. ret = wc_Sha3_384_Final(&sha3, hash);
  13454. if (ret == 0) {
  13455. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  13456. }
  13457. }
  13458. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13459. ret = WOLFSSL_FATAL_ERROR;
  13460. }
  13461. }
  13462. /* Test bad args. */
  13463. if (ret == 0) {
  13464. ret = wc_Sha3_384_Copy(NULL, &sha3);
  13465. if (ret == BAD_FUNC_ARG) {
  13466. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  13467. }
  13468. if (ret == BAD_FUNC_ARG) {
  13469. ret = 0;
  13470. }
  13471. else if (ret == 0) {
  13472. ret = WOLFSSL_FATAL_ERROR;
  13473. }
  13474. }
  13475. res = TEST_RES_CHECK(ret == 0);
  13476. #endif
  13477. return res;
  13478. } /* END test_wc_Sha3_384_Copy */
  13479. /*
  13480. * Testing wc_Sha3_512_Copy()
  13481. */
  13482. static int test_wc_Sha3_512_Copy(void)
  13483. {
  13484. int res = TEST_SKIPPED;
  13485. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13486. wc_Sha3 sha3, sha3Cpy;
  13487. const char* msg = TEST_STRING;
  13488. word32 msglen = (word32)TEST_STRING_SZ;
  13489. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13490. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13491. int ret = 0;
  13492. XMEMSET(hash, 0, sizeof(hash));
  13493. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13494. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  13495. if (ret != 0) {
  13496. return TEST_FAIL;
  13497. }
  13498. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  13499. if (ret != 0) {
  13500. wc_Sha3_512_Free(&sha3);
  13501. return TEST_FAIL;
  13502. }
  13503. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  13504. if (ret == 0) {
  13505. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  13506. if (ret == 0) {
  13507. ret = wc_Sha3_512_Final(&sha3, hash);
  13508. if (ret == 0) {
  13509. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  13510. }
  13511. }
  13512. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13513. ret = WOLFSSL_FATAL_ERROR;
  13514. }
  13515. }
  13516. /* Test bad args. */
  13517. if (ret == 0) {
  13518. ret = wc_Sha3_512_Copy(NULL, &sha3);
  13519. if (ret == BAD_FUNC_ARG) {
  13520. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  13521. }
  13522. if (ret == BAD_FUNC_ARG) {
  13523. ret = 0;
  13524. }
  13525. else if (ret == 0) {
  13526. ret = WOLFSSL_FATAL_ERROR;
  13527. }
  13528. }
  13529. res = TEST_RES_CHECK(ret == 0);
  13530. #endif
  13531. return res;
  13532. } /* END test_wc_Sha3_512_Copy */
  13533. /*
  13534. * Unit test function for wc_Sha3_GetFlags()
  13535. */
  13536. static int test_wc_Sha3_GetFlags(void)
  13537. {
  13538. int res = TEST_SKIPPED;
  13539. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13540. wc_Sha3 sha3;
  13541. word32 flags = 0;
  13542. int ret = 0;
  13543. /* Initialize */
  13544. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  13545. if (ret != 0) {
  13546. return TEST_FAIL;
  13547. }
  13548. if (ret == 0) {
  13549. ret = wc_Sha3_GetFlags(&sha3, &flags);
  13550. }
  13551. if (ret == 0) {
  13552. if (flags & WC_HASH_FLAG_ISCOPY) {
  13553. ret = 0;
  13554. }
  13555. }
  13556. wc_Sha3_224_Free(&sha3);
  13557. res = TEST_RES_CHECK(ret == 0);
  13558. #endif
  13559. return res;
  13560. } /* END test_wc_Sha3_GetFlags */
  13561. static int test_wc_InitShake256(void)
  13562. {
  13563. int res = TEST_SKIPPED;
  13564. #ifdef WOLFSSL_SHAKE256
  13565. wc_Shake shake;
  13566. int ret = 0;
  13567. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13568. /* Test bad args. */
  13569. if (ret == 0) {
  13570. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  13571. if (ret == BAD_FUNC_ARG) {
  13572. ret = 0;
  13573. }
  13574. else if (ret == 0) {
  13575. ret = WOLFSSL_FATAL_ERROR;
  13576. }
  13577. }
  13578. wc_Shake256_Free(&shake);
  13579. res = TEST_RES_CHECK(ret == 0);
  13580. #endif
  13581. return res;
  13582. } /* END test_wc_InitSha3 */
  13583. static int testing_wc_Shake256_Update(void)
  13584. {
  13585. int res = TEST_SKIPPED;
  13586. #ifdef WOLFSSL_SHAKE256
  13587. wc_Shake shake;
  13588. byte msg[] = "Everybody's working for the weekend.";
  13589. byte msg2[] = "Everybody gets Friday off.";
  13590. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13591. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13592. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13593. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13594. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13595. word32 msglen = sizeof(msg) - 1;
  13596. word32 msg2len = sizeof(msg2);
  13597. word32 msgCmplen = sizeof(msgCmp);
  13598. int ret = 0;
  13599. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13600. if (ret != 0) {
  13601. return TEST_FAIL;
  13602. }
  13603. ret = wc_Shake256_Update(&shake, msg, msglen);
  13604. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  13605. ret = WOLFSSL_FATAL_ERROR;
  13606. }
  13607. if (ret == 0) {
  13608. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13609. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  13610. ret = WOLFSSL_FATAL_ERROR;
  13611. }
  13612. }
  13613. /* Pass bad args. */
  13614. if (ret == 0) {
  13615. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  13616. if (ret == BAD_FUNC_ARG) {
  13617. ret = wc_Shake256_Update(&shake, NULL, 5);
  13618. }
  13619. if (ret == BAD_FUNC_ARG) {
  13620. wc_Shake256_Free(&shake);
  13621. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  13622. return TEST_FAIL;
  13623. }
  13624. ret = wc_Shake256_Update(&shake, NULL, 0);
  13625. if (ret == 0) {
  13626. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13627. }
  13628. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  13629. ret = WOLFSSL_FATAL_ERROR;
  13630. }
  13631. }
  13632. }
  13633. wc_Shake256_Free(&shake);
  13634. res = TEST_RES_CHECK(ret == 0);
  13635. #endif /* WOLFSSL_SHAKE256 */
  13636. return res;
  13637. }
  13638. static int test_wc_Shake256_Final(void)
  13639. {
  13640. int res = TEST_SKIPPED;
  13641. #ifdef WOLFSSL_SHAKE256
  13642. wc_Shake shake;
  13643. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13644. "nopnopq";
  13645. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13646. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13647. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13648. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13649. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13650. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13651. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13652. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13653. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13654. byte hash[114];
  13655. int ret = 0;
  13656. /* Init stack variables. */
  13657. XMEMSET(hash, 0, sizeof(hash));
  13658. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13659. if (ret != 0) {
  13660. return TEST_FAIL;
  13661. }
  13662. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  13663. if (ret == 0) {
  13664. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  13665. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  13666. ret = WOLFSSL_FATAL_ERROR;
  13667. }
  13668. }
  13669. /* Test bad args. */
  13670. if (ret == 0) {
  13671. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  13672. if (ret == 0) {
  13673. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  13674. }
  13675. if (ret == BAD_FUNC_ARG) {
  13676. ret = 0;
  13677. }
  13678. else if (ret == 0) {
  13679. ret = WOLFSSL_FATAL_ERROR;
  13680. }
  13681. }
  13682. wc_Shake256_Free(&shake);
  13683. res = TEST_RES_CHECK(ret == 0);
  13684. #endif
  13685. return res;
  13686. }
  13687. /*
  13688. * Testing wc_Shake256_Copy()
  13689. */
  13690. static int test_wc_Shake256_Copy(void)
  13691. {
  13692. int res = TEST_SKIPPED;
  13693. #ifdef WOLFSSL_SHAKE256
  13694. wc_Shake shake, shakeCpy;
  13695. const char* msg = TEST_STRING;
  13696. word32 msglen = (word32)TEST_STRING_SZ;
  13697. byte hash[144];
  13698. byte hashCpy[144];
  13699. word32 hashLen = sizeof(hash);
  13700. word32 hashLenCpy = sizeof(hashCpy);
  13701. int ret;
  13702. XMEMSET(hash, 0, sizeof(hash));
  13703. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13704. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13705. if (ret != 0) {
  13706. return TEST_FAIL;
  13707. }
  13708. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  13709. if (ret != 0) {
  13710. wc_Shake256_Free(&shake);
  13711. return TEST_FAIL;
  13712. }
  13713. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  13714. if (ret == 0) {
  13715. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  13716. if (ret == 0) {
  13717. ret = wc_Shake256_Final(&shake, hash, hashLen);
  13718. if (ret == 0) {
  13719. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  13720. }
  13721. }
  13722. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13723. ret = WOLFSSL_FATAL_ERROR;
  13724. }
  13725. }
  13726. /* Test bad args. */
  13727. if (ret == 0) {
  13728. ret = wc_Shake256_Copy(NULL, &shake);
  13729. if (ret == BAD_FUNC_ARG) {
  13730. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  13731. }
  13732. if (ret == BAD_FUNC_ARG) {
  13733. ret = 0;
  13734. }
  13735. else if (ret == 0) {
  13736. ret = WOLFSSL_FATAL_ERROR;
  13737. }
  13738. }
  13739. wc_Shake256_Free(&shake);
  13740. res = TEST_RES_CHECK(ret == 0);
  13741. #endif
  13742. return res;
  13743. } /* END test_wc_Shake256_Copy */
  13744. /*
  13745. * Unit test function for wc_Shake256Hash()
  13746. */
  13747. static int test_wc_Shake256Hash(void)
  13748. {
  13749. int res = TEST_SKIPPED;
  13750. #ifdef WOLFSSL_SHAKE256
  13751. const byte data[] = { /* Hello World */
  13752. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13753. 0x72,0x6c,0x64
  13754. };
  13755. word32 len = sizeof(data);
  13756. byte hash[144];
  13757. word32 hashLen = sizeof(hash);
  13758. int ret;
  13759. ret = wc_Shake256Hash(data, len, hash, hashLen);
  13760. res = TEST_RES_CHECK(ret == 0);
  13761. #endif
  13762. return res;
  13763. } /* END test_wc_Shake256Hash */
  13764. /*
  13765. * Test function for wc_HmacSetKey
  13766. */
  13767. static int test_wc_Md5HmacSetKey(void)
  13768. {
  13769. int res = TEST_SKIPPED;
  13770. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13771. Hmac hmac;
  13772. int ret, times, itr;
  13773. int flag = 0;
  13774. const char* keys[]=
  13775. {
  13776. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13777. #ifndef HAVE_FIPS
  13778. "Jefe", /* smaller than minimum FIPS key size */
  13779. #endif
  13780. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13781. };
  13782. times = sizeof(keys) / sizeof(char*);
  13783. flag = 0;
  13784. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13785. if (ret != 0)
  13786. return TEST_FAIL;
  13787. for (itr = 0; itr < times; itr++) {
  13788. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13789. (word32)XSTRLEN(keys[itr]));
  13790. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13791. wc_HmacFree(&hmac);
  13792. if (ret == BAD_FUNC_ARG) {
  13793. return TEST_SUCCESS;
  13794. }
  13795. else {
  13796. return TEST_FAIL;
  13797. }
  13798. #else
  13799. if (ret != 0) {
  13800. flag = ret;
  13801. }
  13802. #endif
  13803. }
  13804. /* Bad args. */
  13805. if (!flag) {
  13806. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13807. (word32)XSTRLEN(keys[0]));
  13808. if (ret != BAD_FUNC_ARG) {
  13809. flag = WOLFSSL_FATAL_ERROR;
  13810. }
  13811. }
  13812. if (!flag) {
  13813. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  13814. if (ret != BAD_FUNC_ARG) {
  13815. flag = WOLFSSL_FATAL_ERROR;
  13816. }
  13817. }
  13818. if (!flag) {
  13819. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13820. (word32)XSTRLEN(keys[0]));
  13821. if (ret != BAD_FUNC_ARG) {
  13822. flag = WOLFSSL_FATAL_ERROR;
  13823. }
  13824. }
  13825. if (!flag) {
  13826. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13827. #ifdef HAVE_FIPS
  13828. if (ret != HMAC_MIN_KEYLEN_E) {
  13829. flag = WOLFSSL_FATAL_ERROR;
  13830. }
  13831. #else
  13832. if (ret != 0) {
  13833. flag = WOLFSSL_FATAL_ERROR;
  13834. }
  13835. #endif
  13836. }
  13837. wc_HmacFree(&hmac);
  13838. res = TEST_RES_CHECK(flag == 0);
  13839. #endif
  13840. return res;
  13841. } /* END test_wc_Md5HmacSetKey */
  13842. /*
  13843. * testing wc_HmacSetKey() on wc_Sha hash.
  13844. */
  13845. static int test_wc_ShaHmacSetKey(void)
  13846. {
  13847. int res = TEST_SKIPPED;
  13848. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13849. Hmac hmac;
  13850. int ret, times, itr;
  13851. int flag = 0;
  13852. const char* keys[]=
  13853. {
  13854. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13855. "\x0b\x0b\x0b",
  13856. #ifndef HAVE_FIPS
  13857. "Jefe", /* smaller than minimum FIPS key size */
  13858. #endif
  13859. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13860. "\xAA\xAA\xAA"
  13861. };
  13862. times = sizeof(keys) / sizeof(char*);
  13863. flag = 0;
  13864. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13865. if (ret != 0)
  13866. return ret;
  13867. for (itr = 0; itr < times; itr++) {
  13868. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13869. (word32)XSTRLEN(keys[itr]));
  13870. if (ret != 0) {
  13871. flag = ret;
  13872. }
  13873. }
  13874. /* Bad args. */
  13875. if (!flag) {
  13876. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13877. (word32)XSTRLEN(keys[0]));
  13878. if (ret != BAD_FUNC_ARG) {
  13879. flag = WOLFSSL_FATAL_ERROR;
  13880. }
  13881. }
  13882. if (!flag) {
  13883. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  13884. if (ret != BAD_FUNC_ARG) {
  13885. flag = WOLFSSL_FATAL_ERROR;
  13886. }
  13887. }
  13888. if (!flag) {
  13889. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13890. (word32)XSTRLEN(keys[0]));
  13891. if (ret != BAD_FUNC_ARG) {
  13892. flag = WOLFSSL_FATAL_ERROR;
  13893. }
  13894. }
  13895. if (!flag) {
  13896. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13897. #ifdef HAVE_FIPS
  13898. if (ret != HMAC_MIN_KEYLEN_E) {
  13899. flag = WOLFSSL_FATAL_ERROR;
  13900. }
  13901. #else
  13902. if (ret != 0) {
  13903. flag = WOLFSSL_FATAL_ERROR;
  13904. }
  13905. #endif
  13906. }
  13907. wc_HmacFree(&hmac);
  13908. res = TEST_RES_CHECK(flag == 0);
  13909. #endif
  13910. return res;
  13911. } /* END test_wc_ShaHmacSetKey() */
  13912. /*
  13913. * testing wc_HmacSetKey() on Sha224 hash.
  13914. */
  13915. static int test_wc_Sha224HmacSetKey(void)
  13916. {
  13917. int res = TEST_SKIPPED;
  13918. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13919. Hmac hmac;
  13920. int ret, times, itr;
  13921. int flag = 0;
  13922. const char* keys[]=
  13923. {
  13924. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13925. "\x0b\x0b\x0b",
  13926. #ifndef HAVE_FIPS
  13927. "Jefe", /* smaller than minimum FIPS key size */
  13928. #endif
  13929. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13930. "\xAA\xAA\xAA"
  13931. };
  13932. times = sizeof(keys) / sizeof(char*);
  13933. flag = 0;
  13934. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13935. if (ret != 0)
  13936. return ret;
  13937. for (itr = 0; itr < times; itr++) {
  13938. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13939. (word32)XSTRLEN(keys[itr]));
  13940. if (ret != 0) {
  13941. flag = ret;
  13942. }
  13943. }
  13944. /* Bad args. */
  13945. if (!flag) {
  13946. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13947. (word32)XSTRLEN(keys[0]));
  13948. if (ret != BAD_FUNC_ARG) {
  13949. flag = WOLFSSL_FATAL_ERROR;
  13950. }
  13951. }
  13952. if (!flag) {
  13953. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  13954. if (ret != BAD_FUNC_ARG) {
  13955. flag = WOLFSSL_FATAL_ERROR;
  13956. }
  13957. }
  13958. if (!flag) {
  13959. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13960. (word32)XSTRLEN(keys[0]));
  13961. if (ret != BAD_FUNC_ARG) {
  13962. flag = WOLFSSL_FATAL_ERROR;
  13963. }
  13964. }
  13965. if (!flag) {
  13966. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13967. #ifdef HAVE_FIPS
  13968. if (ret != HMAC_MIN_KEYLEN_E) {
  13969. flag = WOLFSSL_FATAL_ERROR;
  13970. }
  13971. #else
  13972. if (ret != 0) {
  13973. flag = WOLFSSL_FATAL_ERROR;
  13974. }
  13975. #endif
  13976. }
  13977. wc_HmacFree(&hmac);
  13978. res = TEST_RES_CHECK(flag == 0);
  13979. #endif
  13980. return res;
  13981. } /* END test_wc_Sha224HmacSetKey() */
  13982. /*
  13983. * testing wc_HmacSetKey() on Sha256 hash
  13984. */
  13985. static int test_wc_Sha256HmacSetKey(void)
  13986. {
  13987. int res = TEST_SKIPPED;
  13988. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13989. Hmac hmac;
  13990. int ret, times, itr;
  13991. int flag = 0;
  13992. const char* keys[]=
  13993. {
  13994. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13995. "\x0b\x0b\x0b",
  13996. #ifndef HAVE_FIPS
  13997. "Jefe", /* smaller than minimum FIPS key size */
  13998. #endif
  13999. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14000. "\xAA\xAA\xAA"
  14001. };
  14002. times = sizeof(keys) / sizeof(char*);
  14003. flag = 0;
  14004. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14005. if (ret != 0)
  14006. return ret;
  14007. for (itr = 0; itr < times; itr++) {
  14008. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14009. (word32)XSTRLEN(keys[itr]));
  14010. if (ret != 0) {
  14011. flag = ret;
  14012. }
  14013. }
  14014. /* Bad args. */
  14015. if (!flag) {
  14016. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14017. (word32)XSTRLEN(keys[0]));
  14018. if (ret != BAD_FUNC_ARG) {
  14019. flag = WOLFSSL_FATAL_ERROR;
  14020. }
  14021. }
  14022. if (!flag) {
  14023. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  14024. if (ret != BAD_FUNC_ARG) {
  14025. flag = WOLFSSL_FATAL_ERROR;
  14026. }
  14027. }
  14028. if (!flag) {
  14029. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  14030. (word32)XSTRLEN(keys[0]));
  14031. if (ret != BAD_FUNC_ARG) {
  14032. flag = WOLFSSL_FATAL_ERROR;
  14033. }
  14034. }
  14035. if (!flag) {
  14036. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14037. #ifdef HAVE_FIPS
  14038. if (ret != HMAC_MIN_KEYLEN_E) {
  14039. flag = WOLFSSL_FATAL_ERROR;
  14040. }
  14041. #else
  14042. if (ret != 0) {
  14043. flag = WOLFSSL_FATAL_ERROR;
  14044. }
  14045. #endif
  14046. }
  14047. wc_HmacFree(&hmac);
  14048. res = TEST_RES_CHECK(flag == 0);
  14049. #endif
  14050. return res;
  14051. } /* END test_wc_Sha256HmacSetKey() */
  14052. /*
  14053. * testing wc_HmacSetKey on Sha384 hash.
  14054. */
  14055. static int test_wc_Sha384HmacSetKey(void)
  14056. {
  14057. int res = TEST_SKIPPED;
  14058. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14059. Hmac hmac;
  14060. int ret, times, itr;
  14061. int flag = 0;
  14062. const char* keys[]=
  14063. {
  14064. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14065. "\x0b\x0b\x0b",
  14066. #ifndef HAVE_FIPS
  14067. "Jefe", /* smaller than minimum FIPS key size */
  14068. #endif
  14069. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14070. "\xAA\xAA\xAA"
  14071. };
  14072. times = sizeof(keys) / sizeof(char*);
  14073. flag = 0;
  14074. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14075. if (ret != 0)
  14076. return ret;
  14077. for (itr = 0; itr < times; itr++) {
  14078. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14079. (word32)XSTRLEN(keys[itr]));
  14080. if (ret != 0) {
  14081. flag = ret;
  14082. }
  14083. }
  14084. /* Bad args. */
  14085. if (!flag) {
  14086. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14087. (word32)XSTRLEN(keys[0]));
  14088. if (ret != BAD_FUNC_ARG) {
  14089. flag = WOLFSSL_FATAL_ERROR;
  14090. }
  14091. }
  14092. if (!flag) {
  14093. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  14094. if (ret != BAD_FUNC_ARG) {
  14095. flag = WOLFSSL_FATAL_ERROR;
  14096. }
  14097. }
  14098. if (!flag) {
  14099. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  14100. (word32)XSTRLEN(keys[0]));
  14101. if (ret != BAD_FUNC_ARG) {
  14102. flag = WOLFSSL_FATAL_ERROR;
  14103. }
  14104. }
  14105. if (!flag) {
  14106. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14107. #ifdef HAVE_FIPS
  14108. if (ret != HMAC_MIN_KEYLEN_E) {
  14109. flag = WOLFSSL_FATAL_ERROR;
  14110. }
  14111. #else
  14112. if (ret != 0) {
  14113. flag = WOLFSSL_FATAL_ERROR;
  14114. }
  14115. #endif
  14116. }
  14117. wc_HmacFree(&hmac);
  14118. res = TEST_RES_CHECK(flag == 0);
  14119. #endif
  14120. return res;
  14121. } /* END test_wc_Sha384HmacSetKey() */
  14122. /*
  14123. * testing wc_HmacUpdate on wc_Md5 hash.
  14124. */
  14125. static int test_wc_Md5HmacUpdate(void)
  14126. {
  14127. int res = TEST_SKIPPED;
  14128. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14129. Hmac hmac;
  14130. testVector a, b;
  14131. int ret;
  14132. int flag = 0;
  14133. #ifdef HAVE_FIPS
  14134. const char* keys =
  14135. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14136. #else
  14137. const char* keys = "Jefe";
  14138. #endif
  14139. a.input = "what do ya want for nothing?";
  14140. a.inLen = XSTRLEN(a.input);
  14141. b.input = "Hi There";
  14142. b.inLen = XSTRLEN(b.input);
  14143. flag = 0;
  14144. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14145. if (ret != 0)
  14146. return ret;
  14147. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  14148. if (ret != 0) {
  14149. flag = ret;
  14150. }
  14151. if (!flag) {
  14152. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  14153. if (ret != 0) {
  14154. flag = ret;
  14155. }
  14156. }
  14157. /* Update Hmac. */
  14158. if (!flag) {
  14159. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14160. if (ret != 0) {
  14161. flag = ret;
  14162. }
  14163. }
  14164. /* Test bad args. */
  14165. if (!flag) {
  14166. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  14167. if (ret != BAD_FUNC_ARG) {
  14168. flag = WOLFSSL_FATAL_ERROR;
  14169. }
  14170. }
  14171. if (!flag) {
  14172. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  14173. if (ret != BAD_FUNC_ARG) {
  14174. flag = WOLFSSL_FATAL_ERROR;
  14175. }
  14176. }
  14177. if (!flag) {
  14178. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  14179. if (ret != 0) {
  14180. flag = ret;
  14181. }
  14182. }
  14183. wc_HmacFree(&hmac);
  14184. res = TEST_RES_CHECK(flag == 0);
  14185. #endif
  14186. return res;
  14187. } /* END test_wc_Md5HmacUpdate */
  14188. /*
  14189. * testing wc_HmacUpdate on SHA hash.
  14190. */
  14191. static int test_wc_ShaHmacUpdate(void)
  14192. {
  14193. int res = TEST_SKIPPED;
  14194. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14195. Hmac hmac;
  14196. testVector a, b;
  14197. int ret;
  14198. int flag = 0;
  14199. #ifdef HAVE_FIPS
  14200. const char* keys =
  14201. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14202. #else
  14203. const char* keys = "Jefe";
  14204. #endif
  14205. a.input = "what do ya want for nothing?";
  14206. a.inLen = XSTRLEN(a.input);
  14207. b.input = "Hi There";
  14208. b.inLen = XSTRLEN(b.input);
  14209. flag = 0;
  14210. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14211. if (ret != 0)
  14212. return ret;
  14213. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  14214. if (ret != 0) {
  14215. flag = ret;
  14216. }
  14217. if (!flag) {
  14218. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  14219. if (ret != 0) {
  14220. flag = ret;
  14221. }
  14222. }
  14223. /* Update Hmac. */
  14224. if (!flag) {
  14225. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14226. if (ret != 0) {
  14227. flag = ret;
  14228. }
  14229. }
  14230. /* Test bad args. */
  14231. if (!flag) {
  14232. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  14233. if (ret != BAD_FUNC_ARG) {
  14234. flag = WOLFSSL_FATAL_ERROR;
  14235. }
  14236. }
  14237. if (!flag) {
  14238. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  14239. if (ret != BAD_FUNC_ARG) {
  14240. flag = WOLFSSL_FATAL_ERROR;
  14241. }
  14242. }
  14243. if (!flag) {
  14244. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  14245. if (ret != 0) {
  14246. flag = ret;
  14247. }
  14248. }
  14249. wc_HmacFree(&hmac);
  14250. res = TEST_RES_CHECK(flag == 0);
  14251. #endif
  14252. return res;
  14253. } /* END test_wc_ShaHmacUpdate */
  14254. /*
  14255. * testing wc_HmacUpdate on SHA224 hash.
  14256. */
  14257. static int test_wc_Sha224HmacUpdate(void)
  14258. {
  14259. int res = TEST_SKIPPED;
  14260. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14261. Hmac hmac;
  14262. testVector a, b;
  14263. int ret;
  14264. int flag = 0;
  14265. #ifdef HAVE_FIPS
  14266. const char* keys =
  14267. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14268. #else
  14269. const char* keys = "Jefe";
  14270. #endif
  14271. a.input = "what do ya want for nothing?";
  14272. a.inLen = XSTRLEN(a.input);
  14273. b.input = "Hi There";
  14274. b.inLen = XSTRLEN(b.input);
  14275. flag = 0;
  14276. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14277. if (ret != 0)
  14278. return ret;
  14279. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  14280. if (ret != 0) {
  14281. flag = ret;
  14282. }
  14283. if (!flag) {
  14284. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  14285. if (ret != 0) {
  14286. flag = ret;
  14287. }
  14288. }
  14289. /* Update Hmac. */
  14290. if (!flag) {
  14291. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14292. if (ret != 0) {
  14293. flag = ret;
  14294. }
  14295. }
  14296. /* Test bad args. */
  14297. if (!flag) {
  14298. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  14299. if (ret != BAD_FUNC_ARG) {
  14300. flag = WOLFSSL_FATAL_ERROR;
  14301. }
  14302. }
  14303. if (!flag) {
  14304. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  14305. if (ret != BAD_FUNC_ARG) {
  14306. flag = WOLFSSL_FATAL_ERROR;
  14307. }
  14308. }
  14309. if (!flag) {
  14310. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  14311. if (ret != 0) {
  14312. flag = ret;
  14313. }
  14314. }
  14315. wc_HmacFree(&hmac);
  14316. res = TEST_RES_CHECK(flag == 0);
  14317. #endif
  14318. return res;
  14319. } /* END test_wc_Sha224HmacUpdate */
  14320. /*
  14321. * testing wc_HmacUpdate on SHA256 hash.
  14322. */
  14323. static int test_wc_Sha256HmacUpdate(void)
  14324. {
  14325. int res = TEST_SKIPPED;
  14326. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14327. Hmac hmac;
  14328. testVector a, b;
  14329. int ret;
  14330. int flag = 0;
  14331. #ifdef HAVE_FIPS
  14332. const char* keys =
  14333. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14334. #else
  14335. const char* keys = "Jefe";
  14336. #endif
  14337. a.input = "what do ya want for nothing?";
  14338. a.inLen = XSTRLEN(a.input);
  14339. b.input = "Hi There";
  14340. b.inLen = XSTRLEN(b.input);
  14341. flag = 0;
  14342. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14343. if (ret != 0)
  14344. return ret;
  14345. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  14346. if (ret != 0) {
  14347. flag = ret;
  14348. }
  14349. if (!flag) {
  14350. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  14351. if (ret != 0) {
  14352. flag = ret;
  14353. }
  14354. }
  14355. /* Update Hmac. */
  14356. if (!flag) {
  14357. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14358. if (ret != 0) {
  14359. flag = ret;
  14360. }
  14361. }
  14362. /* Test bad args. */
  14363. if (!flag) {
  14364. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  14365. if (ret != BAD_FUNC_ARG) {
  14366. flag = WOLFSSL_FATAL_ERROR;
  14367. }
  14368. }
  14369. if (!flag) {
  14370. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  14371. if (ret != BAD_FUNC_ARG) {
  14372. flag = WOLFSSL_FATAL_ERROR;
  14373. }
  14374. }
  14375. if (!flag) {
  14376. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  14377. if (ret != 0) {
  14378. flag = ret;
  14379. }
  14380. }
  14381. wc_HmacFree(&hmac);
  14382. res = TEST_RES_CHECK(flag == 0);
  14383. #endif
  14384. return res;
  14385. } /* END test_wc_Sha256HmacUpdate */
  14386. /*
  14387. * testing wc_HmacUpdate on SHA384 hash.
  14388. */
  14389. static int test_wc_Sha384HmacUpdate(void)
  14390. {
  14391. int res = TEST_SKIPPED;
  14392. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14393. Hmac hmac;
  14394. testVector a, b;
  14395. int ret;
  14396. int flag = 0;
  14397. #ifdef HAVE_FIPS
  14398. const char* keys =
  14399. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14400. #else
  14401. const char* keys = "Jefe";
  14402. #endif
  14403. a.input = "what do ya want for nothing?";
  14404. a.inLen = XSTRLEN(a.input);
  14405. b.input = "Hi There";
  14406. b.inLen = XSTRLEN(b.input);
  14407. flag = 0;
  14408. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14409. if (ret != 0)
  14410. return ret;
  14411. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  14412. if (ret != 0) {
  14413. flag = ret;
  14414. }
  14415. if (!flag) {
  14416. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  14417. if (ret != 0) {
  14418. flag = ret;
  14419. }
  14420. }
  14421. /* Update Hmac. */
  14422. if (!flag) {
  14423. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14424. if (ret != 0) {
  14425. flag = ret;
  14426. }
  14427. }
  14428. /* Test bad args. */
  14429. if (!flag) {
  14430. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  14431. if (ret != BAD_FUNC_ARG) {
  14432. flag = WOLFSSL_FATAL_ERROR;
  14433. }
  14434. }
  14435. if (!flag) {
  14436. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  14437. if (ret != BAD_FUNC_ARG) {
  14438. flag = WOLFSSL_FATAL_ERROR;
  14439. }
  14440. }
  14441. if (!flag) {
  14442. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  14443. if (ret != 0) {
  14444. flag = ret;
  14445. }
  14446. }
  14447. wc_HmacFree(&hmac);
  14448. res = TEST_RES_CHECK(flag == 0);
  14449. #endif
  14450. return res;
  14451. } /* END test_wc_Sha384HmacUpdate */
  14452. /*
  14453. * Testing wc_HmacFinal() with MD5
  14454. */
  14455. static int test_wc_Md5HmacFinal(void)
  14456. {
  14457. int res = TEST_SKIPPED;
  14458. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14459. Hmac hmac;
  14460. byte hash[WC_MD5_DIGEST_SIZE];
  14461. testVector a;
  14462. int ret;
  14463. const char* key;
  14464. int flag = 0;
  14465. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14466. a.input = "Hi There";
  14467. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14468. "\x9d";
  14469. a.inLen = XSTRLEN(a.input);
  14470. a.outLen = XSTRLEN(a.output);
  14471. flag = 0;
  14472. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14473. if (ret != 0)
  14474. return ret;
  14475. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  14476. if (ret != 0) {
  14477. flag = ret;
  14478. }
  14479. if (!flag) {
  14480. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14481. if (ret != 0) {
  14482. flag = ret;
  14483. }
  14484. }
  14485. if (!flag) {
  14486. ret = wc_HmacFinal(&hmac, hash);
  14487. if (ret != 0) {
  14488. flag = ret;
  14489. }
  14490. }
  14491. if (!flag) {
  14492. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  14493. flag = WOLFSSL_FATAL_ERROR;
  14494. }
  14495. }
  14496. /* Try bad parameters. */
  14497. if (!flag) {
  14498. ret = wc_HmacFinal(NULL, hash);
  14499. if (ret != BAD_FUNC_ARG) {
  14500. flag = WOLFSSL_FATAL_ERROR;
  14501. }
  14502. }
  14503. #ifndef HAVE_FIPS
  14504. if (!flag) {
  14505. ret = wc_HmacFinal(&hmac, NULL);
  14506. if (ret != BAD_FUNC_ARG) {
  14507. flag = WOLFSSL_FATAL_ERROR;
  14508. }
  14509. }
  14510. #endif
  14511. wc_HmacFree(&hmac);
  14512. res = TEST_RES_CHECK(flag == 0);
  14513. #endif
  14514. return res;
  14515. } /* END test_wc_Md5HmacFinal */
  14516. /*
  14517. * Testing wc_HmacFinal() with SHA
  14518. */
  14519. static int test_wc_ShaHmacFinal(void)
  14520. {
  14521. int res = TEST_SKIPPED;
  14522. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14523. Hmac hmac;
  14524. byte hash[WC_SHA_DIGEST_SIZE];
  14525. testVector a;
  14526. int ret;
  14527. int flag = 0;
  14528. const char* key;
  14529. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14530. "\x0b\x0b\x0b";
  14531. a.input = "Hi There";
  14532. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14533. "\x8e\xf1\x46\xbe\x00";
  14534. a.inLen = XSTRLEN(a.input);
  14535. a.outLen = XSTRLEN(a.output);
  14536. flag = 0;
  14537. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14538. if (ret != 0)
  14539. return ret;
  14540. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  14541. if (ret != 0) {
  14542. flag = ret;
  14543. }
  14544. if (!flag) {
  14545. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14546. if (ret != 0) {
  14547. flag = ret;
  14548. }
  14549. }
  14550. if (!flag) {
  14551. ret = wc_HmacFinal(&hmac, hash);
  14552. if (ret != 0) {
  14553. flag = ret;
  14554. }
  14555. }
  14556. if (!flag) {
  14557. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  14558. flag = WOLFSSL_FATAL_ERROR;
  14559. }
  14560. }
  14561. /* Try bad parameters. */
  14562. if (!flag) {
  14563. ret = wc_HmacFinal(NULL, hash);
  14564. if (ret != BAD_FUNC_ARG) {
  14565. flag = WOLFSSL_FATAL_ERROR;
  14566. }
  14567. }
  14568. #ifndef HAVE_FIPS
  14569. if (!flag) {
  14570. ret = wc_HmacFinal(&hmac, NULL);
  14571. if (ret != BAD_FUNC_ARG) {
  14572. flag = WOLFSSL_FATAL_ERROR;
  14573. }
  14574. }
  14575. #endif
  14576. wc_HmacFree(&hmac);
  14577. res = TEST_RES_CHECK(flag == 0);
  14578. #endif
  14579. return res;
  14580. } /* END test_wc_ShaHmacFinal */
  14581. /*
  14582. * Testing wc_HmacFinal() with SHA224
  14583. */
  14584. static int test_wc_Sha224HmacFinal(void)
  14585. {
  14586. int res = TEST_SKIPPED;
  14587. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14588. Hmac hmac;
  14589. byte hash[WC_SHA224_DIGEST_SIZE];
  14590. testVector a;
  14591. int ret;
  14592. int flag = 0;
  14593. const char* key;
  14594. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14595. "\x0b\x0b\x0b";
  14596. a.input = "Hi There";
  14597. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14598. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14599. a.inLen = XSTRLEN(a.input);
  14600. a.outLen = XSTRLEN(a.output);
  14601. flag = 0;
  14602. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14603. if (ret != 0)
  14604. return ret;
  14605. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  14606. if (ret != 0) {
  14607. flag = ret;
  14608. }
  14609. if (!flag) {
  14610. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14611. if (ret != 0) {
  14612. flag = ret;
  14613. }
  14614. }
  14615. if (!flag) {
  14616. ret = wc_HmacFinal(&hmac, hash);
  14617. if (ret != 0) {
  14618. flag = ret;
  14619. }
  14620. }
  14621. if (!flag) {
  14622. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  14623. flag = WOLFSSL_FATAL_ERROR;
  14624. }
  14625. }
  14626. /* Try bad parameters. */
  14627. if (!flag) {
  14628. ret = wc_HmacFinal(NULL, hash);
  14629. if (ret != BAD_FUNC_ARG) {
  14630. flag = WOLFSSL_FATAL_ERROR;
  14631. }
  14632. }
  14633. #ifndef HAVE_FIPS
  14634. if (!flag) {
  14635. ret = wc_HmacFinal(&hmac, NULL);
  14636. if (ret != BAD_FUNC_ARG) {
  14637. flag = WOLFSSL_FATAL_ERROR;
  14638. }
  14639. }
  14640. #endif
  14641. wc_HmacFree(&hmac);
  14642. res = TEST_RES_CHECK(flag == 0);
  14643. #endif
  14644. return res;
  14645. } /* END test_wc_Sha224HmacFinal */
  14646. /*
  14647. * Testing wc_HmacFinal() with SHA256
  14648. */
  14649. static int test_wc_Sha256HmacFinal(void)
  14650. {
  14651. int res = TEST_SKIPPED;
  14652. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14653. Hmac hmac;
  14654. byte hash[WC_SHA256_DIGEST_SIZE];
  14655. testVector a;
  14656. int ret;
  14657. int flag = 0;
  14658. const char* key;
  14659. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14660. "\x0b\x0b\x0b";
  14661. a.input = "Hi There";
  14662. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14663. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14664. "\xcf\xf7";
  14665. a.inLen = XSTRLEN(a.input);
  14666. a.outLen = XSTRLEN(a.output);
  14667. flag = 0;
  14668. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14669. if (ret != 0)
  14670. return TEST_FAIL;
  14671. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  14672. if (ret != 0) {
  14673. flag = ret;
  14674. }
  14675. if (!flag) {
  14676. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14677. if (ret != 0) {
  14678. flag = ret;
  14679. }
  14680. }
  14681. if (!flag) {
  14682. ret = wc_HmacFinal(&hmac, hash);
  14683. if (ret != 0) {
  14684. flag = ret;
  14685. }
  14686. }
  14687. if (!flag) {
  14688. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  14689. flag = WOLFSSL_FATAL_ERROR;
  14690. }
  14691. }
  14692. /* Try bad parameters. */
  14693. if (!flag) {
  14694. ret = wc_HmacFinal(NULL, hash);
  14695. if (ret != BAD_FUNC_ARG) {
  14696. flag = WOLFSSL_FATAL_ERROR;
  14697. }
  14698. }
  14699. #ifndef HAVE_FIPS
  14700. if (!flag) {
  14701. ret = wc_HmacFinal(&hmac, NULL);
  14702. if (ret != BAD_FUNC_ARG) {
  14703. flag = WOLFSSL_FATAL_ERROR;
  14704. }
  14705. }
  14706. #endif
  14707. wc_HmacFree(&hmac);
  14708. res = TEST_RES_CHECK(flag == 0);
  14709. #endif
  14710. return res;
  14711. } /* END test_wc_Sha256HmacFinal */
  14712. /*
  14713. * Testing wc_HmacFinal() with SHA384
  14714. */
  14715. static int test_wc_Sha384HmacFinal(void)
  14716. {
  14717. int res = TEST_SKIPPED;
  14718. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14719. Hmac hmac;
  14720. byte hash[WC_SHA384_DIGEST_SIZE];
  14721. testVector a;
  14722. int ret;
  14723. int flag = 0;
  14724. const char* key;
  14725. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14726. "\x0b\x0b\x0b";
  14727. a.input = "Hi There";
  14728. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14729. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14730. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14731. "\xfa\x9c\xb6";
  14732. a.inLen = XSTRLEN(a.input);
  14733. a.outLen = XSTRLEN(a.output);
  14734. flag = 0;
  14735. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14736. if (ret != 0)
  14737. return ret;
  14738. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  14739. if (ret != 0) {
  14740. flag = ret;
  14741. }
  14742. if (!flag) {
  14743. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14744. if (ret != 0) {
  14745. flag = ret;
  14746. }
  14747. }
  14748. if (!flag) {
  14749. ret = wc_HmacFinal(&hmac, hash);
  14750. if (ret != 0) {
  14751. flag = ret;
  14752. }
  14753. }
  14754. if (!flag) {
  14755. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  14756. flag = WOLFSSL_FATAL_ERROR;
  14757. }
  14758. }
  14759. /* Try bad parameters. */
  14760. if (!flag) {
  14761. ret = wc_HmacFinal(NULL, hash);
  14762. if (ret != BAD_FUNC_ARG) {
  14763. flag = WOLFSSL_FATAL_ERROR;
  14764. }
  14765. }
  14766. #ifndef HAVE_FIPS
  14767. if (!flag) {
  14768. ret = wc_HmacFinal(&hmac, NULL);
  14769. if (ret != BAD_FUNC_ARG) {
  14770. flag = WOLFSSL_FATAL_ERROR;
  14771. }
  14772. }
  14773. #endif
  14774. wc_HmacFree(&hmac);
  14775. res = TEST_RES_CHECK(flag == 0);
  14776. #endif
  14777. return res;
  14778. } /* END test_wc_Sha384HmacFinal */
  14779. /*
  14780. * Testing wc_InitCmac()
  14781. */
  14782. static int test_wc_InitCmac(void)
  14783. {
  14784. int res = TEST_SKIPPED;
  14785. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14786. Cmac cmac1, cmac2, cmac3;
  14787. /* AES 128 key. */
  14788. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14789. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14790. /* AES 192 key. */
  14791. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14792. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14793. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14794. /* AES 256 key. */
  14795. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14796. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14797. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14798. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14799. word32 key1Sz = (word32)sizeof(key1) - 1;
  14800. word32 key2Sz = (word32)sizeof(key2) - 1;
  14801. word32 key3Sz = (word32)sizeof(key3) - 1;
  14802. int type = WC_CMAC_AES;
  14803. int ret = 0;
  14804. #ifdef WOLFSSL_AES_128
  14805. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  14806. #endif
  14807. #ifdef WOLFSSL_AES_192
  14808. if (ret == 0) {
  14809. wc_AesFree(&cmac1.aes);
  14810. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  14811. }
  14812. #endif
  14813. #ifdef WOLFSSL_AES_256
  14814. if (ret == 0) {
  14815. wc_AesFree(&cmac2.aes);
  14816. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  14817. }
  14818. #endif
  14819. /* Test bad args. */
  14820. if (ret == 0) {
  14821. wc_AesFree(&cmac3.aes);
  14822. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  14823. if (ret == BAD_FUNC_ARG) {
  14824. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  14825. }
  14826. if (ret == BAD_FUNC_ARG) {
  14827. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  14828. }
  14829. if (ret == BAD_FUNC_ARG) {
  14830. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  14831. }
  14832. if (ret == BAD_FUNC_ARG) {
  14833. ret = 0;
  14834. }
  14835. else {
  14836. ret = WOLFSSL_FATAL_ERROR;
  14837. }
  14838. }
  14839. (void)key1;
  14840. (void)key1Sz;
  14841. (void)key2;
  14842. (void)key2Sz;
  14843. (void)cmac1;
  14844. (void)cmac2;
  14845. res = TEST_RES_CHECK(ret == 0);
  14846. #endif
  14847. return res;
  14848. } /* END test_wc_InitCmac */
  14849. /*
  14850. * Testing wc_CmacUpdate()
  14851. */
  14852. static int test_wc_CmacUpdate(void)
  14853. {
  14854. int res = TEST_SKIPPED;
  14855. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14856. Cmac cmac;
  14857. byte key[] =
  14858. {
  14859. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14860. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14861. };
  14862. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14863. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14864. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14865. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14866. "\xf4";
  14867. word32 inSz = (word32)sizeof(in) - 1;
  14868. word32 keySz = (word32)sizeof(key);
  14869. int type = WC_CMAC_AES;
  14870. int ret = 0;
  14871. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14872. if (ret != 0) {
  14873. return ret;
  14874. }
  14875. ret = wc_CmacUpdate(&cmac, in, inSz);
  14876. /* Test bad args. */
  14877. if (ret == 0) {
  14878. ret = wc_CmacUpdate(NULL, in, inSz);
  14879. if (ret == BAD_FUNC_ARG) {
  14880. ret = wc_CmacUpdate(&cmac, NULL, 30);
  14881. }
  14882. if (ret == BAD_FUNC_ARG) {
  14883. ret = 0;
  14884. }
  14885. else if (ret == 0) {
  14886. ret = WOLFSSL_FATAL_ERROR;
  14887. }
  14888. wc_AesFree(&cmac.aes);
  14889. }
  14890. res = TEST_RES_CHECK(ret == 0);
  14891. #endif
  14892. return res;
  14893. } /* END test_wc_CmacUpdate */
  14894. /*
  14895. * Testing wc_CmacFinal()
  14896. */
  14897. static int test_wc_CmacFinal(void)
  14898. {
  14899. int res = TEST_SKIPPED;
  14900. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14901. Cmac cmac;
  14902. byte key[] =
  14903. {
  14904. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14905. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14906. };
  14907. byte msg[] =
  14908. {
  14909. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14910. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14911. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14912. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14913. 0xf4
  14914. };
  14915. /* Test vectors from CMACGenAES128.rsp from
  14916. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14917. * Per RFC4493 truncation of lsb is possible.
  14918. */
  14919. byte expMac[] =
  14920. {
  14921. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14922. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14923. };
  14924. byte mac[AES_BLOCK_SIZE];
  14925. word32 msgSz = (word32)sizeof(msg);
  14926. word32 keySz = (word32)sizeof(key);
  14927. word32 macSz = sizeof(mac);
  14928. word32 badMacSz = 17;
  14929. int expMacSz = sizeof(expMac);
  14930. int type = WC_CMAC_AES;
  14931. int ret = 0;
  14932. XMEMSET(mac, 0, macSz);
  14933. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14934. if (ret != 0) {
  14935. return ret;
  14936. }
  14937. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14938. if (ret == 0) {
  14939. ret = wc_CmacFinal(&cmac, mac, &macSz);
  14940. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  14941. ret = WOLFSSL_FATAL_ERROR;
  14942. }
  14943. /* Pass in bad args. */
  14944. if (ret == 0) {
  14945. ret = wc_CmacFinal(NULL, mac, &macSz);
  14946. if (ret == BAD_FUNC_ARG) {
  14947. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  14948. }
  14949. if (ret == BAD_FUNC_ARG) {
  14950. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  14951. if (ret == BUFFER_E) {
  14952. ret = 0;
  14953. }
  14954. }
  14955. else if (ret == 0) {
  14956. ret = WOLFSSL_FATAL_ERROR;
  14957. }
  14958. }
  14959. }
  14960. res = TEST_RES_CHECK(ret == 0);
  14961. #endif
  14962. return res;
  14963. } /* END test_wc_CmacFinal */
  14964. /*
  14965. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14966. */
  14967. static int test_wc_AesCmacGenerate(void)
  14968. {
  14969. int res = TEST_SKIPPED;
  14970. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14971. Cmac cmac;
  14972. byte key[] =
  14973. {
  14974. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14975. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14976. };
  14977. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14978. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14979. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14980. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14981. byte mac[AES_BLOCK_SIZE];
  14982. word32 keySz = sizeof(key);
  14983. word32 macSz = sizeof(mac);
  14984. word32 msgSz = sizeof(msg) - 1;
  14985. word32 expMacSz = sizeof(expMac) - 1;
  14986. int type = WC_CMAC_AES;
  14987. int ret = 0;
  14988. XMEMSET(mac, 0, macSz);
  14989. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14990. if (ret != 0) {
  14991. return ret;
  14992. }
  14993. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14994. if (ret != 0) {
  14995. return ret;
  14996. }
  14997. else {
  14998. wc_AesFree(&cmac.aes);
  14999. }
  15000. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  15001. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  15002. ret = WOLFSSL_FATAL_ERROR;
  15003. }
  15004. /* Pass in bad args. */
  15005. if (ret == 0) {
  15006. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  15007. if (ret == BAD_FUNC_ARG) {
  15008. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  15009. }
  15010. if (ret == BAD_FUNC_ARG) {
  15011. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  15012. }
  15013. if (ret == BAD_FUNC_ARG) {
  15014. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  15015. }
  15016. if (ret == BAD_FUNC_ARG) {
  15017. ret = 0;
  15018. }
  15019. else if (ret == 0) {
  15020. ret = WOLFSSL_FATAL_ERROR;
  15021. }
  15022. }
  15023. if (ret == 0) {
  15024. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  15025. /* Test bad args. */
  15026. if (ret == 0) {
  15027. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  15028. if (ret == BAD_FUNC_ARG) {
  15029. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  15030. }
  15031. if (ret == BAD_FUNC_ARG) {
  15032. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  15033. }
  15034. if (ret == BAD_FUNC_ARG) {
  15035. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  15036. }
  15037. if (ret == BAD_FUNC_ARG) {
  15038. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  15039. }
  15040. if (ret == BAD_FUNC_ARG) {
  15041. ret = 0;
  15042. }
  15043. else if (ret == 0) {
  15044. ret = WOLFSSL_FATAL_ERROR;
  15045. }
  15046. }
  15047. }
  15048. res = TEST_RES_CHECK(ret == 0);
  15049. #endif
  15050. return res;
  15051. } /* END test_wc_AesCmacGenerate */
  15052. /*
  15053. * Testing streaming AES-GCM API.
  15054. */
  15055. static int test_wc_AesGcmStream(void)
  15056. {
  15057. int res = TEST_SKIPPED;
  15058. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  15059. defined(WOLFSSL_AESGCM_STREAM)
  15060. int ret = 0;
  15061. int i;
  15062. WC_RNG rng[1];
  15063. Aes aesEnc[1];
  15064. Aes aesDec[1];
  15065. byte tag[AES_BLOCK_SIZE];
  15066. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  15067. byte out[AES_BLOCK_SIZE * 3 + 2];
  15068. byte plain[AES_BLOCK_SIZE * 3 + 2];
  15069. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  15070. byte key[AES_128_KEY_SIZE] = { 0, };
  15071. byte iv[AES_IV_SIZE] = { 1, };
  15072. byte ivOut[AES_IV_SIZE];
  15073. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  15074. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  15075. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  15076. };
  15077. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  15078. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  15079. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  15080. };
  15081. static const byte expTag[AES_BLOCK_SIZE] = {
  15082. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  15083. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  15084. };
  15085. /* Create a random for generating IV/nonce. */
  15086. AssertIntEQ(wc_InitRng(rng), 0);
  15087. /* Initialize data structures. */
  15088. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  15089. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  15090. /* BadParameters to streaming init. */
  15091. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15092. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15093. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  15094. BAD_FUNC_ARG);
  15095. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  15096. BAD_FUNC_ARG);
  15097. /* Bad parameters to encrypt update. */
  15098. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  15099. BAD_FUNC_ARG);
  15100. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  15101. BAD_FUNC_ARG);
  15102. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  15103. BAD_FUNC_ARG);
  15104. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  15105. BAD_FUNC_ARG);
  15106. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  15107. BAD_FUNC_ARG);
  15108. /* Bad parameters to decrypt update. */
  15109. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  15110. BAD_FUNC_ARG);
  15111. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  15112. BAD_FUNC_ARG);
  15113. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  15114. BAD_FUNC_ARG);
  15115. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  15116. BAD_FUNC_ARG);
  15117. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  15118. BAD_FUNC_ARG);
  15119. /* Bad parameters to encrypt final. */
  15120. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  15121. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  15122. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  15123. BAD_FUNC_ARG);
  15124. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  15125. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  15126. BAD_FUNC_ARG);
  15127. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  15128. BAD_FUNC_ARG);
  15129. /* Bad parameters to decrypt final. */
  15130. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  15131. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  15132. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  15133. BAD_FUNC_ARG);
  15134. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  15135. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  15136. BAD_FUNC_ARG);
  15137. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  15138. BAD_FUNC_ARG);
  15139. /* Check calling final before setting key fails. */
  15140. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  15141. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  15142. /* Check calling update before setting key else fails. */
  15143. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  15144. MISSING_KEY);
  15145. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  15146. MISSING_KEY);
  15147. /* Set key but not IV. */
  15148. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  15149. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  15150. /* Check calling final before setting IV fails. */
  15151. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  15152. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  15153. /* Check calling update before setting IV else fails. */
  15154. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  15155. MISSING_IV);
  15156. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  15157. MISSING_IV);
  15158. /* Set IV using fixed part IV and external IV APIs. */
  15159. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  15160. rng), 0);
  15161. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  15162. GCM_NONCE_MID_SZ), 0);
  15163. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  15164. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  15165. /* Encrypt and decrypt data. */
  15166. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  15167. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  15168. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  15169. /* Finalize and check tag matches. */
  15170. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15171. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15172. /* Set key and IV through streaming init API. */
  15173. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15174. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15175. /* Encrypt/decrypt one block and AAD of one block. */
  15176. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  15177. AES_BLOCK_SIZE), 0);
  15178. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  15179. AES_BLOCK_SIZE), 0);
  15180. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  15181. /* Finalize and check tag matches. */
  15182. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15183. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15184. /* Set key and IV through streaming init API. */
  15185. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15186. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15187. /* No data to encrypt/decrypt one byte of AAD. */
  15188. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  15189. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  15190. /* Finalize and check tag matches. */
  15191. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15192. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  15193. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15194. /* Set key and IV through streaming init API. */
  15195. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15196. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15197. /* Encrypt/decrypt one byte and no AAD. */
  15198. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  15199. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  15200. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  15201. /* Finalize and check tag matches. */
  15202. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15203. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  15204. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15205. /* Set key and IV through streaming init API. */
  15206. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15207. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  15208. /* Encryption AES is one byte at a time */
  15209. for (i = 0; i < (int)sizeof(aad); i++) {
  15210. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  15211. 0);
  15212. }
  15213. for (i = 0; i < (int)sizeof(in); i++) {
  15214. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  15215. 0);
  15216. }
  15217. /* Decryption AES is two bytes at a time */
  15218. for (i = 0; i < (int)sizeof(aad); i += 2) {
  15219. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  15220. 0);
  15221. }
  15222. for (i = 0; i < (int)sizeof(aad); i += 2) {
  15223. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  15224. 0), 0);
  15225. }
  15226. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15227. /* Finalize and check tag matches. */
  15228. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  15229. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  15230. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  15231. /* Check streaming encryption can be decrypted with one shot. */
  15232. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  15233. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  15234. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15235. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15236. wc_AesFree(aesEnc);
  15237. wc_AesFree(aesDec);
  15238. wc_FreeRng(rng);
  15239. res = TEST_RES_CHECK(ret == 0);
  15240. #endif
  15241. return res;
  15242. } /* END test_wc_AesGcmStream */
  15243. /*
  15244. * unit test for wc_Des3_SetIV()
  15245. */
  15246. static int test_wc_Des3_SetIV(void)
  15247. {
  15248. int res = TEST_SKIPPED;
  15249. #ifndef NO_DES3
  15250. Des3 des;
  15251. int ret = 0;
  15252. const byte key[] =
  15253. {
  15254. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15255. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15256. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15257. };
  15258. const byte iv[] =
  15259. {
  15260. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15261. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15262. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15263. };
  15264. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  15265. if (ret != 0)
  15266. return ret;
  15267. /* DES_ENCRYPTION or DES_DECRYPTION */
  15268. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  15269. if (ret == 0) {
  15270. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  15271. ret = WOLFSSL_FATAL_ERROR;
  15272. }
  15273. }
  15274. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15275. /* Test explicitly wc_Des3_SetIV() */
  15276. if (ret == 0) {
  15277. ret = wc_Des3_SetIV(NULL, iv);
  15278. if (ret == BAD_FUNC_ARG) {
  15279. ret = wc_Des3_SetIV(&des, NULL);
  15280. }
  15281. else if (ret == 0) {
  15282. ret = WOLFSSL_FATAL_ERROR;
  15283. }
  15284. }
  15285. #endif
  15286. wc_Des3Free(&des);
  15287. res = TEST_RES_CHECK(ret == 0);
  15288. #endif
  15289. return res;
  15290. } /* END test_wc_Des3_SetIV */
  15291. /*
  15292. * unit test for wc_Des3_SetKey()
  15293. */
  15294. static int test_wc_Des3_SetKey(void)
  15295. {
  15296. int res = TEST_SKIPPED;
  15297. #ifndef NO_DES3
  15298. Des3 des;
  15299. int ret = 0;
  15300. const byte key[] =
  15301. {
  15302. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15303. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15304. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15305. };
  15306. const byte iv[] =
  15307. {
  15308. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15309. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15310. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15311. };
  15312. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  15313. if (ret != 0)
  15314. return ret;
  15315. /* DES_ENCRYPTION or DES_DECRYPTION */
  15316. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  15317. if (ret == 0) {
  15318. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  15319. ret = WOLFSSL_FATAL_ERROR;
  15320. }
  15321. }
  15322. /* Test bad args. */
  15323. if (ret == 0) {
  15324. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  15325. if (ret == BAD_FUNC_ARG) {
  15326. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  15327. }
  15328. if (ret == BAD_FUNC_ARG) {
  15329. ret = wc_Des3_SetKey(&des, key, iv, -1);
  15330. }
  15331. if (ret == BAD_FUNC_ARG) {
  15332. /* Default case. Should return 0. */
  15333. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  15334. }
  15335. } /* END if ret != 0 */
  15336. wc_Des3Free(&des);
  15337. res = TEST_RES_CHECK(ret == 0);
  15338. #endif
  15339. return res;
  15340. } /* END test_wc_Des3_SetKey */
  15341. /*
  15342. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15343. */
  15344. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15345. {
  15346. int res = TEST_SKIPPED;
  15347. #ifndef NO_DES3
  15348. Des3 des;
  15349. int ret = 0;
  15350. byte cipher[24];
  15351. byte plain[24];
  15352. const byte key[] =
  15353. {
  15354. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15355. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15356. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15357. };
  15358. const byte iv[] =
  15359. {
  15360. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15361. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15362. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15363. };
  15364. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15365. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15366. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15367. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15368. };
  15369. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  15370. if (ret != 0)
  15371. return ret;
  15372. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  15373. if (ret == 0) {
  15374. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  15375. if (ret == 0) {
  15376. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  15377. }
  15378. if (ret == 0) {
  15379. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  15380. }
  15381. }
  15382. if (ret == 0) {
  15383. if (XMEMCMP(plain, vector, 24) != 0) {
  15384. ret = WOLFSSL_FATAL_ERROR;
  15385. }
  15386. }
  15387. /* Pass in bad args. */
  15388. if (ret == 0) {
  15389. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  15390. if (ret == BAD_FUNC_ARG) {
  15391. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  15392. }
  15393. if (ret == BAD_FUNC_ARG) {
  15394. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  15395. }
  15396. if (ret != BAD_FUNC_ARG) {
  15397. ret = WOLFSSL_FATAL_ERROR;
  15398. }
  15399. else {
  15400. ret = 0;
  15401. }
  15402. }
  15403. if (ret == 0) {
  15404. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  15405. if (ret == BAD_FUNC_ARG) {
  15406. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  15407. }
  15408. if (ret == BAD_FUNC_ARG) {
  15409. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  15410. }
  15411. if (ret != BAD_FUNC_ARG) {
  15412. ret = WOLFSSL_FATAL_ERROR;
  15413. }
  15414. else {
  15415. ret = 0;
  15416. }
  15417. }
  15418. wc_Des3Free(&des);
  15419. res = TEST_RES_CHECK(ret == 0);
  15420. #endif
  15421. return res;
  15422. } /* END wc_Des3_CbcEncrypt */
  15423. /*
  15424. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15425. */
  15426. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15427. {
  15428. int res = TEST_SKIPPED;
  15429. #ifndef NO_DES3
  15430. int ret = 0;
  15431. word32 vectorSz, cipherSz;
  15432. byte cipher[24];
  15433. byte plain[24];
  15434. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15435. {
  15436. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15437. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15438. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15439. };
  15440. byte key[] =
  15441. {
  15442. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15443. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15444. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15445. };
  15446. byte iv[] =
  15447. {
  15448. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15449. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15450. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15451. };
  15452. vectorSz = sizeof(byte) * 24;
  15453. cipherSz = sizeof(byte) * 24;
  15454. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  15455. if (ret == 0) {
  15456. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  15457. if (ret == 0) {
  15458. if (XMEMCMP(plain, vector, 24) != 0) {
  15459. ret = WOLFSSL_FATAL_ERROR;
  15460. }
  15461. }
  15462. }
  15463. /* pass in bad args. */
  15464. if (ret == 0) {
  15465. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  15466. if (ret == BAD_FUNC_ARG) {
  15467. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  15468. }
  15469. if (ret == BAD_FUNC_ARG) {
  15470. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  15471. }
  15472. if (ret == BAD_FUNC_ARG) {
  15473. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  15474. key, NULL);
  15475. }
  15476. else {
  15477. /* Return code catch. */
  15478. ret = WOLFSSL_FAILURE;
  15479. }
  15480. }
  15481. if (ret == 0) {
  15482. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  15483. if (ret == BAD_FUNC_ARG) {
  15484. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  15485. }
  15486. if (ret == BAD_FUNC_ARG) {
  15487. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  15488. }
  15489. if (ret == BAD_FUNC_ARG) {
  15490. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  15491. }
  15492. else {
  15493. ret = WOLFSSL_FAILURE;
  15494. }
  15495. }
  15496. res = TEST_RES_CHECK(ret == 0);
  15497. #endif
  15498. return res;
  15499. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15500. /*
  15501. * Unit test for wc_Des3_EcbEncrypt
  15502. */
  15503. static int test_wc_Des3_EcbEncrypt(void)
  15504. {
  15505. int res = TEST_SKIPPED;
  15506. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15507. Des3 des;
  15508. int ret = 0;
  15509. byte cipher[24];
  15510. word32 cipherSz = sizeof(cipher);
  15511. const byte key[] =
  15512. {
  15513. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15514. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15515. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15516. };
  15517. const byte iv[] =
  15518. {
  15519. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15520. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15521. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15522. };
  15523. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15524. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15525. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15526. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15527. };
  15528. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  15529. if (ret != 0) {
  15530. return ret;
  15531. }
  15532. if (ret == 0 ) {
  15533. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  15534. }
  15535. /* Bad Cases */
  15536. if (ret == 0) {
  15537. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  15538. if (ret == BAD_FUNC_ARG) {
  15539. ret = 0;
  15540. }
  15541. }
  15542. if (ret == 0) {
  15543. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  15544. if (ret == BAD_FUNC_ARG) {
  15545. ret = 0;
  15546. }
  15547. }
  15548. if (ret == 0) {
  15549. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  15550. if (ret == BAD_FUNC_ARG) {
  15551. ret = 0;
  15552. }
  15553. }
  15554. if (ret == 0) {
  15555. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  15556. if (ret == BAD_FUNC_ARG) {
  15557. ret = 0;
  15558. }
  15559. }
  15560. if (ret == 0) {
  15561. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  15562. if (ret == BAD_FUNC_ARG) {
  15563. ret = 0;
  15564. }
  15565. }
  15566. /* Good Cases */
  15567. if (ret == 0) {
  15568. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  15569. }
  15570. wc_Des3Free(&des);
  15571. res = TEST_RES_CHECK(ret == 0);
  15572. #endif
  15573. return res;
  15574. } /* END test_wc_Des3_EcbEncrypt */
  15575. /*
  15576. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15577. */
  15578. static int test_wc_Chacha_SetKey(void)
  15579. {
  15580. int res = TEST_SKIPPED;
  15581. #ifdef HAVE_CHACHA
  15582. ChaCha ctx;
  15583. const byte key[] =
  15584. {
  15585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15587. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15588. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15589. };
  15590. byte cipher[128];
  15591. int ret = 0;
  15592. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15593. /* Test bad args. */
  15594. if (ret == 0) {
  15595. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15596. if (ret == BAD_FUNC_ARG) {
  15597. ret = wc_Chacha_SetKey(&ctx, key, 18);
  15598. }
  15599. if (ret == BAD_FUNC_ARG) {
  15600. ret = 0;
  15601. }
  15602. else {
  15603. ret = WOLFSSL_FATAL_ERROR;
  15604. }
  15605. }
  15606. if (ret == 0) {
  15607. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  15608. }
  15609. if (ret == 0) {
  15610. /* Test bad args. */
  15611. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  15612. if (ret == BAD_FUNC_ARG) {
  15613. ret = 0;
  15614. }
  15615. else {
  15616. ret = WOLFSSL_FAILURE;
  15617. }
  15618. }
  15619. res = TEST_RES_CHECK(ret == 0);
  15620. #endif
  15621. return res;
  15622. } /* END test_wc_Chacha_SetKey */
  15623. /*
  15624. * unit test for wc_Poly1305SetKey()
  15625. */
  15626. static int test_wc_Poly1305SetKey(void)
  15627. {
  15628. int res = TEST_SKIPPED;
  15629. #ifdef HAVE_POLY1305
  15630. Poly1305 ctx;
  15631. const byte key[] =
  15632. {
  15633. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15634. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15635. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15636. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15637. };
  15638. int ret = 0;
  15639. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15640. /* Test bad args. */
  15641. if (ret == 0) {
  15642. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15643. if (ret == BAD_FUNC_ARG) {
  15644. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  15645. }
  15646. if (ret == BAD_FUNC_ARG) {
  15647. ret = wc_Poly1305SetKey(&ctx, key, 18);
  15648. }
  15649. if (ret == BAD_FUNC_ARG) {
  15650. ret = 0;
  15651. }
  15652. else {
  15653. ret = WOLFSSL_FATAL_ERROR;
  15654. }
  15655. }
  15656. res = TEST_RES_CHECK(ret == 0);
  15657. #endif
  15658. return res;
  15659. } /* END test_wc_Poly1305_SetKey() */
  15660. /*
  15661. * Testing wc_Chacha_Process()
  15662. */
  15663. static int test_wc_Chacha_Process(void)
  15664. {
  15665. int res = TEST_SKIPPED;
  15666. #ifdef HAVE_CHACHA
  15667. ChaCha enc, dec;
  15668. byte cipher[128];
  15669. byte plain[128];
  15670. const byte key[] =
  15671. {
  15672. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15673. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15674. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15675. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15676. };
  15677. const char* input = "Everybody gets Friday off.";
  15678. word32 keySz = sizeof(key)/sizeof(byte);
  15679. unsigned long int inlen = XSTRLEN(input);
  15680. int ret = 0;
  15681. /*Initialize stack varialbes.*/
  15682. XMEMSET(cipher, 0, 128);
  15683. XMEMSET(plain, 0, 128);
  15684. ret = wc_Chacha_SetKey(&enc, key, keySz);
  15685. AssertIntEQ(ret, 0);
  15686. ret = wc_Chacha_SetKey(&dec, key, keySz);
  15687. AssertIntEQ(ret, 0);
  15688. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15689. AssertIntEQ(ret, 0);
  15690. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15691. AssertIntEQ(ret, 0);
  15692. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  15693. AssertIntEQ(ret, 0);
  15694. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  15695. AssertIntEQ(ret, 0);
  15696. ret = XMEMCMP(input, plain, (int)inlen);
  15697. AssertIntEQ(ret, 0);
  15698. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15699. /* test checking and using leftovers, currently just in C code */
  15700. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15701. AssertIntEQ(ret, 0);
  15702. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15703. AssertIntEQ(ret, 0);
  15704. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  15705. AssertIntEQ(ret, 0);
  15706. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15707. (byte*)input + (inlen - 2), 2);
  15708. AssertIntEQ(ret, 0);
  15709. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  15710. AssertIntEQ(ret, 0);
  15711. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15712. (byte*)input + (inlen - 2), 2);
  15713. AssertIntEQ(ret, 0);
  15714. ret = XMEMCMP(input, plain, (int)inlen);
  15715. AssertIntEQ(ret, 0);
  15716. /* check edge cases with counter increment */
  15717. {
  15718. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15719. const byte expected[] = {
  15720. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15721. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15722. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15723. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15724. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15725. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15726. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15727. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15728. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15729. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15730. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15731. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15732. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15733. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15734. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15735. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15736. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15737. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15738. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15739. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15740. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15741. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15742. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15743. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15744. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15745. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15746. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15747. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15748. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15749. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15750. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15751. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15752. };
  15753. const byte iv2[] = {
  15754. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15755. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15756. };
  15757. byte input2[256];
  15758. int i;
  15759. for (i = 0; i < 256; i++)
  15760. input2[i] = i;
  15761. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  15762. AssertIntEQ(ret, 0);
  15763. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  15764. AssertIntEQ(ret, 0);
  15765. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15766. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  15767. AssertIntEQ(ret, 0);
  15768. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15769. /* partial */
  15770. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  15771. AssertIntEQ(ret, 0);
  15772. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15773. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  15774. AssertIntEQ(ret, 0);
  15775. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15776. }
  15777. #endif
  15778. /* Test bad args. */
  15779. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  15780. AssertIntEQ(ret, BAD_FUNC_ARG);
  15781. if (ret == BAD_FUNC_ARG) {
  15782. ret = 0;
  15783. }
  15784. res = TEST_RES_CHECK(ret == 0);
  15785. #endif
  15786. return res;
  15787. } /* END test_wc_Chacha_Process */
  15788. /*
  15789. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15790. */
  15791. static int test_wc_ChaCha20Poly1305_aead(void)
  15792. {
  15793. int res = TEST_SKIPPED;
  15794. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15795. const byte key[] = {
  15796. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15797. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15798. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15799. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15800. };
  15801. const byte plaintext[] = {
  15802. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15803. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15804. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15805. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15806. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15807. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15808. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15809. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15810. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15811. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15812. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15813. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15814. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15815. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15816. 0x74, 0x2e
  15817. };
  15818. const byte iv[] = {
  15819. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15820. 0x44, 0x45, 0x46, 0x47
  15821. };
  15822. const byte aad[] = { /* additional data */
  15823. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15824. 0xc4, 0xc5, 0xc6, 0xc7
  15825. };
  15826. const byte cipher[] = { /* expected output from operation */
  15827. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15828. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15829. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15830. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15831. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15832. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15833. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15834. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15835. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15836. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15837. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15838. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15839. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15840. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15841. 0x61, 0x16
  15842. };
  15843. const byte authTag[] = { /* expected output from operation */
  15844. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15845. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15846. };
  15847. byte generatedCiphertext[272];
  15848. byte generatedPlaintext[272];
  15849. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15850. int ret = 0;
  15851. /* Initialize stack variables. */
  15852. XMEMSET(generatedCiphertext, 0, 272);
  15853. XMEMSET(generatedPlaintext, 0, 272);
  15854. /* Test Encrypt */
  15855. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  15856. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15857. AssertIntEQ(ret, 0);
  15858. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  15859. AssertIntEQ(ret, 0);
  15860. /* Test bad args. */
  15861. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  15862. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15863. AssertIntEQ(ret, BAD_FUNC_ARG);
  15864. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15865. plaintext, sizeof(plaintext),
  15866. generatedCiphertext, generatedAuthTag);
  15867. AssertIntEQ(ret, BAD_FUNC_ARG);
  15868. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15869. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15870. AssertIntEQ(ret, BAD_FUNC_ARG);
  15871. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15872. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15873. AssertIntEQ(ret, BAD_FUNC_ARG);
  15874. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15875. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  15876. AssertIntEQ(ret, BAD_FUNC_ARG);
  15877. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15878. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  15879. if (ret == BAD_FUNC_ARG) {
  15880. ret = 0;
  15881. (void)ret; /* suppress never read */
  15882. }
  15883. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15884. sizeof(cipher), authTag, generatedPlaintext);
  15885. AssertIntEQ(ret, 0);
  15886. ret = XMEMCMP(generatedPlaintext, plaintext,
  15887. sizeof(plaintext)/sizeof(byte));
  15888. AssertIntEQ(ret, 0);
  15889. /* Test bad args. */
  15890. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15891. sizeof(cipher), authTag, generatedPlaintext);
  15892. AssertIntEQ(ret, BAD_FUNC_ARG);
  15893. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15894. cipher, sizeof(cipher), authTag, generatedPlaintext);
  15895. AssertIntEQ(ret, BAD_FUNC_ARG);
  15896. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15897. sizeof(cipher), authTag, generatedPlaintext);
  15898. AssertIntEQ(ret, BAD_FUNC_ARG);
  15899. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15900. sizeof(cipher), NULL, generatedPlaintext);
  15901. AssertIntEQ(ret, BAD_FUNC_ARG);
  15902. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15903. sizeof(cipher), authTag, NULL);
  15904. AssertIntEQ(ret, BAD_FUNC_ARG);
  15905. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15906. sizeof(cipher), authTag, generatedPlaintext);
  15907. AssertIntEQ(ret, BAD_FUNC_ARG);
  15908. if (ret == BAD_FUNC_ARG) {
  15909. ret = 0;
  15910. }
  15911. res = TEST_RES_CHECK(ret == 0);
  15912. #endif
  15913. return res;
  15914. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  15915. /*
  15916. * Testing function for wc_Rc2SetKey().
  15917. */
  15918. static int test_wc_Rc2SetKey(void)
  15919. {
  15920. int res = TEST_SKIPPED;
  15921. #ifdef WC_RC2
  15922. Rc2 rc2;
  15923. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15924. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15925. int ret = 0;
  15926. /* valid key and IV */
  15927. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15928. iv, 40);
  15929. if (ret == 0) {
  15930. /* valid key, no IV */
  15931. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15932. NULL, 40);
  15933. }
  15934. /* bad arguments */
  15935. if (ret == 0) {
  15936. /* null Rc2 struct */
  15937. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15938. iv, 40);
  15939. if (ret == BAD_FUNC_ARG) {
  15940. ret = 0;
  15941. }
  15942. }
  15943. if (ret == 0) {
  15944. /* null key */
  15945. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15946. iv, 40);
  15947. if (ret == BAD_FUNC_ARG) {
  15948. ret = 0;
  15949. }
  15950. }
  15951. if (ret == 0) {
  15952. /* key size == 0 */
  15953. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  15954. if (ret == WC_KEY_SIZE_E) {
  15955. ret = 0;
  15956. }
  15957. }
  15958. if (ret == 0) {
  15959. /* key size > 128 */
  15960. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  15961. if (ret == WC_KEY_SIZE_E) {
  15962. ret = 0;
  15963. }
  15964. }
  15965. if (ret == 0) {
  15966. /* effective bits == 0 */
  15967. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15968. iv, 0);
  15969. if (ret == WC_KEY_SIZE_E) {
  15970. ret = 0;
  15971. }
  15972. }
  15973. if (ret == 0) {
  15974. /* effective bits > 1024 */
  15975. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15976. iv, 1025);
  15977. if (ret == WC_KEY_SIZE_E) {
  15978. ret = 0;
  15979. }
  15980. }
  15981. res = TEST_RES_CHECK(ret == 0);
  15982. #endif
  15983. return res;
  15984. } /* END test_wc_Rc2SetKey */
  15985. /*
  15986. * Testing function for wc_Rc2SetIV().
  15987. */
  15988. static int test_wc_Rc2SetIV(void)
  15989. {
  15990. int res = TEST_SKIPPED;
  15991. #ifdef WC_RC2
  15992. Rc2 rc2;
  15993. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15994. int ret = 0;
  15995. /* valid IV */
  15996. ret = wc_Rc2SetIV(&rc2, iv);
  15997. if (ret == 0) {
  15998. /* valid NULL IV */
  15999. ret = wc_Rc2SetIV(&rc2, NULL);
  16000. }
  16001. /* bad arguments */
  16002. if (ret == 0) {
  16003. ret = wc_Rc2SetIV(NULL, iv);
  16004. if (ret == BAD_FUNC_ARG) {
  16005. ret = 0;
  16006. }
  16007. }
  16008. res = TEST_RES_CHECK(ret == 0);
  16009. #endif
  16010. return res;
  16011. } /* END test_wc_Rc2SetKey */
  16012. /*
  16013. * Testing function for wc_Rc2EcbEncrypt().
  16014. */
  16015. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16016. {
  16017. int res = TEST_SKIPPED;
  16018. #ifdef WC_RC2
  16019. Rc2 rc2;
  16020. int ret = 0;
  16021. int effectiveKeyBits = 63;
  16022. byte cipher[RC2_BLOCK_SIZE];
  16023. byte plain[RC2_BLOCK_SIZE];
  16024. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16025. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16026. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16027. XMEMSET(cipher, 0, sizeof(cipher));
  16028. XMEMSET(plain, 0, sizeof(plain));
  16029. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16030. NULL, effectiveKeyBits);
  16031. if (ret == 0) {
  16032. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  16033. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  16034. ret = WOLFSSL_FATAL_ERROR;
  16035. }
  16036. if (ret == 0) {
  16037. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  16038. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  16039. ret = WOLFSSL_FATAL_ERROR;
  16040. }
  16041. }
  16042. }
  16043. /* Rc2EcbEncrypt bad arguments */
  16044. if (ret == 0) {
  16045. /* null Rc2 struct */
  16046. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  16047. if (ret == BAD_FUNC_ARG) {
  16048. ret = 0;
  16049. }
  16050. }
  16051. if (ret == 0) {
  16052. /* null out buffer */
  16053. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  16054. if (ret == BAD_FUNC_ARG) {
  16055. ret = 0;
  16056. }
  16057. }
  16058. if (ret == 0) {
  16059. /* null input buffer */
  16060. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  16061. if (ret == BAD_FUNC_ARG) {
  16062. ret = 0;
  16063. }
  16064. }
  16065. if (ret == 0) {
  16066. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16067. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  16068. if (ret == BUFFER_E) {
  16069. ret = 0;
  16070. }
  16071. }
  16072. /* Rc2EcbDecrypt bad arguments */
  16073. if (ret == 0) {
  16074. /* null Rc2 struct */
  16075. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  16076. if (ret == BAD_FUNC_ARG) {
  16077. ret = 0;
  16078. }
  16079. }
  16080. if (ret == 0) {
  16081. /* null out buffer */
  16082. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  16083. if (ret == BAD_FUNC_ARG) {
  16084. ret = 0;
  16085. }
  16086. }
  16087. if (ret == 0) {
  16088. /* null input buffer */
  16089. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  16090. if (ret == BAD_FUNC_ARG) {
  16091. ret = 0;
  16092. }
  16093. }
  16094. if (ret == 0) {
  16095. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16096. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  16097. if (ret == BUFFER_E) {
  16098. ret = 0;
  16099. }
  16100. }
  16101. res = TEST_RES_CHECK(ret == 0);
  16102. #endif
  16103. return res;
  16104. } /* END test_wc_Rc2SetKey */
  16105. /*
  16106. * Testing function for wc_Rc2CbcEncrypt().
  16107. */
  16108. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16109. {
  16110. int res = TEST_SKIPPED;
  16111. #ifdef WC_RC2
  16112. Rc2 rc2;
  16113. int ret = 0;
  16114. int effectiveKeyBits = 63;
  16115. byte cipher[RC2_BLOCK_SIZE*2];
  16116. byte plain[RC2_BLOCK_SIZE*2];
  16117. /* vector taken from test.c */
  16118. byte key[] = {
  16119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16120. };
  16121. byte iv[] = {
  16122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16123. };
  16124. byte input[] = {
  16125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16127. };
  16128. byte output[] = {
  16129. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16130. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16131. };
  16132. XMEMSET(cipher, 0, sizeof(cipher));
  16133. XMEMSET(plain, 0, sizeof(plain));
  16134. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16135. iv, effectiveKeyBits);
  16136. if (ret == 0) {
  16137. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  16138. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  16139. ret = WOLFSSL_FATAL_ERROR;
  16140. }
  16141. else {
  16142. /* reset IV for decrypt */
  16143. ret = wc_Rc2SetIV(&rc2, iv);
  16144. }
  16145. if (ret == 0) {
  16146. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  16147. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  16148. ret = WOLFSSL_FATAL_ERROR;
  16149. }
  16150. }
  16151. }
  16152. /* Rc2CbcEncrypt bad arguments */
  16153. if (ret == 0) {
  16154. /* null Rc2 struct */
  16155. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  16156. if (ret == BAD_FUNC_ARG) {
  16157. ret = 0;
  16158. }
  16159. }
  16160. if (ret == 0) {
  16161. /* null out buffer */
  16162. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  16163. if (ret == BAD_FUNC_ARG) {
  16164. ret = 0;
  16165. }
  16166. }
  16167. if (ret == 0) {
  16168. /* null input buffer */
  16169. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  16170. if (ret == BAD_FUNC_ARG) {
  16171. ret = 0;
  16172. }
  16173. }
  16174. /* Rc2CbcDecrypt bad arguments */
  16175. if (ret == 0) {
  16176. /* in size is 0 */
  16177. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  16178. if (ret != 0) {
  16179. ret = WOLFSSL_FATAL_ERROR;
  16180. }
  16181. }
  16182. if (ret == 0) {
  16183. /* null Rc2 struct */
  16184. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  16185. if (ret == BAD_FUNC_ARG) {
  16186. ret = 0;
  16187. }
  16188. }
  16189. if (ret == 0) {
  16190. /* null out buffer */
  16191. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  16192. if (ret == BAD_FUNC_ARG) {
  16193. ret = 0;
  16194. }
  16195. }
  16196. if (ret == 0) {
  16197. /* null input buffer */
  16198. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  16199. if (ret == BAD_FUNC_ARG) {
  16200. ret = 0;
  16201. }
  16202. }
  16203. res = TEST_RES_CHECK(ret == 0);
  16204. #endif
  16205. return res;
  16206. } /* END test_wc_Rc2SetKey */
  16207. /*
  16208. * Testing function for wc_AesSetIV
  16209. */
  16210. static int test_wc_AesSetIV(void)
  16211. {
  16212. int res = TEST_SKIPPED;
  16213. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16214. Aes aes;
  16215. int ret = 0;
  16216. byte key16[] =
  16217. {
  16218. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16219. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16220. };
  16221. byte iv1[] = "1234567890abcdef";
  16222. byte iv2[] = "0987654321fedcba";
  16223. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16224. if (ret != 0)
  16225. return ret;
  16226. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16227. iv1, AES_ENCRYPTION);
  16228. if (ret == 0) {
  16229. ret = wc_AesSetIV(&aes, iv2);
  16230. }
  16231. /* Test bad args. */
  16232. if (ret == 0) {
  16233. ret = wc_AesSetIV(NULL, iv1);
  16234. if (ret == BAD_FUNC_ARG) {
  16235. /* NULL iv should return 0. */
  16236. ret = wc_AesSetIV(&aes, NULL);
  16237. }
  16238. else {
  16239. ret = WOLFSSL_FATAL_ERROR;
  16240. }
  16241. }
  16242. wc_AesFree(&aes);
  16243. res = TEST_RES_CHECK(ret == 0);
  16244. #endif
  16245. return res;
  16246. } /* test_wc_AesSetIV */
  16247. /*
  16248. * Testing function for wc_AesSetKey().
  16249. */
  16250. static int test_wc_AesSetKey(void)
  16251. {
  16252. int res = TEST_SKIPPED;
  16253. #ifndef NO_AES
  16254. Aes aes;
  16255. int ret = 0;
  16256. byte key16[] =
  16257. {
  16258. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16259. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16260. };
  16261. #ifdef WOLFSSL_AES_192
  16262. byte key24[] =
  16263. {
  16264. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16265. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16266. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16267. };
  16268. #endif
  16269. #ifdef WOLFSSL_AES_256
  16270. byte key32[] =
  16271. {
  16272. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16273. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16274. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16275. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16276. };
  16277. #endif
  16278. byte badKey16[] =
  16279. {
  16280. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16281. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16282. };
  16283. byte iv[] = "1234567890abcdef";
  16284. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16285. if (ret != 0)
  16286. return ret;
  16287. #ifdef WOLFSSL_AES_128
  16288. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16289. iv, AES_ENCRYPTION);
  16290. #endif
  16291. #ifdef WOLFSSL_AES_192
  16292. if (ret == 0) {
  16293. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  16294. iv, AES_ENCRYPTION);
  16295. }
  16296. #endif
  16297. #ifdef WOLFSSL_AES_256
  16298. if (ret == 0) {
  16299. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  16300. iv, AES_ENCRYPTION);
  16301. }
  16302. #endif
  16303. /* Pass in bad args. */
  16304. if (ret == 0) {
  16305. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  16306. iv, AES_ENCRYPTION);
  16307. if (ret == BAD_FUNC_ARG) {
  16308. ret = wc_AesSetKey(&aes, badKey16,
  16309. (word32) sizeof(badKey16) / sizeof(byte),
  16310. iv, AES_ENCRYPTION);
  16311. }
  16312. if (ret == BAD_FUNC_ARG) {
  16313. ret = 0;
  16314. }
  16315. else {
  16316. ret = WOLFSSL_FATAL_ERROR;
  16317. }
  16318. }
  16319. wc_AesFree(&aes);
  16320. res = TEST_RES_CHECK(ret == 0);
  16321. #endif
  16322. return res;
  16323. } /* END test_wc_AesSetKey */
  16324. /*
  16325. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16326. * and wc_AesCbcDecryptWithKey()
  16327. */
  16328. static int test_wc_AesCbcEncryptDecrypt(void)
  16329. {
  16330. int res = TEST_SKIPPED;
  16331. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16332. defined(WOLFSSL_AES_256)
  16333. Aes aes;
  16334. int ret = 0;
  16335. byte key32[] =
  16336. {
  16337. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16338. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16339. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16340. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16341. };
  16342. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  16343. {
  16344. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16345. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16346. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  16347. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  16348. };
  16349. byte iv[] = "1234567890abcdef";
  16350. byte enc[sizeof(vector)];
  16351. byte dec[sizeof(vector)];
  16352. int cbcE = WOLFSSL_FATAL_ERROR;
  16353. int cbcD = WOLFSSL_FATAL_ERROR;
  16354. int cbcDWK = WOLFSSL_FATAL_ERROR;
  16355. byte dec2[sizeof(vector)];
  16356. /* Init stack variables. */
  16357. XMEMSET(enc, 0, sizeof(enc));
  16358. XMEMSET(dec, 0, sizeof(vector));
  16359. XMEMSET(dec2, 0, sizeof(vector));
  16360. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16361. if (ret != 0)
  16362. return ret;
  16363. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  16364. if (ret == 0) {
  16365. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  16366. if (ret == 0) {
  16367. /* Re init for decrypt and set flag. */
  16368. cbcE = 0;
  16369. wc_AesFree(&aes);
  16370. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  16371. iv, AES_DECRYPTION);
  16372. }
  16373. if (ret == 0) {
  16374. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  16375. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  16376. ret = WOLFSSL_FATAL_ERROR;
  16377. }
  16378. else {
  16379. /* Set flag. */
  16380. cbcD = 0;
  16381. }
  16382. }
  16383. }
  16384. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  16385. if (ret == 0 || cbcE == 0) {
  16386. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16387. key32, sizeof(key32)/sizeof(byte), iv);
  16388. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  16389. cbcDWK = 0;
  16390. }
  16391. }
  16392. /* Pass in bad args */
  16393. if (cbcE == 0) {
  16394. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  16395. if (cbcE == BAD_FUNC_ARG) {
  16396. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  16397. }
  16398. if (cbcE == BAD_FUNC_ARG) {
  16399. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  16400. }
  16401. if (cbcE == BAD_FUNC_ARG) {
  16402. cbcE = 0;
  16403. }
  16404. else {
  16405. cbcE = WOLFSSL_FATAL_ERROR;
  16406. }
  16407. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16408. if (cbcE == 0) {
  16409. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  16410. }
  16411. if (cbcE == BAD_LENGTH_E) {
  16412. cbcE = 0;
  16413. }
  16414. else {
  16415. cbcE = WOLFSSL_FATAL_ERROR;
  16416. }
  16417. #endif
  16418. }
  16419. if (cbcE == 0) {
  16420. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16421. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16422. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16423. "mode (v2), skip test");
  16424. #else
  16425. /* Test passing in size of 0 */
  16426. XMEMSET(enc, 0, sizeof(enc));
  16427. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  16428. if (cbcE == 0) {
  16429. /* Check enc was not modified */
  16430. int i;
  16431. for (i = 0; i < (int)sizeof(enc); i++)
  16432. cbcE |= enc[i];
  16433. }
  16434. #endif
  16435. }
  16436. if (cbcE != 0) {
  16437. wc_AesFree(&aes);
  16438. return TEST_FAIL;
  16439. }
  16440. if (cbcD == 0) {
  16441. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  16442. if (cbcD == BAD_FUNC_ARG) {
  16443. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  16444. }
  16445. if (cbcD == BAD_FUNC_ARG) {
  16446. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  16447. }
  16448. if (cbcD == BAD_FUNC_ARG) {
  16449. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  16450. }
  16451. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16452. if (cbcD == BAD_LENGTH_E) {
  16453. cbcD = 0;
  16454. }
  16455. else {
  16456. cbcD = WOLFSSL_FATAL_ERROR;
  16457. }
  16458. #else
  16459. if (cbcD == BAD_FUNC_ARG) {
  16460. cbcD = 0;
  16461. }
  16462. else {
  16463. cbcD = WOLFSSL_FATAL_ERROR;
  16464. }
  16465. #endif
  16466. }
  16467. if (cbcD == 0) {
  16468. /* Test passing in size of 0 */
  16469. XMEMSET(dec, 0, sizeof(dec));
  16470. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  16471. if (cbcD == 0) {
  16472. /* Check dec was not modified */
  16473. int i;
  16474. for (i = 0; i < (int)sizeof(dec); i++)
  16475. cbcD |= dec[i];
  16476. }
  16477. }
  16478. if (cbcD != 0) {
  16479. wc_AesFree(&aes);
  16480. return TEST_FAIL;
  16481. }
  16482. if (cbcDWK == 0) {
  16483. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16484. key32, sizeof(key32)/sizeof(byte), iv);
  16485. if (cbcDWK == BAD_FUNC_ARG) {
  16486. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16487. key32, sizeof(key32)/sizeof(byte), iv);
  16488. }
  16489. if (cbcDWK == BAD_FUNC_ARG) {
  16490. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16491. NULL, sizeof(key32)/sizeof(byte), iv);
  16492. }
  16493. if (cbcDWK == BAD_FUNC_ARG) {
  16494. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16495. key32, sizeof(key32)/sizeof(byte), NULL);
  16496. }
  16497. if (cbcDWK == BAD_FUNC_ARG) {
  16498. cbcDWK = 0;
  16499. }
  16500. else {
  16501. cbcDWK = WOLFSSL_FATAL_ERROR;
  16502. }
  16503. }
  16504. wc_AesFree(&aes);
  16505. res = TEST_RES_CHECK(cbcDWK == 0);
  16506. #endif
  16507. return res;
  16508. } /* END test_wc_AesCbcEncryptDecrypt */
  16509. /*
  16510. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16511. */
  16512. static int test_wc_AesCtrEncryptDecrypt(void)
  16513. {
  16514. int res = TEST_SKIPPED;
  16515. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16516. Aes aesEnc, aesDec;
  16517. int ret = 0;
  16518. byte key32[] =
  16519. {
  16520. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16521. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16522. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16523. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16524. };
  16525. byte vector[] = /* Now is the time for all w/o trailing 0 */
  16526. {
  16527. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16528. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16529. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16530. };
  16531. byte iv[] = "1234567890abcdef";
  16532. byte enc[AES_BLOCK_SIZE * 2];
  16533. byte dec[AES_BLOCK_SIZE * 2];
  16534. /* Init stack variables. */
  16535. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16536. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16537. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  16538. if (ret != 0)
  16539. return ret;
  16540. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  16541. if (ret != 0) {
  16542. wc_AesFree(&aesEnc);
  16543. return ret;
  16544. }
  16545. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  16546. iv, AES_ENCRYPTION);
  16547. if (ret == 0) {
  16548. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16549. sizeof(vector)/sizeof(byte));
  16550. if (ret == 0) {
  16551. /* Decrypt with wc_AesCtrEncrypt() */
  16552. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  16553. iv, AES_ENCRYPTION);
  16554. }
  16555. if (ret == 0) {
  16556. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  16557. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  16558. ret = WOLFSSL_FATAL_ERROR;
  16559. }
  16560. }
  16561. }
  16562. /* Test bad args. */
  16563. if (ret == 0) {
  16564. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  16565. if (ret == BAD_FUNC_ARG) {
  16566. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  16567. }
  16568. if (ret == BAD_FUNC_ARG) {
  16569. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  16570. }
  16571. if (ret == BAD_FUNC_ARG) {
  16572. ret = 0;
  16573. }
  16574. else {
  16575. ret = WOLFSSL_FATAL_ERROR;
  16576. }
  16577. }
  16578. wc_AesFree(&aesEnc);
  16579. wc_AesFree(&aesDec);
  16580. res = TEST_RES_CHECK(ret == 0);
  16581. #endif
  16582. return res;
  16583. } /* END test_wc_AesCtrEncryptDecrypt */
  16584. /*
  16585. * test function for wc_AesGcmSetKey()
  16586. */
  16587. static int test_wc_AesGcmSetKey(void)
  16588. {
  16589. int res = TEST_SKIPPED;
  16590. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16591. Aes aes;
  16592. int ret = 0;
  16593. #ifdef WOLFSSL_AES_128
  16594. byte key16[] =
  16595. {
  16596. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16597. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16598. };
  16599. #endif
  16600. #ifdef WOLFSSL_AES_192
  16601. byte key24[] =
  16602. {
  16603. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16604. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16605. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16606. };
  16607. #endif
  16608. #ifdef WOLFSSL_AES_256
  16609. byte key32[] =
  16610. {
  16611. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16612. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16613. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16614. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16615. };
  16616. #endif
  16617. byte badKey16[] =
  16618. {
  16619. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16620. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16621. };
  16622. byte badKey24[] =
  16623. {
  16624. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16625. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16626. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16627. };
  16628. byte badKey32[] =
  16629. {
  16630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16632. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16633. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16634. };
  16635. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16636. if (ret != 0)
  16637. return ret;
  16638. #ifdef WOLFSSL_AES_128
  16639. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  16640. #endif
  16641. #ifdef WOLFSSL_AES_192
  16642. if (ret == 0) {
  16643. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  16644. }
  16645. #endif
  16646. #ifdef WOLFSSL_AES_256
  16647. if (ret == 0) {
  16648. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16649. }
  16650. #endif
  16651. /* Pass in bad args. */
  16652. if (ret == 0) {
  16653. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  16654. if (ret == BAD_FUNC_ARG) {
  16655. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  16656. }
  16657. if (ret == BAD_FUNC_ARG) {
  16658. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  16659. }
  16660. if (ret == BAD_FUNC_ARG) {
  16661. ret = 0;
  16662. }
  16663. else {
  16664. ret = WOLFSSL_FATAL_ERROR;
  16665. }
  16666. }
  16667. wc_AesFree(&aes);
  16668. res = TEST_RES_CHECK(ret == 0);
  16669. #endif
  16670. return res;
  16671. } /* END test_wc_AesGcmSetKey */
  16672. /*
  16673. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16674. */
  16675. static int test_wc_AesGcmEncryptDecrypt(void)
  16676. {
  16677. int res = TEST_SKIPPED;
  16678. /* WOLFSSL_AFALG requires 12 byte IV */
  16679. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16680. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16681. Aes aes;
  16682. byte key32[] =
  16683. {
  16684. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16685. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16686. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16687. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16688. };
  16689. byte vector[] = /* Now is the time for all w/o trailing 0 */
  16690. {
  16691. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16692. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16693. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16694. };
  16695. const byte a[] =
  16696. {
  16697. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16698. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16699. 0xab, 0xad, 0xda, 0xd2
  16700. };
  16701. byte iv[] = "1234567890a";
  16702. byte longIV[] = "1234567890abcdefghij";
  16703. byte enc[sizeof(vector)];
  16704. byte resultT[AES_BLOCK_SIZE];
  16705. byte dec[sizeof(vector)];
  16706. int gcmD = WOLFSSL_FATAL_ERROR;
  16707. int gcmE = WOLFSSL_FATAL_ERROR;
  16708. int ret = 0;
  16709. /* Init stack variables. */
  16710. XMEMSET(enc, 0, sizeof(vector));
  16711. XMEMSET(dec, 0, sizeof(vector));
  16712. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16713. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16714. if (ret != 0)
  16715. return ret;
  16716. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16717. if (ret == 0) {
  16718. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  16719. iv, sizeof(iv)/sizeof(byte), resultT,
  16720. sizeof(resultT), a, sizeof(a));
  16721. }
  16722. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  16723. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  16724. iv, sizeof(iv)/sizeof(byte), resultT,
  16725. sizeof(resultT), a, sizeof(a));
  16726. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  16727. gcmD = WOLFSSL_FATAL_ERROR;
  16728. }
  16729. }
  16730. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16731. if (gcmE == 0) {
  16732. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  16733. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  16734. a, sizeof(a));
  16735. if (gcmE == BAD_FUNC_ARG) {
  16736. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16737. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  16738. resultT, sizeof(resultT) + 1, a, sizeof(a));
  16739. }
  16740. if (gcmE == BAD_FUNC_ARG) {
  16741. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16742. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  16743. resultT, sizeof(resultT) - 5, a, sizeof(a));
  16744. }
  16745. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16746. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16747. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16748. /* FIPS does not check the lower bound of ivSz */
  16749. #else
  16750. if (gcmE == BAD_FUNC_ARG) {
  16751. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16752. sizeof(vector), iv, 0,
  16753. resultT, sizeof(resultT), a, sizeof(a));
  16754. }
  16755. #endif
  16756. if (gcmE == BAD_FUNC_ARG) {
  16757. gcmE = 0;
  16758. }
  16759. else {
  16760. gcmE = WOLFSSL_FATAL_ERROR;
  16761. }
  16762. }
  16763. /* This case is now considered good. Long IVs are now allowed.
  16764. * Except for the original FIPS release, it still has an upper
  16765. * bound on the IV length. */
  16766. #if (!defined(HAVE_FIPS) || \
  16767. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16768. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16769. if (gcmE == 0) {
  16770. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16771. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  16772. a, sizeof(a));
  16773. }
  16774. #else
  16775. (void)longIV;
  16776. #endif /* Old FIPS */
  16777. /* END wc_AesGcmEncrypt */
  16778. if (gcmE != 0) {
  16779. wc_AesFree(&aes);
  16780. return TEST_FAIL;
  16781. }
  16782. #ifdef HAVE_AES_DECRYPT
  16783. if (gcmD == 0) {
  16784. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  16785. iv, sizeof(iv)/sizeof(byte), resultT,
  16786. sizeof(resultT), a, sizeof(a));
  16787. if (gcmD == BAD_FUNC_ARG) {
  16788. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  16789. iv, sizeof(iv)/sizeof(byte), resultT,
  16790. sizeof(resultT), a, sizeof(a));
  16791. }
  16792. if (gcmD == BAD_FUNC_ARG) {
  16793. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  16794. iv, sizeof(iv)/sizeof(byte), resultT,
  16795. sizeof(resultT), a, sizeof(a));
  16796. }
  16797. if (gcmD == BAD_FUNC_ARG) {
  16798. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16799. NULL, sizeof(iv)/sizeof(byte), resultT,
  16800. sizeof(resultT), a, sizeof(a));
  16801. }
  16802. if (gcmD == BAD_FUNC_ARG) {
  16803. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16804. iv, sizeof(iv)/sizeof(byte), NULL,
  16805. sizeof(resultT), a, sizeof(a));
  16806. }
  16807. if (gcmD == BAD_FUNC_ARG) {
  16808. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16809. iv, sizeof(iv)/sizeof(byte), resultT,
  16810. sizeof(resultT) + 1, a, sizeof(a));
  16811. }
  16812. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16813. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16814. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16815. /* FIPS does not check the lower bound of ivSz */
  16816. #else
  16817. if (gcmD == BAD_FUNC_ARG) {
  16818. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16819. iv, 0, resultT,
  16820. sizeof(resultT), a, sizeof(a));
  16821. }
  16822. #endif
  16823. if (gcmD == BAD_FUNC_ARG) {
  16824. gcmD = 0;
  16825. }
  16826. else {
  16827. gcmD = WOLFSSL_FATAL_ERROR;
  16828. }
  16829. res = TEST_RES_CHECK(gcmD == 0);
  16830. } /* END wc_AesGcmDecrypt */
  16831. #endif /* HAVE_AES_DECRYPT */
  16832. wc_AesFree(&aes);
  16833. #endif
  16834. return res;
  16835. } /* END test_wc_AesGcmEncryptDecrypt */
  16836. /*
  16837. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16838. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16839. */
  16840. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16841. {
  16842. int ret = TEST_SKIPPED;
  16843. #if (!defined(HAVE_FIPS) || \
  16844. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16845. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16846. const byte key[] = {
  16847. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16848. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16849. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16850. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16851. };
  16852. const byte in[] = {
  16853. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16854. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16855. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16856. };
  16857. const byte aad[] = {
  16858. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16859. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16860. 0xab, 0xad, 0xda, 0xd2
  16861. };
  16862. Aes aesEnc, aesDec;
  16863. byte iv[] = "1234567890abcdefghij";
  16864. byte out[sizeof(in)];
  16865. byte plain[sizeof(in)];
  16866. byte tag[AES_BLOCK_SIZE];
  16867. XMEMSET(out, 0, sizeof(out));
  16868. XMEMSET(plain, 0, sizeof(plain));
  16869. XMEMSET(tag, 0, sizeof(tag));
  16870. /* Perform one-shot encryption using long IV */
  16871. AssertIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16872. AssertIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16873. AssertIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16874. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16875. /* Perform streaming decryption using long IV */
  16876. AssertIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16877. AssertIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16878. AssertIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16879. sizeof(aad)), 0);
  16880. AssertIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16881. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16882. /* Free resources */
  16883. wc_AesFree(&aesEnc);
  16884. wc_AesFree(&aesDec);
  16885. ret = TEST_SUCCESS;
  16886. #endif
  16887. return ret;
  16888. } /* END wc_AesGcmMixedEncDecLongIV */
  16889. /*
  16890. * unit test for wc_GmacSetKey()
  16891. */
  16892. static int test_wc_GmacSetKey(void)
  16893. {
  16894. int res = TEST_SKIPPED;
  16895. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16896. Gmac gmac;
  16897. byte key16[] =
  16898. {
  16899. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16900. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16901. };
  16902. #ifdef WOLFSSL_AES_192
  16903. byte key24[] =
  16904. {
  16905. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16906. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16907. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16908. };
  16909. #endif
  16910. #ifdef WOLFSSL_AES_256
  16911. byte key32[] =
  16912. {
  16913. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16914. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16915. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16916. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16917. };
  16918. #endif
  16919. byte badKey16[] =
  16920. {
  16921. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16922. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16923. };
  16924. byte badKey24[] =
  16925. {
  16926. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16927. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16928. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16929. };
  16930. byte badKey32[] =
  16931. {
  16932. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16933. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16934. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16935. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16936. };
  16937. int ret = 0;
  16938. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  16939. if (ret != 0)
  16940. return ret;
  16941. #ifdef WOLFSSL_AES_128
  16942. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  16943. #endif
  16944. #ifdef WOLFSSL_AES_192
  16945. if (ret == 0) {
  16946. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  16947. }
  16948. #endif
  16949. #ifdef WOLFSSL_AES_256
  16950. if (ret == 0) {
  16951. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  16952. }
  16953. #endif
  16954. /* Pass in bad args. */
  16955. if (ret == 0) {
  16956. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  16957. if (ret == BAD_FUNC_ARG) {
  16958. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  16959. }
  16960. if (ret == BAD_FUNC_ARG) {
  16961. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  16962. }
  16963. if (ret == BAD_FUNC_ARG) {
  16964. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  16965. }
  16966. if (ret == BAD_FUNC_ARG) {
  16967. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  16968. }
  16969. if (ret == BAD_FUNC_ARG) {
  16970. ret = 0;
  16971. }
  16972. else {
  16973. ret = WOLFSSL_FATAL_ERROR;
  16974. }
  16975. }
  16976. wc_AesFree(&gmac.aes);
  16977. res = TEST_RES_CHECK(ret == 0);
  16978. #endif
  16979. return res;
  16980. } /* END test_wc_GmacSetKey */
  16981. /*
  16982. * unit test for wc_GmacUpdate
  16983. */
  16984. static int test_wc_GmacUpdate(void)
  16985. {
  16986. int res = TEST_SKIPPED;
  16987. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16988. Gmac gmac;
  16989. #ifdef WOLFSSL_AES_128
  16990. const byte key16[] =
  16991. {
  16992. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16993. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16994. };
  16995. #endif
  16996. #ifdef WOLFSSL_AES_192
  16997. byte key24[] =
  16998. {
  16999. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  17000. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  17001. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  17002. };
  17003. #endif
  17004. #ifdef WOLFSSL_AES_256
  17005. byte key32[] =
  17006. {
  17007. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  17008. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  17009. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  17010. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  17011. };
  17012. #endif
  17013. #ifdef WOLFSSL_AES_128
  17014. const byte authIn[] =
  17015. {
  17016. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  17017. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  17018. };
  17019. #endif
  17020. #ifdef WOLFSSL_AES_192
  17021. const byte authIn2[] =
  17022. {
  17023. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  17024. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  17025. };
  17026. #endif
  17027. const byte authIn3[] =
  17028. {
  17029. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  17030. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  17031. };
  17032. #ifdef WOLFSSL_AES_128
  17033. const byte tag1[] = /* Known. */
  17034. {
  17035. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  17036. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  17037. };
  17038. #endif
  17039. #ifdef WOLFSSL_AES_192
  17040. const byte tag2[] = /* Known */
  17041. {
  17042. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  17043. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  17044. };
  17045. #endif
  17046. const byte tag3[] = /* Known */
  17047. {
  17048. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  17049. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  17050. };
  17051. #ifdef WOLFSSL_AES_128
  17052. const byte iv[] =
  17053. {
  17054. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  17055. 0xe2, 0x8c, 0x8f, 0x16
  17056. };
  17057. #endif
  17058. #ifdef WOLFSSL_AES_192
  17059. const byte iv2[] =
  17060. {
  17061. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  17062. 0x7e, 0x1a, 0x6f, 0xbc
  17063. };
  17064. #endif
  17065. const byte iv3[] =
  17066. {
  17067. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  17068. 0xc3, 0xfb, 0x6c, 0x8a
  17069. };
  17070. byte tagOut[16];
  17071. byte tagOut2[24];
  17072. byte tagOut3[32];
  17073. int ret = 0;
  17074. /* Init stack variables. */
  17075. XMEMSET(tagOut, 0, sizeof(tagOut));
  17076. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  17077. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  17078. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  17079. if (ret != 0)
  17080. return ret;
  17081. #ifdef WOLFSSL_AES_128
  17082. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  17083. if (ret == 0) {
  17084. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  17085. tagOut, sizeof(tag1));
  17086. if (ret == 0) {
  17087. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  17088. }
  17089. wc_AesFree(&gmac.aes);
  17090. }
  17091. #endif
  17092. #ifdef WOLFSSL_AES_192
  17093. if (ret == 0) {
  17094. XMEMSET(&gmac, 0, sizeof(Gmac));
  17095. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  17096. }
  17097. if (ret == 0) {
  17098. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  17099. sizeof(authIn2), tagOut2, sizeof(tag2));
  17100. }
  17101. if (ret == 0) {
  17102. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  17103. wc_AesFree(&gmac.aes);
  17104. }
  17105. #endif
  17106. #ifdef WOLFSSL_AES_256
  17107. if (ret == 0) {
  17108. XMEMSET(&gmac, 0, sizeof(Gmac));
  17109. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  17110. }
  17111. if (ret == 0) {
  17112. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  17113. sizeof(authIn3), tagOut3, sizeof(tag3));
  17114. }
  17115. if (ret == 0) {
  17116. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  17117. }
  17118. #endif
  17119. /*Pass bad args. */
  17120. if (ret == 0) {
  17121. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  17122. sizeof(authIn3), tagOut3, sizeof(tag3));
  17123. if (ret == BAD_FUNC_ARG) {
  17124. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  17125. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  17126. }
  17127. if (ret == BAD_FUNC_ARG) {
  17128. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  17129. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  17130. }
  17131. if (ret == BAD_FUNC_ARG) {
  17132. ret = 0;
  17133. }
  17134. else {
  17135. ret = WOLFSSL_FATAL_ERROR;
  17136. }
  17137. }
  17138. wc_AesFree(&gmac.aes);
  17139. res = TEST_RES_CHECK(ret == 0);
  17140. #endif
  17141. return res;
  17142. } /* END test_wc_GmacUpdate */
  17143. /*
  17144. * testing wc_CamelliaSetKey
  17145. */
  17146. static int test_wc_CamelliaSetKey(void)
  17147. {
  17148. int res = TEST_SKIPPED;
  17149. #ifdef HAVE_CAMELLIA
  17150. Camellia camellia;
  17151. /*128-bit key*/
  17152. static const byte key16[] =
  17153. {
  17154. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17155. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  17156. };
  17157. /* 192-bit key */
  17158. static const byte key24[] =
  17159. {
  17160. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17161. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17162. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17163. };
  17164. /* 256-bit key */
  17165. static const byte key32[] =
  17166. {
  17167. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17168. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17169. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  17170. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  17171. };
  17172. static const byte iv[] =
  17173. {
  17174. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17175. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17176. };
  17177. int ret = 0;
  17178. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  17179. if (ret == 0) {
  17180. ret = wc_CamelliaSetKey(&camellia, key16,
  17181. (word32)sizeof(key16), NULL);
  17182. if (ret == 0) {
  17183. ret = wc_CamelliaSetKey(&camellia, key24,
  17184. (word32)sizeof(key24), iv);
  17185. }
  17186. if (ret == 0) {
  17187. ret = wc_CamelliaSetKey(&camellia, key24,
  17188. (word32)sizeof(key24), NULL);
  17189. }
  17190. if (ret == 0) {
  17191. ret = wc_CamelliaSetKey(&camellia, key32,
  17192. (word32)sizeof(key32), iv);
  17193. }
  17194. if (ret == 0) {
  17195. ret = wc_CamelliaSetKey(&camellia, key32,
  17196. (word32)sizeof(key32), NULL);
  17197. }
  17198. }
  17199. /* Bad args. */
  17200. if (ret == 0) {
  17201. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  17202. if (ret != BAD_FUNC_ARG) {
  17203. ret = WOLFSSL_FATAL_ERROR;
  17204. }
  17205. else {
  17206. ret = 0;
  17207. }
  17208. } /* END bad args. */
  17209. res = TEST_RES_CHECK(ret == 0);
  17210. #endif
  17211. return res;
  17212. } /* END test_wc_CammeliaSetKey */
  17213. /*
  17214. * Testing wc_CamelliaSetIV()
  17215. */
  17216. static int test_wc_CamelliaSetIV(void)
  17217. {
  17218. int res = TEST_SKIPPED;
  17219. #ifdef HAVE_CAMELLIA
  17220. Camellia camellia;
  17221. static const byte iv[] =
  17222. {
  17223. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17224. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17225. };
  17226. int ret = 0;
  17227. ret = wc_CamelliaSetIV(&camellia, iv);
  17228. if (ret == 0) {
  17229. ret = wc_CamelliaSetIV(&camellia, NULL);
  17230. }
  17231. /* Bad args. */
  17232. if (ret == 0) {
  17233. ret = wc_CamelliaSetIV(NULL, NULL);
  17234. if (ret != BAD_FUNC_ARG) {
  17235. ret = WOLFSSL_FATAL_ERROR;
  17236. }
  17237. else {
  17238. ret = 0;
  17239. }
  17240. }
  17241. res = TEST_RES_CHECK(ret == 0);
  17242. #endif
  17243. return res;
  17244. } /*END test_wc_CamelliaSetIV*/
  17245. /*
  17246. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  17247. */
  17248. static int test_wc_CamelliaEncryptDecryptDirect(void)
  17249. {
  17250. int res = TEST_SKIPPED;
  17251. #ifdef HAVE_CAMELLIA
  17252. Camellia camellia;
  17253. static const byte key24[] =
  17254. {
  17255. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17256. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17257. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17258. };
  17259. static const byte iv[] =
  17260. {
  17261. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17262. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  17263. };
  17264. static const byte plainT[] =
  17265. {
  17266. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  17267. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  17268. };
  17269. byte enc[sizeof(plainT)];
  17270. byte dec[sizeof(enc)];
  17271. int camE = WOLFSSL_FATAL_ERROR;
  17272. int camD = WOLFSSL_FATAL_ERROR;
  17273. int ret = 0;
  17274. /*Init stack variables.*/
  17275. XMEMSET(enc, 0, 16);
  17276. XMEMSET(enc, 0, 16);
  17277. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  17278. if (ret == 0) {
  17279. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  17280. if (ret == 0) {
  17281. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  17282. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  17283. ret = WOLFSSL_FATAL_ERROR;
  17284. }
  17285. }
  17286. }
  17287. /* Pass bad args. */
  17288. if (ret == 0) {
  17289. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  17290. if (camE == BAD_FUNC_ARG) {
  17291. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  17292. }
  17293. if (camE == BAD_FUNC_ARG) {
  17294. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  17295. }
  17296. if (camE == BAD_FUNC_ARG) {
  17297. camE = 0;
  17298. }
  17299. else {
  17300. camE = WOLFSSL_FATAL_ERROR;
  17301. }
  17302. }
  17303. if (camE != 0) {
  17304. return TEST_FAIL;
  17305. }
  17306. if (ret == 0) {
  17307. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  17308. if (camD == BAD_FUNC_ARG) {
  17309. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  17310. }
  17311. if (camD == BAD_FUNC_ARG) {
  17312. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  17313. }
  17314. if (camD == BAD_FUNC_ARG) {
  17315. camD = 0;
  17316. }
  17317. else {
  17318. camD = WOLFSSL_FATAL_ERROR;
  17319. }
  17320. }
  17321. res = TEST_RES_CHECK(camD == 0);
  17322. #endif
  17323. return res;
  17324. } /* END test-wc_CamelliaEncryptDecryptDirect */
  17325. /*
  17326. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  17327. */
  17328. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  17329. {
  17330. int res = TEST_SKIPPED;
  17331. #ifdef HAVE_CAMELLIA
  17332. Camellia camellia;
  17333. static const byte key24[] =
  17334. {
  17335. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  17336. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  17337. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  17338. };
  17339. static const byte plainT[] =
  17340. {
  17341. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  17342. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  17343. };
  17344. byte enc[CAMELLIA_BLOCK_SIZE];
  17345. byte dec[CAMELLIA_BLOCK_SIZE];
  17346. int camCbcE = WOLFSSL_FATAL_ERROR;
  17347. int camCbcD = WOLFSSL_FATAL_ERROR;
  17348. int ret = 0;
  17349. /* Init stack variables. */
  17350. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  17351. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  17352. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  17353. if (ret == 0) {
  17354. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  17355. if (ret != 0) {
  17356. ret = WOLFSSL_FATAL_ERROR;
  17357. }
  17358. }
  17359. if (ret == 0) {
  17360. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  17361. if (ret == 0) {
  17362. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  17363. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  17364. ret = WOLFSSL_FATAL_ERROR;
  17365. }
  17366. }
  17367. }
  17368. /* Pass in bad args. */
  17369. if (ret == 0) {
  17370. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  17371. if (camCbcE == BAD_FUNC_ARG) {
  17372. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  17373. CAMELLIA_BLOCK_SIZE);
  17374. }
  17375. if (camCbcE == BAD_FUNC_ARG) {
  17376. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  17377. CAMELLIA_BLOCK_SIZE);
  17378. }
  17379. if (camCbcE == BAD_FUNC_ARG) {
  17380. camCbcE = 0;
  17381. }
  17382. else {
  17383. camCbcE = WOLFSSL_FATAL_ERROR;
  17384. }
  17385. }
  17386. if (camCbcE != 0) {
  17387. return TEST_FAIL;
  17388. }
  17389. if (ret == 0) {
  17390. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  17391. if (camCbcD == BAD_FUNC_ARG) {
  17392. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  17393. CAMELLIA_BLOCK_SIZE);
  17394. }
  17395. if (camCbcD == BAD_FUNC_ARG) {
  17396. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  17397. CAMELLIA_BLOCK_SIZE);
  17398. }
  17399. if (camCbcD == BAD_FUNC_ARG) {
  17400. camCbcD = 0;
  17401. }
  17402. else {
  17403. camCbcD = WOLFSSL_FATAL_ERROR;
  17404. }
  17405. } /* END bad args. */
  17406. res = TEST_RES_CHECK(camCbcD == 0);
  17407. #endif
  17408. return res;
  17409. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  17410. /*
  17411. * Testing wc_Arc4SetKey()
  17412. */
  17413. static int test_wc_Arc4SetKey(void)
  17414. {
  17415. int res = TEST_SKIPPED;
  17416. #ifndef NO_RC4
  17417. Arc4 arc;
  17418. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17419. int keyLen = 8;
  17420. int ret = 0;
  17421. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  17422. /* Test bad args. */
  17423. if (ret == 0) {
  17424. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  17425. if (ret == BAD_FUNC_ARG)
  17426. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  17427. if (ret == BAD_FUNC_ARG)
  17428. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  17429. if (ret == BAD_FUNC_ARG)
  17430. ret = WOLFSSL_ERROR_NONE;
  17431. else
  17432. ret = WOLFSSL_FATAL_ERROR;
  17433. } /* END test bad args. */
  17434. res = TEST_RES_CHECK(ret == 0);
  17435. #endif
  17436. return res;
  17437. } /* END test_wc_Arc4SetKey */
  17438. /*
  17439. * Testing wc_Arc4Process for ENC/DEC.
  17440. */
  17441. static int test_wc_Arc4Process(void)
  17442. {
  17443. int res = TEST_SKIPPED;
  17444. #ifndef NO_RC4
  17445. Arc4 enc, dec;
  17446. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17447. int keyLen = 8;
  17448. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  17449. byte cipher[8];
  17450. byte plain[8];
  17451. int ret;
  17452. /* Init stack variables */
  17453. XMEMSET(cipher, 0, sizeof(cipher));
  17454. XMEMSET(plain, 0, sizeof(plain));
  17455. /* Use for async. */
  17456. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  17457. if (ret == 0) {
  17458. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  17459. }
  17460. if (ret == 0) {
  17461. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  17462. }
  17463. if (ret == 0) {
  17464. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  17465. }
  17466. if (ret == 0) {
  17467. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  17468. }
  17469. if (ret == 0) {
  17470. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  17471. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  17472. ret = WOLFSSL_FATAL_ERROR;
  17473. }
  17474. else {
  17475. ret = 0;
  17476. }
  17477. }
  17478. /* Bad args. */
  17479. if (ret == 0) {
  17480. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  17481. if (ret == BAD_FUNC_ARG) {
  17482. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  17483. }
  17484. if (ret == BAD_FUNC_ARG) {
  17485. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  17486. }
  17487. if (ret == BAD_FUNC_ARG) {
  17488. ret = 0;
  17489. }
  17490. else {
  17491. ret = WOLFSSL_FATAL_ERROR;
  17492. }
  17493. }
  17494. wc_Arc4Free(&enc);
  17495. wc_Arc4Free(&dec);
  17496. res = TEST_RES_CHECK(ret == 0);
  17497. #endif
  17498. return res;
  17499. }/* END test_wc_Arc4Process */
  17500. /*
  17501. * Testing wc_Init RsaKey()
  17502. */
  17503. static int test_wc_InitRsaKey(void)
  17504. {
  17505. int res = TEST_SKIPPED;
  17506. #ifndef NO_RSA
  17507. RsaKey key;
  17508. int ret = 0;
  17509. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17510. /* Test bad args. */
  17511. if (ret == 0) {
  17512. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  17513. #ifndef HAVE_USER_RSA
  17514. if (ret == BAD_FUNC_ARG) {
  17515. ret = 0;
  17516. }
  17517. else {
  17518. #else
  17519. if (ret == USER_CRYPTO_ERROR) {
  17520. ret = 0;
  17521. }
  17522. else {
  17523. #endif
  17524. ret = WOLFSSL_FATAL_ERROR;
  17525. }
  17526. } /* end if */
  17527. if (wc_FreeRsaKey(&key) || ret != 0) {
  17528. ret = WOLFSSL_FATAL_ERROR;
  17529. }
  17530. res = TEST_RES_CHECK(ret == 0);
  17531. #endif
  17532. return res;
  17533. } /* END test_wc_InitRsaKey */
  17534. /*
  17535. * Testing wc_RsaPrivateKeyDecode()
  17536. */
  17537. static int test_wc_RsaPrivateKeyDecode(void)
  17538. {
  17539. int res = TEST_SKIPPED;
  17540. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17541. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17542. RsaKey key;
  17543. byte* tmp;
  17544. word32 idx = 0;
  17545. int bytes = 0;
  17546. int ret = 0;
  17547. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17548. if (tmp == NULL) {
  17549. ret = WOLFSSL_FATAL_ERROR;
  17550. }
  17551. if (ret == 0) {
  17552. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17553. }
  17554. if (ret == 0) {
  17555. #ifdef USE_CERT_BUFFERS_1024
  17556. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17557. bytes = sizeof_client_key_der_1024;
  17558. #else
  17559. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17560. bytes = sizeof_client_key_der_2048;
  17561. #endif /* Use cert buffers. */
  17562. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  17563. }
  17564. #ifndef HAVE_USER_RSA
  17565. /* Test bad args. */
  17566. if (ret == 0) {
  17567. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17568. if (ret == BAD_FUNC_ARG) {
  17569. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17570. }
  17571. if (ret == BAD_FUNC_ARG) {
  17572. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17573. }
  17574. if (ret == BAD_FUNC_ARG) {
  17575. ret = 0;
  17576. }
  17577. else {
  17578. ret = WOLFSSL_FATAL_ERROR;
  17579. }
  17580. }
  17581. #else
  17582. /* Test bad args. User RSA. */
  17583. if (ret == 0) {
  17584. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17585. if (ret == USER_CRYPTO_ERROR) {
  17586. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17587. }
  17588. if (ret == USER_CRYPTO_ERROR) {
  17589. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17590. }
  17591. if (ret == USER_CRYPTO_ERROR) {
  17592. ret = 0;
  17593. }
  17594. else {
  17595. ret = WOLFSSL_FATAL_ERROR;
  17596. }
  17597. }
  17598. #endif
  17599. if (tmp != NULL) {
  17600. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17601. }
  17602. if (wc_FreeRsaKey(&key) || ret != 0) {
  17603. ret = WOLFSSL_FATAL_ERROR;
  17604. }
  17605. res = TEST_RES_CHECK(ret == 0);
  17606. #endif
  17607. return res;
  17608. } /* END test_wc_RsaPrivateKeyDecode */
  17609. /*
  17610. * Testing wc_RsaPublicKeyDecode()
  17611. */
  17612. static int test_wc_RsaPublicKeyDecode(void)
  17613. {
  17614. int res = TEST_SKIPPED;
  17615. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17616. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17617. RsaKey keyPub;
  17618. byte* tmp;
  17619. word32 idx = 0;
  17620. int bytes = 0;
  17621. word32 keySz = 0;
  17622. word32 tstKeySz = 0;
  17623. int ret = 0;
  17624. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17625. XFILE f;
  17626. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17627. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17628. byte buf[4096];
  17629. #endif
  17630. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17631. if (tmp == NULL) {
  17632. ret = WOLFSSL_FATAL_ERROR;
  17633. }
  17634. if (ret == 0) {
  17635. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  17636. }
  17637. if (ret == 0) {
  17638. #ifdef USE_CERT_BUFFERS_1024
  17639. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17640. bytes = sizeof_client_keypub_der_1024;
  17641. keySz = 1024;
  17642. #else
  17643. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17644. bytes = sizeof_client_keypub_der_2048;
  17645. keySz = 2048;
  17646. #endif
  17647. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  17648. }
  17649. #ifndef HAVE_USER_RSA
  17650. /* Pass in bad args. */
  17651. if (ret == 0) {
  17652. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17653. if (ret == BAD_FUNC_ARG) {
  17654. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17655. }
  17656. if (ret == BAD_FUNC_ARG) {
  17657. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17658. }
  17659. if (ret == BAD_FUNC_ARG) {
  17660. ret = 0;
  17661. }
  17662. else {
  17663. ret = WOLFSSL_FATAL_ERROR;
  17664. }
  17665. }
  17666. #else
  17667. /* Pass in bad args. */
  17668. if (ret == 0) {
  17669. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17670. if (ret == USER_CRYPTO_ERROR) {
  17671. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17672. }
  17673. if (ret == USER_CRYPTO_ERROR) {
  17674. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17675. }
  17676. if (ret == USER_CRYPTO_ERROR) {
  17677. ret = 0;
  17678. }
  17679. else {
  17680. ret = WOLFSSL_FATAL_ERROR;
  17681. }
  17682. }
  17683. #endif
  17684. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  17685. ret = WOLFSSL_FATAL_ERROR;
  17686. }
  17687. if (ret == 0) {
  17688. /* Test for getting modulus key size */
  17689. idx = 0;
  17690. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17691. &tstKeySz, NULL, NULL);
  17692. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  17693. }
  17694. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17695. f = XFOPEN(rsaPssPubKey, "rb");
  17696. AssertTrue((f != XBADFILE));
  17697. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17698. XFCLOSE(f);
  17699. idx = 0;
  17700. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17701. NULL), 0);
  17702. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  17703. AssertTrue((f != XBADFILE));
  17704. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17705. XFCLOSE(f);
  17706. idx = 0;
  17707. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17708. NULL), 0);
  17709. #endif
  17710. if (tmp != NULL) {
  17711. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17712. }
  17713. res = TEST_RES_CHECK(ret == 0);
  17714. #endif
  17715. return res;
  17716. } /* END test_wc_RsaPublicKeyDecode */
  17717. /*
  17718. * Testing wc_RsaPublicKeyDecodeRaw()
  17719. */
  17720. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17721. {
  17722. int res = TEST_SKIPPED;
  17723. #if !defined(NO_RSA)
  17724. RsaKey key;
  17725. const byte n = 0x23;
  17726. const byte e = 0x03;
  17727. int nSz = sizeof(n);
  17728. int eSz = sizeof(e);
  17729. int ret;
  17730. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17731. if (ret == 0) {
  17732. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  17733. }
  17734. #ifndef HAVE_USER_RSA
  17735. /* Pass in bad args. */
  17736. if (ret == 0) {
  17737. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17738. if (ret == BAD_FUNC_ARG) {
  17739. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17740. }
  17741. if (ret == BAD_FUNC_ARG) {
  17742. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17743. }
  17744. if (ret == BAD_FUNC_ARG) {
  17745. ret = 0;
  17746. }
  17747. else {
  17748. ret = WOLFSSL_FATAL_ERROR;
  17749. }
  17750. }
  17751. #else
  17752. /* Pass in bad args. User RSA. */
  17753. if (ret == 0) {
  17754. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17755. if (ret == USER_CRYPTO_ERROR) {
  17756. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17757. }
  17758. if (ret == USER_CRYPTO_ERROR) {
  17759. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17760. }
  17761. if (ret == USER_CRYPTO_ERROR) {
  17762. ret = 0;
  17763. }
  17764. else {
  17765. ret = WOLFSSL_FATAL_ERROR;
  17766. }
  17767. }
  17768. #endif
  17769. if (wc_FreeRsaKey(&key) || ret != 0) {
  17770. ret = WOLFSSL_FATAL_ERROR;
  17771. }
  17772. res = TEST_RES_CHECK(ret == 0);
  17773. #endif
  17774. return res;
  17775. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17776. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  17777. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17778. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17779. * trying until it gets a probable prime. */
  17780. #ifdef HAVE_FIPS
  17781. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17782. {
  17783. int ret;
  17784. for (;;) {
  17785. ret = wc_MakeRsaKey(key, size, e, rng);
  17786. if (ret != PRIME_GEN_E) break;
  17787. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17788. "trying again.\n");
  17789. }
  17790. return ret;
  17791. }
  17792. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17793. #else
  17794. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17795. #endif
  17796. #endif
  17797. /*
  17798. * Testing wc_MakeRsaKey()
  17799. */
  17800. static int test_wc_MakeRsaKey(void)
  17801. {
  17802. int res = TEST_SKIPPED;
  17803. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17804. RsaKey genKey;
  17805. WC_RNG rng;
  17806. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17807. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17808. int bits = 1024;
  17809. #else
  17810. int bits = 2048;
  17811. #endif
  17812. int ret = 0;
  17813. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17814. if (ret == 0) {
  17815. ret = wc_InitRng(&rng);
  17816. if (ret == 0) {
  17817. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17818. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  17819. ret = WOLFSSL_FATAL_ERROR;
  17820. }
  17821. }
  17822. }
  17823. #ifndef HAVE_USER_RSA
  17824. /* Test bad args. */
  17825. if (ret == 0) {
  17826. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17827. if (ret == BAD_FUNC_ARG) {
  17828. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17829. }
  17830. if (ret == BAD_FUNC_ARG) {
  17831. /* e < 3 */
  17832. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17833. }
  17834. if (ret == BAD_FUNC_ARG) {
  17835. /* e & 1 == 0 */
  17836. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17837. }
  17838. if (ret == BAD_FUNC_ARG) {
  17839. ret = 0;
  17840. }
  17841. else {
  17842. ret = WOLFSSL_FATAL_ERROR;
  17843. }
  17844. }
  17845. #else
  17846. /* Test bad args. */
  17847. if (ret == 0) {
  17848. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17849. if (ret == USER_CRYPTO_ERROR) {
  17850. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17851. }
  17852. if (ret == USER_CRYPTO_ERROR) {
  17853. /* e < 3 */
  17854. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17855. }
  17856. if (ret == USER_CRYPTO_ERROR) {
  17857. /* e & 1 == 0 */
  17858. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17859. }
  17860. if (ret == USER_CRYPTO_ERROR) {
  17861. ret = 0;
  17862. }
  17863. else {
  17864. ret = WOLFSSL_FATAL_ERROR;
  17865. }
  17866. }
  17867. #endif
  17868. if (wc_FreeRng(&rng) || ret != 0) {
  17869. ret = WOLFSSL_FATAL_ERROR;
  17870. }
  17871. res = TEST_RES_CHECK(ret == 0);
  17872. #endif
  17873. return res;
  17874. } /* END test_wc_MakeRsaKey */
  17875. /*
  17876. * Test the bounds checking on the cipher text versus the key modulus.
  17877. * 1. Make a new RSA key.
  17878. * 2. Set c to 1.
  17879. * 3. Decrypt c into k. (error)
  17880. * 4. Copy the key modulus to c and sub 1 from the copy.
  17881. * 5. Decrypt c into k. (error)
  17882. * Valid bounds test cases are covered by all the other RSA tests.
  17883. */
  17884. static int test_RsaDecryptBoundsCheck(void)
  17885. {
  17886. int res = TEST_SKIPPED;
  17887. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17888. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17889. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17890. WC_RNG rng;
  17891. RsaKey key;
  17892. byte flatC[256];
  17893. word32 flatCSz;
  17894. byte out[256];
  17895. word32 outSz = sizeof(out);
  17896. int ret;
  17897. XMEMSET(&rng, 0, sizeof(rng));
  17898. ret = wc_InitRng(&rng);
  17899. if (ret == 0)
  17900. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17901. if (ret == 0) {
  17902. const byte* derKey;
  17903. word32 derKeySz;
  17904. word32 idx = 0;
  17905. #ifdef USE_CERT_BUFFERS_1024
  17906. derKey = server_key_der_1024;
  17907. derKeySz = (word32)sizeof_server_key_der_1024;
  17908. flatCSz = 128;
  17909. #else
  17910. derKey = server_key_der_2048;
  17911. derKeySz = (word32)sizeof_server_key_der_2048;
  17912. flatCSz = 256;
  17913. #endif
  17914. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  17915. }
  17916. if (ret == 0) {
  17917. XMEMSET(flatC, 0, flatCSz);
  17918. flatC[flatCSz-1] = 1;
  17919. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17920. RSA_PRIVATE_DECRYPT, &rng);
  17921. if (ret == RSA_OUT_OF_RANGE_E) {
  17922. mp_int c;
  17923. mp_init_copy(&c, &key.n);
  17924. mp_sub_d(&c, 1, &c);
  17925. mp_to_unsigned_bin(&c, flatC);
  17926. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17927. RSA_PRIVATE_DECRYPT, NULL);
  17928. mp_clear(&c);
  17929. }
  17930. if (ret == RSA_OUT_OF_RANGE_E)
  17931. ret = 0;
  17932. else
  17933. ret = WOLFSSL_FATAL_ERROR;
  17934. }
  17935. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  17936. ret = WOLFSSL_FATAL_ERROR;
  17937. res = TEST_RES_CHECK(ret == 0);
  17938. #endif
  17939. return res;
  17940. } /* END test_wc_RsaDecryptBoundsCheck */
  17941. /*
  17942. * Testing wc_SetKeyUsage()
  17943. */
  17944. static int test_wc_SetKeyUsage(void)
  17945. {
  17946. int res = TEST_SKIPPED;
  17947. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17948. Cert myCert;
  17949. int ret = 0;
  17950. ret = wc_InitCert(&myCert);
  17951. if (ret == 0) {
  17952. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  17953. if (ret == 0) {
  17954. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  17955. }
  17956. if (ret == 0) {
  17957. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  17958. }
  17959. if (ret == 0) {
  17960. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  17961. }
  17962. if (ret == 0) {
  17963. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  17964. }
  17965. }
  17966. /* Test bad args. */
  17967. if (ret == 0) {
  17968. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  17969. if (ret == BAD_FUNC_ARG) {
  17970. ret = wc_SetKeyUsage(&myCert, NULL);
  17971. }
  17972. if (ret == BAD_FUNC_ARG) {
  17973. ret = wc_SetKeyUsage(&myCert, "");
  17974. }
  17975. if (ret == KEYUSAGE_E) {
  17976. ret = wc_SetKeyUsage(&myCert, ",");
  17977. }
  17978. if (ret == KEYUSAGE_E) {
  17979. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  17980. }
  17981. if (ret == KEYUSAGE_E) {
  17982. ret = 0;
  17983. }
  17984. else {
  17985. ret = WOLFSSL_FATAL_ERROR;
  17986. }
  17987. }
  17988. res = TEST_RES_CHECK(ret == 0);
  17989. #endif
  17990. return res;
  17991. } /* END test_wc_SetKeyUsage */
  17992. /*
  17993. * Testing wc_CheckProbablePrime()
  17994. */
  17995. static int test_wc_CheckProbablePrime(void)
  17996. {
  17997. int res = TEST_SKIPPED;
  17998. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17999. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  18000. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  18001. RsaKey key;
  18002. WC_RNG rng;
  18003. byte e[3];
  18004. word32 eSz = (word32)sizeof(e);
  18005. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  18006. word32 nSz = (word32)sizeof(n);
  18007. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  18008. word32 dSz = (word32)sizeof(d);
  18009. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  18010. word32 pSz = (word32)sizeof(p);
  18011. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  18012. word32 qSz = (word32)sizeof(q);
  18013. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  18014. int ret = 0;
  18015. int* isPrime;
  18016. int test[5];
  18017. isPrime = test;
  18018. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18019. if (ret == 0) {
  18020. ret = wc_InitRng(&rng);
  18021. }
  18022. if (ret == 0) {
  18023. ret = wc_RsaSetRNG(&key, &rng);
  18024. }
  18025. if (ret == 0) {
  18026. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  18027. }
  18028. if (ret == 0) {
  18029. PRIVATE_KEY_UNLOCK();
  18030. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  18031. p, &pSz, q, &qSz);
  18032. PRIVATE_KEY_LOCK();
  18033. }
  18034. /* Bad cases */
  18035. if (ret == 0) {
  18036. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  18037. nlen, isPrime);
  18038. if (ret == BAD_FUNC_ARG) {
  18039. ret = 0;
  18040. }
  18041. }
  18042. if (ret == 0) {
  18043. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  18044. nlen, isPrime);
  18045. if (ret == BAD_FUNC_ARG) {
  18046. ret = 0;
  18047. }
  18048. }
  18049. if (ret == 0) {
  18050. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  18051. nlen, isPrime);
  18052. if (ret == BAD_FUNC_ARG) {
  18053. ret = 0;
  18054. }
  18055. }
  18056. if (ret == 0) {
  18057. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  18058. nlen, isPrime);
  18059. if (ret == BAD_FUNC_ARG) {
  18060. ret = 0;
  18061. }
  18062. }
  18063. if (ret == 0) {
  18064. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  18065. nlen, isPrime);
  18066. if (ret == BAD_FUNC_ARG) {
  18067. ret = 0;
  18068. }
  18069. }
  18070. if (ret == 0) {
  18071. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  18072. nlen, isPrime);
  18073. if (ret == BAD_FUNC_ARG) {
  18074. ret = 0;
  18075. }
  18076. }
  18077. if (ret == 0) {
  18078. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  18079. nlen, isPrime);
  18080. if (ret == BAD_FUNC_ARG) {
  18081. ret = 0;
  18082. }
  18083. }
  18084. /* Good case */
  18085. if (ret == 0) {
  18086. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  18087. nlen, isPrime);
  18088. }
  18089. wc_FreeRsaKey(&key);
  18090. wc_FreeRng(&rng);
  18091. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  18092. res = TEST_RES_CHECK(ret == 0);
  18093. #endif
  18094. return res;
  18095. } /* END test_wc_CheckProbablePrime */
  18096. /*
  18097. * Testing wc_RsaPSS_Verify()
  18098. */
  18099. static int test_wc_RsaPSS_Verify(void)
  18100. {
  18101. int res = TEST_SKIPPED;
  18102. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  18103. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  18104. RsaKey key;
  18105. WC_RNG rng;
  18106. int sz = 256;
  18107. byte* pt;
  18108. const char* szMessage = "This is the string to be signed";
  18109. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  18110. unsigned char pDecrypted[2048/8];
  18111. word32 outLen = sizeof(pDecrypted);
  18112. int ret = 0;
  18113. pt = pDecrypted;
  18114. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18115. if (ret == 0) {
  18116. ret = wc_InitRng(&rng);
  18117. }
  18118. if (ret == 0) {
  18119. ret = wc_RsaSetRNG(&key, &rng);
  18120. }
  18121. if (ret == 0) {
  18122. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  18123. }
  18124. if (ret == 0) {
  18125. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  18126. pSignature, sizeof(pSignature),
  18127. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  18128. if (ret > 0) {
  18129. sz = ret;
  18130. ret = 0;
  18131. }
  18132. }
  18133. /* Bad cases */
  18134. if (ret == 0) {
  18135. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  18136. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18137. if (ret == BAD_FUNC_ARG) {
  18138. ret = 0;
  18139. }
  18140. }
  18141. if (ret == 0) {
  18142. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  18143. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18144. if (ret == BAD_FUNC_ARG) {
  18145. ret = 0;
  18146. }
  18147. }
  18148. if (ret == 0) {
  18149. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  18150. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18151. if (ret == BAD_FUNC_ARG) {
  18152. ret = 0;
  18153. }
  18154. }
  18155. if (ret == 0) {
  18156. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  18157. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18158. if (ret == BAD_FUNC_ARG) {
  18159. ret = 0;
  18160. }
  18161. }
  18162. /* Good case */
  18163. if (ret == 0) {
  18164. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  18165. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18166. if (ret > 0) {
  18167. ret = 0;
  18168. }
  18169. }
  18170. wc_FreeRsaKey(&key);
  18171. wc_FreeRng(&rng);
  18172. res = TEST_RES_CHECK(ret == 0);
  18173. #endif
  18174. return res;
  18175. } /* END test_wc_RsaPSS_Verify */
  18176. /*
  18177. * Testing wc_RsaPSS_VerifyCheck()
  18178. */
  18179. static int test_wc_RsaPSS_VerifyCheck(void)
  18180. {
  18181. int res = TEST_SKIPPED;
  18182. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  18183. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  18184. RsaKey key;
  18185. WC_RNG rng;
  18186. int sz = 256; /* 2048/8 */
  18187. byte* pt;
  18188. byte digest[32];
  18189. word32 digestSz = sizeof(digest);
  18190. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  18191. word32 pSignatureSz = sizeof(pSignature);
  18192. unsigned char pDecrypted[2048/8];
  18193. word32 outLen = sizeof(pDecrypted);
  18194. int ret = 0;
  18195. pt = pDecrypted;
  18196. XMEMSET(digest, 0, sizeof(digest));
  18197. XMEMSET(pSignature, 0, sizeof(pSignature));
  18198. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18199. if (ret == 0) {
  18200. ret = wc_InitRng(&rng);
  18201. }
  18202. if (ret == 0) {
  18203. ret = wc_RsaSetRNG(&key, &rng);
  18204. }
  18205. if (ret == 0) {
  18206. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  18207. }
  18208. if (ret == 0) {
  18209. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  18210. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  18211. }
  18212. if (ret == 0) {
  18213. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  18214. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  18215. if (ret > 0) {
  18216. sz = ret;
  18217. ret = 0;
  18218. }
  18219. }
  18220. /* Bad cases */
  18221. if (ret == 0) {
  18222. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  18223. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18224. if (ret == BAD_FUNC_ARG) {
  18225. ret = 0;
  18226. }
  18227. }
  18228. if (ret == 0) {
  18229. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  18230. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18231. if (ret == BAD_FUNC_ARG) {
  18232. ret = 0;
  18233. }
  18234. }
  18235. if (ret == 0) {
  18236. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  18237. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18238. if (ret == BAD_FUNC_ARG) {
  18239. ret = 0;
  18240. }
  18241. }
  18242. if (ret == 0) {
  18243. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  18244. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18245. if (ret == BAD_FUNC_ARG) {
  18246. ret = 0;
  18247. }
  18248. }
  18249. /* Good case */
  18250. if (ret == 0) {
  18251. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  18252. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18253. if (ret > 0) {
  18254. ret = 0;
  18255. }
  18256. }
  18257. wc_FreeRsaKey(&key);
  18258. wc_FreeRng(&rng);
  18259. res = TEST_RES_CHECK(ret == 0);
  18260. #endif
  18261. return res;
  18262. } /* END test_wc_RsaPSS_VerifyCheck */
  18263. /*
  18264. * Testing wc_RsaPSS_VerifyCheckInline()
  18265. */
  18266. static int test_wc_RsaPSS_VerifyCheckInline(void)
  18267. {
  18268. int res = TEST_SKIPPED;
  18269. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  18270. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  18271. RsaKey key;
  18272. WC_RNG rng;
  18273. int sz = 256;
  18274. byte* pt;
  18275. byte digest[32];
  18276. word32 digestSz = sizeof(digest);
  18277. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  18278. unsigned char pDecrypted[2048/8];
  18279. int ret;
  18280. pt = pDecrypted;
  18281. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18282. XMEMSET(digest, 0, sizeof(digest));
  18283. XMEMSET(pSignature, 0, sizeof(pSignature));
  18284. if (ret == 0) {
  18285. ret = wc_InitRng(&rng);
  18286. }
  18287. if (ret == 0) {
  18288. ret = wc_RsaSetRNG(&key, &rng);
  18289. }
  18290. if (ret == 0) {
  18291. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  18292. }
  18293. if (ret == 0) {
  18294. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  18295. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  18296. }
  18297. if (ret == 0) {
  18298. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  18299. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  18300. if (ret > 0) {
  18301. sz = ret;
  18302. ret = 0;
  18303. }
  18304. }
  18305. /* Bad Cases */
  18306. if (ret == 0) {
  18307. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  18308. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18309. if (ret == BAD_FUNC_ARG) {
  18310. ret = 0;
  18311. }
  18312. }
  18313. if (ret == 0) {
  18314. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  18315. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18316. if (ret == BAD_FUNC_ARG) {
  18317. ret = 0;
  18318. }
  18319. }
  18320. if (ret == 0) {
  18321. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  18322. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18323. if (ret == BAD_FUNC_ARG) {
  18324. ret = 0;
  18325. }
  18326. }
  18327. if (ret == 0) {
  18328. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  18329. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  18330. if (ret == BAD_FUNC_ARG) {
  18331. ret = 0;
  18332. }
  18333. }
  18334. /* Good case */
  18335. if (ret == 0) {
  18336. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  18337. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  18338. if (ret > 0) {
  18339. ret = 0;
  18340. }
  18341. }
  18342. wc_FreeRsaKey(&key);
  18343. wc_FreeRng(&rng);
  18344. res = TEST_RES_CHECK(ret == 0);
  18345. #endif
  18346. return res;
  18347. } /* END test_wc_RsaPSS_VerifyCheckInline */
  18348. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  18349. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  18350. {
  18351. (void)flag;
  18352. (void)type;
  18353. (void)file;
  18354. (void)line;
  18355. }
  18356. #endif
  18357. /*
  18358. * Testing wc_LockMutex_ex
  18359. */
  18360. static int test_wc_LockMutex_ex(void)
  18361. {
  18362. int res = TEST_SKIPPED;
  18363. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  18364. int ret = 0;
  18365. int flag = CRYPTO_LOCK;
  18366. int type = 0;
  18367. const char* file = "./test-LockMutex_ex.txt";
  18368. int line = 0;
  18369. /* without SetMutexCb */
  18370. ret = wc_LockMutex_ex(flag, type, file, line);
  18371. if (ret == BAD_STATE_E) {
  18372. ret = 0;
  18373. }
  18374. /* with SetMutexCb */
  18375. if (ret == 0) {
  18376. ret = wc_SetMutexCb(sample_mutex_cb);
  18377. if (ret == 0) {
  18378. ret = wc_LockMutex_ex(flag, type, file, line);
  18379. }
  18380. }
  18381. res = TEST_RES_CHECK(ret == 0);
  18382. #endif
  18383. return res;
  18384. }/*End test_wc_LockMutex_ex*/
  18385. /*
  18386. * Testing wc_SetMutexCb
  18387. */
  18388. static int test_wc_SetMutexCb(void)
  18389. {
  18390. int res = TEST_SKIPPED;
  18391. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  18392. int ret = wc_SetMutexCb(sample_mutex_cb);
  18393. res = TEST_RES_CHECK(ret == 0);
  18394. #endif
  18395. return res;
  18396. }/*End test_wc_SetMutexCb*/
  18397. /*
  18398. * Testing wc_RsaKeyToDer()
  18399. */
  18400. static int test_wc_RsaKeyToDer(void)
  18401. {
  18402. int res = TEST_SKIPPED;
  18403. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18404. RsaKey genKey;
  18405. WC_RNG rng;
  18406. byte* der;
  18407. int ret = 0;
  18408. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18409. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18410. int bits = 1024;
  18411. word32 derSz = 611;
  18412. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  18413. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  18414. #else
  18415. int bits = 2048;
  18416. word32 derSz = 1196;
  18417. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  18418. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  18419. #endif
  18420. XMEMSET(&rng, 0, sizeof(rng));
  18421. XMEMSET(&genKey, 0, sizeof(genKey));
  18422. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18423. if (der == NULL) {
  18424. ret = WOLFSSL_FATAL_ERROR;
  18425. }
  18426. /* Init structures. */
  18427. if (ret == 0) {
  18428. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  18429. }
  18430. if (ret == 0) {
  18431. ret = wc_InitRng(&rng);
  18432. }
  18433. /* Make key. */
  18434. if (ret == 0) {
  18435. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  18436. if (ret != 0) {
  18437. ret = WOLFSSL_FATAL_ERROR;
  18438. }
  18439. }
  18440. if (ret == 0) {
  18441. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  18442. if (ret > 0) {
  18443. ret = 0;
  18444. }
  18445. else {
  18446. ret = WOLFSSL_FATAL_ERROR;
  18447. }
  18448. }
  18449. #ifndef HAVE_USER_RSA
  18450. /* Pass good/bad args. */
  18451. if (ret == 0) {
  18452. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  18453. if (ret == BAD_FUNC_ARG) {
  18454. /* Get just the output length */
  18455. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  18456. }
  18457. if (ret > 0) {
  18458. /* Try Public Key. */
  18459. genKey.type = 0;
  18460. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  18461. #ifdef WOLFSSL_CHECK_MEM_ZERO
  18462. /* Put back to Private Key */
  18463. genKey.type = 1;
  18464. #endif
  18465. }
  18466. if (ret == BAD_FUNC_ARG) {
  18467. ret = 0;
  18468. }
  18469. else {
  18470. ret = WOLFSSL_FATAL_ERROR;
  18471. }
  18472. }
  18473. #else
  18474. /* Pass good/bad args. */
  18475. if (ret == 0) {
  18476. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  18477. if (ret == USER_CRYPTO_ERROR) {
  18478. /* Get just the output length */
  18479. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  18480. }
  18481. if (ret > 0) {
  18482. /* Try Public Key. */
  18483. genKey.type = 0;
  18484. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  18485. #ifdef WOLFSSL_CHECK_MEM_ZERO
  18486. /* Put back to Private Key */
  18487. genKey.type = 1;
  18488. #endif
  18489. }
  18490. if (ret == USER_CRYPTO_ERROR) {
  18491. ret = 0;
  18492. }
  18493. else {
  18494. ret = WOLFSSL_FATAL_ERROR;
  18495. }
  18496. }
  18497. #endif
  18498. if (der != NULL) {
  18499. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18500. }
  18501. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  18502. ret = WOLFSSL_FATAL_ERROR;
  18503. }
  18504. if (wc_FreeRng(&rng) || ret != 0) {
  18505. ret = WOLFSSL_FATAL_ERROR;
  18506. }
  18507. res = TEST_RES_CHECK(ret == 0);
  18508. #endif
  18509. return res;
  18510. } /* END test_wc_RsaKeyToDer */
  18511. /*
  18512. * Testing wc_RsaKeyToPublicDer()
  18513. */
  18514. static int test_wc_RsaKeyToPublicDer(void)
  18515. {
  18516. int res = TEST_SKIPPED;
  18517. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18518. RsaKey key;
  18519. WC_RNG rng;
  18520. byte* der;
  18521. int ret = 0;
  18522. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18523. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18524. int bits = 1024;
  18525. word32 derLen = 162;
  18526. #else
  18527. int bits = 2048;
  18528. word32 derLen = 294;
  18529. #endif
  18530. XMEMSET(&rng, 0, sizeof(rng));
  18531. XMEMSET(&key, 0, sizeof(key));
  18532. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18533. if (der == NULL) {
  18534. ret = WOLFSSL_FATAL_ERROR;
  18535. }
  18536. if (ret == 0) {
  18537. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18538. }
  18539. if (ret == 0) {
  18540. ret = wc_InitRng(&rng);
  18541. }
  18542. if (ret == 0) {
  18543. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18544. }
  18545. if (ret == 0) {
  18546. /* test getting size only */
  18547. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  18548. if (ret >= 0)
  18549. ret = 0;
  18550. }
  18551. if (ret == 0) {
  18552. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  18553. if (ret >= 0) {
  18554. ret = 0;
  18555. }
  18556. else {
  18557. ret = WOLFSSL_FATAL_ERROR;
  18558. }
  18559. }
  18560. if (ret == 0) {
  18561. /* test getting size only */
  18562. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  18563. if (ret >= 0)
  18564. ret = 0;
  18565. }
  18566. if (ret == 0) {
  18567. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  18568. if (ret >= 0) {
  18569. ret = 0;
  18570. }
  18571. else {
  18572. ret = WOLFSSL_FATAL_ERROR;
  18573. }
  18574. }
  18575. #ifndef HAVE_USER_RSA
  18576. /* Pass in bad args. */
  18577. if (ret == 0) {
  18578. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18579. if (ret == BAD_FUNC_ARG) {
  18580. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18581. }
  18582. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  18583. ret = 0;
  18584. }
  18585. else {
  18586. ret = WOLFSSL_FATAL_ERROR;
  18587. }
  18588. }
  18589. #else
  18590. /* Pass in bad args. */
  18591. if (ret == 0) {
  18592. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18593. if (ret == USER_CRYPTO_ERROR) {
  18594. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18595. }
  18596. if (ret == USER_CRYPTO_ERROR) {
  18597. ret = 0;
  18598. }
  18599. else {
  18600. ret = WOLFSSL_FATAL_ERROR;
  18601. }
  18602. }
  18603. #endif
  18604. if (der != NULL) {
  18605. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18606. }
  18607. if (wc_FreeRsaKey(&key) || ret != 0) {
  18608. ret = WOLFSSL_FATAL_ERROR;
  18609. }
  18610. if (wc_FreeRng(&rng) || ret != 0) {
  18611. ret = WOLFSSL_FATAL_ERROR;
  18612. }
  18613. res = TEST_RES_CHECK(ret == 0);
  18614. #endif
  18615. return res;
  18616. } /* END test_wc_RsaKeyToPublicDer */
  18617. /*
  18618. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  18619. */
  18620. static int test_wc_RsaPublicEncryptDecrypt(void)
  18621. {
  18622. int res = TEST_SKIPPED;
  18623. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18624. RsaKey key;
  18625. WC_RNG rng;
  18626. int ret = 0;
  18627. const char inStr[] = TEST_STRING;
  18628. const word32 plainLen = (word32)TEST_STRING_SZ;
  18629. const word32 inLen = (word32)TEST_STRING_SZ;
  18630. int bits = TEST_RSA_BITS;
  18631. const word32 cipherLen = TEST_RSA_BYTES;
  18632. word32 cipherLenResult = cipherLen;
  18633. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18634. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18635. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18636. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18637. if (in == NULL || plain == NULL || cipher == NULL) {
  18638. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  18639. return MEMORY_E;
  18640. }
  18641. #endif
  18642. XMEMCPY(in, inStr, inLen);
  18643. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18644. if (ret == 0) {
  18645. ret = wc_InitRng(&rng);
  18646. }
  18647. if (ret == 0) {
  18648. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18649. }
  18650. /* Encrypt. */
  18651. if (ret == 0) {
  18652. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  18653. if (ret >= 0) {
  18654. cipherLenResult = ret;
  18655. ret = 0;
  18656. }
  18657. else {
  18658. ret = WOLFSSL_FATAL_ERROR;
  18659. }
  18660. }
  18661. /* Pass bad args. */
  18662. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  18663. if (ret != 0) {
  18664. return TEST_FAIL;
  18665. }
  18666. /* Decrypt */
  18667. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18668. /* Bind rng */
  18669. if (ret == 0) {
  18670. ret = wc_RsaSetRNG(&key, &rng);
  18671. }
  18672. #endif
  18673. if (ret == 0) {
  18674. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  18675. }
  18676. if (ret >= 0) {
  18677. ret = XMEMCMP(plain, inStr, plainLen);
  18678. }
  18679. /* Pass in bad args. */
  18680. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  18681. WC_FREE_VAR(in, NULL);
  18682. WC_FREE_VAR(plain, NULL);
  18683. WC_FREE_VAR(cipher, NULL);
  18684. if (wc_FreeRsaKey(&key) || ret != 0) {
  18685. ret = WOLFSSL_FATAL_ERROR;
  18686. }
  18687. if (wc_FreeRng(&rng) || ret != 0) {
  18688. ret = WOLFSSL_FATAL_ERROR;
  18689. }
  18690. res = TEST_RES_CHECK(ret == 0);
  18691. #endif
  18692. return res;
  18693. } /* END test_wc_RsaPublicEncryptDecrypt */
  18694. /*
  18695. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  18696. */
  18697. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  18698. {
  18699. int result = TEST_SKIPPED;
  18700. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  18701. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  18702. && !defined(NO_SHA256)
  18703. RsaKey key;
  18704. WC_RNG rng;
  18705. int ret;
  18706. const char inStr[] = TEST_STRING;
  18707. const word32 inLen = (word32)TEST_STRING_SZ;
  18708. const word32 plainSz = (word32)TEST_STRING_SZ;
  18709. byte* res = NULL;
  18710. int idx = 0;
  18711. int bits = TEST_RSA_BITS;
  18712. const word32 cipherSz = TEST_RSA_BYTES;
  18713. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18714. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18715. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18716. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18717. if (in == NULL || plain == NULL || cipher == NULL) {
  18718. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  18719. return TEST_FAIL;
  18720. }
  18721. #endif
  18722. XMEMCPY(in, inStr, inLen);
  18723. /* Initialize stack structures. */
  18724. XMEMSET(&rng, 0, sizeof(rng));
  18725. XMEMSET(&key, 0, sizeof(key));
  18726. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  18727. if (ret == 0) {
  18728. ret = wc_InitRng(&rng);
  18729. }
  18730. if (ret == 0) {
  18731. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18732. }
  18733. /* Encrypt */
  18734. if (ret == 0) {
  18735. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  18736. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18737. if (ret >= 0) {
  18738. idx = ret;
  18739. ret = 0;
  18740. }
  18741. else {
  18742. ret = WOLFSSL_FATAL_ERROR;
  18743. }
  18744. }
  18745. /* Pass bad args. */
  18746. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  18747. if (ret != 0) {
  18748. return TEST_FAIL;
  18749. }
  18750. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18751. /* Decrypt */
  18752. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18753. if (ret == 0) {
  18754. ret = wc_RsaSetRNG(&key, &rng);
  18755. }
  18756. #endif
  18757. if (ret == 0) {
  18758. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  18759. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18760. WC_MGF1SHA256, NULL, 0);
  18761. }
  18762. if (ret >= 0) {
  18763. if (!XMEMCMP(plain, inStr, plainSz)) {
  18764. ret = 0;
  18765. }
  18766. else {
  18767. ret = WOLFSSL_FATAL_ERROR;
  18768. }
  18769. }
  18770. /*Pass bad args.*/
  18771. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  18772. if (ret != 0) {
  18773. return TEST_FAIL;
  18774. }
  18775. if (ret == 0) {
  18776. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  18777. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18778. WC_MGF1SHA256, NULL, 0);
  18779. if (ret >= 0) {
  18780. if (!XMEMCMP(inStr, res, plainSz)) {
  18781. ret = 0;
  18782. }
  18783. else {
  18784. ret = WOLFSSL_FATAL_ERROR;
  18785. }
  18786. }
  18787. }
  18788. #endif
  18789. WC_FREE_VAR(in, NULL);
  18790. WC_FREE_VAR(plain, NULL);
  18791. WC_FREE_VAR(cipher, NULL);
  18792. if (wc_FreeRsaKey(&key) || ret != 0) {
  18793. ret = WOLFSSL_FATAL_ERROR;
  18794. }
  18795. if (wc_FreeRng(&rng) || ret != 0) {
  18796. ret = WOLFSSL_FATAL_ERROR;
  18797. }
  18798. result = TEST_RES_CHECK(ret == 0);
  18799. #endif
  18800. return result;
  18801. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  18802. /*
  18803. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  18804. */
  18805. static int test_wc_RsaSSL_SignVerify(void)
  18806. {
  18807. int res = TEST_SKIPPED;
  18808. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18809. RsaKey key;
  18810. WC_RNG rng;
  18811. int ret = 0;
  18812. const char inStr[] = TEST_STRING;
  18813. const word32 plainSz = (word32)TEST_STRING_SZ;
  18814. const word32 inLen = (word32)TEST_STRING_SZ;
  18815. word32 idx = 0;
  18816. int bits = TEST_RSA_BITS;
  18817. const word32 outSz = TEST_RSA_BYTES;
  18818. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18819. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  18820. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18821. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18822. if (in == NULL || out == NULL || plain == NULL) {
  18823. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  18824. return TEST_FAIL;
  18825. }
  18826. #endif
  18827. XMEMCPY(in, inStr, inLen);
  18828. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18829. if (ret == 0) {
  18830. ret = wc_InitRng(&rng);
  18831. }
  18832. if (ret == 0) {
  18833. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18834. }
  18835. /* Sign. */
  18836. if (ret == 0) {
  18837. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  18838. if (ret == (int)outSz) {
  18839. idx = ret;
  18840. ret = 0;
  18841. }
  18842. else {
  18843. ret = WOLFSSL_FATAL_ERROR;
  18844. }
  18845. }
  18846. #ifndef HAVE_USER_RSA
  18847. /* Test bad args. */
  18848. if (ret == 0) {
  18849. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18850. if (ret == BAD_FUNC_ARG) {
  18851. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18852. }
  18853. if (ret == BAD_FUNC_ARG) {
  18854. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18855. }
  18856. if (ret == BAD_FUNC_ARG) {
  18857. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18858. }
  18859. if (ret == BAD_FUNC_ARG) {
  18860. ret = 0;
  18861. }
  18862. else {
  18863. ret = WOLFSSL_FATAL_ERROR;
  18864. }
  18865. }
  18866. #else
  18867. /* Test bad args. */
  18868. if (ret == 0) {
  18869. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18870. if (ret == USER_CRYPTO_ERROR) {
  18871. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18872. }
  18873. if (ret == USER_CRYPTO_ERROR) {
  18874. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18875. }
  18876. if (ret == USER_CRYPTO_ERROR) {
  18877. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18878. }
  18879. if (ret == USER_CRYPTO_ERROR) {
  18880. ret = 0;
  18881. }
  18882. else {
  18883. ret = WOLFSSL_FATAL_ERROR;
  18884. }
  18885. }
  18886. #endif
  18887. if (ret != 0) {
  18888. return TEST_FAIL;
  18889. }
  18890. /* Verify. */
  18891. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  18892. if (ret == (int)inLen) {
  18893. ret = 0;
  18894. }
  18895. else {
  18896. ret = WOLFSSL_FATAL_ERROR;
  18897. }
  18898. #ifndef HAVE_USER_RSA
  18899. /* Pass bad args. */
  18900. if (ret == 0) {
  18901. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18902. if (ret == BAD_FUNC_ARG) {
  18903. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18904. }
  18905. if (ret == BAD_FUNC_ARG) {
  18906. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18907. }
  18908. if (ret == BAD_FUNC_ARG) {
  18909. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18910. }
  18911. if (ret == BAD_FUNC_ARG) {
  18912. ret = 0;
  18913. }
  18914. else {
  18915. ret = WOLFSSL_FATAL_ERROR;
  18916. }
  18917. }
  18918. #else
  18919. /* Pass bad args. */
  18920. if (ret == 0) {
  18921. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18922. if (ret == USER_CRYPTO_ERROR) {
  18923. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18924. }
  18925. if (ret == USER_CRYPTO_ERROR) {
  18926. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18927. }
  18928. if (ret == USER_CRYPTO_ERROR) {
  18929. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18930. }
  18931. if (ret == USER_CRYPTO_ERROR) {
  18932. ret = 0;
  18933. }
  18934. else {
  18935. ret = WOLFSSL_FATAL_ERROR;
  18936. }
  18937. }
  18938. #endif
  18939. WC_FREE_VAR(in, NULL);
  18940. WC_FREE_VAR(out, NULL);
  18941. WC_FREE_VAR(plain, NULL);
  18942. if (wc_FreeRsaKey(&key) || ret != 0) {
  18943. ret = WOLFSSL_FATAL_ERROR;
  18944. }
  18945. if (wc_FreeRng(&rng) || ret != 0) {
  18946. ret = WOLFSSL_FATAL_ERROR;
  18947. }
  18948. res = TEST_RES_CHECK(ret == 0);
  18949. #endif
  18950. return res;
  18951. } /* END test_wc_RsaSSL_SignVerify */
  18952. /*
  18953. * Testing wc_RsaEncryptSize()
  18954. */
  18955. static int test_wc_RsaEncryptSize(void)
  18956. {
  18957. int res = TEST_SKIPPED;
  18958. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18959. RsaKey key;
  18960. WC_RNG rng;
  18961. int ret;
  18962. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18963. if (ret == 0) {
  18964. ret = wc_InitRng(&rng);
  18965. }
  18966. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18967. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18968. if (ret == 0) {
  18969. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  18970. if (ret == 0) {
  18971. ret = wc_RsaEncryptSize(&key);
  18972. }
  18973. if (ret == 128) {
  18974. ret = 0;
  18975. }
  18976. else {
  18977. ret = WOLFSSL_FATAL_ERROR;
  18978. }
  18979. }
  18980. if (wc_FreeRsaKey(&key) || ret != 0) {
  18981. ret = WOLFSSL_FATAL_ERROR;
  18982. }
  18983. else {
  18984. ret = 0;
  18985. }
  18986. #endif
  18987. if (ret == 0) {
  18988. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  18989. if (ret == 0) {
  18990. ret = wc_RsaEncryptSize(&key);
  18991. }
  18992. if (ret == 256) {
  18993. ret = 0;
  18994. }
  18995. else {
  18996. ret = WOLFSSL_FATAL_ERROR;
  18997. }
  18998. }
  18999. /* Pass in bad arg. */
  19000. if (ret == 0) {
  19001. ret = wc_RsaEncryptSize(NULL);
  19002. #ifndef HAVE_USER_RSA
  19003. if (ret == BAD_FUNC_ARG) {
  19004. ret = 0;
  19005. }
  19006. else {
  19007. ret = WOLFSSL_FATAL_ERROR;
  19008. }
  19009. #endif
  19010. }
  19011. if (wc_FreeRsaKey(&key) || ret != 0) {
  19012. ret = WOLFSSL_FATAL_ERROR;
  19013. }
  19014. if (wc_FreeRng(&rng) || ret != 0) {
  19015. ret = WOLFSSL_FATAL_ERROR;
  19016. }
  19017. res = TEST_RES_CHECK(ret == 0);
  19018. #endif
  19019. return res;
  19020. } /* END test_wc_RsaEncryptSize*/
  19021. /*
  19022. * Testing wc_RsaFlattenPublicKey()
  19023. */
  19024. static int test_wc_RsaFlattenPublicKey(void)
  19025. {
  19026. int res = TEST_SKIPPED;
  19027. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  19028. RsaKey key;
  19029. WC_RNG rng;
  19030. int ret = 0;
  19031. byte e[256];
  19032. byte n[256];
  19033. word32 eSz = sizeof(e);
  19034. word32 nSz = sizeof(n);
  19035. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  19036. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  19037. int bits = 1024;
  19038. #else
  19039. int bits = 2048;
  19040. #endif
  19041. ret = wc_InitRsaKey(&key, HEAP_HINT);
  19042. if (ret == 0) {
  19043. ret = wc_InitRng(&rng);
  19044. }
  19045. if (ret == 0) {
  19046. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  19047. if (ret >= 0) {
  19048. ret = 0;
  19049. }
  19050. else {
  19051. ret = WOLFSSL_FATAL_ERROR;
  19052. }
  19053. }
  19054. if (ret == 0) {
  19055. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  19056. }
  19057. #ifndef HAVE_USER_RSA
  19058. /* Pass bad args. */
  19059. if (ret == 0) {
  19060. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  19061. if (ret == BAD_FUNC_ARG) {
  19062. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  19063. }
  19064. if (ret == BAD_FUNC_ARG) {
  19065. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  19066. }
  19067. if (ret == BAD_FUNC_ARG) {
  19068. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  19069. }
  19070. if (ret == BAD_FUNC_ARG) {
  19071. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  19072. }
  19073. if (ret == BAD_FUNC_ARG) {
  19074. ret = 0;
  19075. }
  19076. else {
  19077. ret = WOLFSSL_FATAL_ERROR;
  19078. }
  19079. }
  19080. #else
  19081. /* Pass bad args. */
  19082. if (ret == 0) {
  19083. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  19084. if (ret == USER_CRYPTO_ERROR) {
  19085. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  19086. }
  19087. if (ret == USER_CRYPTO_ERROR) {
  19088. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  19089. }
  19090. if (ret == USER_CRYPTO_ERROR) {
  19091. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  19092. }
  19093. if (ret == USER_CRYPTO_ERROR) {
  19094. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  19095. }
  19096. if (ret == USER_CRYPTO_ERROR) {
  19097. ret = 0;
  19098. }
  19099. else {
  19100. ret = WOLFSSL_FATAL_ERROR;
  19101. }
  19102. }
  19103. #endif
  19104. if (wc_FreeRsaKey(&key) || ret != 0) {
  19105. ret = WOLFSSL_FATAL_ERROR;
  19106. }
  19107. if (wc_FreeRng(&rng) || ret != 0) {
  19108. ret = WOLFSSL_FATAL_ERROR;
  19109. }
  19110. res = TEST_RES_CHECK(ret == 0);
  19111. #endif
  19112. return res;
  19113. } /* END test_wc_RsaFlattenPublicKey */
  19114. /*
  19115. * unit test for wc_AesCcmSetKey
  19116. */
  19117. static int test_wc_AesCcmSetKey(void)
  19118. {
  19119. int res = TEST_SKIPPED;
  19120. #ifdef HAVE_AESCCM
  19121. Aes aes;
  19122. int ret = 0;
  19123. const byte key16[] =
  19124. {
  19125. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  19126. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  19127. };
  19128. const byte key24[] =
  19129. {
  19130. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  19131. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  19132. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  19133. };
  19134. const byte key32[] =
  19135. {
  19136. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  19137. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  19138. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  19139. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  19140. };
  19141. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  19142. if (ret != 0)
  19143. return ret;
  19144. #ifdef WOLFSSL_AES_128
  19145. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  19146. #endif
  19147. #ifdef WOLFSSL_AES_192
  19148. if (ret == 0) {
  19149. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  19150. }
  19151. #endif
  19152. #ifdef WOLFSSL_AES_256
  19153. if (ret == 0) {
  19154. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  19155. }
  19156. #endif
  19157. /* Test bad args. */
  19158. if (ret == 0) {
  19159. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  19160. if (ret == BAD_FUNC_ARG) {
  19161. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  19162. }
  19163. if (ret == BAD_FUNC_ARG) {
  19164. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  19165. }
  19166. if (ret != BAD_FUNC_ARG) {
  19167. ret = WOLFSSL_FATAL_ERROR;
  19168. }
  19169. else {
  19170. ret = 0;
  19171. }
  19172. }
  19173. wc_AesFree(&aes);
  19174. res = TEST_RES_CHECK(ret == 0);
  19175. #endif
  19176. return res;
  19177. } /* END test_wc_AesCcmSetKey */
  19178. /*
  19179. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  19180. */
  19181. static int test_wc_AesCcmEncryptDecrypt(void)
  19182. {
  19183. int res = TEST_SKIPPED;
  19184. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  19185. Aes aes;
  19186. int ret = 0;
  19187. const byte key16[] =
  19188. {
  19189. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  19190. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  19191. };
  19192. /* plaintext */
  19193. const byte plainT[] =
  19194. {
  19195. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  19196. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19197. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  19198. };
  19199. /* nonce */
  19200. const byte iv[] =
  19201. {
  19202. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  19203. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  19204. };
  19205. const byte c[] = /* cipher text. */
  19206. {
  19207. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  19208. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  19209. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  19210. };
  19211. const byte t[] = /* Auth tag */
  19212. {
  19213. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  19214. };
  19215. const byte authIn[] =
  19216. {
  19217. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  19218. };
  19219. byte cipherOut[sizeof(plainT)];
  19220. byte authTag[sizeof(t)];
  19221. int ccmE = WOLFSSL_FATAL_ERROR;
  19222. #ifdef HAVE_AES_DECRYPT
  19223. int ccmD = WOLFSSL_FATAL_ERROR;
  19224. byte plainOut[sizeof(cipherOut)];
  19225. #endif
  19226. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  19227. if (ret != 0)
  19228. return ret;
  19229. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  19230. if (ret == 0) {
  19231. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  19232. iv, sizeof(iv), authTag, sizeof(authTag),
  19233. authIn , sizeof(authIn));
  19234. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  19235. XMEMCMP(t, authTag, sizeof(t))) {
  19236. ccmE = WOLFSSL_FATAL_ERROR;
  19237. ret = WOLFSSL_FATAL_ERROR;
  19238. }
  19239. #ifdef HAVE_AES_DECRYPT
  19240. if (ret == 0) {
  19241. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  19242. sizeof(plainOut), iv, sizeof(iv),
  19243. authTag, sizeof(authTag),
  19244. authIn, sizeof(authIn));
  19245. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  19246. ccmD = WOLFSSL_FATAL_ERROR;
  19247. }
  19248. }
  19249. #endif
  19250. }
  19251. /* Pass in bad args. Encrypt*/
  19252. if (ret == 0 && ccmE == 0) {
  19253. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  19254. iv, sizeof(iv), authTag, sizeof(authTag),
  19255. authIn , sizeof(authIn));
  19256. if (ccmE == BAD_FUNC_ARG) {
  19257. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  19258. iv, sizeof(iv), authTag, sizeof(authTag),
  19259. authIn , sizeof(authIn));
  19260. }
  19261. if (ccmE == BAD_FUNC_ARG) {
  19262. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  19263. iv, sizeof(iv), authTag, sizeof(authTag),
  19264. authIn , sizeof(authIn));
  19265. }
  19266. if (ccmE == BAD_FUNC_ARG) {
  19267. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  19268. NULL, sizeof(iv), authTag, sizeof(authTag),
  19269. authIn , sizeof(authIn));
  19270. }
  19271. if (ccmE == BAD_FUNC_ARG) {
  19272. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  19273. iv, sizeof(iv), NULL, sizeof(authTag),
  19274. authIn , sizeof(authIn));
  19275. }
  19276. if (ccmE == BAD_FUNC_ARG) {
  19277. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  19278. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  19279. authIn , sizeof(authIn));
  19280. }
  19281. if (ccmE == BAD_FUNC_ARG) {
  19282. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  19283. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  19284. authIn , sizeof(authIn));
  19285. }
  19286. if (ccmE != BAD_FUNC_ARG) {
  19287. ccmE = WOLFSSL_FATAL_ERROR;
  19288. }
  19289. else {
  19290. ccmE = 0;
  19291. }
  19292. } /* End Encrypt */
  19293. if (ccmE != 0) {
  19294. wc_AesFree(&aes);
  19295. return TEST_FAIL;
  19296. }
  19297. #ifdef HAVE_AES_DECRYPT
  19298. /* Pass in bad args. Decrypt*/
  19299. if (ret == 0 && ccmD == 0) {
  19300. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  19301. iv, sizeof(iv), authTag, sizeof(authTag),
  19302. authIn, sizeof(authIn));
  19303. if (ccmD == BAD_FUNC_ARG) {
  19304. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  19305. iv, sizeof(iv), authTag, sizeof(authTag),
  19306. authIn, sizeof(authIn));
  19307. }
  19308. if (ccmD == BAD_FUNC_ARG) {
  19309. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  19310. iv, sizeof(iv), authTag, sizeof(authTag),
  19311. authIn, sizeof(authIn));
  19312. }
  19313. if (ccmD == BAD_FUNC_ARG) {
  19314. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  19315. sizeof(plainOut), NULL, sizeof(iv),
  19316. authTag, sizeof(authTag),
  19317. authIn, sizeof(authIn));
  19318. }
  19319. if (ccmD == BAD_FUNC_ARG) {
  19320. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  19321. sizeof(plainOut), iv, sizeof(iv), NULL,
  19322. sizeof(authTag), authIn, sizeof(authIn));
  19323. }
  19324. if (ccmD == BAD_FUNC_ARG) {
  19325. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  19326. sizeof(plainOut), iv, sizeof(iv) + 1,
  19327. authTag, sizeof(authTag),
  19328. authIn, sizeof(authIn));
  19329. }
  19330. if (ccmD == BAD_FUNC_ARG) {
  19331. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  19332. sizeof(plainOut), iv, sizeof(iv) - 7,
  19333. authTag, sizeof(authTag),
  19334. authIn, sizeof(authIn));
  19335. }
  19336. if (ccmD != BAD_FUNC_ARG) {
  19337. ccmD = WOLFSSL_FATAL_ERROR;
  19338. }
  19339. else {
  19340. ccmD = 0;
  19341. }
  19342. } /* END Decrypt */
  19343. res = TEST_RES_CHECK(ccmD == 0);
  19344. #endif
  19345. wc_AesFree(&aes);
  19346. #endif /* HAVE_AESCCM */
  19347. return res;
  19348. } /* END test_wc_AesCcmEncryptDecrypt */
  19349. /*
  19350. * Testing wc_InitDsaKey()
  19351. */
  19352. static int test_wc_InitDsaKey(void)
  19353. {
  19354. int res = TEST_SKIPPED;
  19355. #ifndef NO_DSA
  19356. DsaKey key;
  19357. int ret = 0;
  19358. ret = wc_InitDsaKey(&key);
  19359. /* Pass in bad args. */
  19360. if (ret == 0) {
  19361. ret = wc_InitDsaKey(NULL);
  19362. if (ret == BAD_FUNC_ARG) {
  19363. ret = 0;
  19364. }
  19365. else {
  19366. ret = WOLFSSL_FATAL_ERROR;
  19367. }
  19368. }
  19369. wc_FreeDsaKey(&key);
  19370. res = TEST_RES_CHECK(ret == 0);
  19371. #endif
  19372. return res;
  19373. } /* END test_wc_InitDsaKey */
  19374. /*
  19375. * Testing wc_DsaSign() and wc_DsaVerify()
  19376. */
  19377. static int test_wc_DsaSignVerify(void)
  19378. {
  19379. int res = TEST_SKIPPED;
  19380. #if !defined(NO_DSA)
  19381. DsaKey key;
  19382. WC_RNG rng;
  19383. wc_Sha sha;
  19384. int ret = 0;
  19385. byte signature[DSA_SIG_SIZE];
  19386. byte hash[WC_SHA_DIGEST_SIZE];
  19387. word32 idx = 0;
  19388. word32 bytes;
  19389. int answer;
  19390. #ifdef USE_CERT_BUFFERS_1024
  19391. byte tmp[ONEK_BUF];
  19392. XMEMSET(tmp, 0, sizeof(tmp));
  19393. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19394. bytes = sizeof_dsa_key_der_1024;
  19395. #elif defined(USE_CERT_BUFFERS_2048)
  19396. byte tmp[TWOK_BUF];
  19397. XMEMSET(tmp, 0, sizeof(tmp));
  19398. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19399. bytes = sizeof_dsa_key_der_2048;
  19400. #else
  19401. byte tmp[TWOK_BUF];
  19402. XMEMSET(tmp, 0, sizeof(tmp));
  19403. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  19404. if (fp == XBADFILE) {
  19405. return WOLFSSL_BAD_FILE;
  19406. }
  19407. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  19408. XFCLOSE(fp);
  19409. #endif /* END USE_CERT_BUFFERS_1024 */
  19410. ret = wc_InitSha(&sha);
  19411. if (ret == 0) {
  19412. ret = wc_ShaUpdate(&sha, tmp, bytes);
  19413. if (ret == 0) {
  19414. ret = wc_ShaFinal(&sha, hash);
  19415. }
  19416. if (ret == 0) {
  19417. ret = wc_InitDsaKey(&key);
  19418. }
  19419. if (ret == 0) {
  19420. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  19421. }
  19422. if (ret == 0) {
  19423. ret = wc_InitRng(&rng);
  19424. }
  19425. }
  19426. /* Sign. */
  19427. if (ret == 0) {
  19428. ret = wc_DsaSign(hash, signature, &key, &rng);
  19429. }
  19430. /* Test bad args. */
  19431. if (ret == 0) {
  19432. ret = wc_DsaSign(NULL, signature, &key, &rng);
  19433. if (ret == BAD_FUNC_ARG) {
  19434. ret = wc_DsaSign(hash, NULL, &key, &rng);
  19435. }
  19436. if (ret == BAD_FUNC_ARG) {
  19437. ret = wc_DsaSign(hash, signature, NULL, &rng);
  19438. }
  19439. if (ret == BAD_FUNC_ARG) {
  19440. ret = wc_DsaSign(hash, signature, &key, NULL);
  19441. }
  19442. if (ret == BAD_FUNC_ARG) {
  19443. ret = 0;
  19444. }
  19445. else {
  19446. ret = WOLFSSL_FATAL_ERROR;
  19447. }
  19448. }
  19449. if (ret == 0) {
  19450. /* Verify. */
  19451. ret = wc_DsaVerify(hash, signature, &key, &answer);
  19452. if (ret != 0 || answer != 1) {
  19453. ret = WOLFSSL_FATAL_ERROR;
  19454. }
  19455. else {
  19456. ret = 0;
  19457. }
  19458. }
  19459. /* Pass in bad args. */
  19460. if (ret == 0) {
  19461. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  19462. if (ret == BAD_FUNC_ARG) {
  19463. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  19464. }
  19465. if (ret == BAD_FUNC_ARG) {
  19466. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  19467. }
  19468. if (ret == BAD_FUNC_ARG) {
  19469. ret = wc_DsaVerify(hash, signature, &key, NULL);
  19470. }
  19471. if (ret == BAD_FUNC_ARG) {
  19472. ret = 0;
  19473. }
  19474. else {
  19475. ret = WOLFSSL_FATAL_ERROR;
  19476. }
  19477. }
  19478. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19479. /* hard set q to 0 and test fail case */
  19480. mp_free(&key.q);
  19481. mp_init(&key.q);
  19482. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19483. mp_set(&key.q, 1);
  19484. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19485. #endif
  19486. if (wc_FreeRng(&rng) && ret == 0) {
  19487. ret = WOLFSSL_FATAL_ERROR;
  19488. }
  19489. wc_FreeDsaKey(&key);
  19490. wc_ShaFree(&sha);
  19491. res = TEST_RES_CHECK(ret == 0);
  19492. #endif
  19493. return res;
  19494. } /* END test_wc_DsaSign */
  19495. /*
  19496. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19497. */
  19498. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19499. {
  19500. int res = TEST_SKIPPED;
  19501. #if !defined(NO_DSA)
  19502. DsaKey key;
  19503. word32 bytes;
  19504. word32 idx = 0;
  19505. int priv = 0;
  19506. int pub = 0;
  19507. int ret = 0;
  19508. #ifdef USE_CERT_BUFFERS_1024
  19509. byte tmp[ONEK_BUF];
  19510. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19511. bytes = sizeof_dsa_key_der_1024;
  19512. #elif defined(USE_CERT_BUFFERS_2048)
  19513. byte tmp[TWOK_BUF];
  19514. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19515. bytes = sizeof_dsa_key_der_2048;
  19516. #else
  19517. byte tmp[TWOK_BUF];
  19518. XMEMSET(tmp, 0, sizeof(tmp));
  19519. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  19520. if (fp == XBADFILE)
  19521. {
  19522. return WOLFSSL_BAD_FILE;
  19523. }
  19524. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  19525. XFCLOSE(fp);
  19526. #endif /* END USE_CERT_BUFFERS_1024 */
  19527. ret = wc_InitDsaKey(&key);
  19528. if (ret == 0) {
  19529. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  19530. /* Test bad args. */
  19531. if (priv == 0) {
  19532. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  19533. if (priv == BAD_FUNC_ARG) {
  19534. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  19535. }
  19536. if (priv == BAD_FUNC_ARG) {
  19537. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  19538. }
  19539. if (priv == BAD_FUNC_ARG) {
  19540. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  19541. }
  19542. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  19543. priv = 0;
  19544. }
  19545. else {
  19546. priv = WOLFSSL_FATAL_ERROR;
  19547. }
  19548. }
  19549. wc_FreeDsaKey(&key);
  19550. ret = wc_InitDsaKey(&key);
  19551. }
  19552. if (ret == 0) {
  19553. idx = 0; /* Reset */
  19554. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  19555. /* Test bad args. */
  19556. if (pub == 0) {
  19557. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  19558. if (pub == BAD_FUNC_ARG) {
  19559. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  19560. }
  19561. if (pub == BAD_FUNC_ARG) {
  19562. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  19563. }
  19564. if (pub == BAD_FUNC_ARG) {
  19565. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  19566. }
  19567. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  19568. pub = 0;
  19569. }
  19570. else {
  19571. pub = WOLFSSL_FATAL_ERROR;
  19572. }
  19573. }
  19574. } /* END Public Key */
  19575. wc_FreeDsaKey(&key);
  19576. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  19577. #endif /* !NO_DSA */
  19578. return res;
  19579. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19580. /*
  19581. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19582. */
  19583. static int test_wc_MakeDsaKey(void)
  19584. {
  19585. int res = TEST_SKIPPED;
  19586. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19587. DsaKey genKey;
  19588. WC_RNG rng;
  19589. int ret = 0;
  19590. XMEMSET(&rng, 0, sizeof(rng));
  19591. XMEMSET(&genKey, 0, sizeof(genKey));
  19592. ret = wc_InitRng(&rng);
  19593. if (ret == 0) {
  19594. ret = wc_InitDsaKey(&genKey);
  19595. }
  19596. if (ret == 0) {
  19597. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19598. }
  19599. /* Test bad args. */
  19600. if (ret == 0) {
  19601. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  19602. if (ret == BAD_FUNC_ARG) {
  19603. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  19604. }
  19605. if (ret == BAD_FUNC_ARG) {
  19606. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  19607. }
  19608. if (ret == BAD_FUNC_ARG) {
  19609. ret = 0;
  19610. }
  19611. else {
  19612. ret = WOLFSSL_FATAL_ERROR;
  19613. }
  19614. }
  19615. if (ret == 0) {
  19616. ret = wc_MakeDsaKey(&rng, &genKey);
  19617. }
  19618. /* Test bad args. */
  19619. if (ret == 0) {
  19620. ret = wc_MakeDsaKey(NULL, &genKey);
  19621. if (ret == BAD_FUNC_ARG) {
  19622. ret = wc_MakeDsaKey(&rng, NULL);
  19623. }
  19624. if (ret == BAD_FUNC_ARG) {
  19625. ret = 0;
  19626. }
  19627. else {
  19628. ret = WOLFSSL_FATAL_ERROR;
  19629. }
  19630. }
  19631. if (wc_FreeRng(&rng) && ret == 0) {
  19632. ret = WOLFSSL_FAILURE;
  19633. }
  19634. wc_FreeDsaKey(&genKey);
  19635. res = TEST_RES_CHECK(ret == 0);
  19636. #endif
  19637. return res;
  19638. } /* END test_wc_MakeDsaKey */
  19639. /*
  19640. * Testing wc_DsaKeyToDer()
  19641. */
  19642. static int test_wc_DsaKeyToDer(void)
  19643. {
  19644. int res = TEST_SKIPPED;
  19645. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19646. DsaKey genKey;
  19647. WC_RNG rng;
  19648. word32 bytes;
  19649. word32 idx = 0;
  19650. int ret = 0;
  19651. #ifdef USE_CERT_BUFFERS_1024
  19652. byte tmp[ONEK_BUF];
  19653. byte der[ONEK_BUF];
  19654. XMEMSET(tmp, 0, sizeof(tmp));
  19655. XMEMSET(der, 0, sizeof(der));
  19656. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19657. bytes = sizeof_dsa_key_der_1024;
  19658. #elif defined(USE_CERT_BUFFERS_2048)
  19659. byte tmp[TWOK_BUF];
  19660. byte der[TWOK_BUF];
  19661. XMEMSET(tmp, 0, sizeof(tmp));
  19662. XMEMSET(der, 0, sizeof(der));
  19663. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19664. bytes = sizeof_dsa_key_der_2048;
  19665. #else
  19666. byte tmp[TWOK_BUF];
  19667. byte der[TWOK_BUF];
  19668. XMEMSET(tmp, 0, sizeof(tmp));
  19669. XMEMSET(der, 0, sizeof(der));
  19670. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  19671. if (fp == XBADFILE) {
  19672. return WOLFSSL_BAD_FILE;
  19673. }
  19674. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  19675. XFCLOSE(fp);
  19676. #endif /* END USE_CERT_BUFFERS_1024 */
  19677. XMEMSET(&rng, 0, sizeof(rng));
  19678. XMEMSET(&genKey, 0, sizeof(genKey));
  19679. ret = wc_InitRng(&rng);
  19680. if (ret == 0) {
  19681. ret = wc_InitDsaKey(&genKey);
  19682. }
  19683. if (ret == 0) {
  19684. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  19685. if (ret == 0) {
  19686. wc_FreeDsaKey(&genKey);
  19687. ret = wc_InitDsaKey(&genKey);
  19688. }
  19689. }
  19690. if (ret == 0) {
  19691. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  19692. }
  19693. if (ret == 0) {
  19694. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  19695. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  19696. ret = 0;
  19697. }
  19698. }
  19699. /* Test bad args. */
  19700. if (ret == 0) {
  19701. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  19702. if (ret == BAD_FUNC_ARG) {
  19703. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  19704. }
  19705. if (ret == BAD_FUNC_ARG) {
  19706. ret = 0;
  19707. }
  19708. else {
  19709. ret = WOLFSSL_FATAL_ERROR;
  19710. }
  19711. }
  19712. if (wc_FreeRng(&rng) && ret == 0) {
  19713. ret = WOLFSSL_FATAL_ERROR;
  19714. }
  19715. wc_FreeDsaKey(&genKey);
  19716. res = TEST_RES_CHECK(ret == 0);
  19717. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19718. return res;
  19719. } /* END test_wc_DsaKeyToDer */
  19720. /*
  19721. * Testing wc_DsaKeyToPublicDer()
  19722. * (indirectly testing setDsaPublicKey())
  19723. */
  19724. static int test_wc_DsaKeyToPublicDer(void)
  19725. {
  19726. int res = TEST_SKIPPED;
  19727. #ifndef HAVE_SELFTEST
  19728. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19729. DsaKey genKey;
  19730. WC_RNG rng;
  19731. byte* der;
  19732. word32 sz;
  19733. int ret = 0;
  19734. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19735. if (der == NULL) {
  19736. ret = WOLFSSL_FATAL_ERROR;
  19737. }
  19738. if (ret == 0) {
  19739. ret = wc_InitDsaKey(&genKey);
  19740. }
  19741. if (ret == 0) {
  19742. ret = wc_InitRng(&rng);
  19743. }
  19744. if (ret == 0) {
  19745. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19746. }
  19747. if (ret == 0) {
  19748. ret = wc_MakeDsaKey(&rng, &genKey);
  19749. }
  19750. if (ret == 0) {
  19751. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  19752. if (ret >= 0) {
  19753. sz = ret;
  19754. ret = 0;
  19755. }
  19756. else {
  19757. ret = WOLFSSL_FATAL_ERROR;
  19758. }
  19759. }
  19760. if (ret == 0) {
  19761. word32 idx = 0;
  19762. wc_FreeDsaKey(&genKey);
  19763. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19764. }
  19765. /* Test without the SubjectPublicKeyInfo header */
  19766. if (ret == 0) {
  19767. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  19768. if (ret >= 0) {
  19769. sz = ret;
  19770. ret = 0;
  19771. }
  19772. else {
  19773. ret = WOLFSSL_FATAL_ERROR;
  19774. }
  19775. }
  19776. if (ret == 0) {
  19777. word32 idx = 0;
  19778. wc_FreeDsaKey(&genKey);
  19779. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19780. }
  19781. /* Test bad args. */
  19782. if (ret == 0) {
  19783. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  19784. if (ret == BAD_FUNC_ARG) {
  19785. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  19786. }
  19787. if (ret == BAD_FUNC_ARG) {
  19788. ret = 0;
  19789. }
  19790. else {
  19791. ret = WOLFSSL_FATAL_ERROR;
  19792. }
  19793. }
  19794. if (wc_FreeRng(&rng) && ret == 0) {
  19795. ret = WOLFSSL_FATAL_ERROR;
  19796. }
  19797. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19798. wc_FreeDsaKey(&genKey);
  19799. res = TEST_RES_CHECK(ret == 0);
  19800. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19801. #endif /* !HAVE_SELFTEST */
  19802. return res;
  19803. } /* END test_wc_DsaKeyToPublicDer */
  19804. /*
  19805. * Testing wc_DsaImportParamsRaw()
  19806. */
  19807. static int test_wc_DsaImportParamsRaw(void)
  19808. {
  19809. int res = TEST_SKIPPED;
  19810. #if !defined(NO_DSA)
  19811. DsaKey key;
  19812. int ret = 0;
  19813. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19814. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19815. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19816. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19817. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19818. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19819. "80a0093113a8bd73";
  19820. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19821. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19822. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19823. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19824. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19825. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19826. "76341a7e7d9";
  19827. /* invalid p and q parameters */
  19828. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19829. const char* invalidQ = "96c5390a";
  19830. ret = wc_InitDsaKey(&key);
  19831. if (ret == 0) {
  19832. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19833. }
  19834. /* test bad args */
  19835. if (ret == 0) {
  19836. /* null key struct */
  19837. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  19838. if (ret == BAD_FUNC_ARG) {
  19839. /* null param pointers */
  19840. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  19841. }
  19842. if (ret == BAD_FUNC_ARG) {
  19843. /* illegal p length */
  19844. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  19845. }
  19846. if (ret == BAD_FUNC_ARG) {
  19847. /* illegal q length */
  19848. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  19849. if (ret == BAD_FUNC_ARG)
  19850. ret = 0;
  19851. }
  19852. }
  19853. wc_FreeDsaKey(&key);
  19854. res = TEST_RES_CHECK(ret == 0);
  19855. #endif
  19856. return res;
  19857. } /* END test_wc_DsaImportParamsRaw */
  19858. /*
  19859. * Testing wc_DsaImportParamsRawCheck()
  19860. */
  19861. static int test_wc_DsaImportParamsRawCheck(void)
  19862. {
  19863. int res = TEST_SKIPPED;
  19864. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19865. DsaKey key;
  19866. int ret = 0;
  19867. int trusted = 0;
  19868. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19869. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19870. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19871. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19872. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19873. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19874. "80a0093113a8bd73";
  19875. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19876. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19877. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19878. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19879. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19880. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19881. "76341a7e7d9";
  19882. /* invalid p and q parameters */
  19883. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19884. const char* invalidQ = "96c5390a";
  19885. ret = wc_InitDsaKey(&key);
  19886. if (ret == 0) {
  19887. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  19888. }
  19889. /* test bad args */
  19890. if (ret == 0) {
  19891. /* null key struct */
  19892. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  19893. if (ret == BAD_FUNC_ARG) {
  19894. /* null param pointers */
  19895. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  19896. }
  19897. if (ret == BAD_FUNC_ARG) {
  19898. /* illegal p length */
  19899. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  19900. }
  19901. if (ret == BAD_FUNC_ARG) {
  19902. /* illegal q length */
  19903. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  19904. if (ret == BAD_FUNC_ARG)
  19905. ret = 0;
  19906. }
  19907. }
  19908. wc_FreeDsaKey(&key);
  19909. res = TEST_RES_CHECK(ret == 0);
  19910. #endif
  19911. return res;
  19912. } /* END test_wc_DsaImportParamsRawCheck */
  19913. /*
  19914. * Testing wc_DsaExportParamsRaw()
  19915. */
  19916. static int test_wc_DsaExportParamsRaw(void)
  19917. {
  19918. int res = TEST_SKIPPED;
  19919. #if !defined(NO_DSA)
  19920. DsaKey key;
  19921. int ret = 0;
  19922. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19923. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19924. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19925. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19926. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19927. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19928. "80a0093113a8bd73";
  19929. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19930. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19931. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19932. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19933. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19934. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19935. "76341a7e7d9";
  19936. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19937. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19938. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19939. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19940. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19941. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19942. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19943. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19944. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19945. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19946. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19947. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19948. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19949. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19950. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19951. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19952. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19953. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19954. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19955. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19956. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19957. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19958. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19959. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19960. byte pOut[MAX_DSA_PARAM_SIZE];
  19961. byte qOut[MAX_DSA_PARAM_SIZE];
  19962. byte gOut[MAX_DSA_PARAM_SIZE];
  19963. word32 pOutSz, qOutSz, gOutSz;
  19964. ret = wc_InitDsaKey(&key);
  19965. if (ret == 0) {
  19966. /* first test using imported raw parameters, for expected */
  19967. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19968. }
  19969. if (ret == 0) {
  19970. pOutSz = sizeof(pOut);
  19971. qOutSz = sizeof(qOut);
  19972. gOutSz = sizeof(gOut);
  19973. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19974. gOut, &gOutSz);
  19975. }
  19976. if (ret == 0) {
  19977. /* validate exported parameters are correct */
  19978. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  19979. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  19980. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  19981. ret = -1;
  19982. }
  19983. }
  19984. /* test bad args */
  19985. if (ret == 0) {
  19986. /* null key struct */
  19987. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  19988. gOut, &gOutSz);
  19989. if (ret == BAD_FUNC_ARG) {
  19990. /* null output pointers */
  19991. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  19992. NULL, &gOutSz);
  19993. }
  19994. if (ret == LENGTH_ONLY_E) {
  19995. /* null output size pointers */
  19996. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  19997. gOut, NULL);
  19998. }
  19999. if (ret == BAD_FUNC_ARG) {
  20000. /* p output buffer size too small */
  20001. pOutSz = 1;
  20002. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  20003. gOut, &gOutSz);
  20004. pOutSz = sizeof(pOut);
  20005. }
  20006. if (ret == BUFFER_E) {
  20007. /* q output buffer size too small */
  20008. qOutSz = 1;
  20009. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  20010. gOut, &gOutSz);
  20011. qOutSz = sizeof(qOut);
  20012. }
  20013. if (ret == BUFFER_E) {
  20014. /* g output buffer size too small */
  20015. gOutSz = 1;
  20016. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  20017. gOut, &gOutSz);
  20018. if (ret == BUFFER_E)
  20019. ret = 0;
  20020. }
  20021. }
  20022. wc_FreeDsaKey(&key);
  20023. res = TEST_RES_CHECK(ret == 0);
  20024. #endif
  20025. return res;
  20026. } /* END test_wc_DsaExportParamsRaw */
  20027. /*
  20028. * Testing wc_DsaExportKeyRaw()
  20029. */
  20030. static int test_wc_DsaExportKeyRaw(void)
  20031. {
  20032. int res = TEST_SKIPPED;
  20033. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  20034. DsaKey key;
  20035. WC_RNG rng;
  20036. int ret = 0;
  20037. byte xOut[MAX_DSA_PARAM_SIZE];
  20038. byte yOut[MAX_DSA_PARAM_SIZE];
  20039. word32 xOutSz, yOutSz;
  20040. XMEMSET(&rng, 0, sizeof(rng));
  20041. XMEMSET(&key, 0, sizeof(key));
  20042. ret = wc_InitRng(&rng);
  20043. if (ret == 0) {
  20044. ret = wc_InitDsaKey(&key);
  20045. }
  20046. if (ret == 0) {
  20047. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  20048. if (ret == 0) {
  20049. ret = wc_MakeDsaKey(&rng, &key);
  20050. }
  20051. }
  20052. /* try successful export */
  20053. if (ret == 0) {
  20054. xOutSz = sizeof(xOut);
  20055. yOutSz = sizeof(yOut);
  20056. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  20057. }
  20058. /* test bad args */
  20059. if (ret == 0) {
  20060. /* null key struct */
  20061. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  20062. if (ret == BAD_FUNC_ARG) {
  20063. /* null output pointers */
  20064. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  20065. }
  20066. if (ret == LENGTH_ONLY_E) {
  20067. /* null output size pointers */
  20068. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  20069. }
  20070. if (ret == BAD_FUNC_ARG) {
  20071. /* x output buffer size too small */
  20072. xOutSz = 1;
  20073. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  20074. xOutSz = sizeof(xOut);
  20075. }
  20076. if (ret == BUFFER_E) {
  20077. /* y output buffer size too small */
  20078. yOutSz = 1;
  20079. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  20080. if (ret == BUFFER_E)
  20081. ret = 0;
  20082. }
  20083. }
  20084. wc_FreeDsaKey(&key);
  20085. wc_FreeRng(&rng);
  20086. res = TEST_RES_CHECK(ret == 0);
  20087. #endif
  20088. return res;
  20089. } /* END test_wc_DsaExportParamsRaw */
  20090. /*
  20091. * Testing wc_ed25519_make_key().
  20092. */
  20093. static int test_wc_ed25519_make_key(void)
  20094. {
  20095. int res = TEST_SKIPPED;
  20096. #if defined(HAVE_ED25519)
  20097. ed25519_key key;
  20098. WC_RNG rng;
  20099. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  20100. int ret = 0;
  20101. ret = wc_InitRng(&rng);
  20102. if (ret == 0) {
  20103. ret = wc_ed25519_init(&key);
  20104. }
  20105. if (ret == 0) {
  20106. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  20107. if (ret == ECC_PRIV_KEY_E) {
  20108. ret = 0;
  20109. }
  20110. else if (ret == 0) {
  20111. ret = -1;
  20112. }
  20113. }
  20114. if (ret == 0) {
  20115. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20116. }
  20117. /* Test bad args. */
  20118. if (ret == 0) {
  20119. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  20120. if (ret == BAD_FUNC_ARG) {
  20121. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  20122. }
  20123. if (ret == BAD_FUNC_ARG) {
  20124. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  20125. }
  20126. if (ret == BAD_FUNC_ARG) {
  20127. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  20128. }
  20129. if (ret == BAD_FUNC_ARG) {
  20130. ret = 0;
  20131. }
  20132. else if (ret == 0) {
  20133. ret = WOLFSSL_FATAL_ERROR;
  20134. }
  20135. }
  20136. if (wc_FreeRng(&rng) && ret == 0) {
  20137. ret = WOLFSSL_FATAL_ERROR;
  20138. }
  20139. wc_ed25519_free(&key);
  20140. res = TEST_RES_CHECK(ret == 0);
  20141. #endif
  20142. return res;
  20143. } /* END test_wc_ed25519_make_key */
  20144. /*
  20145. * Testing wc_ed25519_init()
  20146. */
  20147. static int test_wc_ed25519_init(void)
  20148. {
  20149. int res = TEST_SKIPPED;
  20150. #if defined(HAVE_ED25519)
  20151. ed25519_key key;
  20152. int ret = 0;
  20153. ret = wc_ed25519_init(&key);
  20154. /* Test bad args. */
  20155. if (ret == 0) {
  20156. ret = wc_ed25519_init(NULL);
  20157. if (ret == BAD_FUNC_ARG) {
  20158. ret = 0;
  20159. }
  20160. else if (ret == 0) {
  20161. ret = WOLFSSL_FATAL_ERROR;
  20162. }
  20163. }
  20164. wc_ed25519_free(&key);
  20165. res = TEST_RES_CHECK(ret == 0);
  20166. #endif
  20167. return res;
  20168. } /* END test_wc_ed25519_init */
  20169. /*
  20170. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20171. */
  20172. static int test_wc_ed25519_sign_msg(void)
  20173. {
  20174. int res = TEST_SKIPPED;
  20175. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20176. WC_RNG rng;
  20177. ed25519_key key;
  20178. int ret = 0;
  20179. byte msg[] = "Everybody gets Friday off.\n";
  20180. byte sig[ED25519_SIG_SIZE];
  20181. word32 msglen = sizeof(msg);
  20182. word32 siglen = sizeof(sig);
  20183. word32 badSigLen = sizeof(sig) - 1;
  20184. #ifdef HAVE_ED25519_VERIFY
  20185. int verify_ok = 0; /*1 = Verify success.*/
  20186. #endif
  20187. /* Initialize stack variables. */
  20188. XMEMSET(sig, 0, siglen);
  20189. /* Initialize key. */
  20190. ret = wc_InitRng(&rng);
  20191. if (ret == 0) {
  20192. ret = wc_ed25519_init(&key);
  20193. if (ret == 0) {
  20194. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20195. }
  20196. }
  20197. if (ret == 0) {
  20198. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  20199. }
  20200. /* Test bad args. */
  20201. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  20202. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  20203. if (ret == BAD_FUNC_ARG) {
  20204. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  20205. }
  20206. if (ret == BAD_FUNC_ARG) {
  20207. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  20208. }
  20209. if (ret == BAD_FUNC_ARG) {
  20210. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  20211. }
  20212. if (ret == BAD_FUNC_ARG) {
  20213. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  20214. }
  20215. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  20216. badSigLen -= 1;
  20217. ret = 0;
  20218. }
  20219. else if (ret == 0) {
  20220. ret = WOLFSSL_FATAL_ERROR;
  20221. }
  20222. } /* END sign */
  20223. #ifdef HAVE_ED25519_VERIFY
  20224. if (ret == 0) {
  20225. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  20226. if (ret == 0 && verify_ok == 1) {
  20227. ret = 0;
  20228. }
  20229. else if (ret == 0) {
  20230. ret = WOLFSSL_FATAL_ERROR;
  20231. }
  20232. /* Test bad args. */
  20233. if (ret == 0) {
  20234. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  20235. msglen, &verify_ok, &key),
  20236. BAD_FUNC_ARG);
  20237. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  20238. msglen, &verify_ok, &key),
  20239. BAD_FUNC_ARG);
  20240. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20241. &key);
  20242. if (ret == BAD_FUNC_ARG) {
  20243. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  20244. &verify_ok, &key);
  20245. }
  20246. if (ret == BAD_FUNC_ARG) {
  20247. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  20248. NULL, &key);
  20249. }
  20250. if (ret == BAD_FUNC_ARG) {
  20251. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  20252. &verify_ok, NULL);
  20253. }
  20254. if (ret == BAD_FUNC_ARG) {
  20255. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  20256. &verify_ok, &key);
  20257. }
  20258. if (ret == BAD_FUNC_ARG) {
  20259. ret = 0;
  20260. }
  20261. else if (ret == 0) {
  20262. ret = WOLFSSL_FATAL_ERROR;
  20263. }
  20264. }
  20265. } /* END verify. */
  20266. #endif /* Verify. */
  20267. if (wc_FreeRng(&rng) && ret == 0) {
  20268. ret = WOLFSSL_FATAL_ERROR;
  20269. }
  20270. wc_ed25519_free(&key);
  20271. res = TEST_RES_CHECK(ret == 0);
  20272. #endif
  20273. return res;
  20274. } /* END test_wc_ed25519_sign_msg */
  20275. /*
  20276. * Testing wc_ed25519_import_public()
  20277. */
  20278. static int test_wc_ed25519_import_public(void)
  20279. {
  20280. int res = TEST_SKIPPED;
  20281. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20282. WC_RNG rng;
  20283. ed25519_key pubKey;
  20284. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20285. word32 inlen = sizeof(in);
  20286. int ret = 0;
  20287. ret = wc_InitRng(&rng);
  20288. if (ret == 0) {
  20289. ret = wc_ed25519_init(&pubKey);
  20290. if (ret == 0) {
  20291. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  20292. }
  20293. }
  20294. if (ret == 0) {
  20295. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  20296. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  20297. ret = 0;
  20298. }
  20299. else {
  20300. ret = WOLFSSL_FATAL_ERROR;
  20301. }
  20302. /* Test bad args. */
  20303. if (ret == 0) {
  20304. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  20305. if (ret == BAD_FUNC_ARG) {
  20306. ret = wc_ed25519_import_public(in, inlen, NULL);
  20307. }
  20308. if (ret == BAD_FUNC_ARG) {
  20309. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  20310. }
  20311. if (ret == BAD_FUNC_ARG) {
  20312. ret = 0;
  20313. }
  20314. else if (ret == 0) {
  20315. ret = WOLFSSL_FATAL_ERROR;
  20316. }
  20317. }
  20318. }
  20319. if (wc_FreeRng(&rng) && ret == 0) {
  20320. ret = WOLFSSL_FATAL_ERROR;
  20321. }
  20322. wc_ed25519_free(&pubKey);
  20323. res = TEST_RES_CHECK(ret == 0);
  20324. #endif
  20325. return res;
  20326. } /* END wc_ed25519_import_public */
  20327. /*
  20328. * Testing wc_ed25519_import_private_key()
  20329. */
  20330. static int test_wc_ed25519_import_private_key(void)
  20331. {
  20332. int res = TEST_SKIPPED;
  20333. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20334. WC_RNG rng;
  20335. ed25519_key key;
  20336. int ret;
  20337. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20338. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20339. word32 privKeySz = sizeof(privKey);
  20340. word32 pubKeySz = sizeof(pubKey);
  20341. #ifdef HAVE_ED25519_KEY_EXPORT
  20342. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20343. word32 bothKeysSz = sizeof(bothKeys);
  20344. #endif
  20345. ret = wc_InitRng(&rng);
  20346. if (ret != 0) {
  20347. return ret;
  20348. }
  20349. ret = wc_ed25519_init(&key);
  20350. if (ret != 0) {
  20351. wc_FreeRng(&rng);
  20352. return ret;
  20353. }
  20354. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20355. if (ret == 0) {
  20356. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20357. pubKeySz, &key, 1);
  20358. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  20359. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20360. ret = WOLFSSL_FATAL_ERROR;
  20361. }
  20362. }
  20363. #ifdef HAVE_ED25519_KEY_EXPORT
  20364. if (ret == 0)
  20365. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  20366. if (ret == 0) {
  20367. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20368. &key, 1);
  20369. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  20370. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20371. ret = WOLFSSL_FATAL_ERROR;
  20372. }
  20373. }
  20374. #endif
  20375. /* Test bad args. */
  20376. if (ret == 0) {
  20377. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20378. &key);
  20379. if (ret == BAD_FUNC_ARG) {
  20380. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20381. pubKeySz, &key);
  20382. }
  20383. if (ret == BAD_FUNC_ARG) {
  20384. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20385. pubKeySz, NULL);
  20386. }
  20387. if (ret == BAD_FUNC_ARG) {
  20388. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20389. pubKeySz, &key);
  20390. }
  20391. if (ret == BAD_FUNC_ARG) {
  20392. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20393. pubKeySz - 1, &key);
  20394. }
  20395. if (ret == BAD_FUNC_ARG) {
  20396. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20397. 0, &key);
  20398. }
  20399. if (ret == BAD_FUNC_ARG) {
  20400. ret = 0;
  20401. }
  20402. else if (ret == 0) {
  20403. ret = WOLFSSL_FATAL_ERROR;
  20404. }
  20405. }
  20406. if (wc_FreeRng(&rng) && ret == 0) {
  20407. ret = WOLFSSL_FATAL_ERROR;
  20408. }
  20409. wc_ed25519_free(&key);
  20410. res = TEST_RES_CHECK(ret == 0);
  20411. #endif
  20412. return res;
  20413. } /* END test_wc_ed25519_import_private_key */
  20414. /*
  20415. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20416. */
  20417. static int test_wc_ed25519_export(void)
  20418. {
  20419. int res = TEST_SKIPPED;
  20420. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20421. WC_RNG rng;
  20422. ed25519_key key;
  20423. int ret = 0;
  20424. byte priv[ED25519_PRV_KEY_SIZE];
  20425. byte pub[ED25519_PUB_KEY_SIZE];
  20426. word32 privSz = sizeof(priv);
  20427. word32 pubSz = sizeof(pub);
  20428. ret = wc_InitRng(&rng);
  20429. if (ret != 0) {
  20430. return ret;
  20431. }
  20432. ret = wc_ed25519_init(&key);
  20433. if (ret != 0) {
  20434. wc_FreeRng(&rng);
  20435. return ret;
  20436. }
  20437. if (ret == 0) {
  20438. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20439. }
  20440. if (ret == 0) {
  20441. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  20442. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  20443. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  20444. ret = WOLFSSL_FATAL_ERROR;
  20445. }
  20446. if (ret == 0) {
  20447. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  20448. if (ret == BAD_FUNC_ARG) {
  20449. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  20450. }
  20451. if (ret == BAD_FUNC_ARG) {
  20452. ret = wc_ed25519_export_public(&key, pub, NULL);
  20453. }
  20454. if (ret == BAD_FUNC_ARG) {
  20455. ret = 0;
  20456. }
  20457. else if (ret == 0) {
  20458. ret = WOLFSSL_FATAL_ERROR;
  20459. }
  20460. }
  20461. }
  20462. if (ret == 0) {
  20463. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  20464. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  20465. || XMEMCMP(key.k, priv, privSz) != 0)) {
  20466. ret = WOLFSSL_FATAL_ERROR;
  20467. }
  20468. if (ret == 0) {
  20469. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  20470. if (ret == BAD_FUNC_ARG) {
  20471. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  20472. }
  20473. if (ret == BAD_FUNC_ARG) {
  20474. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  20475. }
  20476. if (ret == BAD_FUNC_ARG) {
  20477. ret = 0;
  20478. }
  20479. else if (ret == 0) {
  20480. ret = WOLFSSL_FATAL_ERROR;
  20481. }
  20482. }
  20483. }
  20484. if (wc_FreeRng(&rng) && ret == 0) {
  20485. ret = WOLFSSL_FATAL_ERROR;
  20486. }
  20487. wc_ed25519_free(&key);
  20488. res = TEST_RES_CHECK(ret == 0);
  20489. #endif
  20490. return res;
  20491. } /* END test_wc_ed25519_export */
  20492. /*
  20493. * Testing wc_ed25519_size()
  20494. */
  20495. static int test_wc_ed25519_size(void)
  20496. {
  20497. int res = TEST_SKIPPED;
  20498. #if defined(HAVE_ED25519)
  20499. WC_RNG rng;
  20500. ed25519_key key;
  20501. int ret;
  20502. ret = wc_InitRng(&rng);
  20503. if (ret != 0) {
  20504. return ret;
  20505. }
  20506. ret = wc_ed25519_init(&key);
  20507. if (ret != 0) {
  20508. wc_FreeRng(&rng);
  20509. return ret;
  20510. }
  20511. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20512. if (ret != 0) {
  20513. wc_FreeRng(&rng);
  20514. wc_ed25519_free(&key);
  20515. return ret;
  20516. }
  20517. ret = wc_ed25519_size(&key);
  20518. /* Test bad args. */
  20519. if (ret == ED25519_KEY_SIZE) {
  20520. ret = wc_ed25519_size(NULL);
  20521. if (ret == BAD_FUNC_ARG) {
  20522. ret = 0;
  20523. }
  20524. }
  20525. if (ret == 0) {
  20526. ret = wc_ed25519_sig_size(&key);
  20527. if (ret == ED25519_SIG_SIZE) {
  20528. ret = 0;
  20529. }
  20530. /* Test bad args. */
  20531. if (ret == 0) {
  20532. ret = wc_ed25519_sig_size(NULL);
  20533. if (ret == BAD_FUNC_ARG) {
  20534. ret = 0;
  20535. }
  20536. }
  20537. } /* END wc_ed25519_sig_size() */
  20538. if (ret == 0) {
  20539. ret = wc_ed25519_pub_size(&key);
  20540. if (ret == ED25519_PUB_KEY_SIZE) {
  20541. ret = 0;
  20542. }
  20543. if (ret == 0) {
  20544. ret = wc_ed25519_pub_size(NULL);
  20545. if (ret == BAD_FUNC_ARG) {
  20546. ret = 0;
  20547. }
  20548. }
  20549. } /* END wc_ed25519_pub_size */
  20550. if (ret == 0) {
  20551. ret = wc_ed25519_priv_size(&key);
  20552. if (ret == ED25519_PRV_KEY_SIZE) {
  20553. ret = 0;
  20554. }
  20555. if (ret == 0) {
  20556. ret = wc_ed25519_priv_size(NULL);
  20557. if (ret == BAD_FUNC_ARG) {
  20558. ret = 0;
  20559. }
  20560. }
  20561. } /* END wc_ed25519_pub_size */
  20562. if (wc_FreeRng(&rng) && ret == 0) {
  20563. ret = WOLFSSL_FATAL_ERROR;
  20564. }
  20565. wc_ed25519_free(&key);
  20566. res = TEST_RES_CHECK(ret == 0);
  20567. #endif
  20568. return res;
  20569. } /* END test_wc_ed25519_size */
  20570. /*
  20571. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20572. */
  20573. static int test_wc_ed25519_exportKey(void)
  20574. {
  20575. int res = TEST_SKIPPED;
  20576. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20577. WC_RNG rng;
  20578. ed25519_key key;
  20579. int ret = 0;
  20580. byte priv[ED25519_PRV_KEY_SIZE];
  20581. byte pub[ED25519_PUB_KEY_SIZE];
  20582. byte privOnly[ED25519_PRV_KEY_SIZE];
  20583. word32 privSz = sizeof(priv);
  20584. word32 pubSz = sizeof(pub);
  20585. word32 privOnlySz = sizeof(privOnly);
  20586. ret = wc_InitRng(&rng);
  20587. if (ret != 0) {
  20588. return TEST_FAIL;
  20589. }
  20590. ret = wc_ed25519_init(&key);
  20591. if (ret != 0) {
  20592. wc_FreeRng(&rng);
  20593. return TEST_FAIL;
  20594. }
  20595. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20596. if (ret != 0) {
  20597. wc_FreeRng(&rng);
  20598. wc_ed25519_free(&key);
  20599. return TEST_FAIL;
  20600. }
  20601. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  20602. if (ret == 0) {
  20603. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  20604. if (ret == BAD_FUNC_ARG) {
  20605. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  20606. }
  20607. if (ret == BAD_FUNC_ARG) {
  20608. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  20609. }
  20610. if (ret == BAD_FUNC_ARG) {
  20611. ret = 0;
  20612. }
  20613. else if (ret == 0) {
  20614. ret = WOLFSSL_FATAL_ERROR;
  20615. }
  20616. }
  20617. if (ret == 0) {
  20618. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  20619. if (ret == 0) {
  20620. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  20621. if (ret == BAD_FUNC_ARG) {
  20622. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  20623. }
  20624. if (ret == BAD_FUNC_ARG) {
  20625. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  20626. }
  20627. if (ret == BAD_FUNC_ARG) {
  20628. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  20629. }
  20630. if (ret == BAD_FUNC_ARG) {
  20631. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  20632. }
  20633. if (ret == BAD_FUNC_ARG) {
  20634. ret = 0;
  20635. }
  20636. else if (ret == 0) {
  20637. ret = WOLFSSL_FATAL_ERROR;
  20638. }
  20639. }
  20640. } /* END wc_ed25519_export_key() */
  20641. /* Cross check output. */
  20642. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20643. ret = WOLFSSL_FATAL_ERROR;
  20644. }
  20645. if (wc_FreeRng(&rng) && ret == 0) {
  20646. ret = WOLFSSL_FATAL_ERROR;
  20647. }
  20648. wc_ed25519_free(&key);
  20649. res = TEST_RES_CHECK(ret == 0);
  20650. #endif
  20651. return res;
  20652. } /* END test_wc_ed25519_exportKey */
  20653. /*
  20654. * Testing wc_Ed25519PublicKeyToDer
  20655. */
  20656. static int test_wc_Ed25519PublicKeyToDer(void)
  20657. {
  20658. int res = TEST_SKIPPED;
  20659. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20660. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20661. int tmp;
  20662. ed25519_key key;
  20663. byte derBuf[1024];
  20664. int ret = 0;
  20665. /* Test bad args */
  20666. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  20667. if (tmp != BAD_FUNC_ARG) {
  20668. ret = WOLFSSL_FATAL_ERROR;
  20669. }
  20670. if (ret == 0) {
  20671. wc_ed25519_init(&key);
  20672. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  20673. if (tmp != BUFFER_E) {
  20674. ret = WOLFSSL_FATAL_ERROR;
  20675. }
  20676. wc_ed25519_free(&key);
  20677. }
  20678. /* Test good args */
  20679. if (ret == 0) {
  20680. WC_RNG rng;
  20681. ret = wc_InitRng(&rng);
  20682. if (ret != 0) {
  20683. return TEST_FAIL;
  20684. }
  20685. ret = wc_ed25519_init(&key);
  20686. if (ret != 0) {
  20687. wc_FreeRng(&rng);
  20688. return TEST_FAIL;
  20689. }
  20690. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20691. if (ret != 0) {
  20692. wc_FreeRng(&rng);
  20693. wc_ed25519_free(&key);
  20694. return TEST_FAIL;
  20695. }
  20696. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  20697. if (tmp <= 0) {
  20698. ret = WOLFSSL_FATAL_ERROR;
  20699. }
  20700. wc_FreeRng(&rng);
  20701. wc_ed25519_free(&key);
  20702. }
  20703. res = TEST_RES_CHECK(ret == 0);
  20704. #endif
  20705. return res;
  20706. } /* END testing wc_Ed25519PublicKeyToDer */
  20707. /*
  20708. * Testing wc_curve25519_init and wc_curve25519_free.
  20709. */
  20710. static int test_wc_curve25519_init(void)
  20711. {
  20712. int res = TEST_SKIPPED;
  20713. #if defined(HAVE_CURVE25519)
  20714. curve25519_key key;
  20715. int ret = 0;
  20716. ret = wc_curve25519_init(&key);
  20717. /* Test bad args for wc_curve25519_init */
  20718. if (ret == 0) {
  20719. ret = wc_curve25519_init(NULL);
  20720. if (ret == BAD_FUNC_ARG) {
  20721. ret = 0;
  20722. }
  20723. else if (ret == 0) {
  20724. ret = WOLFSSL_FATAL_ERROR;
  20725. }
  20726. }
  20727. /* Test good args for wc_curve_25519_free */
  20728. wc_curve25519_free(&key);
  20729. wc_curve25519_free(NULL);
  20730. res = TEST_RES_CHECK(ret == 0);
  20731. #endif
  20732. return res;
  20733. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20734. /*
  20735. * Testing test_wc_curve25519_size.
  20736. */
  20737. static int test_wc_curve25519_size(void)
  20738. {
  20739. int res = TEST_SKIPPED;
  20740. #if defined(HAVE_CURVE25519)
  20741. curve25519_key key;
  20742. int ret = 0;
  20743. ret = wc_curve25519_init(&key);
  20744. /* Test good args for wc_curve25519_size */
  20745. if (ret == 0) {
  20746. ret = wc_curve25519_size(&key);
  20747. }
  20748. /* Test bad args for wc_curve25519_size */
  20749. if (ret != 0) {
  20750. ret = wc_curve25519_size(NULL);
  20751. }
  20752. wc_curve25519_free(&key);
  20753. res = TEST_RES_CHECK(ret == 0);
  20754. #endif
  20755. return res;
  20756. } /* END test_wc_curve25519_size*/
  20757. /*
  20758. * Testing test_wc_curve25519_export_key_raw().
  20759. */
  20760. static int test_wc_curve25519_export_key_raw(void)
  20761. {
  20762. int res = TEST_SKIPPED;
  20763. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20764. curve25519_key key;
  20765. WC_RNG rng;
  20766. int ret = 0;
  20767. byte privateKey[CURVE25519_KEYSIZE];
  20768. byte publicKey[CURVE25519_KEYSIZE];
  20769. word32 prvkSz;
  20770. word32 pubkSz;
  20771. byte prik[CURVE25519_KEYSIZE];
  20772. byte pubk[CURVE25519_KEYSIZE];
  20773. word32 prksz;
  20774. word32 pbksz;
  20775. if (0 != wc_InitRng(&rng)) {
  20776. return TEST_FAIL;
  20777. }
  20778. if (0 != wc_curve25519_init(&key)) {
  20779. wc_FreeRng(&rng);
  20780. return TEST_FAIL;
  20781. }
  20782. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20783. /*
  20784. bad-argument-test cases
  20785. target function sould return BAD_FUNC_ARG
  20786. */
  20787. if (ret == 0) {
  20788. prvkSz = CURVE25519_KEYSIZE;
  20789. pubkSz = CURVE25519_KEYSIZE;
  20790. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20791. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  20792. ret = -1;
  20793. }
  20794. }
  20795. if (ret == 0) {
  20796. prvkSz = CURVE25519_KEYSIZE;
  20797. pubkSz = CURVE25519_KEYSIZE;
  20798. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20799. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  20800. ret = -1;
  20801. }
  20802. }
  20803. if (ret == 0) {
  20804. prvkSz = CURVE25519_KEYSIZE;
  20805. pubkSz = CURVE25519_KEYSIZE;
  20806. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20807. &key, privateKey, NULL, publicKey, &pubkSz)) {
  20808. ret = -1;
  20809. }
  20810. }
  20811. if (ret == 0) {
  20812. /* prvkSz = CURVE25519_KEYSIZE; */
  20813. pubkSz = CURVE25519_KEYSIZE;
  20814. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20815. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  20816. ret = -1;
  20817. }
  20818. }
  20819. if (ret == 0) {
  20820. prvkSz = CURVE25519_KEYSIZE;
  20821. pubkSz = CURVE25519_KEYSIZE;
  20822. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20823. &key, privateKey, &prvkSz, publicKey, NULL )) {
  20824. ret = -1;
  20825. }
  20826. }
  20827. /*
  20828. cross-testing
  20829. */
  20830. if (ret == 0) {
  20831. prksz = CURVE25519_KEYSIZE;
  20832. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  20833. }
  20834. if (ret == 0) {
  20835. pbksz = CURVE25519_KEYSIZE;
  20836. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  20837. }
  20838. if (ret == 0) {
  20839. prvkSz = CURVE25519_KEYSIZE;
  20840. /* pubkSz = CURVE25519_KEYSIZE; */
  20841. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20842. publicKey, &pubkSz);
  20843. }
  20844. if (ret == 0) {
  20845. if ((prksz == CURVE25519_KEYSIZE) &&
  20846. (pbksz == CURVE25519_KEYSIZE) &&
  20847. (prvkSz == CURVE25519_KEYSIZE) &&
  20848. (pubkSz == CURVE25519_KEYSIZE)) {
  20849. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  20850. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  20851. ret = -1;
  20852. }
  20853. }
  20854. }
  20855. wc_curve25519_free(&key);
  20856. wc_FreeRng(&rng);
  20857. res = TEST_RES_CHECK(ret == 0);
  20858. #endif
  20859. return res;
  20860. } /* end of test_wc_curve25519_export_key_raw */
  20861. /*
  20862. * Testing test_wc_curve25519_export_key_raw_ex().
  20863. */
  20864. static int test_wc_curve25519_export_key_raw_ex(void)
  20865. {
  20866. int res = TEST_SKIPPED;
  20867. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20868. curve25519_key key;
  20869. WC_RNG rng;
  20870. int ret;
  20871. byte privateKey[CURVE25519_KEYSIZE];
  20872. byte publicKey[CURVE25519_KEYSIZE];
  20873. word32 prvkSz;
  20874. word32 pubkSz;
  20875. byte prik[CURVE25519_KEYSIZE];
  20876. byte pubk[CURVE25519_KEYSIZE];
  20877. word32 prksz;
  20878. word32 pbksz;
  20879. if (0 != wc_InitRng(&rng)) {
  20880. return TEST_FAIL;
  20881. }
  20882. if (0 != wc_curve25519_init(&key)) {
  20883. wc_FreeRng(&rng);
  20884. return TEST_FAIL;
  20885. }
  20886. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20887. /*
  20888. bad-argument-test cases
  20889. target function sould return BAD_FUNC_ARG
  20890. */
  20891. if (ret == 0) {
  20892. prvkSz = CURVE25519_KEYSIZE;
  20893. pubkSz = CURVE25519_KEYSIZE;
  20894. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  20895. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20896. ret = -1;
  20897. }
  20898. }
  20899. if (ret == 0) {
  20900. prvkSz = CURVE25519_KEYSIZE;
  20901. pubkSz = CURVE25519_KEYSIZE;
  20902. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  20903. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20904. ret = -1;
  20905. }
  20906. }
  20907. if (ret == 0) {
  20908. prvkSz = CURVE25519_KEYSIZE;
  20909. pubkSz = CURVE25519_KEYSIZE;
  20910. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  20911. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20912. ret = -1;
  20913. }
  20914. }
  20915. if (ret == 0) {
  20916. /* prvkSz = CURVE25519_KEYSIZE; */
  20917. pubkSz = CURVE25519_KEYSIZE;
  20918. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20919. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20920. ret = -1;
  20921. }
  20922. }
  20923. if (ret == 0) {
  20924. prvkSz = CURVE25519_KEYSIZE;
  20925. pubkSz = CURVE25519_KEYSIZE;
  20926. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20927. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  20928. ret = -1;
  20929. }
  20930. }
  20931. if (ret == 0) {
  20932. prvkSz = CURVE25519_KEYSIZE;
  20933. /* pubkSz = CURVE25519_KEYSIZE; */
  20934. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  20935. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20936. ret = -1;
  20937. }
  20938. }
  20939. if (ret == 0) {
  20940. prvkSz = CURVE25519_KEYSIZE;
  20941. pubkSz = CURVE25519_KEYSIZE;
  20942. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  20943. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20944. ret = -1;
  20945. }
  20946. }
  20947. if (ret == 0) {
  20948. prvkSz = CURVE25519_KEYSIZE;
  20949. pubkSz = CURVE25519_KEYSIZE;
  20950. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20951. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20952. ret = -1;
  20953. }
  20954. }
  20955. if (ret == 0) {
  20956. /* prvkSz = CURVE25519_KEYSIZE; */
  20957. pubkSz = CURVE25519_KEYSIZE;
  20958. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20959. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  20960. ret = -1;
  20961. }
  20962. }
  20963. if (ret == 0) {
  20964. prvkSz = CURVE25519_KEYSIZE;
  20965. pubkSz = CURVE25519_KEYSIZE;
  20966. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20967. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  20968. ret = -1;
  20969. }
  20970. }
  20971. /* illegal value for endien */
  20972. if (ret == 0) {
  20973. prvkSz = CURVE25519_KEYSIZE;
  20974. /* pubkSz = CURVE25519_KEYSIZE; */
  20975. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  20976. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  20977. ret = -1;
  20978. }
  20979. }
  20980. /*
  20981. cross-testing
  20982. */
  20983. if (ret == 0) {
  20984. prksz = CURVE25519_KEYSIZE;
  20985. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  20986. }
  20987. if (ret == 0) {
  20988. pbksz = CURVE25519_KEYSIZE;
  20989. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  20990. }
  20991. if (ret == 0) {
  20992. prvkSz = CURVE25519_KEYSIZE;
  20993. /* pubkSz = CURVE25519_KEYSIZE; */
  20994. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20995. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  20996. }
  20997. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  20998. pbksz != CURVE25519_KEYSIZE ||
  20999. prvkSz != CURVE25519_KEYSIZE ||
  21000. pubkSz != CURVE25519_KEYSIZE)) {
  21001. ret = -1;
  21002. }
  21003. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  21004. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  21005. ret = -1;
  21006. }
  21007. if (ret == 0) {
  21008. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  21009. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  21010. }
  21011. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  21012. pubkSz != CURVE25519_KEYSIZE)) {
  21013. ret = -1;
  21014. }
  21015. /*
  21016. try once with another endian
  21017. */
  21018. if (ret == 0) {
  21019. prvkSz = CURVE25519_KEYSIZE;
  21020. pubkSz = CURVE25519_KEYSIZE;
  21021. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  21022. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  21023. }
  21024. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  21025. pubkSz != CURVE25519_KEYSIZE)) {
  21026. ret = -1;
  21027. }
  21028. wc_curve25519_free(&key);
  21029. wc_FreeRng(&rng);
  21030. res = TEST_RES_CHECK(ret == 0);
  21031. #endif
  21032. return res;
  21033. } /* end of test_wc_curve25519_export_key_raw_ex */
  21034. /*
  21035. * Testing wc_curve25519_make_key
  21036. */
  21037. static int test_wc_curve25519_make_key(void)
  21038. {
  21039. int res = TEST_SKIPPED;
  21040. #if defined(HAVE_CURVE25519)
  21041. WC_RNG rng;
  21042. curve25519_key key;
  21043. int keysize;
  21044. int ret;
  21045. ret = wc_curve25519_init(&key);
  21046. if (ret == 0) {
  21047. ret = wc_InitRng(&rng);
  21048. }
  21049. if (ret == 0) {
  21050. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  21051. if (ret == 0) {
  21052. keysize = wc_curve25519_size(&key);
  21053. if (keysize != CURVE25519_KEYSIZE) {
  21054. ret = WOLFSSL_FATAL_ERROR;
  21055. }
  21056. }
  21057. if (ret == 0) {
  21058. ret = wc_curve25519_make_key(&rng, keysize, &key);
  21059. }
  21060. }
  21061. /*test bad cases*/
  21062. if (ret == 0) {
  21063. ret = wc_curve25519_make_key(NULL, 0, NULL);
  21064. if (ret == BAD_FUNC_ARG) {
  21065. ret = 0;
  21066. }
  21067. }
  21068. if (ret == 0) {
  21069. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  21070. if (ret == BAD_FUNC_ARG) {
  21071. ret = 0;
  21072. }
  21073. }
  21074. if (ret == 0) {
  21075. ret = wc_curve25519_make_key(NULL, keysize, &key);
  21076. if (ret == BAD_FUNC_ARG) {
  21077. ret = 0;
  21078. }
  21079. }
  21080. if (ret == 0) {
  21081. ret = wc_curve25519_make_key(&rng, 0, &key);
  21082. if (ret == ECC_BAD_ARG_E) {
  21083. ret = 0;
  21084. }
  21085. }
  21086. wc_curve25519_free(&key);
  21087. wc_FreeRng(&rng);
  21088. res = TEST_RES_CHECK(ret == 0);
  21089. #endif
  21090. return res;
  21091. } /*END test_wc_curve25519_make_key*/
  21092. /*
  21093. * Testing wc_curve25519_shared_secret_ex
  21094. */
  21095. static int test_wc_curve25519_shared_secret_ex(void)
  21096. {
  21097. int res = TEST_SKIPPED;
  21098. #if defined(HAVE_CURVE25519)
  21099. WC_RNG rng;
  21100. curve25519_key private_key, public_key;
  21101. byte out[CURVE25519_KEYSIZE];
  21102. word32 outLen = sizeof(out);
  21103. int endian = EC25519_BIG_ENDIAN;
  21104. int ret;
  21105. ret = wc_curve25519_init(&private_key);
  21106. if (ret == 0) {
  21107. ret = wc_curve25519_init(&public_key);
  21108. }
  21109. if (ret == 0) {
  21110. ret = wc_InitRng(&rng);
  21111. }
  21112. if (ret == 0) {
  21113. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  21114. }
  21115. if (ret == 0) {
  21116. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  21117. }
  21118. if (ret == 0) {
  21119. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21120. &outLen, endian);
  21121. }
  21122. /*test bad cases*/
  21123. if (ret == 0) {
  21124. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  21125. 0, endian);
  21126. if (ret == 0) {
  21127. ret = -1;
  21128. }
  21129. if (ret == BAD_FUNC_ARG) {
  21130. ret = 0;
  21131. }
  21132. }
  21133. if (ret == 0) {
  21134. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  21135. &outLen, endian);
  21136. if (ret == 0) {
  21137. ret = -1;
  21138. }
  21139. else if (ret == BAD_FUNC_ARG) {
  21140. ret = 0;
  21141. }
  21142. }
  21143. if (ret == 0) {
  21144. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  21145. &outLen, endian);
  21146. if (ret == 0) {
  21147. ret = -1;
  21148. }
  21149. else if (ret == BAD_FUNC_ARG) {
  21150. ret = 0;
  21151. }
  21152. }
  21153. if (ret == 0) {
  21154. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  21155. &outLen, endian);
  21156. if (ret == 0) {
  21157. ret = -1;
  21158. }
  21159. else if (ret == BAD_FUNC_ARG) {
  21160. ret = 0;
  21161. }
  21162. }
  21163. if (ret == 0) {
  21164. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21165. NULL, endian);
  21166. if (ret == 0) {
  21167. ret = -1;
  21168. }
  21169. else if (ret == BAD_FUNC_ARG) {
  21170. ret = 0;
  21171. }
  21172. }
  21173. if (ret == 0) {
  21174. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  21175. *increasing to 0x8f checks for error being returned*/
  21176. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  21177. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21178. &outLen, endian);
  21179. if (ret == 0) {
  21180. ret = -1;
  21181. }
  21182. else if (ret == ECC_BAD_ARG_E) {
  21183. ret = 0;
  21184. }
  21185. }
  21186. outLen = outLen - 2;
  21187. if (ret == 0) {
  21188. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  21189. &outLen, endian);
  21190. if (ret == 0) {
  21191. ret = -1;
  21192. }
  21193. else if (ret == BAD_FUNC_ARG) {
  21194. ret = 0;
  21195. }
  21196. }
  21197. wc_curve25519_free(&private_key);
  21198. wc_curve25519_free(&public_key);
  21199. wc_FreeRng(&rng);
  21200. res = TEST_RES_CHECK(ret == 0);
  21201. #endif
  21202. return res;
  21203. } /*END test_wc_curve25519_shared_secret_ex*/
  21204. /*
  21205. * Testing wc_curve25519_make_pub
  21206. */
  21207. static int test_wc_curve25519_make_pub(void)
  21208. {
  21209. int res = TEST_SKIPPED;
  21210. #ifdef HAVE_CURVE25519
  21211. WC_RNG rng;
  21212. curve25519_key key;
  21213. byte out[CURVE25519_KEYSIZE];
  21214. int ret;
  21215. ret = wc_curve25519_init(&key);
  21216. if (ret == 0) {
  21217. ret = wc_InitRng(&rng);
  21218. if (ret == 0) {
  21219. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  21220. }
  21221. }
  21222. if (ret == 0) {
  21223. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  21224. }
  21225. /*test bad cases*/
  21226. if (ret == 0) {
  21227. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  21228. if (ret == ECC_BAD_ARG_E) {
  21229. ret = 0;
  21230. }
  21231. }
  21232. if (ret == 0) {
  21233. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  21234. if (ret == ECC_BAD_ARG_E) {
  21235. ret = 0;
  21236. }
  21237. }
  21238. if (ret == 0) {
  21239. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  21240. if (ret == ECC_BAD_ARG_E) {
  21241. ret = 0;
  21242. }
  21243. }
  21244. if (ret == 0) {
  21245. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  21246. if (ret == ECC_BAD_ARG_E) {
  21247. ret = 0;
  21248. }
  21249. }
  21250. if (ret == 0) {
  21251. /* verify clamping test */
  21252. key.k[0] |= ~248;
  21253. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  21254. if (ret == ECC_BAD_ARG_E) {
  21255. ret = 0;
  21256. }
  21257. key.k[0] &= 248;
  21258. }
  21259. /* repeat the expected-to-succeed test. */
  21260. if (ret == 0) {
  21261. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  21262. }
  21263. wc_curve25519_free(&key);
  21264. wc_FreeRng(&rng);
  21265. res = TEST_RES_CHECK(ret == 0);
  21266. #endif
  21267. return res;
  21268. } /*END test_wc_curve25519_make_pub */
  21269. /*
  21270. * Testing test_wc_curve25519_export_public_ex
  21271. */
  21272. static int test_wc_curve25519_export_public_ex(void)
  21273. {
  21274. int res = TEST_SKIPPED;
  21275. #if defined(HAVE_CURVE25519)
  21276. WC_RNG rng;
  21277. curve25519_key key;
  21278. byte out[CURVE25519_KEYSIZE];
  21279. word32 outLen = sizeof(out);
  21280. int endian = EC25519_BIG_ENDIAN;
  21281. int ret;
  21282. ret = wc_curve25519_init(&key);
  21283. if (ret == 0) {
  21284. ret = wc_InitRng(&rng);
  21285. }
  21286. if (ret == 0) {
  21287. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  21288. if (ret == 0) {
  21289. ret = wc_curve25519_export_public(&key, out, &outLen);
  21290. }
  21291. if (ret == 0) {
  21292. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  21293. }
  21294. }
  21295. /*test bad cases*/
  21296. if (ret == 0) {
  21297. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  21298. if (ret == BAD_FUNC_ARG) {
  21299. ret = 0;
  21300. }
  21301. }
  21302. if (ret == 0) {
  21303. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  21304. if (ret == BAD_FUNC_ARG) {
  21305. ret = 0;
  21306. }
  21307. }
  21308. if (ret == 0) {
  21309. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  21310. if (ret == BAD_FUNC_ARG) {
  21311. ret = 0;
  21312. }
  21313. }
  21314. if (ret == 0) {
  21315. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  21316. if (ret == BAD_FUNC_ARG) {
  21317. ret = 0;
  21318. }
  21319. }
  21320. outLen = outLen - 2;
  21321. if (ret == 0) {
  21322. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  21323. if (ret == ECC_BAD_ARG_E) {
  21324. ret = 0;
  21325. }
  21326. }
  21327. wc_curve25519_free(&key);
  21328. wc_FreeRng(&rng);
  21329. res = TEST_RES_CHECK(ret == 0);
  21330. #endif
  21331. return res;
  21332. } /*END test_wc_curve25519_export_public_ex*/
  21333. /*
  21334. * Testing test_wc_curve25519_import_private_raw_ex
  21335. */
  21336. static int test_wc_curve25519_import_private_raw_ex(void)
  21337. {
  21338. int res = TEST_SKIPPED;
  21339. #if defined(HAVE_CURVE25519)
  21340. WC_RNG rng;
  21341. curve25519_key key;
  21342. byte priv[CURVE25519_KEYSIZE];
  21343. byte pub[CURVE25519_KEYSIZE];
  21344. word32 privSz = sizeof(priv);
  21345. word32 pubSz = sizeof(pub);
  21346. int endian = EC25519_BIG_ENDIAN;
  21347. int ret;
  21348. ret = wc_curve25519_init(&key);
  21349. if (ret == 0) {
  21350. ret = wc_InitRng(&rng);
  21351. }
  21352. if (ret == 0) {
  21353. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  21354. if (ret == 0) {
  21355. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  21356. }
  21357. if (ret == 0) {
  21358. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  21359. }
  21360. if (ret == 0) {
  21361. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21362. &key, endian);
  21363. }
  21364. }
  21365. /*test bad cases*/
  21366. if (ret == 0) {
  21367. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  21368. endian);
  21369. if (ret == BAD_FUNC_ARG) {
  21370. ret = 0;
  21371. }
  21372. }
  21373. if (ret == 0) {
  21374. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21375. &key, endian);
  21376. if (ret == BAD_FUNC_ARG) {
  21377. ret = 0;
  21378. }
  21379. }
  21380. if (ret == 0) {
  21381. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21382. &key, endian);
  21383. if (ret == BAD_FUNC_ARG) {
  21384. ret = 0;
  21385. }
  21386. }
  21387. if (ret == 0) {
  21388. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21389. NULL, endian);
  21390. if (ret == BAD_FUNC_ARG) {
  21391. ret = 0;
  21392. }
  21393. }
  21394. if (ret == 0) {
  21395. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  21396. &key, endian);
  21397. if (ret == ECC_BAD_ARG_E) {
  21398. ret = 0;
  21399. }
  21400. }
  21401. if (ret == 0) {
  21402. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  21403. &key, endian);
  21404. if (ret == ECC_BAD_ARG_E) {
  21405. ret = 0;
  21406. }
  21407. }
  21408. if (ret == 0) {
  21409. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  21410. &key, EC25519_LITTLE_ENDIAN);
  21411. }
  21412. wc_curve25519_free(&key);
  21413. wc_FreeRng(&rng);
  21414. res = TEST_RES_CHECK(ret == 0);
  21415. #endif
  21416. return res;
  21417. } /*END test_wc_curve25519_import_private_raw_ex*/
  21418. /*
  21419. * Testing test_wc_curve25519_import_private
  21420. */
  21421. static int test_wc_curve25519_import_private(void)
  21422. {
  21423. int res = TEST_SKIPPED;
  21424. #if defined(HAVE_CURVE25519)
  21425. curve25519_key key;
  21426. WC_RNG rng;
  21427. byte priv[CURVE25519_KEYSIZE];
  21428. word32 privSz = sizeof(priv);
  21429. int ret;
  21430. ret = wc_curve25519_init(&key);
  21431. if (ret == 0) {
  21432. ret = wc_InitRng(&rng);
  21433. }
  21434. if (ret == 0) {
  21435. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  21436. if (ret == 0) {
  21437. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  21438. }
  21439. }
  21440. if (ret == 0) {
  21441. ret = wc_curve25519_import_private(priv, privSz, &key);
  21442. }
  21443. wc_curve25519_free(&key);
  21444. wc_FreeRng(&rng);
  21445. res = TEST_RES_CHECK(ret == 0);
  21446. #endif
  21447. return res;
  21448. } /*END test_wc_curve25519_import*/
  21449. /*
  21450. * Testing test_wc_curve25519_export_private_raw_ex
  21451. */
  21452. static int test_wc_curve25519_export_private_raw_ex(void)
  21453. {
  21454. int res = TEST_SKIPPED;
  21455. #if defined(HAVE_CURVE25519)
  21456. curve25519_key key;
  21457. byte out[CURVE25519_KEYSIZE];
  21458. word32 outLen = sizeof(out);
  21459. int endian = EC25519_BIG_ENDIAN;
  21460. int ret;
  21461. ret = wc_curve25519_init(&key);
  21462. if (ret == 0) {
  21463. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  21464. }
  21465. /*test bad cases*/
  21466. if (ret == 0) {
  21467. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  21468. if (ret == BAD_FUNC_ARG) {
  21469. ret = 0;
  21470. }
  21471. }
  21472. if (ret == 0) {
  21473. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  21474. if (ret == BAD_FUNC_ARG) {
  21475. ret = 0;
  21476. }
  21477. }
  21478. if (ret == 0) {
  21479. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  21480. if (ret == BAD_FUNC_ARG) {
  21481. ret = 0;
  21482. }
  21483. }
  21484. if (ret == 0) {
  21485. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  21486. if (ret == BAD_FUNC_ARG) {
  21487. ret = 0;
  21488. }
  21489. }
  21490. if (ret == 0) {
  21491. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  21492. EC25519_LITTLE_ENDIAN);
  21493. }
  21494. outLen = outLen - 2;
  21495. if (ret == 0) {
  21496. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  21497. if (ret == ECC_BAD_ARG_E) {
  21498. ret = 0;
  21499. }
  21500. }
  21501. wc_curve25519_free(&key);
  21502. res = TEST_RES_CHECK(ret == 0);
  21503. #endif
  21504. return res;
  21505. }/*END test_wc_curve25519_export_private_raw_ex*/
  21506. /*
  21507. * Testing wc_ed448_make_key().
  21508. */
  21509. static int test_wc_ed448_make_key(void)
  21510. {
  21511. int res = TEST_SKIPPED;
  21512. #if defined(HAVE_ED448)
  21513. ed448_key key;
  21514. WC_RNG rng;
  21515. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  21516. int ret;
  21517. ret = wc_InitRng(&rng);
  21518. if (ret == 0) {
  21519. ret = wc_ed448_init(&key);
  21520. }
  21521. if (ret == 0) {
  21522. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  21523. if (ret == ECC_PRIV_KEY_E) {
  21524. ret = 0;
  21525. }
  21526. else if (ret == 0) {
  21527. ret = -1;
  21528. }
  21529. }
  21530. if (ret == 0) {
  21531. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21532. }
  21533. /* Test bad args. */
  21534. if (ret == 0) {
  21535. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  21536. if (ret == BAD_FUNC_ARG) {
  21537. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  21538. }
  21539. if (ret == BAD_FUNC_ARG) {
  21540. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  21541. }
  21542. if (ret == BAD_FUNC_ARG) {
  21543. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  21544. }
  21545. if (ret == BAD_FUNC_ARG) {
  21546. ret = 0;
  21547. }
  21548. else if (ret == 0) {
  21549. ret = WOLFSSL_FATAL_ERROR;
  21550. }
  21551. }
  21552. if (wc_FreeRng(&rng) && ret == 0) {
  21553. ret = WOLFSSL_FATAL_ERROR;
  21554. }
  21555. wc_ed448_free(&key);
  21556. res = TEST_RES_CHECK(ret == 0);
  21557. #endif
  21558. return res;
  21559. } /* END test_wc_ed448_make_key */
  21560. /*
  21561. * Testing wc_ed448_init()
  21562. */
  21563. static int test_wc_ed448_init(void)
  21564. {
  21565. int res = TEST_SKIPPED;
  21566. #if defined(HAVE_ED448)
  21567. ed448_key key;
  21568. int ret;
  21569. ret = wc_ed448_init(&key);
  21570. /* Test bad args. */
  21571. if (ret == 0) {
  21572. ret = wc_ed448_init(NULL);
  21573. if (ret == BAD_FUNC_ARG) {
  21574. ret = 0;
  21575. }
  21576. else if (ret == 0) {
  21577. ret = WOLFSSL_FATAL_ERROR;
  21578. }
  21579. }
  21580. wc_ed448_free(&key);
  21581. res = TEST_RES_CHECK(ret == 0);
  21582. #endif
  21583. return res;
  21584. } /* END test_wc_ed448_init */
  21585. /*
  21586. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  21587. */
  21588. static int test_wc_ed448_sign_msg(void)
  21589. {
  21590. int res = TEST_SKIPPED;
  21591. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  21592. WC_RNG rng;
  21593. ed448_key key;
  21594. byte msg[] = "Everybody gets Friday off.\n";
  21595. byte sig[ED448_SIG_SIZE];
  21596. word32 msglen = sizeof(msg);
  21597. word32 siglen = sizeof(sig);
  21598. word32 badSigLen = sizeof(sig) - 1;
  21599. #ifdef HAVE_ED448_VERIFY
  21600. int verify_ok = 0; /*1 = Verify success.*/
  21601. #endif
  21602. int ret;
  21603. /* Initialize stack variables. */
  21604. XMEMSET(sig, 0, siglen);
  21605. /* Initialize key. */
  21606. ret = wc_InitRng(&rng);
  21607. if (ret == 0) {
  21608. ret = wc_ed448_init(&key);
  21609. if (ret == 0) {
  21610. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21611. }
  21612. }
  21613. if (ret == 0) {
  21614. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  21615. }
  21616. /* Test bad args. */
  21617. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  21618. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  21619. if (ret == BAD_FUNC_ARG) {
  21620. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  21621. }
  21622. if (ret == BAD_FUNC_ARG) {
  21623. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  21624. }
  21625. if (ret == BAD_FUNC_ARG) {
  21626. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  21627. }
  21628. if (ret == BAD_FUNC_ARG) {
  21629. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  21630. NULL, 0);
  21631. }
  21632. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  21633. badSigLen -= 1;
  21634. ret = 0;
  21635. }
  21636. else if (ret == 0) {
  21637. ret = WOLFSSL_FATAL_ERROR;
  21638. }
  21639. } /* END sign */
  21640. #ifdef HAVE_ED448_VERIFY
  21641. if (ret == 0) {
  21642. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  21643. &key, NULL, 0);
  21644. if (ret == 0 && verify_ok == 1) {
  21645. ret = 0;
  21646. }
  21647. else if (ret == 0) {
  21648. ret = WOLFSSL_FATAL_ERROR;
  21649. }
  21650. /* Test bad args. */
  21651. if (ret == 0) {
  21652. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  21653. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21654. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  21655. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21656. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  21657. &key, NULL, 0);
  21658. if (ret == BAD_FUNC_ARG) {
  21659. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  21660. &verify_ok, &key, NULL, 0);
  21661. }
  21662. if (ret == BAD_FUNC_ARG) {
  21663. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21664. NULL, &key, NULL, 0);
  21665. }
  21666. if (ret == BAD_FUNC_ARG) {
  21667. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21668. &verify_ok, NULL, NULL, 0);
  21669. }
  21670. if (ret == BAD_FUNC_ARG) {
  21671. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  21672. &verify_ok, &key, NULL, 0);
  21673. }
  21674. if (ret == BAD_FUNC_ARG) {
  21675. ret = 0;
  21676. }
  21677. else if (ret == 0) {
  21678. ret = WOLFSSL_FATAL_ERROR;
  21679. }
  21680. }
  21681. } /* END verify. */
  21682. #endif /* Verify. */
  21683. if (wc_FreeRng(&rng) && ret == 0) {
  21684. ret = WOLFSSL_FATAL_ERROR;
  21685. }
  21686. wc_ed448_free(&key);
  21687. res = TEST_RES_CHECK(ret == 0);
  21688. #endif
  21689. return res;
  21690. } /* END test_wc_ed448_sign_msg */
  21691. /*
  21692. * Testing wc_ed448_import_public()
  21693. */
  21694. static int test_wc_ed448_import_public(void)
  21695. {
  21696. int res = TEST_SKIPPED;
  21697. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21698. WC_RNG rng;
  21699. ed448_key pubKey;
  21700. const byte in[] =
  21701. "Ed448PublicKeyUnitTest.................................\n";
  21702. word32 inlen = sizeof(in);
  21703. int ret = 0;
  21704. ret = wc_InitRng(&rng);
  21705. if (ret == 0) {
  21706. ret = wc_ed448_init(&pubKey);
  21707. if (ret == 0) {
  21708. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  21709. }
  21710. }
  21711. if (ret == 0) {
  21712. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  21713. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  21714. ret = 0;
  21715. }
  21716. else {
  21717. ret = WOLFSSL_FATAL_ERROR;
  21718. }
  21719. /* Test bad args. */
  21720. if (ret == 0) {
  21721. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  21722. if (ret == BAD_FUNC_ARG) {
  21723. ret = wc_ed448_import_public(in, inlen, NULL);
  21724. }
  21725. if (ret == BAD_FUNC_ARG) {
  21726. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  21727. }
  21728. if (ret == BAD_FUNC_ARG) {
  21729. ret = 0;
  21730. }
  21731. else if (ret == 0) {
  21732. ret = WOLFSSL_FATAL_ERROR;
  21733. }
  21734. }
  21735. }
  21736. if (wc_FreeRng(&rng) && ret == 0) {
  21737. ret = WOLFSSL_FATAL_ERROR;
  21738. }
  21739. wc_ed448_free(&pubKey);
  21740. res = TEST_RES_CHECK(ret == 0);
  21741. #endif
  21742. return res;
  21743. } /* END wc_ed448_import_public */
  21744. /*
  21745. * Testing wc_ed448_import_private_key()
  21746. */
  21747. static int test_wc_ed448_import_private_key(void)
  21748. {
  21749. int res = TEST_SKIPPED;
  21750. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21751. WC_RNG rng;
  21752. ed448_key key;
  21753. const byte privKey[] =
  21754. "Ed448PrivateKeyUnitTest................................\n";
  21755. const byte pubKey[] =
  21756. "Ed448PublicKeyUnitTest.................................\n";
  21757. word32 privKeySz = sizeof(privKey);
  21758. word32 pubKeySz = sizeof(pubKey);
  21759. #ifdef HAVE_ED448_KEY_EXPORT
  21760. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21761. word32 bothKeysSz = sizeof(bothKeys);
  21762. #endif
  21763. int ret;
  21764. ret = wc_InitRng(&rng);
  21765. if (ret != 0) {
  21766. return TEST_FAIL;
  21767. }
  21768. ret = wc_ed448_init(&key);
  21769. if (ret != 0) {
  21770. wc_FreeRng(&rng);
  21771. return TEST_FAIL;
  21772. }
  21773. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21774. if (ret == 0) {
  21775. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21776. pubKeySz, &key, 1);
  21777. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21778. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21779. ret = WOLFSSL_FATAL_ERROR;
  21780. }
  21781. }
  21782. #ifdef HAVE_ED448_KEY_EXPORT
  21783. if (ret == 0)
  21784. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  21785. if (ret == 0) {
  21786. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21787. &key, 1);
  21788. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21789. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21790. ret = WOLFSSL_FATAL_ERROR;
  21791. }
  21792. }
  21793. #endif
  21794. /* Test bad args. */
  21795. if (ret == 0) {
  21796. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21797. &key);
  21798. if (ret == BAD_FUNC_ARG) {
  21799. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21800. pubKeySz, &key);
  21801. }
  21802. if (ret == BAD_FUNC_ARG) {
  21803. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21804. pubKeySz, NULL);
  21805. }
  21806. if (ret == BAD_FUNC_ARG) {
  21807. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21808. pubKeySz, &key);
  21809. }
  21810. if (ret == BAD_FUNC_ARG) {
  21811. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21812. pubKeySz - 1, &key);
  21813. }
  21814. if (ret == BAD_FUNC_ARG) {
  21815. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21816. 0, &key);
  21817. }
  21818. if (ret == BAD_FUNC_ARG) {
  21819. ret = 0;
  21820. }
  21821. else if (ret == 0) {
  21822. ret = WOLFSSL_FATAL_ERROR;
  21823. }
  21824. }
  21825. if (wc_FreeRng(&rng) && ret == 0) {
  21826. ret = WOLFSSL_FATAL_ERROR;
  21827. }
  21828. wc_ed448_free(&key);
  21829. res = TEST_RES_CHECK(ret == 0);
  21830. #endif
  21831. return res;
  21832. } /* END test_wc_ed448_import_private_key */
  21833. /*
  21834. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21835. */
  21836. static int test_wc_ed448_export(void)
  21837. {
  21838. int res = TEST_SKIPPED;
  21839. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21840. WC_RNG rng;
  21841. ed448_key key;
  21842. byte priv[ED448_PRV_KEY_SIZE];
  21843. byte pub[ED448_PUB_KEY_SIZE];
  21844. word32 privSz = sizeof(priv);
  21845. word32 pubSz = sizeof(pub);
  21846. int ret;
  21847. ret = wc_InitRng(&rng);
  21848. if (ret != 0) {
  21849. return TEST_FAIL;
  21850. }
  21851. ret = wc_ed448_init(&key);
  21852. if (ret != 0) {
  21853. wc_FreeRng(&rng);
  21854. return TEST_FAIL;
  21855. }
  21856. if (ret == 0) {
  21857. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21858. }
  21859. if (ret == 0) {
  21860. ret = wc_ed448_export_public(&key, pub, &pubSz);
  21861. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  21862. XMEMCMP(key.p, pub, pubSz) != 0)) {
  21863. ret = WOLFSSL_FATAL_ERROR;
  21864. }
  21865. if (ret == 0) {
  21866. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  21867. if (ret == BAD_FUNC_ARG) {
  21868. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  21869. }
  21870. if (ret == BAD_FUNC_ARG) {
  21871. ret = wc_ed448_export_public(&key, pub, NULL);
  21872. }
  21873. if (ret == BAD_FUNC_ARG) {
  21874. ret = 0;
  21875. }
  21876. else if (ret == 0) {
  21877. ret = WOLFSSL_FATAL_ERROR;
  21878. }
  21879. }
  21880. }
  21881. if (ret == 0) {
  21882. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  21883. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  21884. XMEMCMP(key.k, priv, privSz) != 0)) {
  21885. ret = WOLFSSL_FATAL_ERROR;
  21886. }
  21887. if (ret == 0) {
  21888. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  21889. if (ret == BAD_FUNC_ARG) {
  21890. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  21891. }
  21892. if (ret == BAD_FUNC_ARG) {
  21893. ret = wc_ed448_export_private_only(&key, priv, NULL);
  21894. }
  21895. if (ret == BAD_FUNC_ARG) {
  21896. ret = 0;
  21897. }
  21898. else if (ret == 0) {
  21899. ret = WOLFSSL_FATAL_ERROR;
  21900. }
  21901. }
  21902. }
  21903. if (wc_FreeRng(&rng) && ret == 0) {
  21904. ret = WOLFSSL_FATAL_ERROR;
  21905. }
  21906. wc_ed448_free(&key);
  21907. res = TEST_RES_CHECK(ret == 0);
  21908. #endif
  21909. return res;
  21910. } /* END test_wc_ed448_export */
  21911. /*
  21912. * Testing wc_ed448_size()
  21913. */
  21914. static int test_wc_ed448_size(void)
  21915. {
  21916. int res = TEST_SKIPPED;
  21917. #if defined(HAVE_ED448)
  21918. WC_RNG rng;
  21919. ed448_key key;
  21920. int ret = 0;
  21921. ret = wc_InitRng(&rng);
  21922. if (ret != 0) {
  21923. return TEST_FAIL;
  21924. }
  21925. ret = wc_ed448_init(&key);
  21926. if (ret != 0) {
  21927. wc_FreeRng(&rng);
  21928. return TEST_FAIL;
  21929. }
  21930. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21931. if (ret != 0) {
  21932. wc_FreeRng(&rng);
  21933. wc_ed448_free(&key);
  21934. return TEST_FAIL;
  21935. }
  21936. ret = wc_ed448_size(&key);
  21937. /* Test bad args. */
  21938. if (ret == ED448_KEY_SIZE) {
  21939. ret = wc_ed448_size(NULL);
  21940. if (ret == BAD_FUNC_ARG) {
  21941. ret = 0;
  21942. }
  21943. }
  21944. if (ret == 0) {
  21945. ret = wc_ed448_sig_size(&key);
  21946. if (ret == ED448_SIG_SIZE) {
  21947. ret = 0;
  21948. }
  21949. /* Test bad args. */
  21950. if (ret == 0) {
  21951. ret = wc_ed448_sig_size(NULL);
  21952. if (ret == BAD_FUNC_ARG) {
  21953. ret = 0;
  21954. }
  21955. }
  21956. } /* END wc_ed448_sig_size() */
  21957. if (ret == 0) {
  21958. ret = wc_ed448_pub_size(&key);
  21959. if (ret == ED448_PUB_KEY_SIZE) {
  21960. ret = 0;
  21961. }
  21962. if (ret == 0) {
  21963. ret = wc_ed448_pub_size(NULL);
  21964. if (ret == BAD_FUNC_ARG) {
  21965. ret = 0;
  21966. }
  21967. }
  21968. } /* END wc_ed448_pub_size */
  21969. if (ret == 0) {
  21970. ret = wc_ed448_priv_size(&key);
  21971. if (ret == ED448_PRV_KEY_SIZE) {
  21972. ret = 0;
  21973. }
  21974. if (ret == 0) {
  21975. ret = wc_ed448_priv_size(NULL);
  21976. if (ret == BAD_FUNC_ARG) {
  21977. ret = 0;
  21978. }
  21979. }
  21980. } /* END wc_ed448_pub_size */
  21981. if (wc_FreeRng(&rng) && ret == 0) {
  21982. ret = WOLFSSL_FATAL_ERROR;
  21983. }
  21984. wc_ed448_free(&key);
  21985. res = TEST_RES_CHECK(ret == 0);
  21986. #endif
  21987. return res;
  21988. } /* END test_wc_ed448_size */
  21989. /*
  21990. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21991. */
  21992. static int test_wc_ed448_exportKey(void)
  21993. {
  21994. int res = TEST_SKIPPED;
  21995. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21996. WC_RNG rng;
  21997. ed448_key key;
  21998. byte priv[ED448_PRV_KEY_SIZE];
  21999. byte pub[ED448_PUB_KEY_SIZE];
  22000. byte privOnly[ED448_PRV_KEY_SIZE];
  22001. word32 privSz = sizeof(priv);
  22002. word32 pubSz = sizeof(pub);
  22003. word32 privOnlySz = sizeof(privOnly);
  22004. int ret;
  22005. ret = wc_InitRng(&rng);
  22006. if (ret != 0) {
  22007. return TEST_FAIL;
  22008. }
  22009. ret = wc_ed448_init(&key);
  22010. if (ret != 0) {
  22011. wc_FreeRng(&rng);
  22012. return TEST_FAIL;
  22013. }
  22014. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  22015. if (ret != 0) {
  22016. wc_FreeRng(&rng);
  22017. wc_ed448_free(&key);
  22018. return TEST_FAIL;
  22019. }
  22020. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  22021. if (ret == 0) {
  22022. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  22023. if (ret == BAD_FUNC_ARG) {
  22024. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  22025. }
  22026. if (ret == BAD_FUNC_ARG) {
  22027. ret = wc_ed448_export_private(&key, privOnly, NULL);
  22028. }
  22029. if (ret == BAD_FUNC_ARG) {
  22030. ret = 0;
  22031. }
  22032. else if (ret == 0) {
  22033. ret = WOLFSSL_FATAL_ERROR;
  22034. }
  22035. }
  22036. if (ret == 0) {
  22037. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  22038. if (ret == 0) {
  22039. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  22040. if (ret == BAD_FUNC_ARG) {
  22041. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  22042. }
  22043. if (ret == BAD_FUNC_ARG) {
  22044. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  22045. }
  22046. if (ret == BAD_FUNC_ARG) {
  22047. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  22048. }
  22049. if (ret == BAD_FUNC_ARG) {
  22050. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  22051. }
  22052. if (ret == BAD_FUNC_ARG) {
  22053. ret = 0;
  22054. }
  22055. else if (ret == 0) {
  22056. ret = WOLFSSL_FATAL_ERROR;
  22057. }
  22058. }
  22059. } /* END wc_ed448_export_key() */
  22060. /* Cross check output. */
  22061. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  22062. ret = WOLFSSL_FATAL_ERROR;
  22063. }
  22064. if (wc_FreeRng(&rng) && ret == 0) {
  22065. ret = WOLFSSL_FATAL_ERROR;
  22066. }
  22067. wc_ed448_free(&key);
  22068. res = TEST_RES_CHECK(ret == 0);
  22069. #endif
  22070. return res;
  22071. } /* END test_wc_ed448_exportKey */
  22072. /*
  22073. * Testing wc_Ed448PublicKeyToDer
  22074. */
  22075. static int test_wc_Ed448PublicKeyToDer(void)
  22076. {
  22077. int res = TEST_SKIPPED;
  22078. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22079. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22080. int tmp;
  22081. ed448_key key;
  22082. byte derBuf[1024];
  22083. int ret = 0;
  22084. /* Test bad args */
  22085. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  22086. if (tmp != BAD_FUNC_ARG) {
  22087. ret = WOLFSSL_FATAL_ERROR;
  22088. }
  22089. if (ret == 0) {
  22090. wc_ed448_init(&key);
  22091. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  22092. if (tmp != BUFFER_E) {
  22093. ret = WOLFSSL_FATAL_ERROR;
  22094. }
  22095. wc_ed448_free(&key);
  22096. }
  22097. /* Test good args */
  22098. if (ret == 0) {
  22099. WC_RNG rng;
  22100. ret = wc_InitRng(&rng);
  22101. if (ret != 0) {
  22102. return TEST_FAIL;
  22103. }
  22104. ret = wc_ed448_init(&key);
  22105. if (ret != 0) {
  22106. wc_FreeRng(&rng);
  22107. return TEST_FAIL;
  22108. }
  22109. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  22110. if (ret != 0) {
  22111. wc_FreeRng(&rng);
  22112. wc_ed448_free(&key);
  22113. return TEST_FAIL;
  22114. }
  22115. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  22116. if (tmp <= 0) {
  22117. ret = WOLFSSL_FATAL_ERROR;
  22118. }
  22119. wc_FreeRng(&rng);
  22120. wc_ed448_free(&key);
  22121. }
  22122. res = TEST_RES_CHECK(ret == 0);
  22123. #endif
  22124. return res;
  22125. } /* END testing wc_Ed448PublicKeyToDer */
  22126. /*
  22127. * Testing wc_curve448_init and wc_curve448_free.
  22128. */
  22129. static int test_wc_curve448_init(void)
  22130. {
  22131. int res = TEST_SKIPPED;
  22132. #if defined(HAVE_CURVE448)
  22133. curve448_key key;
  22134. int ret = 0;
  22135. ret = wc_curve448_init(&key);
  22136. /* Test bad args for wc_curve448_init */
  22137. if (ret == 0) {
  22138. ret = wc_curve448_init(NULL);
  22139. if (ret == BAD_FUNC_ARG) {
  22140. ret = 0;
  22141. }
  22142. else if (ret == 0) {
  22143. ret = WOLFSSL_FATAL_ERROR;
  22144. }
  22145. }
  22146. /* Test good args for wc_curve_448_free */
  22147. wc_curve448_free(&key);
  22148. wc_curve448_free(NULL);
  22149. res = TEST_RES_CHECK(ret == 0);
  22150. #endif
  22151. return res;
  22152. } /* END test_wc_curve448_init and wc_curve_448_free*/
  22153. /*
  22154. * Testing wc_curve448_make_key
  22155. */
  22156. static int test_wc_curve448_make_key(void)
  22157. {
  22158. int res = TEST_SKIPPED;
  22159. #if defined(HAVE_CURVE448)
  22160. WC_RNG rng;
  22161. curve448_key key;
  22162. int keysize;
  22163. int ret;
  22164. ret = wc_curve448_init(&key);
  22165. if (ret == 0) {
  22166. ret = wc_InitRng(&rng);
  22167. }
  22168. if (ret == 0) {
  22169. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22170. if (ret == 0) {
  22171. keysize = wc_curve448_size(&key);
  22172. if (keysize != CURVE448_KEY_SIZE) {
  22173. ret = WOLFSSL_FATAL_ERROR;
  22174. }
  22175. }
  22176. if (ret == 0) {
  22177. ret = wc_curve448_make_key(&rng, keysize, &key);
  22178. }
  22179. }
  22180. /* test bad cases */
  22181. if (ret == 0) {
  22182. ret = wc_curve448_make_key(NULL, 0, NULL);
  22183. if (ret == BAD_FUNC_ARG) {
  22184. ret = 0;
  22185. }
  22186. }
  22187. if (ret == 0) {
  22188. ret = wc_curve448_make_key(&rng, keysize, NULL);
  22189. if (ret == BAD_FUNC_ARG) {
  22190. ret = 0;
  22191. }
  22192. }
  22193. if (ret == 0) {
  22194. ret = wc_curve448_make_key(NULL, keysize, &key);
  22195. if (ret == BAD_FUNC_ARG) {
  22196. ret = 0;
  22197. }
  22198. }
  22199. if (ret == 0) {
  22200. ret = wc_curve448_make_key(&rng, 0, &key);
  22201. if (ret == ECC_BAD_ARG_E) {
  22202. ret = 0;
  22203. }
  22204. }
  22205. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  22206. ret = WOLFSSL_FATAL_ERROR;
  22207. }
  22208. wc_curve448_free(&key);
  22209. res = TEST_RES_CHECK(ret == 0);
  22210. #endif
  22211. return res;
  22212. } /*END test_wc_curve448_make_key*/
  22213. /*
  22214. * Testing test_wc_curve448_shared_secret_ex
  22215. */
  22216. static int test_wc_curve448_shared_secret_ex(void)
  22217. {
  22218. int res = TEST_SKIPPED;
  22219. #if defined(HAVE_CURVE448)
  22220. WC_RNG rng;
  22221. curve448_key private_key, public_key;
  22222. byte out[CURVE448_KEY_SIZE];
  22223. word32 outLen = sizeof(out);
  22224. int endian = EC448_BIG_ENDIAN;
  22225. int ret;
  22226. ret = wc_curve448_init(&private_key);
  22227. if (ret == 0) {
  22228. ret = wc_InitRng(&rng);
  22229. if (ret == 0) {
  22230. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  22231. }
  22232. }
  22233. if (ret == 0) {
  22234. ret = wc_curve448_init(&public_key);
  22235. }
  22236. if (ret == 0) {
  22237. if (ret == 0) {
  22238. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  22239. }
  22240. }
  22241. if (ret == 0) {
  22242. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  22243. &outLen, endian);
  22244. }
  22245. /* test bad cases */
  22246. if (ret == 0) {
  22247. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  22248. if (ret == BAD_FUNC_ARG) {
  22249. ret = 0;
  22250. }
  22251. }
  22252. if (ret == 0) {
  22253. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  22254. &outLen, endian);
  22255. if (ret == BAD_FUNC_ARG) {
  22256. ret = 0;
  22257. }
  22258. }
  22259. if (ret == 0) {
  22260. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  22261. &outLen, endian);
  22262. if (ret == BAD_FUNC_ARG) {
  22263. ret = 0;
  22264. }
  22265. }
  22266. if (ret == 0) {
  22267. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  22268. &outLen, endian);
  22269. if (ret == BAD_FUNC_ARG) {
  22270. ret = 0;
  22271. }
  22272. }
  22273. if (ret == 0) {
  22274. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  22275. NULL, endian);
  22276. if (ret == BAD_FUNC_ARG) {
  22277. ret = 0;
  22278. }
  22279. }
  22280. outLen = outLen - 2;
  22281. if (ret == 0) {
  22282. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  22283. &outLen, endian);
  22284. if (ret == BAD_FUNC_ARG) {
  22285. ret = 0;
  22286. }
  22287. }
  22288. wc_curve448_free(&private_key);
  22289. wc_curve448_free(&public_key);
  22290. wc_FreeRng(&rng);
  22291. res = TEST_RES_CHECK(ret == 0);
  22292. #endif
  22293. return res;
  22294. } /*END test_wc_curve448_shared_secret_ex*/
  22295. /*
  22296. * Testing test_wc_curve448_export_public_ex
  22297. */
  22298. static int test_wc_curve448_export_public_ex(void)
  22299. {
  22300. int res = TEST_SKIPPED;
  22301. #if defined(HAVE_CURVE448)
  22302. WC_RNG rng;
  22303. curve448_key key;
  22304. byte out[CURVE448_KEY_SIZE];
  22305. word32 outLen = sizeof(out);
  22306. int endian = EC448_BIG_ENDIAN;
  22307. int ret;
  22308. ret = wc_curve448_init(&key);
  22309. if (ret == 0) {
  22310. ret = wc_InitRng(&rng);
  22311. }
  22312. if (ret == 0) {
  22313. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22314. if (ret == 0) {
  22315. ret = wc_curve448_export_public(&key, out, &outLen);
  22316. }
  22317. if (ret == 0) {
  22318. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  22319. }
  22320. }
  22321. /*test bad cases*/
  22322. if (ret == 0) {
  22323. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  22324. if (ret == BAD_FUNC_ARG) {
  22325. ret = 0;
  22326. }
  22327. }
  22328. if (ret == 0) {
  22329. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  22330. if (ret == BAD_FUNC_ARG) {
  22331. ret = 0;
  22332. }
  22333. }
  22334. if (ret == 0) {
  22335. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  22336. if (ret == BAD_FUNC_ARG) {
  22337. ret = 0;
  22338. }
  22339. }
  22340. if (ret == 0) {
  22341. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  22342. if (ret == BAD_FUNC_ARG) {
  22343. ret = 0;
  22344. }
  22345. }
  22346. outLen = outLen - 2;
  22347. if (ret == 0) {
  22348. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  22349. if (ret == ECC_BAD_ARG_E) {
  22350. ret = 0;
  22351. }
  22352. }
  22353. wc_curve448_free(&key);
  22354. wc_FreeRng(&rng);
  22355. res = TEST_RES_CHECK(ret == 0);
  22356. #endif
  22357. return res;
  22358. } /*END test_wc_curve448_export_public_ex*/
  22359. /*
  22360. * Testing test_wc_curve448_export_private_raw_ex
  22361. */
  22362. static int test_wc_curve448_export_private_raw_ex(void)
  22363. {
  22364. int res = TEST_SKIPPED;
  22365. #if defined(HAVE_CURVE448)
  22366. curve448_key key;
  22367. byte out[CURVE448_KEY_SIZE];
  22368. word32 outLen = sizeof(out);
  22369. int endian = EC448_BIG_ENDIAN;
  22370. int ret;
  22371. ret = wc_curve448_init(&key);
  22372. if (ret == 0) {
  22373. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  22374. }
  22375. /*test bad cases*/
  22376. if (ret == 0) {
  22377. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  22378. if (ret == BAD_FUNC_ARG) {
  22379. ret = 0;
  22380. }
  22381. }
  22382. if (ret == 0) {
  22383. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  22384. if (ret == BAD_FUNC_ARG) {
  22385. ret = 0;
  22386. }
  22387. }
  22388. if (ret == 0) {
  22389. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  22390. if (ret == BAD_FUNC_ARG) {
  22391. ret = 0;
  22392. }
  22393. }
  22394. if (ret == 0) {
  22395. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  22396. if (ret == BAD_FUNC_ARG) {
  22397. ret = 0;
  22398. }
  22399. }
  22400. if (ret == 0) {
  22401. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  22402. EC448_LITTLE_ENDIAN);
  22403. }
  22404. outLen = outLen - 2;
  22405. if (ret == 0) {
  22406. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  22407. if (ret == ECC_BAD_ARG_E) {
  22408. ret = 0;
  22409. }
  22410. }
  22411. wc_curve448_free(&key);
  22412. res = TEST_RES_CHECK(ret == 0);
  22413. #endif
  22414. return res;
  22415. }/*END test_wc_curve448_export_private_raw_ex*/
  22416. /*
  22417. * Testing test_wc_curve448_import_private_raw_ex
  22418. */
  22419. static int test_wc_curve448_import_private_raw_ex(void)
  22420. {
  22421. int res = TEST_SKIPPED;
  22422. #if defined(HAVE_CURVE448)
  22423. WC_RNG rng;
  22424. curve448_key key;
  22425. byte priv[CURVE448_KEY_SIZE];
  22426. byte pub[CURVE448_KEY_SIZE];
  22427. word32 privSz = sizeof(priv);
  22428. word32 pubSz = sizeof(pub);
  22429. int endian = EC448_BIG_ENDIAN;
  22430. int ret;
  22431. ret = wc_curve448_init(&key);
  22432. if (ret == 0) {
  22433. ret = wc_InitRng(&rng);
  22434. }
  22435. if (ret == 0) {
  22436. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22437. if (ret == 0) {
  22438. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  22439. }
  22440. if (ret == 0) {
  22441. ret = wc_curve448_export_public(&key, pub, &pubSz);
  22442. }
  22443. if (ret == 0) {
  22444. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  22445. &key, endian);
  22446. }
  22447. }
  22448. /* test bad cases */
  22449. if (ret == 0) {
  22450. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  22451. if (ret == BAD_FUNC_ARG) {
  22452. ret = 0;
  22453. }
  22454. }
  22455. if (ret == 0) {
  22456. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  22457. &key, endian);
  22458. if (ret == BAD_FUNC_ARG) {
  22459. ret = 0;
  22460. }
  22461. }
  22462. if (ret == 0) {
  22463. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  22464. &key, endian);
  22465. if (ret == BAD_FUNC_ARG) {
  22466. ret = 0;
  22467. }
  22468. }
  22469. if (ret == 0) {
  22470. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  22471. NULL, endian);
  22472. if (ret == BAD_FUNC_ARG) {
  22473. ret = 0;
  22474. }
  22475. }
  22476. if (ret == 0) {
  22477. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  22478. &key, endian);
  22479. if (ret == ECC_BAD_ARG_E) {
  22480. ret = 0;
  22481. }
  22482. }
  22483. if (ret == 0) {
  22484. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  22485. &key, endian);
  22486. if (ret == ECC_BAD_ARG_E) {
  22487. ret = 0;
  22488. }
  22489. }
  22490. if (ret == 0) {
  22491. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  22492. &key, EC448_LITTLE_ENDIAN);
  22493. }
  22494. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  22495. ret = WOLFSSL_FATAL_ERROR;
  22496. }
  22497. wc_curve448_free(&key);
  22498. res = TEST_RES_CHECK(ret == 0);
  22499. #endif
  22500. return res;
  22501. } /*END test_wc_curve448_import_private_raw_ex*/
  22502. /*
  22503. * Testing test_curve448_export_key_raw
  22504. */
  22505. static int test_wc_curve448_export_key_raw(void)
  22506. {
  22507. int res = TEST_SKIPPED;
  22508. #if defined(HAVE_CURVE448)
  22509. WC_RNG rng;
  22510. curve448_key key;
  22511. byte priv[CURVE448_KEY_SIZE];
  22512. byte pub[CURVE448_KEY_SIZE];
  22513. word32 privSz = sizeof(priv);
  22514. word32 pubSz = sizeof(pub);
  22515. int ret;
  22516. ret = wc_curve448_init(&key);
  22517. if (ret == 0) {
  22518. ret = wc_InitRng(&rng);
  22519. }
  22520. if (ret == 0) {
  22521. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22522. if (ret == 0) {
  22523. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  22524. }
  22525. if (ret == 0) {
  22526. ret = wc_curve448_export_public(&key, pub, &pubSz);
  22527. }
  22528. if (ret == 0) {
  22529. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  22530. }
  22531. }
  22532. wc_curve448_free(&key);
  22533. wc_FreeRng(&rng);
  22534. res = TEST_RES_CHECK(ret == 0);
  22535. #endif
  22536. return res;
  22537. }/*END test_wc_curve448_import_private_raw_ex*/
  22538. /*
  22539. * Testing test_wc_curve448_import_private
  22540. */
  22541. static int test_wc_curve448_import_private(void)
  22542. {
  22543. int res = TEST_SKIPPED;
  22544. #if defined(HAVE_CURVE448)
  22545. curve448_key key;
  22546. WC_RNG rng;
  22547. byte priv[CURVE448_KEY_SIZE];
  22548. word32 privSz = sizeof(priv);
  22549. int ret;
  22550. ret = wc_curve448_init(&key);
  22551. if (ret == 0) {
  22552. ret = wc_InitRng(&rng);
  22553. }
  22554. if (ret == 0) {
  22555. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22556. if (ret == 0) {
  22557. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  22558. }
  22559. }
  22560. if (ret == 0) {
  22561. ret = wc_curve448_import_private(priv, privSz, &key);
  22562. }
  22563. wc_curve448_free(&key);
  22564. wc_FreeRng(&rng);
  22565. res = TEST_RES_CHECK(ret == 0);
  22566. #endif
  22567. return res;
  22568. } /*END test_wc_curve448_import*/
  22569. /*
  22570. * Testing test_wc_curve448_size.
  22571. */
  22572. static int test_wc_curve448_size(void)
  22573. {
  22574. int res = TEST_SKIPPED;
  22575. #if defined(HAVE_CURVE448)
  22576. curve448_key key;
  22577. int ret = 0;
  22578. ret = wc_curve448_init(&key);
  22579. /* Test good args for wc_curve448_size */
  22580. if (ret == 0) {
  22581. ret = wc_curve448_size(&key);
  22582. }
  22583. /* Test bad args for wc_curve448_size */
  22584. if (ret != 0) {
  22585. ret = wc_curve448_size(NULL);
  22586. }
  22587. wc_curve448_free(&key);
  22588. res = TEST_RES_CHECK(ret == 0);
  22589. #endif
  22590. return res;
  22591. } /* END test_wc_curve448_size*/
  22592. /*
  22593. * Testing wc_ecc_make_key.
  22594. */
  22595. static int test_wc_ecc_make_key(void)
  22596. {
  22597. int res = TEST_SKIPPED;
  22598. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22599. WC_RNG rng;
  22600. ecc_key key;
  22601. int ret;
  22602. ret = wc_InitRng(&rng);
  22603. if (ret != 0)
  22604. return TEST_FAIL;
  22605. ret = wc_ecc_init(&key);
  22606. if (ret == 0) {
  22607. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22608. #if defined(WOLFSSL_ASYNC_CRYPT)
  22609. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22610. #endif
  22611. /* Pass in bad args. */
  22612. if (ret == 0) {
  22613. ret = wc_ecc_make_key(NULL, KEY14, &key);
  22614. if (ret == BAD_FUNC_ARG) {
  22615. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  22616. }
  22617. if (ret == BAD_FUNC_ARG) {
  22618. ret = 0;
  22619. }
  22620. else if (ret == 0) {
  22621. ret = WOLFSSL_FATAL_ERROR;
  22622. }
  22623. }
  22624. wc_ecc_free(&key);
  22625. }
  22626. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  22627. ret = WOLFSSL_FATAL_ERROR;
  22628. }
  22629. #ifdef FP_ECC
  22630. wc_ecc_fp_free();
  22631. #endif
  22632. res = TEST_RES_CHECK(ret == 0);
  22633. #endif
  22634. return res;
  22635. } /* END test_wc_ecc_make_key */
  22636. /*
  22637. * Testing wc_ecc_init()
  22638. */
  22639. static int test_wc_ecc_init(void)
  22640. {
  22641. int res = TEST_SKIPPED;
  22642. #ifdef HAVE_ECC
  22643. ecc_key key;
  22644. int ret;
  22645. ret = wc_ecc_init(&key);
  22646. /* Pass in bad args. */
  22647. if (ret == 0) {
  22648. ret = wc_ecc_init(NULL);
  22649. if (ret == BAD_FUNC_ARG) {
  22650. ret = 0;
  22651. }
  22652. else if (ret == 0) {
  22653. ret = WOLFSSL_FATAL_ERROR;
  22654. }
  22655. }
  22656. wc_ecc_free(&key);
  22657. res = TEST_RES_CHECK(ret == 0);
  22658. #endif
  22659. return res;
  22660. } /* END test_wc_ecc_init */
  22661. /*
  22662. * Testing wc_ecc_check_key()
  22663. */
  22664. static int test_wc_ecc_check_key(void)
  22665. {
  22666. int res = TEST_SKIPPED;
  22667. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22668. WC_RNG rng;
  22669. ecc_key key;
  22670. int ret;
  22671. XMEMSET(&rng, 0, sizeof(rng));
  22672. XMEMSET(&key, 0, sizeof(key));
  22673. ret = wc_InitRng(&rng);
  22674. if (ret == 0) {
  22675. ret = wc_ecc_init(&key);
  22676. if (ret == 0) {
  22677. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22678. #if defined(WOLFSSL_ASYNC_CRYPT)
  22679. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22680. #endif
  22681. }
  22682. }
  22683. if (ret == 0) {
  22684. ret = wc_ecc_check_key(&key);
  22685. }
  22686. /* Pass in bad args. */
  22687. if (ret == 0) {
  22688. ret = wc_ecc_check_key(NULL);
  22689. if (ret == BAD_FUNC_ARG) {
  22690. ret = 0;
  22691. }
  22692. else if (ret == 0) {
  22693. ret = WOLFSSL_FATAL_ERROR;
  22694. }
  22695. }
  22696. if (wc_FreeRng(&rng) && ret == 0) {
  22697. ret = WOLFSSL_FATAL_ERROR;
  22698. }
  22699. wc_ecc_free(&key);
  22700. #ifdef FP_ECC
  22701. wc_ecc_fp_free();
  22702. #endif
  22703. res = TEST_RES_CHECK(ret == 0);
  22704. #endif
  22705. return res;
  22706. } /* END test_wc_ecc_check_key */
  22707. /*
  22708. * Testing wc_ecc_get_generator()
  22709. */
  22710. static int test_wc_ecc_get_generator(void)
  22711. {
  22712. int res = TEST_SKIPPED;
  22713. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  22714. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  22715. ecc_point* pt;
  22716. int ret = 0;
  22717. pt = wc_ecc_new_point();
  22718. if (!pt) {
  22719. ret = WOLFSSL_FATAL_ERROR;
  22720. }
  22721. if (ret == 0) {
  22722. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22723. }
  22724. /* Test bad args. */
  22725. if (ret == MP_OKAY) {
  22726. /* Returns Zero for bad arg. */
  22727. ret = wc_ecc_get_generator(pt, -1);
  22728. if (ret != MP_OKAY)
  22729. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22730. if (ret != MP_OKAY)
  22731. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  22732. * increase this number */
  22733. if (ret != MP_OKAY)
  22734. wc_ecc_get_generator(NULL, -1);
  22735. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  22736. }
  22737. wc_ecc_del_point(pt);
  22738. res = TEST_RES_CHECK(ret == 0);
  22739. #endif
  22740. return res;
  22741. } /* END test_wc_ecc_get_generator */
  22742. /*
  22743. * Testing wc_ecc_size()
  22744. */
  22745. static int test_wc_ecc_size(void)
  22746. {
  22747. int res = TEST_SKIPPED;
  22748. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22749. WC_RNG rng;
  22750. ecc_key key;
  22751. int ret;
  22752. XMEMSET(&rng, 0, sizeof(rng));
  22753. XMEMSET(&key, 0, sizeof(key));
  22754. ret = wc_InitRng(&rng);
  22755. if (ret == 0) {
  22756. ret = wc_ecc_init(&key);
  22757. if (ret == 0) {
  22758. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22759. #if defined(WOLFSSL_ASYNC_CRYPT)
  22760. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22761. #endif
  22762. }
  22763. }
  22764. if (ret == 0) {
  22765. ret = wc_ecc_size(&key);
  22766. if (ret == KEY14) {
  22767. ret = 0;
  22768. }
  22769. else if (ret == 0) {
  22770. ret = WOLFSSL_FATAL_ERROR;
  22771. }
  22772. }
  22773. /* Test bad args. */
  22774. if (ret == 0) {
  22775. /* Returns Zero for bad arg. */
  22776. ret = wc_ecc_size(NULL);
  22777. }
  22778. if (wc_FreeRng(&rng) && ret == 0) {
  22779. ret = WOLFSSL_FATAL_ERROR;
  22780. }
  22781. wc_ecc_free(&key);
  22782. res = TEST_RES_CHECK(ret == 0);
  22783. #endif
  22784. return res;
  22785. } /* END test_wc_ecc_size */
  22786. static int test_wc_ecc_params(void)
  22787. {
  22788. int res = TEST_SKIPPED;
  22789. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  22790. It was added after certifications */
  22791. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  22792. const ecc_set_type* ecc_set;
  22793. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  22794. /* Test for SECP256R1 curve */
  22795. int curve_id = ECC_SECP256R1;
  22796. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  22797. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  22798. ecc_set = wc_ecc_get_curve_params(curve_idx);
  22799. AssertNotNull(ecc_set);
  22800. AssertIntEQ(ecc_set->id, curve_id);
  22801. #endif
  22802. /* Test case when SECP256R1 is not enabled */
  22803. /* Test that we get curve params for index 0 */
  22804. ecc_set = wc_ecc_get_curve_params(0);
  22805. AssertNotNull(ecc_set);
  22806. res = TEST_RES_CHECK(1);
  22807. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  22808. return res;
  22809. }
  22810. /*
  22811. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  22812. */
  22813. static int test_wc_ecc_signVerify_hash(void)
  22814. {
  22815. int res = TEST_SKIPPED;
  22816. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  22817. WC_RNG rng;
  22818. ecc_key key;
  22819. int ret;
  22820. int signH = WOLFSSL_FATAL_ERROR;
  22821. #ifdef HAVE_ECC_VERIFY
  22822. int verifyH = WOLFSSL_FATAL_ERROR;
  22823. int verify = 0;
  22824. #endif
  22825. word32 siglen = ECC_BUFSIZE;
  22826. byte sig[ECC_BUFSIZE];
  22827. byte adjustedSig[ECC_BUFSIZE+1];
  22828. byte digest[] = TEST_STRING;
  22829. word32 digestlen = (word32)TEST_STRING_SZ;
  22830. /* Init stack var */
  22831. XMEMSET(sig, 0, siglen);
  22832. XMEMSET(&key, 0, sizeof(key));
  22833. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  22834. /* Init structs. */
  22835. ret = wc_InitRng(&rng);
  22836. if (ret == 0) {
  22837. ret = wc_ecc_init(&key);
  22838. if (ret == 0) {
  22839. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22840. #if defined(WOLFSSL_ASYNC_CRYPT)
  22841. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22842. #endif
  22843. }
  22844. }
  22845. if (ret == 0) {
  22846. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  22847. }
  22848. /* Check bad args. */
  22849. if (ret == 0) {
  22850. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  22851. if (signH == ECC_BAD_ARG_E) {
  22852. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  22853. &rng, &key);
  22854. }
  22855. if (signH == ECC_BAD_ARG_E) {
  22856. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  22857. &rng, &key);
  22858. }
  22859. if (signH == ECC_BAD_ARG_E) {
  22860. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22861. NULL, &key);
  22862. }
  22863. if (signH == ECC_BAD_ARG_E) {
  22864. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22865. &rng, NULL);
  22866. }
  22867. if (signH == ECC_BAD_ARG_E) {
  22868. signH = 0;
  22869. }
  22870. else {
  22871. signH = WOLFSSL_FATAL_ERROR;
  22872. }
  22873. }
  22874. #ifdef HAVE_ECC_VERIFY
  22875. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  22876. if (verify != 1 && ret == 0) {
  22877. ret = WOLFSSL_FATAL_ERROR;
  22878. }
  22879. /* test check on length of signature passed in */
  22880. XMEMCPY(adjustedSig, sig, siglen);
  22881. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  22882. #ifndef NO_STRICT_ECDSA_LEN
  22883. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22884. &verify, &key), 0);
  22885. #else
  22886. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  22887. * is allowed */
  22888. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22889. &verify, &key), 0);
  22890. #endif
  22891. /* Test bad args. */
  22892. if (ret == 0) {
  22893. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  22894. &verify, &key);
  22895. if (verifyH == ECC_BAD_ARG_E) {
  22896. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  22897. &verify, &key);
  22898. }
  22899. if (verifyH == ECC_BAD_ARG_E) {
  22900. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22901. NULL, &key);
  22902. }
  22903. if (verifyH == ECC_BAD_ARG_E) {
  22904. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22905. &verify, NULL);
  22906. }
  22907. if (verifyH == ECC_BAD_ARG_E) {
  22908. verifyH = 0;
  22909. }
  22910. else {
  22911. verifyH = WOLFSSL_FATAL_ERROR;
  22912. }
  22913. }
  22914. #endif /* HAVE_ECC_VERIFY */
  22915. if (wc_FreeRng(&rng) && ret == 0) {
  22916. ret = WOLFSSL_FATAL_ERROR;
  22917. }
  22918. wc_ecc_free(&key);
  22919. #ifdef FP_ECC
  22920. wc_ecc_fp_free();
  22921. #endif
  22922. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  22923. #endif
  22924. return res;
  22925. } /* END test_wc_ecc_sign_hash */
  22926. /*
  22927. * Testing wc_ecc_shared_secret()
  22928. */
  22929. static int test_wc_ecc_shared_secret(void)
  22930. {
  22931. int res = TEST_SKIPPED;
  22932. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  22933. ecc_key key, pubKey;
  22934. WC_RNG rng;
  22935. int ret;
  22936. byte out[KEY32];
  22937. int keySz = sizeof(out);
  22938. word32 outlen = (word32)sizeof(out);
  22939. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22940. const char* qx =
  22941. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22942. const char* qy =
  22943. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22944. const char* d =
  22945. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22946. const char* curveName = "SECP256R1";
  22947. const byte expected_shared_secret[] =
  22948. {
  22949. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  22950. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  22951. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  22952. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  22953. };
  22954. #endif
  22955. PRIVATE_KEY_UNLOCK();
  22956. /* Initialize variables. */
  22957. XMEMSET(out, 0, keySz);
  22958. XMEMSET(&rng, 0, sizeof(rng));
  22959. XMEMSET(&key, 0, sizeof(key));
  22960. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22961. ret = wc_InitRng(&rng);
  22962. if (ret == 0) {
  22963. ret = wc_ecc_init(&key);
  22964. if (ret == 0) {
  22965. ret = wc_ecc_init(&pubKey);
  22966. }
  22967. }
  22968. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22969. if (ret == 0) {
  22970. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22971. }
  22972. if (ret == 0) {
  22973. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  22974. }
  22975. #else
  22976. if (ret == 0) {
  22977. ret = wc_ecc_make_key(&rng, keySz, &key);
  22978. #if defined(WOLFSSL_ASYNC_CRYPT)
  22979. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22980. #endif
  22981. }
  22982. if (ret == 0) {
  22983. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  22984. #if defined(WOLFSSL_ASYNC_CRYPT)
  22985. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22986. #endif
  22987. }
  22988. #endif
  22989. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22990. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22991. !defined(HAVE_SELFTEST)
  22992. if (ret == 0) {
  22993. ret = wc_ecc_set_rng(&key, &rng);
  22994. }
  22995. #endif
  22996. if (ret == 0) {
  22997. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  22998. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22999. if (ret == 0) {
  23000. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  23001. ret = WOLFSSL_FATAL_ERROR;
  23002. }
  23003. }
  23004. #endif
  23005. /* Test bad args. */
  23006. if (ret == 0) {
  23007. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  23008. if (ret == BAD_FUNC_ARG) {
  23009. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  23010. }
  23011. if (ret == BAD_FUNC_ARG) {
  23012. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  23013. }
  23014. if (ret == BAD_FUNC_ARG) {
  23015. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  23016. }
  23017. if (ret == BAD_FUNC_ARG) {
  23018. /* Invalid length */
  23019. outlen = 1;
  23020. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  23021. }
  23022. if (ret == BUFFER_E) {
  23023. ret = 0;
  23024. }
  23025. else if (ret == 0) {
  23026. ret = WOLFSSL_FATAL_ERROR;
  23027. }
  23028. }
  23029. }
  23030. if (wc_FreeRng(&rng) && ret == 0) {
  23031. ret = WOLFSSL_FATAL_ERROR;
  23032. }
  23033. wc_ecc_free(&key);
  23034. wc_ecc_free(&pubKey);
  23035. #ifdef FP_ECC
  23036. wc_ecc_fp_free();
  23037. #endif
  23038. PRIVATE_KEY_LOCK();
  23039. res = TEST_RES_CHECK(ret == 0);
  23040. #endif
  23041. return res;
  23042. } /* END tests_wc_ecc_shared_secret */
  23043. /*
  23044. * testint wc_ecc_export_x963()
  23045. */
  23046. static int test_wc_ecc_export_x963(void)
  23047. {
  23048. int res = TEST_SKIPPED;
  23049. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23050. ecc_key key;
  23051. WC_RNG rng;
  23052. byte out[ECC_ASN963_MAX_BUF_SZ];
  23053. word32 outlen = sizeof(out);
  23054. int ret = 0;
  23055. PRIVATE_KEY_UNLOCK();
  23056. /* Initialize variables. */
  23057. XMEMSET(out, 0, outlen);
  23058. XMEMSET(&rng, 0, sizeof(rng));
  23059. XMEMSET(&key, 0, sizeof(key));
  23060. ret = wc_InitRng(&rng);
  23061. if (ret == 0) {
  23062. ret = wc_ecc_init(&key);
  23063. if (ret == 0) {
  23064. ret = wc_ecc_make_key(&rng, KEY20, &key);
  23065. #if defined(WOLFSSL_ASYNC_CRYPT)
  23066. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23067. #endif
  23068. }
  23069. }
  23070. if (ret == 0) {
  23071. ret = wc_ecc_export_x963(&key, out, &outlen);
  23072. }
  23073. /* Test bad args. */
  23074. if (ret == 0) {
  23075. ret = wc_ecc_export_x963(NULL, out, &outlen);
  23076. if (ret == ECC_BAD_ARG_E) {
  23077. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  23078. }
  23079. if (ret == LENGTH_ONLY_E) {
  23080. ret = wc_ecc_export_x963(&key, out, NULL);
  23081. }
  23082. if (ret == ECC_BAD_ARG_E) {
  23083. key.idx = -4;
  23084. ret = wc_ecc_export_x963(&key, out, &outlen);
  23085. }
  23086. if (ret == ECC_BAD_ARG_E) {
  23087. ret = 0;
  23088. }
  23089. else {
  23090. ret = WOLFSSL_FATAL_ERROR;
  23091. }
  23092. }
  23093. if (wc_FreeRng(&rng) && ret == 0) {
  23094. ret = WOLFSSL_FATAL_ERROR;
  23095. }
  23096. wc_ecc_free(&key);
  23097. #ifdef FP_ECC
  23098. wc_ecc_fp_free();
  23099. #endif
  23100. PRIVATE_KEY_LOCK();
  23101. res = TEST_RES_CHECK(ret == 0);
  23102. #endif
  23103. return res;
  23104. } /* END test_wc_ecc_export_x963 */
  23105. /*
  23106. * Testing wc_ecc_export_x963_ex()
  23107. * compile with --enable-compkey will use compression.
  23108. */
  23109. static int test_wc_ecc_export_x963_ex(void)
  23110. {
  23111. int res = TEST_SKIPPED;
  23112. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23113. ecc_key key;
  23114. WC_RNG rng;
  23115. int ret = 0;
  23116. byte out[ECC_ASN963_MAX_BUF_SZ];
  23117. word32 outlen = sizeof(out);
  23118. #ifdef HAVE_COMP_KEY
  23119. word32 badOutLen = 5;
  23120. #endif
  23121. /* Init stack variables. */
  23122. XMEMSET(out, 0, outlen);
  23123. XMEMSET(&rng, 0, sizeof(rng));
  23124. XMEMSET(&key, 0, sizeof(key));
  23125. ret = wc_InitRng(&rng);
  23126. if (ret == 0) {
  23127. ret = wc_ecc_init(&key);
  23128. if (ret == 0) {
  23129. ret = wc_ecc_make_key(&rng, KEY64, &key);
  23130. #if defined(WOLFSSL_ASYNC_CRYPT)
  23131. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23132. #endif
  23133. }
  23134. }
  23135. #ifdef HAVE_COMP_KEY
  23136. if (ret == 0) {
  23137. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  23138. }
  23139. #else
  23140. if (ret == 0) {
  23141. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  23142. }
  23143. #endif
  23144. /* Test bad args. */
  23145. #ifdef HAVE_COMP_KEY
  23146. if (ret == 0) {
  23147. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  23148. if (ret == BAD_FUNC_ARG) {
  23149. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  23150. }
  23151. if (ret == BAD_FUNC_ARG) {
  23152. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  23153. }
  23154. if (ret == BAD_FUNC_ARG) {
  23155. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  23156. }
  23157. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  23158. if (ret == BUFFER_E)
  23159. #else
  23160. if (ret == LENGTH_ONLY_E)
  23161. #endif
  23162. {
  23163. key.idx = -4;
  23164. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  23165. }
  23166. if (ret == ECC_BAD_ARG_E) {
  23167. ret = 0;
  23168. }
  23169. else {
  23170. ret = WOLFSSL_FATAL_ERROR;
  23171. }
  23172. }
  23173. #else
  23174. if (ret == 0) {
  23175. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  23176. if (ret == BAD_FUNC_ARG) {
  23177. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  23178. }
  23179. if (ret == BAD_FUNC_ARG) {
  23180. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  23181. }
  23182. if (ret == NOT_COMPILED_IN) {
  23183. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  23184. }
  23185. if (ret == BAD_FUNC_ARG) {
  23186. key.idx = -4;
  23187. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  23188. }
  23189. if (ret == ECC_BAD_ARG_E) {
  23190. ret = 0;
  23191. }
  23192. else if (ret == 0) {
  23193. ret = WOLFSSL_FATAL_ERROR;
  23194. }
  23195. }
  23196. #endif
  23197. if (wc_FreeRng(&rng) && ret == 0) {
  23198. ret = WOLFSSL_FATAL_ERROR;
  23199. }
  23200. wc_ecc_free(&key);
  23201. #ifdef FP_ECC
  23202. wc_ecc_fp_free();
  23203. #endif
  23204. res = TEST_RES_CHECK(ret == 0);
  23205. #endif
  23206. return res;
  23207. } /* END test_wc_ecc_export_x963_ex */
  23208. /*
  23209. * testing wc_ecc_import_x963()
  23210. */
  23211. static int test_wc_ecc_import_x963(void)
  23212. {
  23213. int res = TEST_SKIPPED;
  23214. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  23215. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23216. ecc_key pubKey, key;
  23217. WC_RNG rng;
  23218. byte x963[ECC_ASN963_MAX_BUF_SZ];
  23219. word32 x963Len = (word32)sizeof(x963);
  23220. int ret;
  23221. /* Init stack variables. */
  23222. XMEMSET(x963, 0, x963Len);
  23223. XMEMSET(&rng, 0, sizeof(rng));
  23224. XMEMSET(&key, 0, sizeof(key));
  23225. XMEMSET(&pubKey, 0, sizeof(pubKey));
  23226. ret = wc_InitRng(&rng);
  23227. if (ret == 0) {
  23228. ret = wc_ecc_init(&pubKey);
  23229. if (ret == 0) {
  23230. ret = wc_ecc_init(&key);
  23231. }
  23232. if (ret == 0) {
  23233. ret = wc_ecc_make_key(&rng, KEY24, &key);
  23234. #if defined(WOLFSSL_ASYNC_CRYPT)
  23235. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23236. #endif
  23237. }
  23238. if (ret == 0) {
  23239. PRIVATE_KEY_UNLOCK();
  23240. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  23241. PRIVATE_KEY_LOCK();
  23242. }
  23243. }
  23244. if (ret == 0) {
  23245. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  23246. }
  23247. /* Test bad args. */
  23248. if (ret == 0) {
  23249. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  23250. if (ret == BAD_FUNC_ARG) {
  23251. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  23252. }
  23253. if (ret == BAD_FUNC_ARG) {
  23254. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  23255. }
  23256. if (ret == ECC_BAD_ARG_E) {
  23257. ret = 0;
  23258. }
  23259. else if (ret == 0) {
  23260. ret = WOLFSSL_FATAL_ERROR;
  23261. }
  23262. }
  23263. if (wc_FreeRng(&rng) && ret == 0) {
  23264. ret = WOLFSSL_FATAL_ERROR;
  23265. }
  23266. wc_ecc_free(&key);
  23267. wc_ecc_free(&pubKey);
  23268. #ifdef FP_ECC
  23269. wc_ecc_fp_free();
  23270. #endif
  23271. res = TEST_RES_CHECK(ret == 0);
  23272. #endif
  23273. return res;
  23274. } /* END wc_ecc_import_x963 */
  23275. /*
  23276. * testing wc_ecc_import_private_key()
  23277. */
  23278. static int ecc_import_private_key(void)
  23279. {
  23280. int res = TEST_SKIPPED;
  23281. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  23282. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23283. ecc_key key, keyImp;
  23284. WC_RNG rng;
  23285. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  23286. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  23287. word32 privKeySz = (word32)sizeof(privKey);
  23288. word32 x963KeySz = (word32)sizeof(x963Key);
  23289. int ret;
  23290. /* Init stack variables. */
  23291. XMEMSET(privKey, 0, privKeySz);
  23292. XMEMSET(x963Key, 0, x963KeySz);
  23293. XMEMSET(&rng, 0, sizeof(rng));
  23294. XMEMSET(&key, 0, sizeof(key));
  23295. XMEMSET(&keyImp, 0, sizeof(keyImp));
  23296. ret = wc_InitRng(&rng);
  23297. if (ret == 0) {
  23298. ret = wc_ecc_init(&key);
  23299. if (ret == 0) {
  23300. ret = wc_ecc_init(&keyImp);
  23301. }
  23302. if (ret == 0) {
  23303. ret = wc_ecc_make_key(&rng, KEY48, &key);
  23304. #if defined(WOLFSSL_ASYNC_CRYPT)
  23305. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23306. #endif
  23307. }
  23308. if (ret == 0) {
  23309. PRIVATE_KEY_UNLOCK();
  23310. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  23311. PRIVATE_KEY_LOCK();
  23312. }
  23313. if (ret == 0) {
  23314. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  23315. }
  23316. }
  23317. if (ret == 0) {
  23318. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  23319. x963KeySz, &keyImp);
  23320. }
  23321. /* Pass in bad args. */
  23322. if (ret == 0) {
  23323. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  23324. x963KeySz, NULL);
  23325. if (ret == BAD_FUNC_ARG) {
  23326. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  23327. x963KeySz, &keyImp);
  23328. }
  23329. if (ret == BAD_FUNC_ARG) {
  23330. ret = 0;
  23331. }
  23332. else if (ret == 0) {
  23333. ret = WOLFSSL_FATAL_ERROR;
  23334. }
  23335. }
  23336. if (wc_FreeRng(&rng) && ret == 0) {
  23337. ret = WOLFSSL_FATAL_ERROR;
  23338. }
  23339. wc_ecc_free(&key);
  23340. wc_ecc_free(&keyImp);
  23341. #ifdef FP_ECC
  23342. wc_ecc_fp_free();
  23343. #endif
  23344. res = TEST_RES_CHECK(ret == 0);
  23345. #endif
  23346. return res;
  23347. } /* END wc_ecc_import_private_key */
  23348. /*
  23349. * Testing wc_ecc_export_private_only()
  23350. */
  23351. static int test_wc_ecc_export_private_only(void)
  23352. {
  23353. int res = TEST_SKIPPED;
  23354. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23355. ecc_key key;
  23356. WC_RNG rng;
  23357. byte out[ECC_PRIV_KEY_BUF];
  23358. word32 outlen = sizeof(out);
  23359. int ret;
  23360. /* Init stack variables. */
  23361. XMEMSET(out, 0, outlen);
  23362. XMEMSET(&rng, 0, sizeof(rng));
  23363. XMEMSET(&key, 0, sizeof(key));
  23364. ret = wc_InitRng(&rng);
  23365. if (ret == 0) {
  23366. ret = wc_ecc_init(&key);
  23367. if (ret == 0) {
  23368. ret = wc_ecc_make_key(&rng, KEY32, &key);
  23369. #if defined(WOLFSSL_ASYNC_CRYPT)
  23370. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23371. #endif
  23372. }
  23373. }
  23374. if (ret == 0) {
  23375. ret = wc_ecc_export_private_only(&key, out, &outlen);
  23376. }
  23377. /* Pass in bad args. */
  23378. if (ret == 0) {
  23379. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  23380. if (ret == BAD_FUNC_ARG) {
  23381. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  23382. }
  23383. if (ret == BAD_FUNC_ARG) {
  23384. ret = wc_ecc_export_private_only(&key, out, NULL);
  23385. }
  23386. if (ret == BAD_FUNC_ARG) {
  23387. ret = 0;
  23388. }
  23389. else if (ret == 0) {
  23390. ret = WOLFSSL_FATAL_ERROR;
  23391. }
  23392. }
  23393. if (wc_FreeRng(&rng) && ret == 0) {
  23394. ret = WOLFSSL_FATAL_ERROR;
  23395. }
  23396. wc_ecc_free(&key);
  23397. #ifdef FP_ECC
  23398. wc_ecc_fp_free();
  23399. #endif
  23400. res = TEST_RES_CHECK(ret == 0);
  23401. #endif
  23402. return res;
  23403. } /* END test_wc_ecc_export_private_only */
  23404. /*
  23405. * Testing wc_ecc_rs_to_sig()
  23406. */
  23407. static int test_wc_ecc_rs_to_sig(void)
  23408. {
  23409. int res = TEST_SKIPPED;
  23410. #if defined(HAVE_ECC) && !defined(NO_ASN)
  23411. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  23412. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  23413. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  23414. const char* zeroStr = "0";
  23415. byte sig[ECC_MAX_SIG_SIZE];
  23416. word32 siglen = (word32)sizeof(sig);
  23417. /*R and S max size is the order of curve. 2^192.*/
  23418. int keySz = KEY24;
  23419. byte r[KEY24];
  23420. byte s[KEY24];
  23421. word32 rlen = (word32)sizeof(r);
  23422. word32 slen = (word32)sizeof(s);
  23423. int ret;
  23424. /* Init stack variables. */
  23425. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  23426. XMEMSET(r, 0, keySz);
  23427. XMEMSET(s, 0, keySz);
  23428. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  23429. if (ret == 0) {
  23430. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  23431. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) || \
  23432. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  23433. if (ret == ASN_PARSE_E) {
  23434. ret = 0;
  23435. }
  23436. #endif
  23437. }
  23438. /* Test bad args. */
  23439. if (ret == 0) {
  23440. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  23441. if (ret == ECC_BAD_ARG_E) {
  23442. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  23443. }
  23444. if (ret == ECC_BAD_ARG_E) {
  23445. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  23446. }
  23447. if (ret == ECC_BAD_ARG_E) {
  23448. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  23449. }
  23450. if (ret == ECC_BAD_ARG_E) {
  23451. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  23452. }
  23453. if (ret == MP_ZERO_E) {
  23454. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  23455. }
  23456. if (ret == MP_ZERO_E) {
  23457. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  23458. }
  23459. if (ret == ECC_BAD_ARG_E) {
  23460. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  23461. }
  23462. if (ret == ECC_BAD_ARG_E) {
  23463. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  23464. }
  23465. if (ret == ECC_BAD_ARG_E) {
  23466. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  23467. }
  23468. if (ret == ECC_BAD_ARG_E) {
  23469. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  23470. }
  23471. if (ret == ECC_BAD_ARG_E) {
  23472. ret = 0;
  23473. }
  23474. else if (ret == 0) {
  23475. ret = WOLFSSL_FATAL_ERROR;
  23476. }
  23477. }
  23478. res = TEST_RES_CHECK(ret == 0);
  23479. #endif
  23480. return res;
  23481. } /* END test_wc_ecc_rs_to_sig */
  23482. static int test_wc_ecc_import_raw(void)
  23483. {
  23484. int res = TEST_SKIPPED;
  23485. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  23486. ecc_key key;
  23487. int ret = 0;
  23488. const char* qx =
  23489. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  23490. const char* qy =
  23491. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  23492. const char* d =
  23493. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  23494. const char* curveName = "SECP256R1";
  23495. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23496. const char* kNullStr = "";
  23497. #endif
  23498. ret = wc_ecc_init(&key);
  23499. /* Test good import */
  23500. if (ret == 0) {
  23501. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  23502. }
  23503. /* Test bad args. */
  23504. if (ret == 0) {
  23505. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  23506. if (ret == BAD_FUNC_ARG) {
  23507. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  23508. }
  23509. if (ret == BAD_FUNC_ARG) {
  23510. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  23511. }
  23512. if (ret == BAD_FUNC_ARG) {
  23513. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  23514. }
  23515. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23516. if (ret == BAD_FUNC_ARG) {
  23517. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  23518. wc_ecc_free(&key);
  23519. #endif
  23520. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  23521. if (ret == ECC_INF_E)
  23522. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  23523. }
  23524. #endif
  23525. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  23526. if (ret == BAD_FUNC_ARG) {
  23527. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  23528. wc_ecc_free(&key);
  23529. #endif
  23530. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  23531. /* Note: SP math "is point" failure returns MP_VAL */
  23532. if (ret == ECC_INF_E || ret == MP_VAL) {
  23533. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  23534. }
  23535. }
  23536. if (ret == BAD_FUNC_ARG) {
  23537. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  23538. wc_ecc_free(&key);
  23539. #endif
  23540. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  23541. /* Note: SP math "is point" failure returns MP_VAL */
  23542. if (ret == ECC_INF_E || ret == MP_VAL) {
  23543. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  23544. }
  23545. }
  23546. #endif
  23547. if (ret == BAD_FUNC_ARG) {
  23548. ret = 0;
  23549. }
  23550. }
  23551. wc_ecc_free(&key);
  23552. res = TEST_RES_CHECK(ret == 0);
  23553. #endif
  23554. return res;
  23555. } /* END test_wc_ecc_import_raw */
  23556. static int test_wc_ecc_import_unsigned(void)
  23557. {
  23558. int res = TEST_SKIPPED;
  23559. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23560. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  23561. ecc_key key;
  23562. const byte qx[] = {
  23563. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  23564. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  23565. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  23566. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  23567. };
  23568. const byte qy[] = {
  23569. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  23570. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  23571. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  23572. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  23573. };
  23574. const byte d[] = {
  23575. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  23576. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  23577. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  23578. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  23579. };
  23580. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23581. const byte nullBytes[32] = {0};
  23582. #endif
  23583. int curveId = ECC_SECP256R1;
  23584. int ret;
  23585. ret = wc_ecc_init(&key);
  23586. if (ret == 0) {
  23587. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23588. curveId);
  23589. }
  23590. /* Test bad args. */
  23591. if (ret == 0) {
  23592. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  23593. curveId);
  23594. if (ret == BAD_FUNC_ARG) {
  23595. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  23596. curveId);
  23597. }
  23598. if (ret == BAD_FUNC_ARG) {
  23599. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  23600. curveId);
  23601. }
  23602. if (ret == BAD_FUNC_ARG) {
  23603. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23604. ECC_CURVE_INVALID);
  23605. }
  23606. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23607. if (ret == BAD_FUNC_ARG) {
  23608. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  23609. (byte*)nullBytes, (byte*)nullBytes, curveId);
  23610. }
  23611. #endif
  23612. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  23613. ret = 0;
  23614. }
  23615. }
  23616. wc_ecc_free(&key);
  23617. res = TEST_RES_CHECK(ret == 0);
  23618. #endif
  23619. return res;
  23620. } /* END test_wc_ecc_import_unsigned */
  23621. /*
  23622. * Testing wc_ecc_sig_size()
  23623. */
  23624. static int test_wc_ecc_sig_size(void)
  23625. {
  23626. int res = TEST_SKIPPED;
  23627. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23628. ecc_key key;
  23629. WC_RNG rng;
  23630. int keySz = KEY16;
  23631. int ret = 0;
  23632. XMEMSET(&rng, 0, sizeof(rng));
  23633. XMEMSET(&key, 0, sizeof(key));
  23634. ret = wc_InitRng(&rng);
  23635. if (ret == 0) {
  23636. ret = wc_ecc_init(&key);
  23637. if (ret == 0) {
  23638. ret = wc_ecc_make_key(&rng, keySz, &key);
  23639. #if defined(WOLFSSL_ASYNC_CRYPT)
  23640. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23641. #endif
  23642. }
  23643. }
  23644. if (ret == 0) {
  23645. ret = wc_ecc_sig_size(&key);
  23646. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  23647. ret = 0;
  23648. }
  23649. }
  23650. if (wc_FreeRng(&rng) && ret == 0) {
  23651. ret = WOLFSSL_FATAL_ERROR;
  23652. }
  23653. wc_ecc_free(&key);
  23654. res = TEST_RES_CHECK(ret == 0);
  23655. #endif
  23656. return res;
  23657. } /* END test_wc_ecc_sig_size */
  23658. /*
  23659. * Testing wc_ecc_ctx_new()
  23660. */
  23661. static int test_wc_ecc_ctx_new(void)
  23662. {
  23663. int res = TEST_SKIPPED;
  23664. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23665. WC_RNG rng;
  23666. int ret = 0;
  23667. ecEncCtx* cli = NULL;
  23668. ecEncCtx* srv = NULL;
  23669. ret = wc_InitRng(&rng);
  23670. if (ret == 0) {
  23671. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  23672. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  23673. }
  23674. if (ret == 0 && (cli == NULL || srv == NULL)) {
  23675. ret = WOLFSSL_FATAL_ERROR;
  23676. }
  23677. wc_ecc_ctx_free(cli);
  23678. wc_ecc_ctx_free(srv);
  23679. /* Test bad args. */
  23680. if (ret == 0) {
  23681. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  23682. cli = wc_ecc_ctx_new(0, &rng);
  23683. if (cli != NULL) {
  23684. ret = WOLFSSL_FATAL_ERROR;
  23685. }
  23686. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  23687. if (cli != NULL) {
  23688. ret = WOLFSSL_FATAL_ERROR;
  23689. }
  23690. }
  23691. if (wc_FreeRng(&rng) && ret == 0) {
  23692. ret = WOLFSSL_FATAL_ERROR;
  23693. }
  23694. wc_ecc_ctx_free(cli);
  23695. res = TEST_RES_CHECK(ret == 0);
  23696. #endif
  23697. return res;
  23698. } /* END test_wc_ecc_ctx_new */
  23699. /*
  23700. * Tesing wc_ecc_reset()
  23701. */
  23702. static int test_wc_ecc_ctx_reset(void)
  23703. {
  23704. int res = TEST_SKIPPED;
  23705. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23706. ecEncCtx* ctx = NULL;
  23707. WC_RNG rng;
  23708. int ret = 0;
  23709. ret = wc_InitRng(&rng);
  23710. if (ret == 0) {
  23711. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  23712. ret = WOLFSSL_FATAL_ERROR;
  23713. }
  23714. }
  23715. if (ret == 0) {
  23716. ret = wc_ecc_ctx_reset(ctx, &rng);
  23717. }
  23718. /* Pass in bad args. */
  23719. if (ret == 0) {
  23720. ret = wc_ecc_ctx_reset(NULL, &rng);
  23721. if (ret == BAD_FUNC_ARG) {
  23722. ret = wc_ecc_ctx_reset(ctx, NULL);
  23723. }
  23724. if (ret == BAD_FUNC_ARG) {
  23725. ret = 0;
  23726. }
  23727. else if (ret == 0) {
  23728. ret = WOLFSSL_FATAL_ERROR;
  23729. }
  23730. }
  23731. if (wc_FreeRng(&rng) && ret == 0) {
  23732. ret = WOLFSSL_FATAL_ERROR;
  23733. }
  23734. wc_ecc_ctx_free(ctx);
  23735. res = TEST_RES_CHECK(ret == 0);
  23736. #endif
  23737. return res;
  23738. } /* END test_wc_ecc_ctx_reset */
  23739. /*
  23740. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  23741. */
  23742. static int test_wc_ecc_ctx_set_peer_salt(void)
  23743. {
  23744. int res = TEST_SKIPPED;
  23745. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23746. WC_RNG rng;
  23747. ecEncCtx* cliCtx = NULL;
  23748. ecEncCtx* servCtx = NULL;
  23749. const byte* cliSalt = NULL;
  23750. const byte* servSalt = NULL;
  23751. int ret = 0;
  23752. ret = wc_InitRng(&rng);
  23753. if (ret == 0) {
  23754. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  23755. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  23756. ret = WOLFSSL_FATAL_ERROR;
  23757. }
  23758. }
  23759. /* Test bad args. */
  23760. if (ret == 0) {
  23761. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  23762. if (cliSalt != NULL) {
  23763. ret = WOLFSSL_FATAL_ERROR;
  23764. }
  23765. }
  23766. if (ret == 0) {
  23767. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23768. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  23769. if (cliSalt == NULL || servSalt == NULL) {
  23770. ret = WOLFSSL_FATAL_ERROR;
  23771. }
  23772. }
  23773. if (ret == 0) {
  23774. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  23775. }
  23776. /* Test bad args. */
  23777. if (ret == 0) {
  23778. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  23779. if (ret == BAD_FUNC_ARG) {
  23780. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  23781. }
  23782. if (ret == BAD_FUNC_ARG) {
  23783. ret = 0;
  23784. }
  23785. else if (ret == 0) {
  23786. ret = WOLFSSL_FATAL_ERROR;
  23787. }
  23788. }
  23789. if (wc_FreeRng(&rng) && ret == 0) {
  23790. ret = WOLFSSL_FATAL_ERROR;
  23791. }
  23792. wc_ecc_ctx_free(cliCtx);
  23793. wc_ecc_ctx_free(servCtx);
  23794. res = TEST_RES_CHECK(ret == 0);
  23795. #endif
  23796. return res;
  23797. } /* END test_wc_ecc_ctx_set_peer_salt */
  23798. /*
  23799. * Testing wc_ecc_ctx_set_info()
  23800. */
  23801. static int test_wc_ecc_ctx_set_info(void)
  23802. {
  23803. int res = TEST_SKIPPED;
  23804. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23805. ecEncCtx* ctx = NULL;
  23806. WC_RNG rng;
  23807. int ret;
  23808. const char* optInfo = "Optional Test Info.";
  23809. int optInfoSz = (int)XSTRLEN(optInfo);
  23810. const char* badOptInfo = NULL;
  23811. ret = wc_InitRng(&rng);
  23812. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  23813. ret = WOLFSSL_FATAL_ERROR;
  23814. }
  23815. if (ret == 0) {
  23816. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  23817. }
  23818. /* Test bad args. */
  23819. if (ret == 0) {
  23820. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  23821. if (ret == BAD_FUNC_ARG) {
  23822. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  23823. }
  23824. if (ret == BAD_FUNC_ARG) {
  23825. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  23826. }
  23827. if (ret == BAD_FUNC_ARG) {
  23828. ret = 0;
  23829. }
  23830. else if (ret == 0) {
  23831. ret = WOLFSSL_FATAL_ERROR;
  23832. }
  23833. }
  23834. if (wc_FreeRng(&rng) && ret == 0) {
  23835. ret = WOLFSSL_FATAL_ERROR;
  23836. }
  23837. wc_ecc_ctx_free(ctx);
  23838. res = TEST_RES_CHECK(ret == 0);
  23839. #endif
  23840. return res;
  23841. } /* END test_wc_ecc_ctx_set_info */
  23842. /*
  23843. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  23844. */
  23845. static int test_wc_ecc_encryptDecrypt(void)
  23846. {
  23847. int res = TEST_SKIPPED;
  23848. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  23849. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23850. ecc_key srvKey, cliKey, tmpKey;
  23851. WC_RNG rng;
  23852. int ret;
  23853. const char* msg = "EccBlock Size 16";
  23854. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  23855. #ifdef WOLFSSL_ECIES_OLD
  23856. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23857. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23858. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  23859. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23860. #else
  23861. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23862. #endif
  23863. word32 outSz = (word32)sizeof(out);
  23864. byte plain[sizeof("EccBlock Size 16")];
  23865. word32 plainSz = (word32)sizeof(plain);
  23866. int keySz = KEY20;
  23867. /* Init stack variables. */
  23868. XMEMSET(out, 0, outSz);
  23869. XMEMSET(plain, 0, plainSz);
  23870. XMEMSET(&rng, 0, sizeof(rng));
  23871. XMEMSET(&srvKey, 0, sizeof(srvKey));
  23872. XMEMSET(&cliKey, 0, sizeof(cliKey));
  23873. ret = wc_InitRng(&rng);
  23874. if (ret == 0) {
  23875. ret = wc_ecc_init(&cliKey);
  23876. if (ret == 0) {
  23877. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  23878. #if defined(WOLFSSL_ASYNC_CRYPT)
  23879. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23880. #endif
  23881. }
  23882. if (ret == 0) {
  23883. ret = wc_ecc_init(&srvKey);
  23884. }
  23885. if (ret == 0) {
  23886. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  23887. #if defined(WOLFSSL_ASYNC_CRYPT)
  23888. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23889. #endif
  23890. }
  23891. if (ret == 0) {
  23892. ret = wc_ecc_init(&tmpKey);
  23893. }
  23894. }
  23895. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23896. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23897. !defined(HAVE_SELFTEST)
  23898. if (ret == 0) {
  23899. ret = wc_ecc_set_rng(&srvKey, &rng);
  23900. }
  23901. if (ret == 0) {
  23902. ret = wc_ecc_set_rng(&cliKey, &rng);
  23903. }
  23904. #endif
  23905. if (ret == 0) {
  23906. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23907. &outSz, NULL);
  23908. }
  23909. if (ret == 0) {
  23910. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  23911. &outSz, NULL);
  23912. if (ret == BAD_FUNC_ARG) {
  23913. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  23914. &outSz, NULL);
  23915. }
  23916. if (ret == BAD_FUNC_ARG) {
  23917. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  23918. &outSz, NULL);
  23919. }
  23920. if (ret == BAD_FUNC_ARG) {
  23921. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  23922. &outSz, NULL);
  23923. }
  23924. if (ret == BAD_FUNC_ARG) {
  23925. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23926. NULL, NULL);
  23927. }
  23928. if (ret == BAD_FUNC_ARG) {
  23929. ret = 0;
  23930. }
  23931. else if (ret == 0) {
  23932. ret = WOLFSSL_FATAL_ERROR;
  23933. }
  23934. }
  23935. #ifdef WOLFSSL_ECIES_OLD
  23936. if (ret == 0) {
  23937. tmpKey.dp = cliKey.dp;
  23938. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  23939. }
  23940. #endif
  23941. if (ret == 0) {
  23942. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  23943. &plainSz, NULL);
  23944. }
  23945. if (ret == 0) {
  23946. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  23947. &plainSz, NULL);
  23948. #ifdef WOLFSSL_ECIES_OLD
  23949. /* NULL parameter allowed in new implementations - public key comes from
  23950. * the message. */
  23951. if (ret == BAD_FUNC_ARG) {
  23952. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  23953. &plainSz, NULL);
  23954. }
  23955. #endif
  23956. if (ret == BAD_FUNC_ARG) {
  23957. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  23958. &plainSz, NULL);
  23959. }
  23960. if (ret == BAD_FUNC_ARG) {
  23961. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  23962. &plainSz, NULL);
  23963. }
  23964. if (ret == BAD_FUNC_ARG) {
  23965. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  23966. plain, NULL, NULL);
  23967. }
  23968. if (ret == BAD_FUNC_ARG) {
  23969. ret = 0;
  23970. }
  23971. else if (ret == 0) {
  23972. ret = WOLFSSL_FATAL_ERROR;
  23973. }
  23974. }
  23975. if (XMEMCMP(msg, plain, msgSz) != 0) {
  23976. ret = WOLFSSL_FATAL_ERROR;
  23977. }
  23978. if (wc_FreeRng(&rng) && ret == 0) {
  23979. ret = WOLFSSL_FATAL_ERROR;
  23980. }
  23981. wc_ecc_free(&tmpKey);
  23982. wc_ecc_free(&cliKey);
  23983. wc_ecc_free(&srvKey);
  23984. res = TEST_RES_CHECK(ret == 0);
  23985. #endif
  23986. return res;
  23987. } /* END test_wc_ecc_encryptDecrypt */
  23988. /*
  23989. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  23990. */
  23991. static int test_wc_ecc_del_point(void)
  23992. {
  23993. int res = TEST_SKIPPED;
  23994. #if defined(HAVE_ECC)
  23995. ecc_point* pt;
  23996. pt = wc_ecc_new_point();
  23997. wc_ecc_del_point(pt);
  23998. res = TEST_RES_CHECK(pt != NULL);
  23999. #endif
  24000. return res;
  24001. } /* END test_wc_ecc_del_point */
  24002. /*
  24003. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  24004. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  24005. * and wc_ecc_cmp_point()
  24006. */
  24007. static int test_wc_ecc_pointFns(void)
  24008. {
  24009. int res = TEST_SKIPPED;
  24010. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  24011. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  24012. !defined(WOLFSSL_ATECC608A)
  24013. ecc_key key;
  24014. WC_RNG rng;
  24015. int ret;
  24016. ecc_point* point = NULL;
  24017. ecc_point* cpypt = NULL;
  24018. int idx = 0;
  24019. int keySz = KEY32;
  24020. byte der[DER_SZ(KEY32)];
  24021. word32 derlenChk = 0;
  24022. word32 derSz = DER_SZ(KEY32);
  24023. /* Init stack variables. */
  24024. XMEMSET(der, 0, derSz);
  24025. XMEMSET(&rng, 0, sizeof(rng));
  24026. XMEMSET(&key, 0, sizeof(key));
  24027. ret = wc_InitRng(&rng);
  24028. if (ret == 0) {
  24029. ret = wc_ecc_init(&key);
  24030. if (ret == 0) {
  24031. ret = wc_ecc_make_key(&rng, keySz, &key);
  24032. #if defined(WOLFSSL_ASYNC_CRYPT)
  24033. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24034. #endif
  24035. }
  24036. }
  24037. if (ret == 0) {
  24038. point = wc_ecc_new_point();
  24039. if (!point) {
  24040. ret = WOLFSSL_FATAL_ERROR;
  24041. }
  24042. }
  24043. if (ret == 0) {
  24044. cpypt = wc_ecc_new_point();
  24045. if (!cpypt) {
  24046. ret = WOLFSSL_FATAL_ERROR;
  24047. }
  24048. }
  24049. /* Export */
  24050. if (ret == 0) {
  24051. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  24052. NULL, &derlenChk);
  24053. /* Check length value. */
  24054. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  24055. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  24056. der, &derSz);
  24057. }
  24058. }
  24059. /* Test bad args. */
  24060. if (ret == 0) {
  24061. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  24062. if (ret == ECC_BAD_ARG_E) {
  24063. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  24064. }
  24065. if (ret == ECC_BAD_ARG_E) {
  24066. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  24067. der, NULL);
  24068. }
  24069. if (ret == ECC_BAD_ARG_E) {
  24070. ret = 0;
  24071. }
  24072. else if (ret == 0) {
  24073. ret = WOLFSSL_FATAL_ERROR;
  24074. }
  24075. }
  24076. /* Import */
  24077. if (ret == 0) {
  24078. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  24079. /* Condition double checks wc_ecc_cmp_point(). */
  24080. if (ret == 0 &&
  24081. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  24082. ret = wc_ecc_cmp_point(&key.pubkey, point);
  24083. }
  24084. }
  24085. /* Test bad args. */
  24086. if (ret == 0) {
  24087. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  24088. if (ret == ECC_BAD_ARG_E) {
  24089. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  24090. }
  24091. if (ret == ECC_BAD_ARG_E) {
  24092. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  24093. }
  24094. if (ret == ECC_BAD_ARG_E) {
  24095. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  24096. }
  24097. if (ret == ECC_BAD_ARG_E) {
  24098. ret = 0;
  24099. }
  24100. else if (ret == 0) {
  24101. ret = WOLFSSL_FATAL_ERROR;
  24102. }
  24103. }
  24104. /* Copy */
  24105. if (ret == 0) {
  24106. ret = wc_ecc_copy_point(point, cpypt);
  24107. }
  24108. /* Test bad args. */
  24109. if (ret == 0) {
  24110. ret = wc_ecc_copy_point(NULL, cpypt);
  24111. if (ret == ECC_BAD_ARG_E) {
  24112. ret = wc_ecc_copy_point(point, NULL);
  24113. }
  24114. if (ret == ECC_BAD_ARG_E) {
  24115. ret = 0;
  24116. }
  24117. else if (ret == 0) {
  24118. ret = WOLFSSL_FATAL_ERROR;
  24119. }
  24120. }
  24121. /* Compare point */
  24122. if (ret == 0) {
  24123. ret = wc_ecc_cmp_point(point, cpypt);
  24124. }
  24125. /* Test bad args. */
  24126. if (ret == 0) {
  24127. ret = wc_ecc_cmp_point(NULL, cpypt);
  24128. if (ret == BAD_FUNC_ARG) {
  24129. ret = wc_ecc_cmp_point(point, NULL);
  24130. }
  24131. if (ret == BAD_FUNC_ARG) {
  24132. ret = 0;
  24133. }
  24134. else if (ret == 0) {
  24135. ret = WOLFSSL_FATAL_ERROR;
  24136. }
  24137. }
  24138. /* At infinity if return == 1, otherwise return == 0. */
  24139. if (ret == 0) {
  24140. ret = wc_ecc_point_is_at_infinity(point);
  24141. }
  24142. /* Test bad args. */
  24143. if (ret == 0) {
  24144. ret = wc_ecc_point_is_at_infinity(NULL);
  24145. if (ret == BAD_FUNC_ARG) {
  24146. ret = 0;
  24147. }
  24148. else if (ret == 0) {
  24149. ret = WOLFSSL_FATAL_ERROR;
  24150. }
  24151. }
  24152. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  24153. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  24154. #ifdef USE_ECC_B_PARAM
  24155. /* On curve if ret == 0 */
  24156. if (ret == 0) {
  24157. ret = wc_ecc_point_is_on_curve(point, idx);
  24158. }
  24159. /* Test bad args. */
  24160. if (ret == 0) {
  24161. ret = wc_ecc_point_is_on_curve(NULL, idx);
  24162. if (ret == BAD_FUNC_ARG) {
  24163. ret = wc_ecc_point_is_on_curve(point, 1000);
  24164. }
  24165. if (ret == ECC_BAD_ARG_E) {
  24166. ret = 0;
  24167. }
  24168. else if (ret == 0) {
  24169. ret = WOLFSSL_FATAL_ERROR;
  24170. }
  24171. }
  24172. #endif /* USE_ECC_B_PARAM */
  24173. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  24174. /* Free */
  24175. wc_ecc_del_point(point);
  24176. wc_ecc_del_point(cpypt);
  24177. wc_ecc_free(&key);
  24178. if (wc_FreeRng(&rng) && ret == 0) {
  24179. ret = WOLFSSL_FATAL_ERROR;
  24180. }
  24181. res = TEST_RES_CHECK(ret == 0);
  24182. #endif
  24183. return res;
  24184. } /* END test_wc_ecc_pointFns */
  24185. /*
  24186. * Testing wc_ecc_sahred_secret_ssh()
  24187. */
  24188. static int test_wc_ecc_shared_secret_ssh(void)
  24189. {
  24190. int res = TEST_SKIPPED;
  24191. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  24192. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  24193. !defined(WOLFSSL_ATECC608A)
  24194. ecc_key key, key2;
  24195. WC_RNG rng;
  24196. int ret;
  24197. int keySz = KEY32;
  24198. int key2Sz = KEY24;
  24199. byte secret[KEY32];
  24200. word32 secretLen = keySz;
  24201. /* Init stack variables. */
  24202. XMEMSET(secret, 0, secretLen);
  24203. XMEMSET(&rng, 0, sizeof(rng));
  24204. XMEMSET(&key, 0, sizeof(key));
  24205. XMEMSET(&key2, 0, sizeof(key2));
  24206. /* Make keys */
  24207. ret = wc_InitRng(&rng);
  24208. if (ret == 0) {
  24209. ret = wc_ecc_init(&key);
  24210. if (ret == 0) {
  24211. ret = wc_ecc_make_key(&rng, keySz, &key);
  24212. #if defined(WOLFSSL_ASYNC_CRYPT)
  24213. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24214. #endif
  24215. }
  24216. if (wc_FreeRng(&rng) && ret == 0) {
  24217. ret = WOLFSSL_FATAL_ERROR;
  24218. }
  24219. }
  24220. if (ret == 0) {
  24221. ret = wc_InitRng(&rng);
  24222. if (ret == 0) {
  24223. ret = wc_ecc_init(&key2);
  24224. }
  24225. if (ret == 0) {
  24226. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  24227. #if defined(WOLFSSL_ASYNC_CRYPT)
  24228. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  24229. #endif
  24230. }
  24231. }
  24232. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24233. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24234. !defined(HAVE_SELFTEST)
  24235. if (ret == 0) {
  24236. ret = wc_ecc_set_rng(&key, &rng);
  24237. }
  24238. #endif
  24239. if (ret == 0) {
  24240. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  24241. }
  24242. /* Pass in bad args. */
  24243. if (ret == 0) {
  24244. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  24245. if (ret == BAD_FUNC_ARG) {
  24246. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  24247. }
  24248. if (ret == BAD_FUNC_ARG) {
  24249. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  24250. }
  24251. if (ret == BAD_FUNC_ARG) {
  24252. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  24253. }
  24254. if (ret == BAD_FUNC_ARG) {
  24255. key.type = ECC_PUBLICKEY;
  24256. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  24257. if (ret == ECC_BAD_ARG_E) {
  24258. ret = 0;
  24259. }
  24260. else if (ret == 0) {
  24261. ret = WOLFSSL_FATAL_ERROR;
  24262. }
  24263. }
  24264. else if (ret == 0) {
  24265. ret = WOLFSSL_FATAL_ERROR;
  24266. }
  24267. }
  24268. if (wc_FreeRng(&rng) && ret == 0) {
  24269. ret = WOLFSSL_FATAL_ERROR;
  24270. }
  24271. wc_ecc_free(&key);
  24272. wc_ecc_free(&key2);
  24273. #ifdef FP_ECC
  24274. wc_ecc_fp_free();
  24275. #endif
  24276. res = TEST_RES_CHECK(ret == 0);
  24277. #endif
  24278. return res;
  24279. } /* END test_wc_ecc_shared_secret_ssh */
  24280. /*
  24281. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  24282. */
  24283. static int test_wc_ecc_verify_hash_ex(void)
  24284. {
  24285. int res = TEST_SKIPPED;
  24286. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  24287. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  24288. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  24289. ecc_key key;
  24290. WC_RNG rng;
  24291. int ret;
  24292. mp_int r;
  24293. mp_int s;
  24294. mp_int z;
  24295. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  24296. unsigned char iHash[] = "Everyone gets Friday off.......";
  24297. unsigned char shortHash[] = TEST_STRING;
  24298. word32 hashlen = sizeof(hash);
  24299. word32 iHashLen = sizeof(iHash);
  24300. word32 shortHashLen = sizeof(shortHash);
  24301. int keySz = KEY32;
  24302. int sig = WOLFSSL_FATAL_ERROR;
  24303. int ver = WOLFSSL_FATAL_ERROR;
  24304. int verify_ok = 0;
  24305. /* Initialize r and s. */
  24306. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  24307. if (ret != MP_OKAY) {
  24308. return MP_INIT_E;
  24309. }
  24310. ret = wc_InitRng(&rng);
  24311. if (ret == 0) {
  24312. ret = wc_ecc_init(&key);
  24313. if (ret == 0) {
  24314. ret = wc_ecc_make_key(&rng, keySz, &key);
  24315. #if defined(WOLFSSL_ASYNC_CRYPT)
  24316. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24317. #endif
  24318. }
  24319. }
  24320. if (ret == 0) {
  24321. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  24322. if (ret == 0) {
  24323. /* verify_ok should be 1. */
  24324. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  24325. if (verify_ok != 1 && ret == 0) {
  24326. ret = WOLFSSL_FATAL_ERROR;
  24327. }
  24328. }
  24329. if (ret == 0) {
  24330. /* verify_ok should be 0 */
  24331. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  24332. &verify_ok, &key);
  24333. if (verify_ok != 0 && ret == 0) {
  24334. ret = WOLFSSL_FATAL_ERROR;
  24335. }
  24336. }
  24337. if (ret == 0) {
  24338. /* verify_ok should be 0. */
  24339. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  24340. &verify_ok, &key);
  24341. if (verify_ok != 0 && ret == 0) {
  24342. ret = WOLFSSL_FATAL_ERROR;
  24343. }
  24344. }
  24345. }
  24346. /* Test bad args. */
  24347. if (ret == 0) {
  24348. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  24349. == ECC_BAD_ARG_E) {
  24350. sig = 0;
  24351. }
  24352. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  24353. != ECC_BAD_ARG_E) {
  24354. sig = WOLFSSL_FATAL_ERROR;
  24355. }
  24356. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  24357. != ECC_BAD_ARG_E) {
  24358. sig = WOLFSSL_FATAL_ERROR;
  24359. }
  24360. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  24361. != ECC_BAD_ARG_E) {
  24362. sig = WOLFSSL_FATAL_ERROR;
  24363. }
  24364. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  24365. != ECC_BAD_ARG_E) {
  24366. sig = WOLFSSL_FATAL_ERROR;
  24367. }
  24368. }
  24369. /* Test bad args. */
  24370. if (ret == 0) {
  24371. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  24372. == ECC_BAD_ARG_E) {
  24373. ver = 0;
  24374. }
  24375. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  24376. &verify_ok, &key) != ECC_BAD_ARG_E) {
  24377. ver = WOLFSSL_FATAL_ERROR;
  24378. }
  24379. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  24380. != MP_ZERO_E) {
  24381. ver = WOLFSSL_FATAL_ERROR;
  24382. }
  24383. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  24384. != MP_ZERO_E) {
  24385. ver = WOLFSSL_FATAL_ERROR;
  24386. }
  24387. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  24388. != MP_ZERO_E) {
  24389. ver = WOLFSSL_FATAL_ERROR;
  24390. }
  24391. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  24392. &key) != ECC_BAD_ARG_E) {
  24393. ver = WOLFSSL_FATAL_ERROR;
  24394. }
  24395. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  24396. NULL, &key) != ECC_BAD_ARG_E) {
  24397. ver = WOLFSSL_FATAL_ERROR;
  24398. }
  24399. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  24400. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  24401. ver = WOLFSSL_FATAL_ERROR;
  24402. }
  24403. }
  24404. wc_ecc_free(&key);
  24405. mp_free(&r);
  24406. mp_free(&s);
  24407. if (wc_FreeRng(&rng)) {
  24408. return WOLFSSL_FATAL_ERROR;
  24409. }
  24410. if (ret == 0 && (sig != 0 || ver != 0)) {
  24411. ret = WOLFSSL_FATAL_ERROR;
  24412. }
  24413. res = TEST_RES_CHECK(ret == 0);
  24414. #endif
  24415. return res;
  24416. } /* END test_wc_ecc_verify_hash_ex */
  24417. /*
  24418. * Testing wc_ecc_mulmod()
  24419. */
  24420. static int test_wc_ecc_mulmod(void)
  24421. {
  24422. int res = TEST_SKIPPED;
  24423. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  24424. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  24425. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  24426. ecc_key key1, key2, key3;
  24427. WC_RNG rng;
  24428. int ret = 0;
  24429. ret = wc_InitRng(&rng);
  24430. if (ret == 0) {
  24431. ret = wc_ecc_init(&key1);
  24432. if (ret == 0) {
  24433. ret = wc_ecc_init(&key2);
  24434. }
  24435. if (ret == 0) {
  24436. ret = wc_ecc_init(&key3);
  24437. }
  24438. if (ret == 0) {
  24439. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  24440. #if defined(WOLFSSL_ASYNC_CRYPT)
  24441. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  24442. #endif
  24443. }
  24444. wc_FreeRng(&rng);
  24445. }
  24446. if (ret == 0) {
  24447. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  24448. ECC_SECP256R1);
  24449. if (ret == 0) {
  24450. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  24451. key1.dp->prime, ECC_SECP256R1);
  24452. }
  24453. }
  24454. if (ret == 0) {
  24455. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  24456. &key3.pubkey, wc_ecc_key_get_priv(&key2),
  24457. wc_ecc_key_get_priv(&key3), 1);
  24458. }
  24459. /* Test bad args. */
  24460. if (ret == 0) {
  24461. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  24462. wc_ecc_key_get_priv(&key2),
  24463. wc_ecc_key_get_priv(&key3), 1);
  24464. if (ret == ECC_BAD_ARG_E) {
  24465. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  24466. wc_ecc_key_get_priv(&key2),
  24467. wc_ecc_key_get_priv(&key3), 1);
  24468. }
  24469. if (ret == ECC_BAD_ARG_E) {
  24470. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  24471. wc_ecc_key_get_priv(&key2),
  24472. wc_ecc_key_get_priv(&key3), 1);
  24473. }
  24474. if (ret == ECC_BAD_ARG_E) {
  24475. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  24476. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL,
  24477. 1);
  24478. }
  24479. if (ret == ECC_BAD_ARG_E) {
  24480. ret = 0;
  24481. }
  24482. else if (ret == 0) {
  24483. ret = WOLFSSL_FATAL_ERROR;
  24484. }
  24485. }
  24486. wc_ecc_free(&key1);
  24487. wc_ecc_free(&key2);
  24488. wc_ecc_free(&key3);
  24489. #ifdef FP_ECC
  24490. wc_ecc_fp_free();
  24491. #endif
  24492. res = TEST_RES_CHECK(ret == 0);
  24493. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  24494. return res;
  24495. } /* END test_wc_ecc_mulmod */
  24496. /*
  24497. * Testing wc_ecc_is_valid_idx()
  24498. */
  24499. static int test_wc_ecc_is_valid_idx(void)
  24500. {
  24501. int res = TEST_SKIPPED;
  24502. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  24503. ecc_key key;
  24504. WC_RNG rng;
  24505. int ret;
  24506. int iVal = -2;
  24507. int iVal2 = 3000;
  24508. XMEMSET(&rng, 0, sizeof(rng));
  24509. XMEMSET(&key, 0, sizeof(key));
  24510. ret = wc_InitRng(&rng);
  24511. if (ret == 0) {
  24512. ret = wc_ecc_init(&key);
  24513. if (ret == 0) {
  24514. ret = wc_ecc_make_key(&rng, 32, &key);
  24515. #if defined(WOLFSSL_ASYNC_CRYPT)
  24516. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24517. #endif
  24518. }
  24519. }
  24520. if (ret == 0) {
  24521. ret = wc_ecc_is_valid_idx(key.idx);
  24522. if (ret == 1) {
  24523. ret = 0;
  24524. }
  24525. else {
  24526. ret = WOLFSSL_FATAL_ERROR;
  24527. }
  24528. }
  24529. /* Test bad args. */
  24530. if (ret == 0) {
  24531. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  24532. if (ret == 0) {
  24533. ret = wc_ecc_is_valid_idx(iVal2);
  24534. }
  24535. if (ret != 0) {
  24536. ret = WOLFSSL_FATAL_ERROR;
  24537. }
  24538. }
  24539. if (wc_FreeRng(&rng) && ret == 0) {
  24540. ret = WOLFSSL_FATAL_ERROR;
  24541. }
  24542. wc_ecc_free(&key);
  24543. #ifdef FP_ECC
  24544. wc_ecc_fp_free();
  24545. #endif
  24546. res = TEST_RES_CHECK(ret == 0);
  24547. #endif
  24548. return res;
  24549. } /* END test_wc_ecc_is_valid_idx */
  24550. /*
  24551. * Testing wc_ecc_get_curve_id_from_oid()
  24552. */
  24553. static int test_wc_ecc_get_curve_id_from_oid(void)
  24554. {
  24555. int res = TEST_SKIPPED;
  24556. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  24557. !defined(HAVE_FIPS)
  24558. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  24559. word32 len = sizeof(oid);
  24560. int ret;
  24561. /* Bad Cases */
  24562. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  24563. if (ret == BAD_FUNC_ARG) {
  24564. ret = 0;
  24565. }
  24566. if (ret == 0) {
  24567. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  24568. if (ret == ECC_CURVE_INVALID) {
  24569. ret = 0;
  24570. }
  24571. }
  24572. /* Good Case */
  24573. if (ret == 0) {
  24574. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  24575. if (ret == ECC_SECP256R1) {
  24576. ret = 0;
  24577. }
  24578. }
  24579. res = TEST_RES_CHECK(ret == 0);
  24580. #endif
  24581. return res;
  24582. }/* END test_wc_ecc_get_curve_id_from_oid */
  24583. /*
  24584. * Testing wc_ecc_sig_size_calc()
  24585. */
  24586. static int test_wc_ecc_sig_size_calc(void)
  24587. {
  24588. int res = TEST_SKIPPED;
  24589. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  24590. ecc_key key;
  24591. WC_RNG rng;
  24592. int sz = 0;
  24593. int ret = 0;
  24594. ret = wc_InitRng(&rng);
  24595. if (ret == 0) {
  24596. ret = wc_ecc_init(&key);
  24597. if (ret == 0) {
  24598. ret = wc_ecc_make_key(&rng, 16, &key);
  24599. #if defined(WOLFSSL_ASYNC_CRYPT)
  24600. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24601. #endif
  24602. }
  24603. sz = key.dp->size;
  24604. }
  24605. if (ret == 0) {
  24606. ret = wc_ecc_sig_size_calc(sz);
  24607. if (ret > 0) {
  24608. ret = 0;
  24609. }
  24610. }
  24611. wc_ecc_free(&key);
  24612. wc_FreeRng(&rng);
  24613. res = TEST_RES_CHECK(ret == 0);
  24614. #endif
  24615. return res;
  24616. } /* END test_wc_ecc_sig_size_calc */
  24617. /*
  24618. * Testing ToTraditional
  24619. */
  24620. static int test_ToTraditional(void)
  24621. {
  24622. int res = TEST_SKIPPED;
  24623. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  24624. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  24625. defined(OPENSSL_EXTRA_X509_SMALL))
  24626. XFILE f;
  24627. byte input[TWOK_BUF];
  24628. word32 sz;
  24629. int ret;
  24630. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  24631. AssertTrue((f != XBADFILE));
  24632. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  24633. XFCLOSE(f);
  24634. /* Good case */
  24635. ret = ToTraditional(input, sz);
  24636. if (ret > 0) {
  24637. ret = 0;
  24638. }
  24639. /* Bad cases */
  24640. if (ret == 0) {
  24641. ret = ToTraditional(NULL, 0);
  24642. if (ret == BAD_FUNC_ARG) {
  24643. ret = 0;
  24644. }
  24645. }
  24646. if (ret == 0) {
  24647. ret = ToTraditional(NULL, sz);
  24648. if (ret == BAD_FUNC_ARG) {
  24649. ret = 0;
  24650. }
  24651. }
  24652. if (ret == 0) {
  24653. ret = ToTraditional(input, 0);
  24654. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  24655. ret = 0;
  24656. }
  24657. }
  24658. res = TEST_RES_CHECK(ret == 0);
  24659. #endif
  24660. return res;
  24661. }/* End test_ToTraditional*/
  24662. /*
  24663. * Testing wc_EccPrivateKeyToDer
  24664. */
  24665. static int test_wc_EccPrivateKeyToDer(void)
  24666. {
  24667. int res = TEST_SKIPPED;
  24668. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24669. byte output[ONEK_BUF];
  24670. ecc_key eccKey;
  24671. WC_RNG rng;
  24672. word32 inLen;
  24673. int ret;
  24674. ret = wc_InitRng(&rng);
  24675. if (ret == 0) {
  24676. ret = wc_ecc_init(&eccKey);
  24677. if (ret == 0) {
  24678. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24679. #if defined(WOLFSSL_ASYNC_CRYPT)
  24680. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24681. #endif
  24682. }
  24683. inLen = (word32)sizeof(output);
  24684. /* Bad Cases */
  24685. if (ret == 0) {
  24686. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  24687. if (ret == BAD_FUNC_ARG) {
  24688. ret = 0;
  24689. }
  24690. }
  24691. if (ret == 0) {
  24692. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  24693. if (ret == BAD_FUNC_ARG) {
  24694. ret = 0;
  24695. }
  24696. }
  24697. if (ret == 0) {
  24698. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  24699. if (ret == LENGTH_ONLY_E) {
  24700. ret = 0;
  24701. }
  24702. }
  24703. if (ret == 0) {
  24704. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  24705. if (ret == BAD_FUNC_ARG) {
  24706. ret = 0;
  24707. }
  24708. }
  24709. /*Good Case */
  24710. if (ret == 0) {
  24711. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  24712. if (ret > 0) {
  24713. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  24714. /* test importing private only into a PKEY struct */
  24715. EC_KEY* ec;
  24716. EVP_PKEY* pkey;
  24717. const unsigned char* der = output;
  24718. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  24719. AssertNotNull(pkey);
  24720. der = output;
  24721. ec = d2i_ECPrivateKey(NULL, &der, ret);
  24722. AssertNotNull(ec);
  24723. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  24724. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  24725. #endif
  24726. ret = 0;
  24727. }
  24728. }
  24729. wc_ecc_free(&eccKey);
  24730. }
  24731. wc_FreeRng(&rng);
  24732. res = TEST_RES_CHECK(ret == 0);
  24733. #endif
  24734. return res;
  24735. }/* End test_wc_EccPrivateKeyToDer*/
  24736. /*
  24737. * Testing wc_DhPublicKeyDecode
  24738. */
  24739. static int test_wc_DhPublicKeyDecode(void)
  24740. {
  24741. int res = TEST_SKIPPED;
  24742. #ifndef NO_DH
  24743. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  24744. DhKey key;
  24745. word32 inOutIdx;
  24746. AssertIntEQ(wc_InitDhKey(&key), 0);
  24747. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  24748. BAD_FUNC_ARG);
  24749. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24750. BAD_FUNC_ARG);
  24751. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24752. BAD_FUNC_ARG);
  24753. inOutIdx = 0;
  24754. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  24755. BAD_FUNC_ARG);
  24756. inOutIdx = 0;
  24757. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  24758. BAD_FUNC_ARG);
  24759. inOutIdx = 0;
  24760. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  24761. sizeof_dh_pub_key_der_2048), 0);
  24762. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  24763. key.pub.used != 0 && key.priv.used == 0);
  24764. wc_FreeDhKey(&key);
  24765. res = TEST_RES_CHECK(1);
  24766. #endif
  24767. #endif /* !NO_DH */
  24768. return res;
  24769. }
  24770. /*
  24771. * Testing wc_Ed25519KeyToDer
  24772. */
  24773. static int test_wc_Ed25519KeyToDer(void)
  24774. {
  24775. int res = TEST_SKIPPED;
  24776. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24777. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24778. byte output[ONEK_BUF];
  24779. ed25519_key ed25519Key;
  24780. WC_RNG rng;
  24781. word32 inLen;
  24782. int ret;
  24783. ret = wc_InitRng(&rng);
  24784. if (ret == 0) {
  24785. ret = wc_ed25519_init(&ed25519Key);
  24786. if (ret == 0) {
  24787. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24788. }
  24789. inLen = (word32)sizeof(output);
  24790. /* Bad Cases */
  24791. if (ret == 0) {
  24792. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  24793. if (ret == BAD_FUNC_ARG) {
  24794. ret = 0;
  24795. }
  24796. }
  24797. if (ret == 0) {
  24798. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  24799. if (ret == BAD_FUNC_ARG) {
  24800. ret = 0;
  24801. }
  24802. }
  24803. if (ret == 0) {
  24804. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  24805. if (ret == BAD_FUNC_ARG) {
  24806. ret = 0;
  24807. }
  24808. }
  24809. /* Good Cases */
  24810. if (ret == 0) {
  24811. /* length only */
  24812. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  24813. if (ret > 0) {
  24814. ret = 0;
  24815. }
  24816. }
  24817. if (ret == 0) {
  24818. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  24819. if (ret > 0) {
  24820. ret = 0;
  24821. }
  24822. }
  24823. wc_ed25519_free(&ed25519Key);
  24824. }
  24825. wc_FreeRng(&rng);
  24826. res = TEST_RES_CHECK(ret == 0);
  24827. #endif
  24828. return res;
  24829. }/* End test_wc_Ed25519KeyToDer*/
  24830. /*
  24831. * Testing wc_Ed25519PrivateKeyToDer
  24832. */
  24833. static int test_wc_Ed25519PrivateKeyToDer(void)
  24834. {
  24835. int res = TEST_SKIPPED;
  24836. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24837. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24838. byte output[ONEK_BUF];
  24839. ed25519_key ed25519PrivKey;
  24840. WC_RNG rng;
  24841. word32 inLen;
  24842. int ret;
  24843. ret = wc_InitRng(&rng);
  24844. if (ret == 0) {
  24845. ret = wc_ed25519_init(&ed25519PrivKey);
  24846. if (ret == 0) {
  24847. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  24848. }
  24849. inLen = (word32)sizeof(output);
  24850. /* Bad Cases */
  24851. if (ret == 0) {
  24852. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  24853. if (ret == BAD_FUNC_ARG) {
  24854. ret = 0;
  24855. }
  24856. }
  24857. if (ret == 0) {
  24858. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  24859. if (ret == BAD_FUNC_ARG) {
  24860. ret = 0;
  24861. }
  24862. }
  24863. if (ret == 0) {
  24864. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  24865. if (ret == BAD_FUNC_ARG) {
  24866. ret = 0;
  24867. }
  24868. }
  24869. /* Good Cases */
  24870. if (ret == 0) {
  24871. /* length only */
  24872. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  24873. if (ret > 0) {
  24874. ret = 0;
  24875. }
  24876. }
  24877. if (ret == 0) {
  24878. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  24879. if (ret > 0) {
  24880. ret = 0;
  24881. }
  24882. }
  24883. wc_ed25519_free(&ed25519PrivKey);
  24884. }
  24885. wc_FreeRng(&rng);
  24886. res = TEST_RES_CHECK(ret == 0);
  24887. #endif
  24888. return res;
  24889. }/* End test_wc_Ed25519PrivateKeyToDer*/
  24890. /*
  24891. * Testing wc_Ed448KeyToDer
  24892. */
  24893. static int test_wc_Ed448KeyToDer(void)
  24894. {
  24895. int res = TEST_SKIPPED;
  24896. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24897. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24898. byte output[ONEK_BUF];
  24899. ed448_key ed448Key;
  24900. WC_RNG rng;
  24901. word32 inLen;
  24902. int ret;
  24903. ret = wc_InitRng(&rng);
  24904. if (ret == 0) {
  24905. ret = wc_ed448_init(&ed448Key);
  24906. if (ret == 0) {
  24907. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24908. }
  24909. inLen = sizeof(output);
  24910. /* Bad Cases */
  24911. if (ret == 0) {
  24912. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  24913. if (ret == BAD_FUNC_ARG) {
  24914. ret = 0;
  24915. }
  24916. }
  24917. if (ret == 0) {
  24918. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  24919. if (ret == BAD_FUNC_ARG) {
  24920. ret = 0;
  24921. }
  24922. }
  24923. if (ret == 0) {
  24924. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  24925. if (ret == BAD_FUNC_ARG) {
  24926. ret = 0;
  24927. }
  24928. }
  24929. /* Good Cases */
  24930. if (ret == 0) {
  24931. /* length only */
  24932. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  24933. if (ret > 0) {
  24934. ret = 0;
  24935. }
  24936. }
  24937. if (ret == 0) {
  24938. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  24939. if (ret > 0) {
  24940. ret = 0;
  24941. }
  24942. }
  24943. wc_ed448_free(&ed448Key);
  24944. }
  24945. wc_FreeRng(&rng);
  24946. res = TEST_RES_CHECK(ret == 0);
  24947. #endif
  24948. return res;
  24949. }/* End test_wc_Ed448KeyToDer*/
  24950. /*
  24951. * Testing wc_Ed448PrivateKeyToDer
  24952. */
  24953. static int test_wc_Ed448PrivateKeyToDer(void)
  24954. {
  24955. int res = TEST_SKIPPED;
  24956. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24957. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24958. byte output[ONEK_BUF];
  24959. ed448_key ed448PrivKey;
  24960. WC_RNG rng;
  24961. word32 inLen;
  24962. int ret;
  24963. ret = wc_InitRng(&rng);
  24964. if (ret == 0) {
  24965. ret = wc_ed448_init(&ed448PrivKey);
  24966. if (ret == 0) {
  24967. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  24968. }
  24969. inLen = sizeof(output);
  24970. /* Bad Cases */
  24971. if (ret == 0) {
  24972. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  24973. if (ret == BAD_FUNC_ARG) {
  24974. ret = 0;
  24975. }
  24976. }
  24977. if (ret == 0) {
  24978. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  24979. if (ret == BAD_FUNC_ARG) {
  24980. ret = 0;
  24981. }
  24982. }
  24983. if (ret == 0) {
  24984. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  24985. if (ret == BAD_FUNC_ARG) {
  24986. ret = 0;
  24987. }
  24988. }
  24989. /* Good cases */
  24990. if (ret == 0) {
  24991. /* length only */
  24992. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  24993. if (ret > 0) {
  24994. ret = 0;
  24995. }
  24996. }
  24997. if (ret == 0) {
  24998. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  24999. if (ret > 0) {
  25000. ret = 0;
  25001. }
  25002. }
  25003. wc_ed448_free(&ed448PrivKey);
  25004. }
  25005. wc_FreeRng(&rng);
  25006. res = TEST_RES_CHECK(ret == 0);
  25007. #endif
  25008. return res;
  25009. }/* End test_wc_Ed448PrivateKeyToDer*/
  25010. /*
  25011. * Testing wc_SetSubjectBuffer
  25012. */
  25013. static int test_wc_SetSubjectBuffer(void)
  25014. {
  25015. int res = TEST_SKIPPED;
  25016. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  25017. Cert cert;
  25018. FILE* file;
  25019. byte* der;
  25020. word32 derSz;
  25021. int ret = 0;
  25022. derSz = FOURK_BUF;
  25023. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25024. if (der == NULL) {
  25025. ret = -1;
  25026. }
  25027. if (ret == 0) {
  25028. file = XFOPEN("./certs/ca-cert.der", "rb");
  25029. if (file != NULL) {
  25030. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  25031. XFCLOSE(file);
  25032. }
  25033. else {
  25034. ret = -1;
  25035. }
  25036. }
  25037. if (ret == 0) {
  25038. ret = wc_InitCert(&cert);
  25039. }
  25040. if (ret == 0) {
  25041. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  25042. }
  25043. if (ret == 0) {
  25044. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  25045. if (ret == BAD_FUNC_ARG) {
  25046. ret = 0;
  25047. }
  25048. }
  25049. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25050. res = TEST_RES_CHECK(ret == 0);
  25051. #endif
  25052. return res;
  25053. }/* End test_wc_SetSubjectBuffer*/
  25054. /*
  25055. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  25056. */
  25057. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  25058. {
  25059. int res = TEST_SKIPPED;
  25060. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25061. WC_RNG rng;
  25062. Cert cert;
  25063. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25064. ed25519_key ed25519Key;
  25065. #endif
  25066. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25067. RsaKey rsaKey;
  25068. int bits = 2048;
  25069. #endif
  25070. #if defined(HAVE_ECC)
  25071. ecc_key eccKey;
  25072. #endif
  25073. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25074. ed448_key ed448Key;
  25075. #endif
  25076. int ret = 0;
  25077. #ifndef HAVE_FIPS
  25078. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  25079. #else
  25080. ret = wc_InitRng(&rng);
  25081. #endif
  25082. wc_InitCert(&cert);
  25083. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25084. if (ret == 0) { /*ED25519*/
  25085. ret = wc_ed25519_init(&ed25519Key);
  25086. if (ret == 0) {
  25087. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  25088. }
  25089. if (ret == 0) {
  25090. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25091. &ed25519Key);
  25092. }
  25093. wc_ed25519_free(&ed25519Key);
  25094. }
  25095. #endif
  25096. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25097. if (ret == 0) { /*RSA*/
  25098. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  25099. if (ret == 0) {
  25100. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  25101. }
  25102. if (ret == 0) {
  25103. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  25104. }
  25105. wc_FreeRsaKey(&rsaKey);
  25106. }
  25107. #endif
  25108. #if defined(HAVE_ECC)
  25109. if (ret == 0) { /*ECC*/
  25110. ret = wc_ecc_init(&eccKey);
  25111. if (ret == 0) {
  25112. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25113. #if defined(WOLFSSL_ASYNC_CRYPT)
  25114. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25115. #endif
  25116. }
  25117. if (ret == 0) {
  25118. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  25119. }
  25120. wc_ecc_free(&eccKey);
  25121. }
  25122. #endif
  25123. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25124. if (ret == 0) { /*ED448*/
  25125. ret = wc_ed448_init(&ed448Key);
  25126. if (ret == 0) {
  25127. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  25128. }
  25129. if (ret == 0) {
  25130. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  25131. &ed448Key);
  25132. }
  25133. wc_ed448_free(&ed448Key);
  25134. }
  25135. #endif
  25136. wc_FreeRng(&rng);
  25137. res = TEST_RES_CHECK(ret == 0);
  25138. #endif
  25139. return res;
  25140. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  25141. /*
  25142. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  25143. */
  25144. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  25145. {
  25146. int res = TEST_SKIPPED;
  25147. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25148. WC_RNG rng;
  25149. Cert cert;
  25150. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25151. ed25519_key ed25519Key;
  25152. #endif
  25153. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25154. RsaKey rsaKey;
  25155. int bits = 2048;
  25156. #endif
  25157. #if defined(HAVE_ECC)
  25158. ecc_key eccKey;
  25159. #endif
  25160. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25161. ed448_key ed448Key;
  25162. #endif
  25163. int ret = 0;
  25164. #ifndef HAVE_FIPS
  25165. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  25166. #else
  25167. ret = wc_InitRng(&rng);
  25168. #endif
  25169. wc_InitCert(&cert);
  25170. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25171. if (ret == 0) { /*ED25519*/
  25172. ret = wc_ed25519_init(&ed25519Key);
  25173. if (ret == 0) {
  25174. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  25175. }
  25176. if (ret == 0) {
  25177. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25178. &ed25519Key);
  25179. }
  25180. wc_ed25519_free(&ed25519Key);
  25181. }
  25182. #endif
  25183. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25184. if (ret == 0) { /*RSA*/
  25185. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  25186. if (ret == 0) {
  25187. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  25188. }
  25189. if (ret == 0) {
  25190. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  25191. }
  25192. wc_FreeRsaKey(&rsaKey);
  25193. }
  25194. #endif
  25195. #if defined(HAVE_ECC)
  25196. if (ret == 0) { /*ECC*/
  25197. ret = wc_ecc_init(&eccKey);
  25198. if (ret == 0) {
  25199. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25200. #if defined(WOLFSSL_ASYNC_CRYPT)
  25201. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25202. #endif
  25203. }
  25204. if (ret == 0) {
  25205. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  25206. }
  25207. wc_ecc_free(&eccKey);
  25208. }
  25209. #endif
  25210. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25211. if (ret == 0) { /*ED448*/
  25212. ret = wc_ed448_init(&ed448Key);
  25213. if (ret == 0) {
  25214. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  25215. }
  25216. if (ret == 0) {
  25217. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  25218. &ed448Key);
  25219. }
  25220. wc_ed448_free(&ed448Key);
  25221. }
  25222. #endif
  25223. wc_FreeRng(&rng);
  25224. res = TEST_RES_CHECK(ret == 0);
  25225. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  25226. return res;
  25227. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  25228. /*
  25229. * Testing wc_PKCS7_New()
  25230. */
  25231. static int test_wc_PKCS7_New(void)
  25232. {
  25233. int res = TEST_SKIPPED;
  25234. #if defined(HAVE_PKCS7)
  25235. PKCS7* pkcs7;
  25236. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  25237. wc_PKCS7_Free(pkcs7);
  25238. res = TEST_RES_CHECK(pkcs7 != NULL);
  25239. #endif
  25240. return res;
  25241. } /* END test-wc_PKCS7_New */
  25242. /*
  25243. * Testing wc_PKCS7_Init()
  25244. */
  25245. static int test_wc_PKCS7_Init(void)
  25246. {
  25247. int res = TEST_SKIPPED;
  25248. #if defined(HAVE_PKCS7)
  25249. PKCS7* pkcs7;
  25250. void* heap = NULL;
  25251. pkcs7 = wc_PKCS7_New(heap, testDevId);
  25252. AssertNotNull(pkcs7);
  25253. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25254. /* Pass in bad args. */
  25255. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  25256. wc_PKCS7_Free(pkcs7);
  25257. res = TEST_RES_CHECK(1);
  25258. #endif
  25259. return res;
  25260. } /* END test-wc_PKCS7_Init */
  25261. /*
  25262. * Testing wc_PKCS7_InitWithCert()
  25263. */
  25264. static int test_wc_PKCS7_InitWithCert(void)
  25265. {
  25266. int res = TEST_SKIPPED;
  25267. #if defined(HAVE_PKCS7)
  25268. PKCS7* pkcs7;
  25269. #ifndef NO_RSA
  25270. #if defined(USE_CERT_BUFFERS_2048)
  25271. unsigned char cert[sizeof(client_cert_der_2048)];
  25272. int certSz = (int)sizeof(cert);
  25273. XMEMSET(cert, 0, certSz);
  25274. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  25275. #elif defined(USE_CERT_BUFFERS_1024)
  25276. unsigned char cert[sizeof(client_cert_der_1024)];
  25277. int certSz = (int)sizeof(cert);
  25278. XMEMSET(cert, 0, certSz);
  25279. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  25280. #else
  25281. unsigned char cert[ONEK_BUF];
  25282. XFILE fp;
  25283. int certSz;
  25284. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25285. AssertTrue(fp != XBADFILE);
  25286. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25287. XFCLOSE(fp);
  25288. #endif
  25289. #elif defined(HAVE_ECC)
  25290. #if defined(USE_CERT_BUFFERS_256)
  25291. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25292. int certSz = (int)sizeof(cert);
  25293. XMEMSET(cert, 0, certSz);
  25294. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  25295. #else
  25296. unsigned char cert[ONEK_BUF];
  25297. XFILE fp;
  25298. int certSz;
  25299. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  25300. AssertTrue(fp != XBADFILE);
  25301. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  25302. XFCLOSE(fp);
  25303. #endif
  25304. #else
  25305. #error PKCS7 requires ECC or RSA
  25306. #endif
  25307. #ifdef HAVE_ECC
  25308. {
  25309. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  25310. static unsigned char certWithInvalidEccKey[] = {
  25311. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  25312. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  25313. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  25314. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  25315. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25316. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  25317. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25318. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25319. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25320. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25321. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25322. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25323. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25324. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25325. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25326. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  25327. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  25328. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  25329. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  25330. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  25331. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  25332. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  25333. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  25334. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  25335. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  25336. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  25337. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25338. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  25339. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25340. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  25341. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  25342. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  25343. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  25344. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  25345. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  25346. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  25347. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  25348. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  25349. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  25350. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  25351. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25352. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  25353. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  25354. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25355. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  25356. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25357. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  25358. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25359. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25360. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25361. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25362. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25363. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25364. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25365. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25366. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25367. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  25368. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  25369. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  25370. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  25371. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  25372. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  25373. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  25374. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  25375. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  25376. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  25377. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  25378. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  25379. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  25380. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  25381. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  25382. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  25383. 0x08, 0xA8, 0xB4};
  25384. #endif
  25385. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25386. /* If initialization is not successful, it's free'd in init func. */
  25387. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  25388. wc_PKCS7_Free(pkcs7);
  25389. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25390. /* Valid initialization usage. */
  25391. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25392. /* Pass in bad args. No need free for null checks, free at end.*/
  25393. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  25394. BAD_FUNC_ARG);
  25395. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  25396. BAD_FUNC_ARG);
  25397. #ifdef HAVE_ECC
  25398. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  25399. sizeof(certWithInvalidEccKey)), 0);
  25400. }
  25401. #endif
  25402. wc_PKCS7_Free(pkcs7);
  25403. res = TEST_RES_CHECK(1);
  25404. #endif
  25405. return res;
  25406. } /* END test_wc_PKCS7_InitWithCert */
  25407. /*
  25408. * Testing wc_PKCS7_EncodeData()
  25409. */
  25410. static int test_wc_PKCS7_EncodeData(void)
  25411. {
  25412. int res = TEST_SKIPPED;
  25413. #if defined(HAVE_PKCS7)
  25414. PKCS7* pkcs7;
  25415. byte output[FOURK_BUF];
  25416. byte data[] = "My encoded DER cert.";
  25417. #ifndef NO_RSA
  25418. #if defined(USE_CERT_BUFFERS_2048)
  25419. unsigned char cert[sizeof(client_cert_der_2048)];
  25420. unsigned char key[sizeof(client_key_der_2048)];
  25421. int certSz = (int)sizeof(cert);
  25422. int keySz = (int)sizeof(key);
  25423. XMEMSET(cert, 0, certSz);
  25424. XMEMSET(key, 0, keySz);
  25425. XMEMCPY(cert, client_cert_der_2048, certSz);
  25426. XMEMCPY(key, client_key_der_2048, keySz);
  25427. #elif defined(USE_CERT_BUFFERS_1024)
  25428. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  25429. unsigned char key[sizeof_client_key_der_1024];
  25430. int certSz = (int)sizeof(cert);
  25431. int keySz = (int)sizeof(key);
  25432. XMEMSET(cert, 0, certSz);
  25433. XMEMSET(key, 0, keySz);
  25434. XMEMCPY(cert, client_cert_der_1024, certSz);
  25435. XMEMCPY(key, client_key_der_1024, keySz);
  25436. #else
  25437. unsigned char cert[ONEK_BUF];
  25438. unsigned char key[ONEK_BUF];
  25439. XFILE fp;
  25440. int certSz;
  25441. int keySz;
  25442. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25443. AssertTrue(fp != XBADFILE);
  25444. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25445. XFCLOSE(fp);
  25446. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25447. AssertTrue(fp != XBADFILE);
  25448. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25449. XFCLOSE(fp);
  25450. #endif
  25451. #elif defined(HAVE_ECC)
  25452. #if defined(USE_CERT_BUFFERS_256)
  25453. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25454. unsigned char key[sizeof(ecc_clikey_der_256)];
  25455. int certSz = (int)sizeof(cert);
  25456. int keySz = (int)sizeof(key);
  25457. XMEMSET(cert, 0, certSz);
  25458. XMEMSET(key, 0, keySz);
  25459. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25460. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25461. #else
  25462. unsigned char cert[ONEK_BUF];
  25463. unsigned char key[ONEK_BUF];
  25464. XFILE fp;
  25465. int certSz, keySz;
  25466. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  25467. AssertTrue(fp != XBADFILE);
  25468. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  25469. XFCLOSE(fp);
  25470. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25471. AssertTrue(fp != XBADFILE);
  25472. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  25473. XFCLOSE(fp);
  25474. #endif
  25475. #endif
  25476. XMEMSET(output, 0, sizeof(output));
  25477. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25478. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25479. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  25480. pkcs7->content = data;
  25481. pkcs7->contentSz = sizeof(data);
  25482. pkcs7->privateKey = key;
  25483. pkcs7->privateKeySz = keySz;
  25484. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  25485. /* Test bad args. */
  25486. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  25487. BAD_FUNC_ARG);
  25488. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  25489. BAD_FUNC_ARG);
  25490. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  25491. wc_PKCS7_Free(pkcs7);
  25492. res = TEST_RES_CHECK(1);
  25493. #endif
  25494. return res;
  25495. } /* END test_wc_PKCS7_EncodeData */
  25496. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25497. !defined(NO_RSA) && !defined(NO_SHA256)
  25498. /* RSA sign raw digest callback */
  25499. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  25500. byte* out, word32 outSz, byte* privateKey,
  25501. word32 privateKeySz, int devid, int hashOID)
  25502. {
  25503. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  25504. byte digInfoEncoding[] = {
  25505. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  25506. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  25507. 0x00, 0x04, 0x20
  25508. };
  25509. int ret;
  25510. byte digestInfo[ONEK_BUF];
  25511. byte sig[FOURK_BUF];
  25512. word32 digestInfoSz = 0;
  25513. word32 idx = 0;
  25514. RsaKey rsa;
  25515. /* SHA-256 required only for this example callback due to above
  25516. * digInfoEncoding[] */
  25517. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  25518. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  25519. (hashOID != SHA256h)) {
  25520. return -1;
  25521. }
  25522. /* build DigestInfo */
  25523. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  25524. digestInfoSz += sizeof(digInfoEncoding);
  25525. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  25526. digestInfoSz += digestSz;
  25527. /* set up RSA key */
  25528. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  25529. if (ret != 0) {
  25530. return ret;
  25531. }
  25532. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  25533. /* sign DigestInfo */
  25534. if (ret == 0) {
  25535. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  25536. &rsa, pkcs7->rng);
  25537. if (ret > 0) {
  25538. if (ret > (int)outSz) {
  25539. /* output buffer too small */
  25540. ret = -1;
  25541. }
  25542. else {
  25543. /* success, ret holds sig size */
  25544. XMEMCPY(out, sig, ret);
  25545. }
  25546. }
  25547. }
  25548. wc_FreeRsaKey(&rsa);
  25549. return ret;
  25550. }
  25551. #endif
  25552. /*
  25553. * Testing wc_PKCS7_EncodeSignedData()
  25554. */
  25555. static int test_wc_PKCS7_EncodeSignedData(void)
  25556. {
  25557. int res = TEST_SKIPPED;
  25558. #if defined(HAVE_PKCS7)
  25559. PKCS7* pkcs7;
  25560. WC_RNG rng;
  25561. byte output[FOURK_BUF];
  25562. byte badOut[1];
  25563. word32 outputSz = (word32)sizeof(output);
  25564. word32 badOutSz = 0;
  25565. byte data[] = "Test data to encode.";
  25566. #ifndef NO_RSA
  25567. #if defined(USE_CERT_BUFFERS_2048)
  25568. byte key[sizeof(client_key_der_2048)];
  25569. byte cert[sizeof(client_cert_der_2048)];
  25570. word32 keySz = (word32)sizeof(key);
  25571. word32 certSz = (word32)sizeof(cert);
  25572. XMEMSET(key, 0, keySz);
  25573. XMEMSET(cert, 0, certSz);
  25574. XMEMCPY(key, client_key_der_2048, keySz);
  25575. XMEMCPY(cert, client_cert_der_2048, certSz);
  25576. #elif defined(USE_CERT_BUFFERS_1024)
  25577. byte key[sizeof_client_key_der_1024];
  25578. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25579. word32 keySz = (word32)sizeof(key);
  25580. word32 certSz = (word32)sizeof(cert);
  25581. XMEMSET(key, 0, keySz);
  25582. XMEMSET(cert, 0, certSz);
  25583. XMEMCPY(key, client_key_der_1024, keySz);
  25584. XMEMCPY(cert, client_cert_der_1024, certSz);
  25585. #else
  25586. unsigned char cert[ONEK_BUF];
  25587. unsigned char key[ONEK_BUF];
  25588. XFILE fp;
  25589. int certSz;
  25590. int keySz;
  25591. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25592. AssertTrue(fp != XBADFILE);
  25593. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25594. XFCLOSE(fp);
  25595. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25596. AssertTrue(fp != XBADFILE);
  25597. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25598. XFCLOSE(fp);
  25599. #endif
  25600. #elif defined(HAVE_ECC)
  25601. #if defined(USE_CERT_BUFFERS_256)
  25602. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25603. unsigned char key[sizeof(ecc_clikey_der_256)];
  25604. int certSz = (int)sizeof(cert);
  25605. int keySz = (int)sizeof(key);
  25606. XMEMSET(cert, 0, certSz);
  25607. XMEMSET(key, 0, keySz);
  25608. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  25609. XMEMCPY(key, ecc_clikey_der_256, keySz);
  25610. #else
  25611. unsigned char cert[ONEK_BUF];
  25612. unsigned char key[ONEK_BUF];
  25613. XFILE fp;
  25614. int certSz, keySz;
  25615. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  25616. AssertTrue(fp != XBADFILE);
  25617. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  25618. XFCLOSE(fp);
  25619. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25620. AssertTrue(fp != XBADFILE);
  25621. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  25622. XFCLOSE(fp);
  25623. #endif
  25624. #endif
  25625. XMEMSET(output, 0, outputSz);
  25626. AssertIntEQ(wc_InitRng(&rng), 0);
  25627. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25628. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25629. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25630. pkcs7->content = data;
  25631. pkcs7->contentSz = (word32)sizeof(data);
  25632. pkcs7->privateKey = key;
  25633. pkcs7->privateKeySz = (word32)sizeof(key);
  25634. pkcs7->encryptOID = RSAk;
  25635. #ifdef NO_SHA
  25636. pkcs7->hashOID = SHA256h;
  25637. #else
  25638. pkcs7->hashOID = SHAh;
  25639. #endif
  25640. pkcs7->rng = &rng;
  25641. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25642. wc_PKCS7_Free(pkcs7);
  25643. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25644. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25645. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25646. /* Pass in bad args. */
  25647. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  25648. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  25649. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  25650. badOutSz), BAD_FUNC_ARG);
  25651. pkcs7->hashOID = 0; /* bad hashOID */
  25652. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  25653. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25654. !defined(NO_RSA) && !defined(NO_SHA256)
  25655. /* test RSA sign raw digest callback, if using RSA and compiled in.
  25656. * Example callback assumes SHA-256, so only run test if compiled in. */
  25657. wc_PKCS7_Free(pkcs7);
  25658. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25659. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25660. pkcs7->content = data;
  25661. pkcs7->contentSz = (word32)sizeof(data);
  25662. pkcs7->privateKey = key;
  25663. pkcs7->privateKeySz = (word32)sizeof(key);
  25664. pkcs7->encryptOID = RSAk;
  25665. pkcs7->hashOID = SHA256h;
  25666. pkcs7->rng = &rng;
  25667. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  25668. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25669. #endif
  25670. wc_PKCS7_Free(pkcs7);
  25671. wc_FreeRng(&rng);
  25672. res = TEST_RES_CHECK(1);
  25673. #endif
  25674. return res;
  25675. } /* END test_wc_PKCS7_EncodeSignedData */
  25676. /*
  25677. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  25678. */
  25679. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  25680. {
  25681. int res = TEST_SKIPPED;
  25682. #if defined(HAVE_PKCS7)
  25683. int ret, i;
  25684. PKCS7* pkcs7;
  25685. WC_RNG rng;
  25686. byte outputHead[FOURK_BUF/2];
  25687. byte outputFoot[FOURK_BUF/2];
  25688. word32 outputHeadSz = (word32)sizeof(outputHead);
  25689. word32 outputFootSz = (word32)sizeof(outputFoot);
  25690. byte data[FOURK_BUF];
  25691. wc_HashAlg hash;
  25692. #ifdef NO_SHA
  25693. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25694. #else
  25695. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25696. #endif
  25697. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25698. word32 hashSz = wc_HashGetDigestSize(hashType);
  25699. #ifndef NO_RSA
  25700. #if defined(USE_CERT_BUFFERS_2048)
  25701. byte key[sizeof(client_key_der_2048)];
  25702. byte cert[sizeof(client_cert_der_2048)];
  25703. word32 keySz = (word32)sizeof(key);
  25704. word32 certSz = (word32)sizeof(cert);
  25705. XMEMSET(key, 0, keySz);
  25706. XMEMSET(cert, 0, certSz);
  25707. XMEMCPY(key, client_key_der_2048, keySz);
  25708. XMEMCPY(cert, client_cert_der_2048, certSz);
  25709. #elif defined(USE_CERT_BUFFERS_1024)
  25710. byte key[sizeof_client_key_der_1024];
  25711. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25712. word32 keySz = (word32)sizeof(key);
  25713. word32 certSz = (word32)sizeof(cert);
  25714. XMEMSET(key, 0, keySz);
  25715. XMEMSET(cert, 0, certSz);
  25716. XMEMCPY(key, client_key_der_1024, keySz);
  25717. XMEMCPY(cert, client_cert_der_1024, certSz);
  25718. #else
  25719. unsigned char cert[ONEK_BUF];
  25720. unsigned char key[ONEK_BUF];
  25721. XFILE fp;
  25722. int certSz;
  25723. int keySz;
  25724. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25725. AssertTrue((fp != XBADFILE));
  25726. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25727. XFCLOSE(fp);
  25728. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25729. AssertTrue(fp != XBADFILE);
  25730. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25731. XFCLOSE(fp);
  25732. #endif
  25733. #elif defined(HAVE_ECC)
  25734. #if defined(USE_CERT_BUFFERS_256)
  25735. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25736. unsigned char key[sizeof(ecc_clikey_der_256)];
  25737. int certSz = (int)sizeof(cert);
  25738. int keySz = (int)sizeof(key);
  25739. XMEMSET(cert, 0, certSz);
  25740. XMEMSET(key, 0, keySz);
  25741. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25742. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25743. #else
  25744. unsigned char cert[ONEK_BUF];
  25745. unsigned char key[ONEK_BUF];
  25746. XFILE fp;
  25747. int certSz, keySz;
  25748. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  25749. AssertTrue(fp != XBADFILE);
  25750. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  25751. XFCLOSE(fp);
  25752. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25753. AssertTrue(fp != XBADFILE);
  25754. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  25755. XFCLOSE(fp);
  25756. #endif
  25757. #endif
  25758. /* initialize large data with sequence */
  25759. for (i=0; i<(int)sizeof(data); i++)
  25760. data[i] = i & 0xff;
  25761. XMEMSET(outputHead, 0, outputHeadSz);
  25762. XMEMSET(outputFoot, 0, outputFootSz);
  25763. AssertIntEQ(wc_InitRng(&rng), 0);
  25764. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25765. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25766. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25767. pkcs7->content = NULL; /* not used for ex */
  25768. pkcs7->contentSz = (word32)sizeof(data);
  25769. pkcs7->privateKey = key;
  25770. pkcs7->privateKeySz = (word32)sizeof(key);
  25771. pkcs7->encryptOID = RSAk;
  25772. #ifdef NO_SHA
  25773. pkcs7->hashOID = SHA256h;
  25774. #else
  25775. pkcs7->hashOID = SHAh;
  25776. #endif
  25777. pkcs7->rng = &rng;
  25778. /* calculate hash for content */
  25779. ret = wc_HashInit(&hash, hashType);
  25780. if (ret == 0) {
  25781. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25782. if (ret == 0) {
  25783. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25784. }
  25785. wc_HashFree(&hash, hashType);
  25786. }
  25787. AssertIntEQ(ret, 0);
  25788. /* Perform PKCS7 sign using hash directly */
  25789. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25790. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  25791. AssertIntGT(outputHeadSz, 0);
  25792. AssertIntGT(outputFootSz, 0);
  25793. wc_PKCS7_Free(pkcs7);
  25794. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25795. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25796. /* required parameter even on verify when using _ex, if using outputHead
  25797. * and outputFoot */
  25798. pkcs7->contentSz = (word32)sizeof(data);
  25799. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25800. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  25801. wc_PKCS7_Free(pkcs7);
  25802. /* assembly complete PKCS7 sign and use normal verify */
  25803. {
  25804. byte* output = (byte*)XMALLOC(
  25805. outputHeadSz + sizeof(data) + outputFootSz,
  25806. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25807. word32 outputSz = 0;
  25808. AssertNotNull(output);
  25809. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  25810. outputSz += outputHeadSz;
  25811. XMEMCPY(&output[outputSz], data, sizeof(data));
  25812. outputSz += sizeof(data);
  25813. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  25814. outputSz += outputFootSz;
  25815. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25816. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25817. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25818. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25819. }
  25820. /* Pass in bad args. */
  25821. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25822. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25823. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25824. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25825. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25826. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25827. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25828. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25829. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25830. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25831. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25832. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  25833. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25834. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  25835. pkcs7->hashOID = 0; /* bad hashOID */
  25836. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25837. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25838. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25839. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25840. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25841. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25842. #ifndef NO_PKCS7_STREAM
  25843. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25844. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25845. #else
  25846. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25847. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  25848. #endif
  25849. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25850. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25851. #ifndef NO_PKCS7_STREAM
  25852. /* can pass in 0 buffer length with streaming API */
  25853. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25854. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25855. #else
  25856. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25857. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25858. #endif
  25859. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25860. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  25861. #ifndef NO_PKCS7_STREAM
  25862. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25863. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  25864. #else
  25865. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25866. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  25867. #endif
  25868. wc_PKCS7_Free(pkcs7);
  25869. wc_FreeRng(&rng);
  25870. res = TEST_RES_CHECK(1);
  25871. #endif
  25872. return res;
  25873. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  25874. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25875. /**
  25876. * Loads certs/keys from files or buffers into the argument buffers,
  25877. * helper function called by CreatePKCS7SignedData().
  25878. *
  25879. * Returns 0 on success, negative on error.
  25880. */
  25881. static int LoadPKCS7SignedDataCerts(
  25882. int useIntermediateCertChain, int pkAlgoType,
  25883. byte* intCARoot, word32* intCARootSz,
  25884. byte* intCA1, word32* intCA1Sz,
  25885. byte* intCA2, word32* intCA2Sz,
  25886. byte* cert, word32* certSz,
  25887. byte* key, word32* keySz)
  25888. {
  25889. int ret = 0;
  25890. FILE* fp = NULL;
  25891. #ifndef NO_RSA
  25892. const char* intCARootRSA = "./certs/ca-cert.der";
  25893. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  25894. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  25895. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  25896. const char* intServKeyRSA = "./certs/server-key.der";
  25897. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  25898. const char* cli1024Cert = "./certs/1024/client-cert.der";
  25899. const char* cli1024Key = "./certs/1024/client-key.der";
  25900. #endif
  25901. #endif
  25902. #ifdef HAVE_ECC
  25903. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  25904. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  25905. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  25906. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  25907. const char* intServKeyECC = "./certs/ecc-key.der";
  25908. #ifndef USE_CERT_BUFFERS_256
  25909. const char* cliEccCert = "./certs/client-ecc-cert.der";
  25910. const char* cliEccKey = "./certs/client-ecc-key.der";
  25911. #endif
  25912. #endif
  25913. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  25914. ((useIntermediateCertChain == 1) &&
  25915. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  25916. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  25917. return BAD_FUNC_ARG;
  25918. }
  25919. /* Read/load certs and keys to use for signing based on PK type and chain */
  25920. switch (pkAlgoType) {
  25921. #ifndef NO_RSA
  25922. case RSA_TYPE:
  25923. if (useIntermediateCertChain == 1) {
  25924. fp = XFOPEN(intCARootRSA, "rb");
  25925. AssertNotNull(fp);
  25926. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25927. XFCLOSE(fp);
  25928. AssertIntGT(*intCARootSz, 0);
  25929. fp = XFOPEN(intCA1RSA, "rb");
  25930. AssertNotNull(fp);
  25931. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25932. XFCLOSE(fp);
  25933. AssertIntGT(*intCA1Sz, 0);
  25934. fp = XFOPEN(intCA2RSA, "rb");
  25935. AssertNotNull(fp);
  25936. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25937. XFCLOSE(fp);
  25938. AssertIntGT(*intCA2Sz, 0);
  25939. fp = XFOPEN(intServCertRSA, "rb");
  25940. AssertNotNull(fp);
  25941. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25942. XFCLOSE(fp);
  25943. AssertIntGT(*certSz, 0);
  25944. fp = XFOPEN(intServKeyRSA, "rb");
  25945. AssertNotNull(fp);
  25946. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25947. XFCLOSE(fp);
  25948. AssertIntGT(*keySz, 0);
  25949. }
  25950. else {
  25951. #if defined(USE_CERT_BUFFERS_2048)
  25952. *keySz = sizeof_client_key_der_2048;
  25953. *certSz = sizeof_client_cert_der_2048;
  25954. XMEMCPY(key, client_key_der_2048, *keySz);
  25955. XMEMCPY(cert, client_cert_der_2048, *certSz);
  25956. #elif defined(USE_CERT_BUFFERS_1024)
  25957. *keySz = sizeof_client_key_der_1024;
  25958. *certSz = sizeof_client_cert_der_1024;
  25959. XMEMCPY(key, client_key_der_1024, *keySz);
  25960. XMEMCPY(cert, client_cert_der_1024, *certSz);
  25961. #else
  25962. fp = XFOPEN(cli1024Key, "rb");
  25963. AssertNotNull(fp);
  25964. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25965. XFCLOSE(fp);
  25966. AssertIntGT(*keySz, 0);
  25967. fp = XFOPEN(cli1024Cert, "rb");
  25968. AssertNotNull(fp);
  25969. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25970. XFCLOSE(fp);
  25971. AssertIntGT(*certSz, 0);
  25972. #endif /* USE_CERT_BUFFERS_2048 */
  25973. }
  25974. break;
  25975. #endif /* !NO_RSA */
  25976. #ifdef HAVE_ECC
  25977. case ECC_TYPE:
  25978. if (useIntermediateCertChain == 1) {
  25979. fp = XFOPEN(intCARootECC, "rb");
  25980. AssertNotNull(fp);
  25981. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25982. XFCLOSE(fp);
  25983. AssertIntGT(*intCARootSz, 0);
  25984. fp = XFOPEN(intCA1ECC, "rb");
  25985. AssertNotNull(fp);
  25986. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25987. XFCLOSE(fp);
  25988. AssertIntGT(*intCA1Sz, 0);
  25989. fp = XFOPEN(intCA2ECC, "rb");
  25990. AssertNotNull(fp);
  25991. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25992. XFCLOSE(fp);
  25993. AssertIntGT(*intCA2Sz, 0);
  25994. fp = XFOPEN(intServCertECC, "rb");
  25995. AssertNotNull(fp);
  25996. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25997. XFCLOSE(fp);
  25998. AssertIntGT(*certSz, 0);
  25999. fp = XFOPEN(intServKeyECC, "rb");
  26000. AssertNotNull(fp);
  26001. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26002. XFCLOSE(fp);
  26003. AssertIntGT(*keySz, 0);
  26004. }
  26005. else {
  26006. #if defined(USE_CERT_BUFFERS_256)
  26007. *keySz = sizeof_ecc_clikey_der_256;
  26008. *certSz = sizeof_cliecc_cert_der_256;
  26009. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  26010. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  26011. #else
  26012. fp = XFOPEN(cliEccKey, "rb");
  26013. AssertNotNull(fp);
  26014. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26015. XFCLOSE(fp);
  26016. AssertIntGT(*keySz, 0);
  26017. fp = XFOPEN(cliEccCert, "rb");
  26018. AssertNotNull(fp);
  26019. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26020. XFCLOSE(fp);
  26021. AssertIntGT(*certSz, 0);
  26022. #endif /* USE_CERT_BUFFERS_256 */
  26023. }
  26024. break;
  26025. #endif /* HAVE_ECC */
  26026. default:
  26027. WOLFSSL_MSG("Unsupported SignedData PK type");
  26028. ret = BAD_FUNC_ARG;
  26029. break;
  26030. }
  26031. return ret;
  26032. }
  26033. /**
  26034. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  26035. *
  26036. * output output buffer to place SignedData
  26037. * outputSz size of output buffer
  26038. * data data buffer to be signed
  26039. * dataSz size of data buffer
  26040. * withAttribs [1/0] include attributes in SignedData message
  26041. * detachedSig [1/0] create detached signature, no content
  26042. * useIntCertChain [1/0] use certificate chain and include intermediate and
  26043. * root CAs in bundle
  26044. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  26045. *
  26046. * Return size of bundle created on success, negative on error */
  26047. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  26048. byte* data, word32 dataSz,
  26049. int withAttribs, int detachedSig,
  26050. int useIntermediateCertChain,
  26051. int pkAlgoType)
  26052. {
  26053. int ret = 0;
  26054. WC_RNG rng;
  26055. PKCS7* pkcs7 = NULL;
  26056. static byte messageTypeOid[] =
  26057. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26058. 0x09, 0x02 };
  26059. static byte messageType[] = { 0x13, 2, '1', '9' };
  26060. PKCS7Attrib attribs[] =
  26061. {
  26062. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26063. sizeof(messageType) }
  26064. };
  26065. byte intCARoot[TWOK_BUF];
  26066. byte intCA1[TWOK_BUF];
  26067. byte intCA2[TWOK_BUF];
  26068. byte cert[TWOK_BUF];
  26069. byte key[TWOK_BUF];
  26070. word32 intCARootSz = sizeof(intCARoot);
  26071. word32 intCA1Sz = sizeof(intCA1);
  26072. word32 intCA2Sz = sizeof(intCA2);
  26073. word32 certSz = sizeof(cert);
  26074. word32 keySz = sizeof(key);
  26075. XMEMSET(intCARoot, 0, intCARootSz);
  26076. XMEMSET(intCA1, 0, intCA1Sz);
  26077. XMEMSET(intCA2, 0, intCA2Sz);
  26078. XMEMSET(cert, 0, certSz);
  26079. XMEMSET(key, 0, keySz);
  26080. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  26081. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  26082. cert, &certSz, key, &keySz);
  26083. AssertIntEQ(ret, 0);
  26084. XMEMSET(output, 0, outputSz);
  26085. AssertIntEQ(wc_InitRng(&rng), 0);
  26086. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26087. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26088. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26089. if (useIntermediateCertChain == 1) {
  26090. /* Add intermediate and root CA certs into SignedData Certs SET */
  26091. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  26092. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  26093. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  26094. }
  26095. pkcs7->content = data;
  26096. pkcs7->contentSz = dataSz;
  26097. pkcs7->privateKey = key;
  26098. pkcs7->privateKeySz = (word32)sizeof(key);
  26099. if (pkAlgoType == RSA_TYPE) {
  26100. pkcs7->encryptOID = RSAk;
  26101. }
  26102. else {
  26103. pkcs7->encryptOID = ECDSAk;
  26104. }
  26105. #ifdef NO_SHA
  26106. pkcs7->hashOID = SHA256h;
  26107. #else
  26108. pkcs7->hashOID = SHAh;
  26109. #endif
  26110. pkcs7->rng = &rng;
  26111. if (withAttribs) {
  26112. /* include a signed attribute */
  26113. pkcs7->signedAttribs = attribs;
  26114. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  26115. }
  26116. if (detachedSig) {
  26117. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  26118. }
  26119. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  26120. AssertIntGT(outputSz, 0);
  26121. wc_PKCS7_Free(pkcs7);
  26122. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26123. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26124. if (detachedSig) {
  26125. pkcs7->content = data;
  26126. pkcs7->contentSz = dataSz;
  26127. }
  26128. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26129. wc_PKCS7_Free(pkcs7);
  26130. wc_FreeRng(&rng);
  26131. return outputSz;
  26132. }
  26133. #endif
  26134. /*
  26135. * Testing wc_PKCS_VerifySignedData()
  26136. */
  26137. static int test_wc_PKCS7_VerifySignedData(void)
  26138. {
  26139. int res = TEST_SKIPPED;
  26140. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26141. PKCS7* pkcs7;
  26142. byte output[6000]; /* Large size needed for bundles with int CA certs */
  26143. word32 outputSz = sizeof(output);
  26144. byte data[] = "Test data to encode.";
  26145. byte badOut[1];
  26146. word32 badOutSz = 0;
  26147. byte badContent[] = "This is different content than was signed";
  26148. int ret;
  26149. wc_HashAlg hash;
  26150. #ifdef NO_SHA
  26151. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  26152. #else
  26153. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  26154. #endif
  26155. byte hashBuf[WC_MAX_DIGEST_SIZE];
  26156. word32 hashSz = wc_HashGetDigestSize(hashType);
  26157. #ifndef NO_RSA
  26158. PKCS7DecodedAttrib* decodedAttrib = NULL;
  26159. /* contentType OID (1.2.840.113549.1.9.3) */
  26160. static const byte contentTypeOid[] =
  26161. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  26162. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  26163. static const byte dataType[] =
  26164. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  26165. /* messageDigest OID (1.2.840.113549.1.9.4) */
  26166. static const byte messageDigestOid[] =
  26167. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  26168. #ifndef NO_ASN_TIME
  26169. /* signingTime OID () */
  26170. static const byte signingTimeOid[] =
  26171. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  26172. #endif
  26173. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26174. int dateLength = 0;
  26175. byte dateFormat;
  26176. const byte* datePart = NULL;
  26177. struct tm timearg;
  26178. time_t now;
  26179. struct tm* nowTm = NULL;
  26180. #ifdef NEED_TMP_TIME
  26181. struct tm tmpTimeStorage;
  26182. struct tm* tmpTime = &tmpTimeStorage;
  26183. #endif
  26184. #endif /* !NO_ASN && !NO_ASN_TIME */
  26185. /* Success test with RSA certs/key */
  26186. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26187. (word32)sizeof(data),
  26188. 0, 0, 0, RSA_TYPE)), 0);
  26189. /* calculate hash for content, used later */
  26190. ret = wc_HashInit(&hash, hashType);
  26191. if (ret == 0) {
  26192. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  26193. if (ret == 0) {
  26194. ret = wc_HashFinal(&hash, hashType, hashBuf);
  26195. }
  26196. wc_HashFree(&hash, hashType);
  26197. }
  26198. AssertIntEQ(ret, 0);
  26199. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26200. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26201. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26202. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26203. /* Check that decoded signed attributes are correct */
  26204. /* messageDigest should be first */
  26205. decodedAttrib = pkcs7->decodedAttrib;
  26206. AssertNotNull(decodedAttrib);
  26207. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  26208. AssertIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  26209. decodedAttrib->oidSz), 0);
  26210. /* + 2 for OCTET STRING and length bytes */
  26211. AssertIntEQ(decodedAttrib->valueSz, hashSz + 2);
  26212. AssertNotNull(decodedAttrib->value);
  26213. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  26214. #ifndef NO_ASN_TIME
  26215. /* signingTime should be second */
  26216. decodedAttrib = decodedAttrib->next;
  26217. AssertNotNull(decodedAttrib);
  26218. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  26219. AssertIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  26220. decodedAttrib->oidSz), 0);
  26221. AssertIntGT(decodedAttrib->valueSz, 0);
  26222. AssertNotNull(decodedAttrib->value);
  26223. #endif
  26224. /* Verify signingTime if ASN and time are available */
  26225. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26226. AssertIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  26227. &datePart, &dateFormat, &dateLength), 0);
  26228. AssertNotNull(datePart);
  26229. AssertIntGT(dateLength, 0);
  26230. XMEMSET(&timearg, 0, sizeof(timearg));
  26231. AssertIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  26232. &timearg), 0);
  26233. /* Get current time and compare year/month/day against attribute value */
  26234. AssertIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  26235. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  26236. AssertNotNull(nowTm);
  26237. AssertIntEQ(timearg.tm_year, nowTm->tm_year);
  26238. AssertIntEQ(timearg.tm_mon, nowTm->tm_mon);
  26239. AssertIntEQ(timearg.tm_mday, nowTm->tm_mday);
  26240. #endif /* !NO_ASN && !NO_ASN_TIME */
  26241. /* contentType should be third */
  26242. decodedAttrib = decodedAttrib->next;
  26243. AssertNotNull(decodedAttrib);
  26244. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  26245. AssertIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  26246. decodedAttrib->oidSz), 0);
  26247. AssertIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  26248. AssertNotNull(decodedAttrib->value);
  26249. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType,
  26250. sizeof(dataType)), 0);
  26251. #endif /* !NO_RSA */
  26252. #ifdef HAVE_ECC
  26253. #ifndef NO_RSA
  26254. wc_PKCS7_Free(pkcs7);
  26255. #endif
  26256. /* Success test with ECC certs/key */
  26257. outputSz = sizeof(output);
  26258. XMEMSET(output, 0, outputSz);
  26259. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26260. (word32)sizeof(data),
  26261. 0, 0, 0, ECC_TYPE)), 0);
  26262. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26263. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26264. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26265. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26266. #endif /* HAVE_ECC */
  26267. /* Test bad args. */
  26268. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26269. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  26270. BAD_FUNC_ARG);
  26271. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  26272. BAD_FUNC_ARG);
  26273. #ifndef NO_PKCS7_STREAM
  26274. /* can pass in 0 buffer length with streaming API */
  26275. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26276. badOutSz), WC_PKCS7_WANT_READ_E);
  26277. #else
  26278. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26279. badOutSz), BAD_FUNC_ARG);
  26280. #endif
  26281. wc_PKCS7_Free(pkcs7);
  26282. #endif /* !NO_RSA || HAVE_ECC */
  26283. /* Invalid content should error, use detached signature so we can
  26284. * easily change content */
  26285. #ifndef NO_RSA
  26286. /* Try RSA certs/key/sig first */
  26287. outputSz = sizeof(output);
  26288. XMEMSET(output, 0, outputSz);
  26289. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26290. (word32)sizeof(data),
  26291. 1, 1, 0, RSA_TYPE)), 0);
  26292. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26293. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26294. pkcs7->content = badContent;
  26295. pkcs7->contentSz = sizeof(badContent);
  26296. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26297. SIG_VERIFY_E);
  26298. wc_PKCS7_Free(pkcs7);
  26299. /* Test success case with detached signature and valid content */
  26300. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26301. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26302. pkcs7->content = data;
  26303. pkcs7->contentSz = sizeof(data);
  26304. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26305. wc_PKCS7_Free(pkcs7);
  26306. /* verify using pre-computed content digest only (no content) */
  26307. {
  26308. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26309. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26310. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26311. output, outputSz,
  26312. NULL, 0), 0);
  26313. wc_PKCS7_Free(pkcs7);
  26314. }
  26315. #endif /* !NO_RSA */
  26316. #ifdef HAVE_ECC
  26317. /* Try ECC certs/key/sig next */
  26318. outputSz = sizeof(output);
  26319. XMEMSET(output, 0, outputSz);
  26320. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26321. (word32)sizeof(data),
  26322. 1, 1, 0, ECC_TYPE)), 0);
  26323. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26324. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26325. pkcs7->content = badContent;
  26326. pkcs7->contentSz = sizeof(badContent);
  26327. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26328. SIG_VERIFY_E);
  26329. wc_PKCS7_Free(pkcs7);
  26330. /* Test success case with detached signature and valid content */
  26331. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26332. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26333. pkcs7->content = data;
  26334. pkcs7->contentSz = sizeof(data);
  26335. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26336. wc_PKCS7_Free(pkcs7);
  26337. /* verify using pre-computed content digest only (no content) */
  26338. {
  26339. /* calculate hash for content */
  26340. ret = wc_HashInit(&hash, hashType);
  26341. if (ret == 0) {
  26342. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  26343. if (ret == 0) {
  26344. ret = wc_HashFinal(&hash, hashType, hashBuf);
  26345. }
  26346. wc_HashFree(&hash, hashType);
  26347. }
  26348. AssertIntEQ(ret, 0);
  26349. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26350. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26351. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26352. output, outputSz,
  26353. NULL, 0), 0);
  26354. wc_PKCS7_Free(pkcs7);
  26355. }
  26356. #endif
  26357. /* Test verify on signedData containing intermediate/root CA certs */
  26358. #ifndef NO_RSA
  26359. outputSz = sizeof(output);
  26360. XMEMSET(output, 0, outputSz);
  26361. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26362. (word32)sizeof(data),
  26363. 0, 0, 1, RSA_TYPE)), 0);
  26364. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26365. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26366. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26367. wc_PKCS7_Free(pkcs7);
  26368. #endif /* !NO_RSA */
  26369. #ifdef HAVE_ECC
  26370. outputSz = sizeof(output);
  26371. XMEMSET(output, 0, outputSz);
  26372. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  26373. (word32)sizeof(data),
  26374. 0, 0, 1, ECC_TYPE)), 0);
  26375. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26376. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26377. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26378. wc_PKCS7_Free(pkcs7);
  26379. #endif /* HAVE_ECC */
  26380. res = TEST_RES_CHECK(1);
  26381. #endif
  26382. return res;
  26383. } /* END test_wc_PKCS7_VerifySignedData() */
  26384. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  26385. !defined(NO_AES_256)
  26386. static const byte defKey[] = {
  26387. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26388. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26389. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26390. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26391. };
  26392. static byte aesHandle[32]; /* simulated hardware key handle */
  26393. /* return 0 on success */
  26394. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  26395. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  26396. byte* in, int inSz, byte* out, void* usrCtx)
  26397. {
  26398. int ret;
  26399. Aes aes;
  26400. if (usrCtx == NULL) {
  26401. /* no simulated handle passed in */
  26402. return -1;
  26403. }
  26404. switch (encryptOID) {
  26405. case AES256CBCb:
  26406. if (ivSz != AES_BLOCK_SIZE)
  26407. return BAD_FUNC_ARG;
  26408. break;
  26409. default:
  26410. WOLFSSL_MSG("Unsupported content cipher type for test");
  26411. return ALGO_ID_E;
  26412. };
  26413. /* simulate using handle to get key */
  26414. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  26415. if (ret == 0) {
  26416. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  26417. if (ret == 0)
  26418. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  26419. wc_AesFree(&aes);
  26420. }
  26421. (void)aad;
  26422. (void)aadSz;
  26423. (void)authTag;
  26424. (void)authTagSz;
  26425. (void)pkcs7;
  26426. return ret;
  26427. }
  26428. /* returns key size on success */
  26429. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  26430. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  26431. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  26432. {
  26433. int ret = -1;
  26434. if (out == NULL)
  26435. return BAD_FUNC_ARG;
  26436. if (keyId[0] != 0x00) {
  26437. return -1;
  26438. }
  26439. if (type != (int)PKCS7_KEKRI) {
  26440. return -1;
  26441. }
  26442. switch (keyWrapAlgo) {
  26443. case AES256_WRAP:
  26444. /* simulate setting a handle for later decryption but use key
  26445. * as handle in the test case here */
  26446. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  26447. aesHandle, sizeof(aesHandle), NULL);
  26448. if (ret < 0)
  26449. return ret;
  26450. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  26451. if (ret < 0)
  26452. return ret;
  26453. /* return key size on success */
  26454. return sizeof(defKey);
  26455. default:
  26456. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  26457. return BAD_KEYWRAP_ALG_E;
  26458. };
  26459. (void)cekSz;
  26460. (void)cek;
  26461. (void)outSz;
  26462. (void)keyIdSz;
  26463. (void)direction;
  26464. (void)orginKey; /* used with KAKRI */
  26465. (void)orginKeySz;
  26466. return ret;
  26467. }
  26468. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  26469. /*
  26470. * Testing wc_PKCS7_EncodeEnvelopedData()
  26471. */
  26472. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  26473. {
  26474. int res = TEST_SKIPPED;
  26475. #if defined(HAVE_PKCS7)
  26476. PKCS7* pkcs7;
  26477. #ifdef ECC_TIMING_RESISTANT
  26478. WC_RNG rng;
  26479. #endif
  26480. word32 tempWrd32 = 0;
  26481. byte* tmpBytePtr = NULL;
  26482. const char input[] = "Test data to encode.";
  26483. int i;
  26484. int testSz = 0;
  26485. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  26486. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26487. byte* rsaCert = NULL;
  26488. byte* rsaPrivKey = NULL;
  26489. word32 rsaCertSz;
  26490. word32 rsaPrivKeySz;
  26491. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  26492. !defined(USE_CERT_BUFFERS_2048) )
  26493. static const char* rsaClientCert = "./certs/client-cert.der";
  26494. static const char* rsaClientKey = "./certs/client-key.der";
  26495. rsaCertSz = (word32)sizeof(rsaClientCert);
  26496. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  26497. #endif
  26498. #endif
  26499. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26500. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26501. byte* eccCert = NULL;
  26502. byte* eccPrivKey = NULL;
  26503. word32 eccCertSz;
  26504. word32 eccPrivKeySz;
  26505. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  26506. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  26507. static const char* eccClientKey = "./certs/ecc-client-key.der";
  26508. #endif
  26509. #endif
  26510. /* Generic buffer size. */
  26511. byte output[ONEK_BUF];
  26512. byte decoded[sizeof(input)/sizeof(char)];
  26513. int decodedSz = 0;
  26514. #ifndef NO_FILESYSTEM
  26515. XFILE certFile;
  26516. XFILE keyFile;
  26517. #endif
  26518. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26519. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26520. /* RSA certs and keys. */
  26521. #if defined(USE_CERT_BUFFERS_1024)
  26522. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  26523. /* Allocate buffer space. */
  26524. AssertNotNull(rsaCert =
  26525. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26526. /* Init buffer. */
  26527. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  26528. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  26529. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  26530. DYNAMIC_TYPE_TMP_BUFFER));
  26531. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  26532. #elif defined(USE_CERT_BUFFERS_2048)
  26533. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  26534. /* Allocate buffer */
  26535. AssertNotNull(rsaCert =
  26536. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26537. /* Init buffer. */
  26538. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  26539. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  26540. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  26541. DYNAMIC_TYPE_TMP_BUFFER));
  26542. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  26543. #else
  26544. /* File system. */
  26545. certFile = XFOPEN(rsaClientCert, "rb");
  26546. AssertTrue(certFile != XBADFILE);
  26547. rsaCertSz = (word32)FOURK_BUF;
  26548. AssertNotNull(rsaCert =
  26549. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26550. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  26551. XFCLOSE(certFile);
  26552. keyFile = XFOPEN(rsaClientKey, "rb");
  26553. AssertTrue(keyFile != XBADFILE);
  26554. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26555. DYNAMIC_TYPE_TMP_BUFFER));
  26556. rsaPrivKeySz = (word32)FOURK_BUF;
  26557. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  26558. XFCLOSE(keyFile);
  26559. #endif /* USE_CERT_BUFFERS */
  26560. #endif /* NO_RSA */
  26561. /* ECC */
  26562. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26563. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26564. #ifdef USE_CERT_BUFFERS_256
  26565. AssertNotNull(eccCert =
  26566. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26567. /* Init buffer. */
  26568. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  26569. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  26570. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  26571. DYNAMIC_TYPE_TMP_BUFFER));
  26572. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  26573. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  26574. #else /* File system. */
  26575. certFile = XFOPEN(eccClientCert, "rb");
  26576. AssertTrue(certFile != XBADFILE);
  26577. eccCertSz = (word32)FOURK_BUF;
  26578. AssertNotNull(eccCert =
  26579. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26580. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  26581. XFCLOSE(certFile);
  26582. keyFile = XFOPEN(eccClientKey, "rb");
  26583. AssertTrue(keyFile != XBADFILE);
  26584. eccPrivKeySz = (word32)FOURK_BUF;
  26585. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26586. DYNAMIC_TYPE_TMP_BUFFER));
  26587. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  26588. XFCLOSE(keyFile);
  26589. #endif /* USE_CERT_BUFFERS_256 */
  26590. #endif /* END HAVE_ECC */
  26591. #ifndef NO_FILESYSTEM
  26592. /* Silence. */
  26593. (void)keyFile;
  26594. (void)certFile;
  26595. #endif
  26596. {
  26597. const pkcs7EnvelopedVector testVectors[] = {
  26598. /* DATA is a global variable defined in the makefile. */
  26599. #if !defined(NO_RSA)
  26600. #ifndef NO_DES3
  26601. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  26602. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26603. #endif /* NO_DES3 */
  26604. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26605. #ifndef NO_AES_128
  26606. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26607. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26608. #endif
  26609. #ifndef NO_AES_192
  26610. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  26611. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26612. #endif
  26613. #ifndef NO_AES_256
  26614. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26615. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26616. #endif
  26617. #endif /* NO_AES && HAVE_AES_CBC */
  26618. #endif /* NO_RSA */
  26619. #if defined(HAVE_ECC)
  26620. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26621. #if !defined(NO_SHA) && !defined(NO_AES_128)
  26622. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26623. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  26624. eccCertSz, eccPrivKey, eccPrivKeySz},
  26625. #endif
  26626. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  26627. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26628. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  26629. eccCertSz, eccPrivKey, eccPrivKeySz},
  26630. #endif
  26631. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  26632. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26633. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  26634. eccCertSz, eccPrivKey, eccPrivKeySz},
  26635. #endif
  26636. #endif /* NO_AES && HAVE_AES_CBC*/
  26637. #endif /* END HAVE_ECC */
  26638. }; /* END pkcs7EnvelopedVector */
  26639. #ifdef ECC_TIMING_RESISTANT
  26640. AssertIntEQ(wc_InitRng(&rng), 0);
  26641. #endif
  26642. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26643. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26644. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  26645. for (i = 0; i < testSz; i++) {
  26646. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  26647. (word32)(testVectors + i)->certSz), 0);
  26648. #ifdef ECC_TIMING_RESISTANT
  26649. pkcs7->rng = &rng;
  26650. #endif
  26651. pkcs7->content = (byte*)(testVectors + i)->content;
  26652. pkcs7->contentSz = (testVectors + i)->contentSz;
  26653. pkcs7->contentOID = (testVectors + i)->contentOID;
  26654. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  26655. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  26656. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  26657. pkcs7->privateKey = (testVectors + i)->privateKey;
  26658. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  26659. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26660. (word32)sizeof(output)), 0);
  26661. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26662. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  26663. AssertIntGE(decodedSz, 0);
  26664. /* Verify the size of each buffer. */
  26665. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  26666. /* Don't free the last time through the loop. */
  26667. if (i < testSz - 1) {
  26668. wc_PKCS7_Free(pkcs7);
  26669. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26670. }
  26671. } /* END test loop. */
  26672. }
  26673. /* Test bad args. */
  26674. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  26675. (word32)sizeof(output)), BAD_FUNC_ARG);
  26676. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  26677. (word32)sizeof(output)), BAD_FUNC_ARG);
  26678. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  26679. /* Decode. */
  26680. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  26681. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26682. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26683. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26684. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26685. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  26686. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  26687. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26688. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  26689. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26690. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  26691. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  26692. /* only a failure for KARI test cases */
  26693. tempWrd32 = pkcs7->singleCertSz;
  26694. pkcs7->singleCertSz = 0;
  26695. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26696. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26697. pkcs7->singleCertSz = tempWrd32;
  26698. tmpBytePtr = pkcs7->singleCert;
  26699. pkcs7->singleCert = NULL;
  26700. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26701. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26702. pkcs7->singleCert = tmpBytePtr;
  26703. #endif
  26704. tempWrd32 = pkcs7->privateKeySz;
  26705. pkcs7->privateKeySz = 0;
  26706. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26707. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26708. pkcs7->privateKeySz = tempWrd32;
  26709. tmpBytePtr = pkcs7->privateKey;
  26710. pkcs7->privateKey = NULL;
  26711. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26712. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26713. pkcs7->privateKey = tmpBytePtr;
  26714. wc_PKCS7_Free(pkcs7);
  26715. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  26716. /* test of decrypt callback with KEKRI enveloped data */
  26717. {
  26718. int envelopedSz;
  26719. const byte keyId[] = { 0x00 };
  26720. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26721. pkcs7->content = (byte*)input;
  26722. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  26723. pkcs7->contentOID = DATA;
  26724. pkcs7->encryptOID = AES256CBCb;
  26725. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  26726. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  26727. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  26728. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  26729. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26730. (word32)sizeof(output))), 0);
  26731. wc_PKCS7_Free(pkcs7);
  26732. /* decode envelopedData */
  26733. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26734. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  26735. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  26736. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26737. envelopedSz, decoded, sizeof(decoded))), 0);
  26738. wc_PKCS7_Free(pkcs7);
  26739. }
  26740. #endif /* !NO_AES && !NO_AES_256 */
  26741. #ifndef NO_RSA
  26742. if (rsaCert) {
  26743. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26744. }
  26745. if (rsaPrivKey) {
  26746. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26747. }
  26748. #endif /*NO_RSA */
  26749. #ifdef HAVE_ECC
  26750. if (eccCert) {
  26751. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26752. }
  26753. if (eccPrivKey) {
  26754. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26755. }
  26756. #endif /* HAVE_ECC */
  26757. #ifdef ECC_TIMING_RESISTANT
  26758. wc_FreeRng(&rng);
  26759. #endif
  26760. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  26761. !defined(NO_RSA) && !defined(NO_SHA)
  26762. {
  26763. byte out[7];
  26764. byte *cms;
  26765. word32 cmsSz;
  26766. XFILE cmsFile;
  26767. XMEMSET(out, 0, sizeof(out));
  26768. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26769. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  26770. AssertTrue(cmsFile != XBADFILE);
  26771. cmsSz = (word32)FOURK_BUF;
  26772. AssertNotNull(cms =
  26773. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26774. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  26775. XFCLOSE(cmsFile);
  26776. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  26777. sizeof_client_cert_der_2048), 0);
  26778. pkcs7->privateKey = (byte*)client_key_der_2048;
  26779. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  26780. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26781. 2), 0);
  26782. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26783. sizeof(out)), 0);
  26784. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26785. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  26786. wc_PKCS7_Free(pkcs7);
  26787. }
  26788. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  26789. res = TEST_RES_CHECK(1);
  26790. #endif /* HAVE_PKCS7 */
  26791. return res;
  26792. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  26793. /*
  26794. * Testing wc_PKCS7_EncodeEncryptedData()
  26795. */
  26796. static int test_wc_PKCS7_EncodeEncryptedData(void)
  26797. {
  26798. int res = TEST_SKIPPED;
  26799. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26800. PKCS7* pkcs7 = NULL;
  26801. byte* tmpBytePtr = NULL;
  26802. byte encrypted[TWOK_BUF];
  26803. byte decoded[TWOK_BUF];
  26804. word32 tmpWrd32 = 0;
  26805. int tmpInt = 0;
  26806. int decodedSz;
  26807. int encryptedSz;
  26808. int testSz;
  26809. int i;
  26810. const byte data[] = { /* Hello World */
  26811. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26812. 0x72,0x6c,0x64
  26813. };
  26814. #ifndef NO_DES3
  26815. byte desKey[] = {
  26816. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  26817. };
  26818. byte des3Key[] = {
  26819. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  26820. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  26821. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  26822. };
  26823. #endif
  26824. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26825. #ifndef NO_AES_128
  26826. byte aes128Key[] = {
  26827. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26828. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26829. };
  26830. #endif
  26831. #ifndef NO_AES_192
  26832. byte aes192Key[] = {
  26833. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26834. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26835. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26836. };
  26837. #endif
  26838. #ifndef NO_AES_256
  26839. byte aes256Key[] = {
  26840. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26841. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26842. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26843. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26844. };
  26845. #endif
  26846. #endif /* !NO_AES && HAVE_AES_CBC */
  26847. const pkcs7EncryptedVector testVectors[] =
  26848. {
  26849. #ifndef NO_DES3
  26850. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  26851. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  26852. #endif /* !NO_DES3 */
  26853. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26854. #ifndef NO_AES_128
  26855. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  26856. sizeof(aes128Key)},
  26857. #endif
  26858. #ifndef NO_AES_192
  26859. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  26860. sizeof(aes192Key)},
  26861. #endif
  26862. #ifndef NO_AES_256
  26863. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  26864. sizeof(aes256Key)},
  26865. #endif
  26866. #endif /* !NO_AES && HAVE_AES_CBC */
  26867. };
  26868. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  26869. for (i = 0; i < testSz; i++) {
  26870. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26871. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26872. pkcs7->content = (byte*)testVectors[i].content;
  26873. pkcs7->contentSz = testVectors[i].contentSz;
  26874. pkcs7->contentOID = testVectors[i].contentOID;
  26875. pkcs7->encryptOID = testVectors[i].encryptOID;
  26876. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  26877. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  26878. pkcs7->heap = HEAP_HINT;
  26879. /* encode encryptedData */
  26880. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26881. sizeof(encrypted));
  26882. AssertIntGT(encryptedSz, 0);
  26883. /* Decode encryptedData */
  26884. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26885. decoded, sizeof(decoded));
  26886. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  26887. /* Keep values for last itr. */
  26888. if (i < testSz - 1) {
  26889. wc_PKCS7_Free(pkcs7);
  26890. }
  26891. }
  26892. if (pkcs7 == NULL || testSz == 0) {
  26893. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26894. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26895. }
  26896. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26897. sizeof(encrypted)),BAD_FUNC_ARG);
  26898. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26899. sizeof(encrypted)), BAD_FUNC_ARG);
  26900. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26901. 0), BAD_FUNC_ARG);
  26902. /* Testing the struct. */
  26903. tmpBytePtr = pkcs7->content;
  26904. pkcs7->content = NULL;
  26905. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26906. sizeof(encrypted)), BAD_FUNC_ARG);
  26907. pkcs7->content = tmpBytePtr;
  26908. tmpWrd32 = pkcs7->contentSz;
  26909. pkcs7->contentSz = 0;
  26910. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26911. sizeof(encrypted)), BAD_FUNC_ARG);
  26912. pkcs7->contentSz = tmpWrd32;
  26913. tmpInt = pkcs7->encryptOID;
  26914. pkcs7->encryptOID = 0;
  26915. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26916. sizeof(encrypted)), BAD_FUNC_ARG);
  26917. pkcs7->encryptOID = tmpInt;
  26918. tmpBytePtr = pkcs7->encryptionKey;
  26919. pkcs7->encryptionKey = NULL;
  26920. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26921. sizeof(encrypted)), BAD_FUNC_ARG);
  26922. pkcs7->encryptionKey = tmpBytePtr;
  26923. tmpWrd32 = pkcs7->encryptionKeySz;
  26924. pkcs7->encryptionKeySz = 0;
  26925. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26926. sizeof(encrypted)), BAD_FUNC_ARG);
  26927. pkcs7->encryptionKeySz = tmpWrd32;
  26928. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  26929. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26930. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  26931. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26932. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26933. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26934. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26935. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26936. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26937. decoded, 0), BAD_FUNC_ARG);
  26938. /* Test struct fields */
  26939. tmpBytePtr = pkcs7->encryptionKey;
  26940. pkcs7->encryptionKey = NULL;
  26941. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26942. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26943. pkcs7->encryptionKey = tmpBytePtr;
  26944. pkcs7->encryptionKeySz = 0;
  26945. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26946. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26947. wc_PKCS7_Free(pkcs7);
  26948. res = TEST_RES_CHECK(1);
  26949. #endif
  26950. return res;
  26951. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26952. /*
  26953. * Testing wc_PKCS7_Degenerate()
  26954. */
  26955. static int test_wc_PKCS7_Degenerate(void)
  26956. {
  26957. int res = TEST_SKIPPED;
  26958. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26959. PKCS7* pkcs7;
  26960. char fName[] = "./certs/test-degenerate.p7b";
  26961. XFILE f;
  26962. byte der[4096];
  26963. word32 derSz;
  26964. int ret;
  26965. AssertNotNull(f = XFOPEN(fName, "rb"));
  26966. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26967. derSz = (word32)ret;
  26968. XFCLOSE(f);
  26969. /* test degenerate success */
  26970. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26971. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26972. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26973. #ifndef NO_RSA
  26974. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26975. #else
  26976. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26977. #endif
  26978. wc_PKCS7_Free(pkcs7);
  26979. /* test with turning off degenerate cases */
  26980. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26981. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26982. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26983. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26984. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  26985. wc_PKCS7_Free(pkcs7);
  26986. res = TEST_RES_CHECK(1);
  26987. #endif
  26988. return res;
  26989. } /* END test_wc_PKCS7_Degenerate() */
  26990. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26991. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26992. static byte berContent[] = {
  26993. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26994. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26995. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26996. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26997. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26998. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26999. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  27000. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  27001. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  27002. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  27003. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  27004. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  27005. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  27006. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  27007. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  27008. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  27009. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  27010. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  27011. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  27012. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  27013. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  27014. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27015. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  27016. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  27017. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  27018. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  27019. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  27020. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  27021. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  27022. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  27023. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  27024. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  27025. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  27026. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  27027. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  27028. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  27029. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  27030. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  27031. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  27032. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  27033. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  27034. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  27035. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  27036. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  27037. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  27038. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  27039. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  27040. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  27041. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  27042. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  27043. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  27044. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  27045. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  27046. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  27047. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  27048. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  27049. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  27050. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  27051. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  27052. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  27053. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  27054. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  27055. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  27056. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  27057. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  27058. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  27059. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  27060. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  27061. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  27062. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  27063. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  27064. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  27065. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  27066. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  27067. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  27068. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  27069. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  27070. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  27071. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  27072. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  27073. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  27074. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  27075. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  27076. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  27077. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  27078. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  27079. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  27080. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  27081. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  27082. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  27083. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  27084. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  27085. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  27086. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  27087. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  27088. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  27089. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  27090. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  27091. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  27092. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  27093. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  27094. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  27095. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  27096. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  27097. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  27098. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  27099. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  27100. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  27101. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  27102. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  27103. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  27104. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  27105. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  27106. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  27107. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  27108. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  27109. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  27110. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  27111. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  27112. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  27113. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  27114. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  27115. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  27116. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  27117. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  27118. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  27119. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  27120. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  27121. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  27122. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  27123. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  27124. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  27125. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  27126. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  27127. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  27128. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  27129. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  27130. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  27131. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  27132. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  27133. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  27134. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  27135. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  27136. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  27137. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  27138. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  27139. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  27140. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  27141. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  27142. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  27143. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  27144. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  27145. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  27146. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  27147. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  27148. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  27149. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  27150. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  27151. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  27152. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  27153. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  27154. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  27155. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  27156. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  27157. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  27158. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  27159. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  27160. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  27161. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  27162. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  27163. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  27164. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  27165. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  27166. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  27167. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  27168. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  27169. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  27170. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  27171. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  27172. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  27173. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  27174. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  27175. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  27176. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  27177. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  27178. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  27179. 0x00, 0x00, 0x00, 0x00, 0x00
  27180. };
  27181. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  27182. * !NO_DES3 && !NO_SHA
  27183. */
  27184. /*
  27185. * Testing wc_PKCS7_BER()
  27186. */
  27187. static int test_wc_PKCS7_BER(void)
  27188. {
  27189. int res = TEST_SKIPPED;
  27190. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  27191. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  27192. PKCS7* pkcs7;
  27193. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  27194. XFILE f;
  27195. byte der[4096];
  27196. #ifndef NO_DES3
  27197. byte decoded[2048];
  27198. #endif
  27199. word32 derSz;
  27200. int ret;
  27201. AssertNotNull(f = XFOPEN(fName, "rb"));
  27202. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  27203. derSz = (word32)ret;
  27204. XFCLOSE(f);
  27205. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27206. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27207. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27208. #ifndef NO_RSA
  27209. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27210. #else
  27211. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27212. #endif
  27213. wc_PKCS7_Free(pkcs7);
  27214. #ifndef NO_DES3
  27215. /* decode BER content */
  27216. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  27217. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  27218. derSz = (word32)ret;
  27219. XFCLOSE(f);
  27220. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27221. #ifndef NO_RSA
  27222. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27223. #else
  27224. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27225. #endif
  27226. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  27227. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  27228. derSz = (word32)ret;
  27229. XFCLOSE(f);
  27230. pkcs7->privateKey = der;
  27231. pkcs7->privateKeySz = derSz;
  27232. #ifndef NO_RSA
  27233. #ifdef WOLFSSL_SP_MATH
  27234. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27235. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  27236. #else
  27237. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27238. sizeof(berContent), decoded, sizeof(decoded)), 0);
  27239. #endif
  27240. #else
  27241. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27242. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  27243. #endif
  27244. wc_PKCS7_Free(pkcs7);
  27245. #endif /* !NO_DES3 */
  27246. res = TEST_RES_CHECK(1);
  27247. #endif
  27248. return res;
  27249. } /* END test_wc_PKCS7_BER() */
  27250. static int test_PKCS7_signed_enveloped(void)
  27251. {
  27252. int res = TEST_SKIPPED;
  27253. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  27254. !defined(NO_FILESYSTEM)
  27255. XFILE f;
  27256. PKCS7* pkcs7;
  27257. #ifdef HAVE_AES_CBC
  27258. PKCS7* inner;
  27259. #endif
  27260. void* pt;
  27261. WC_RNG rng;
  27262. unsigned char key[FOURK_BUF/2];
  27263. unsigned char cert[FOURK_BUF/2];
  27264. unsigned char env[FOURK_BUF];
  27265. int envSz = FOURK_BUF;
  27266. int keySz;
  27267. int certSz;
  27268. unsigned char sig[FOURK_BUF * 2];
  27269. int sigSz = FOURK_BUF * 2;
  27270. #ifdef HAVE_AES_CBC
  27271. unsigned char decoded[FOURK_BUF];
  27272. int decodedSz = FOURK_BUF;
  27273. #endif
  27274. /* load cert */
  27275. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  27276. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  27277. XFCLOSE(f);
  27278. /* load key */
  27279. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  27280. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  27281. XFCLOSE(f);
  27282. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  27283. /* sign cert for envelope */
  27284. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27285. AssertIntEQ(wc_InitRng(&rng), 0);
  27286. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  27287. pkcs7->content = cert;
  27288. pkcs7->contentSz = certSz;
  27289. pkcs7->contentOID = DATA;
  27290. pkcs7->privateKey = key;
  27291. pkcs7->privateKeySz = keySz;
  27292. pkcs7->encryptOID = RSAk;
  27293. pkcs7->hashOID = SHA256h;
  27294. pkcs7->rng = &rng;
  27295. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  27296. wc_PKCS7_Free(pkcs7);
  27297. wc_FreeRng(&rng);
  27298. #ifdef HAVE_AES_CBC
  27299. /* create envelope */
  27300. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27301. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  27302. pkcs7->content = sig;
  27303. pkcs7->contentSz = sigSz;
  27304. pkcs7->contentOID = DATA;
  27305. pkcs7->encryptOID = AES256CBCb;
  27306. pkcs7->privateKey = key;
  27307. pkcs7->privateKeySz = keySz;
  27308. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  27309. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  27310. wc_PKCS7_Free(pkcs7);
  27311. #endif
  27312. /* create bad signed enveloped data */
  27313. sigSz = FOURK_BUF * 2;
  27314. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27315. AssertIntEQ(wc_InitRng(&rng), 0);
  27316. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  27317. pkcs7->content = env;
  27318. pkcs7->contentSz = envSz;
  27319. pkcs7->contentOID = DATA;
  27320. pkcs7->privateKey = key;
  27321. pkcs7->privateKeySz = keySz;
  27322. pkcs7->encryptOID = RSAk;
  27323. pkcs7->hashOID = SHA256h;
  27324. pkcs7->rng = &rng;
  27325. /* Set no certs in bundle for this test. Hang on to the pointer though to
  27326. * free it later. */
  27327. pt = (void*)pkcs7->certList;
  27328. pkcs7->certList = NULL; /* no certs in bundle */
  27329. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  27330. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  27331. wc_PKCS7_Free(pkcs7);
  27332. /* check verify fails */
  27333. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27334. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27335. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  27336. PKCS7_SIGNEEDS_CHECK);
  27337. /* try verifying the signature manually */
  27338. {
  27339. RsaKey rKey;
  27340. word32 idx = 0;
  27341. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  27342. WC_MAX_DIGEST_SIZE];
  27343. int digestSz;
  27344. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  27345. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  27346. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  27347. digest, sizeof(digest), &rKey);
  27348. AssertIntGT(digestSz, 0);
  27349. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  27350. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  27351. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  27352. /* verify was success */
  27353. }
  27354. wc_PKCS7_Free(pkcs7);
  27355. /* initializing the PKCS7 struct with the signing certificate should pass */
  27356. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27357. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  27358. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  27359. wc_PKCS7_Free(pkcs7);
  27360. /* create valid degenerate bundle */
  27361. sigSz = FOURK_BUF * 2;
  27362. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27363. pkcs7->content = env;
  27364. pkcs7->contentSz = envSz;
  27365. pkcs7->contentOID = DATA;
  27366. pkcs7->privateKey = key;
  27367. pkcs7->privateKeySz = keySz;
  27368. pkcs7->encryptOID = RSAk;
  27369. pkcs7->hashOID = SHA256h;
  27370. pkcs7->rng = &rng;
  27371. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  27372. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  27373. wc_PKCS7_Free(pkcs7);
  27374. wc_FreeRng(&rng);
  27375. /* check verify */
  27376. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27377. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27378. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  27379. AssertNotNull(pkcs7->content);
  27380. #ifdef HAVE_AES_CBC
  27381. /* check decode */
  27382. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  27383. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  27384. inner->privateKey = key;
  27385. inner->privateKeySz = keySz;
  27386. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  27387. pkcs7->contentSz, decoded, decodedSz)), 0);
  27388. wc_PKCS7_Free(inner);
  27389. #endif
  27390. wc_PKCS7_Free(pkcs7);
  27391. #ifdef HAVE_AES_CBC
  27392. /* check cert set */
  27393. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27394. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27395. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  27396. AssertNotNull(pkcs7->singleCert);
  27397. AssertIntNE(pkcs7->singleCertSz, 0);
  27398. wc_PKCS7_Free(pkcs7);
  27399. #endif
  27400. res = TEST_RES_CHECK(1);
  27401. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  27402. return res;
  27403. }
  27404. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  27405. {
  27406. int res = TEST_SKIPPED;
  27407. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  27408. && !defined(NO_AES)
  27409. PKCS7* pkcs7;
  27410. void* heap = NULL;
  27411. pkcs7 = wc_PKCS7_New(heap, testDevId);
  27412. AssertNotNull(pkcs7);
  27413. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  27414. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  27415. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  27416. wc_PKCS7_Free(pkcs7);
  27417. res = TEST_RES_CHECK(1);
  27418. #endif
  27419. return res;
  27420. }
  27421. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  27422. {
  27423. int res = TEST_SKIPPED;
  27424. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  27425. && !defined(NO_AES)
  27426. PKCS7* pkcs7;
  27427. void* heap = NULL;
  27428. WOLFSSL_CTX* ctx;
  27429. ctx = NULL;
  27430. pkcs7 = wc_PKCS7_New(heap, testDevId);
  27431. AssertNotNull(pkcs7);
  27432. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  27433. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  27434. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  27435. wc_PKCS7_Free(pkcs7);
  27436. res = TEST_RES_CHECK(1);
  27437. #endif
  27438. return res;
  27439. }
  27440. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  27441. {
  27442. int res = TEST_SKIPPED;
  27443. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  27444. && !defined(NO_AES)
  27445. PKCS7* pkcs7;
  27446. void* heap = NULL;
  27447. WOLFSSL_CTX* ctx;
  27448. ctx = NULL;
  27449. pkcs7 = wc_PKCS7_New(heap, testDevId);
  27450. AssertNotNull(pkcs7);
  27451. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  27452. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  27453. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  27454. wc_PKCS7_Free(pkcs7);
  27455. res = TEST_RES_CHECK(1);
  27456. #endif
  27457. return res;
  27458. }
  27459. static int test_wc_PKCS7_DecodeCompressedData(void)
  27460. {
  27461. int res = TEST_SKIPPED;
  27462. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  27463. && !defined(NO_AES) && defined(HAVE_LIBZ)
  27464. PKCS7* pkcs7;
  27465. void* heap = NULL;
  27466. byte out[4096];
  27467. byte *decompressed;
  27468. int outSz, decompressedSz;
  27469. const char* cert = "./certs/client-cert.pem";
  27470. byte* cert_buf = NULL;
  27471. size_t cert_sz = 0;
  27472. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  27473. AssertNotNull((decompressed =
  27474. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  27475. decompressedSz = (int)cert_sz;
  27476. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  27477. pkcs7->content = (byte*)cert_buf;
  27478. pkcs7->contentSz = (word32)cert_sz;
  27479. pkcs7->contentOID = DATA;
  27480. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  27481. sizeof(out))), 0);
  27482. wc_PKCS7_Free(pkcs7);
  27483. /* compressed key should be smaller than when started */
  27484. AssertIntLT(outSz, cert_sz);
  27485. /* test decompression */
  27486. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  27487. AssertIntEQ(pkcs7->contentOID, 0);
  27488. /* fail case with out buffer too small */
  27489. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  27490. decompressed, outSz), 0);
  27491. /* success case */
  27492. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  27493. decompressed, decompressedSz), cert_sz);
  27494. AssertIntEQ(pkcs7->contentOID, DATA);
  27495. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  27496. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  27497. decompressed = NULL;
  27498. /* test decompression function with different 'max' inputs */
  27499. outSz = sizeof(out);
  27500. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  27501. 0);
  27502. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  27503. out, outSz, 0, heap), 0);
  27504. AssertNull(decompressed);
  27505. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  27506. out, outSz, 0, heap), 0);
  27507. AssertNotNull(decompressed);
  27508. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  27509. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  27510. decompressed = NULL;
  27511. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  27512. out, outSz, 0, heap), 0);
  27513. AssertNotNull(decompressed);
  27514. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  27515. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  27516. if (cert_buf)
  27517. free(cert_buf);
  27518. wc_PKCS7_Free(pkcs7);
  27519. res = TEST_RES_CHECK(1);
  27520. #endif
  27521. return res;
  27522. }
  27523. static int test_wc_i2d_PKCS12(void)
  27524. {
  27525. int res = TEST_SKIPPED;
  27526. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  27527. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  27528. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  27529. WC_PKCS12* pkcs12 = NULL;
  27530. unsigned char der[FOURK_BUF * 2];
  27531. unsigned char* pt;
  27532. int derSz;
  27533. unsigned char out[FOURK_BUF * 2];
  27534. int outSz = FOURK_BUF * 2;
  27535. const char p12_f[] = "./certs/test-servercert.p12";
  27536. XFILE f;
  27537. f = XFOPEN(p12_f, "rb");
  27538. AssertNotNull(f);
  27539. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  27540. AssertIntGT(derSz, 0);
  27541. XFCLOSE(f);
  27542. AssertNotNull(pkcs12 = wc_PKCS12_new());
  27543. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  27544. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27545. AssertIntEQ(outSz, derSz);
  27546. outSz = derSz - 1;
  27547. pt = out;
  27548. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  27549. outSz = derSz;
  27550. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  27551. AssertIntEQ((pt == out), 0);
  27552. pt = NULL;
  27553. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  27554. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  27555. wc_PKCS12_free(pkcs12);
  27556. /* Run the same test but use wc_d2i_PKCS12_fp. */
  27557. AssertNotNull(pkcs12 = wc_PKCS12_new());
  27558. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  27559. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27560. AssertIntEQ(outSz, derSz);
  27561. wc_PKCS12_free(pkcs12);
  27562. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  27563. pkcs12 = NULL;
  27564. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  27565. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27566. AssertIntEQ(outSz, derSz);
  27567. wc_PKCS12_free(pkcs12);
  27568. res = TEST_RES_CHECK(1);
  27569. #endif
  27570. return res;
  27571. }
  27572. /* Testing wc_SignatureGetSize() for signature type ECC */
  27573. static int test_wc_SignatureGetSize_ecc(void)
  27574. {
  27575. int res = TEST_SKIPPED;
  27576. #ifndef NO_SIG_WRAPPER
  27577. int ret;
  27578. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  27579. enum wc_SignatureType sig_type;
  27580. word32 key_len;
  27581. /* Initialize ECC Key */
  27582. ecc_key ecc;
  27583. const char* qx =
  27584. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  27585. const char* qy =
  27586. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  27587. const char* d =
  27588. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  27589. ret = wc_ecc_init(&ecc);
  27590. if (ret == 0) {
  27591. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  27592. }
  27593. if (ret == 0) {
  27594. /* Input for signature type ECC */
  27595. sig_type = WC_SIGNATURE_TYPE_ECC;
  27596. key_len = sizeof(ecc_key);
  27597. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27598. /* Test bad args */
  27599. if (ret > 0) {
  27600. sig_type = (enum wc_SignatureType) 100;
  27601. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27602. if (ret == BAD_FUNC_ARG) {
  27603. sig_type = WC_SIGNATURE_TYPE_ECC;
  27604. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27605. }
  27606. if (ret >= 0) {
  27607. key_len = (word32) 0;
  27608. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27609. }
  27610. if (ret == BAD_FUNC_ARG) {
  27611. ret = SIG_TYPE_E;
  27612. }
  27613. }
  27614. }
  27615. else {
  27616. ret = WOLFSSL_FATAL_ERROR;
  27617. }
  27618. wc_ecc_free(&ecc);
  27619. #else
  27620. ret = SIG_TYPE_E;
  27621. #endif
  27622. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27623. #endif /* NO_SIG_WRAPPER */
  27624. return res;
  27625. }/* END test_wc_SignatureGetSize_ecc() */
  27626. /* Testing wc_SignatureGetSize() for signature type rsa */
  27627. static int test_wc_SignatureGetSize_rsa(void)
  27628. {
  27629. int res = TEST_SKIPPED;
  27630. #ifndef NO_SIG_WRAPPER
  27631. int ret = 0;
  27632. #ifndef NO_RSA
  27633. enum wc_SignatureType sig_type;
  27634. word32 key_len;
  27635. word32 idx = 0;
  27636. /* Initialize RSA Key */
  27637. RsaKey rsa_key;
  27638. byte* tmp = NULL;
  27639. size_t bytes;
  27640. #ifdef USE_CERT_BUFFERS_1024
  27641. bytes = (size_t)sizeof_client_key_der_1024;
  27642. if (bytes < (size_t)sizeof_client_key_der_1024)
  27643. bytes = (size_t)sizeof_client_cert_der_1024;
  27644. #elif defined(USE_CERT_BUFFERS_2048)
  27645. bytes = (size_t)sizeof_client_key_der_2048;
  27646. if (bytes < (size_t)sizeof_client_cert_der_2048)
  27647. bytes = (size_t)sizeof_client_cert_der_2048;
  27648. #else
  27649. bytes = FOURK_BUF;
  27650. #endif
  27651. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27652. if (tmp != NULL) {
  27653. #ifdef USE_CERT_BUFFERS_1024
  27654. XMEMCPY(tmp, client_key_der_1024,
  27655. (size_t)sizeof_client_key_der_1024);
  27656. #elif defined(USE_CERT_BUFFERS_2048)
  27657. XMEMCPY(tmp, client_key_der_2048,
  27658. (size_t)sizeof_client_key_der_2048);
  27659. #elif !defined(NO_FILESYSTEM)
  27660. file = XFOPEN(clientKey, "rb");
  27661. if (file != XBADFILE) {
  27662. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  27663. XFCLOSE(file);
  27664. }
  27665. else {
  27666. ret = WOLFSSL_FATAL_ERROR;
  27667. }
  27668. #else
  27669. ret = WOLFSSL_FATAL_ERROR;
  27670. #endif
  27671. }
  27672. else {
  27673. ret = WOLFSSL_FATAL_ERROR;
  27674. }
  27675. if (ret == 0) {
  27676. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  27677. }
  27678. if (ret == 0) {
  27679. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  27680. }
  27681. if (ret == 0) {
  27682. /* Input for signature type RSA */
  27683. sig_type = WC_SIGNATURE_TYPE_RSA;
  27684. key_len = sizeof(RsaKey);
  27685. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27686. /* Test bad args */
  27687. if (ret > 0) {
  27688. sig_type = (enum wc_SignatureType) 100;
  27689. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27690. if (ret == BAD_FUNC_ARG) {
  27691. sig_type = WC_SIGNATURE_TYPE_RSA;
  27692. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27693. }
  27694. #ifndef HAVE_USER_RSA
  27695. if (ret == BAD_FUNC_ARG)
  27696. #else
  27697. if (ret == 0)
  27698. #endif
  27699. {
  27700. key_len = (word32)0;
  27701. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27702. }
  27703. if (ret == BAD_FUNC_ARG) {
  27704. ret = SIG_TYPE_E;
  27705. }
  27706. }
  27707. }
  27708. else {
  27709. ret = WOLFSSL_FATAL_ERROR;
  27710. }
  27711. wc_FreeRsaKey(&rsa_key);
  27712. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27713. #else
  27714. ret = SIG_TYPE_E;
  27715. #endif
  27716. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27717. #endif /* NO_SIG_WRAPPER */
  27718. return res;
  27719. }/* END test_wc_SignatureGetSize_rsa(void) */
  27720. /*----------------------------------------------------------------------------*
  27721. | hash.h Tests
  27722. *----------------------------------------------------------------------------*/
  27723. static int test_wc_HashInit(void)
  27724. {
  27725. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  27726. wc_HashAlg hash;
  27727. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27728. enum wc_HashType enumArray[] = {
  27729. #ifndef NO_MD5
  27730. WC_HASH_TYPE_MD5,
  27731. #endif
  27732. #ifndef NO_SHA
  27733. WC_HASH_TYPE_SHA,
  27734. #endif
  27735. #ifndef WOLFSSL_SHA224
  27736. WC_HASH_TYPE_SHA224,
  27737. #endif
  27738. #ifndef NO_SHA256
  27739. WC_HASH_TYPE_SHA256,
  27740. #endif
  27741. #ifndef WOLFSSL_SHA384
  27742. WC_HASH_TYPE_SHA384,
  27743. #endif
  27744. #ifndef WOLFSSL_SHA512
  27745. WC_HASH_TYPE_SHA512,
  27746. #endif
  27747. };
  27748. /* dynamically finds the length */
  27749. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27750. /* For loop to test various arguments... */
  27751. for (i = 0; i < enumlen; i++) {
  27752. /* check for bad args */
  27753. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  27754. ret = 1;
  27755. break;
  27756. }
  27757. wc_HashFree(&hash, enumArray[i]);
  27758. /* check for null ptr */
  27759. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  27760. ret = 1;
  27761. break;
  27762. }
  27763. } /* end of for loop */
  27764. return TEST_RES_CHECK(ret == 0);
  27765. } /* end of test_wc_HashInit */
  27766. /*
  27767. * Unit test function for wc_HashSetFlags()
  27768. */
  27769. static int test_wc_HashSetFlags(void)
  27770. {
  27771. int res = TEST_SKIPPED;
  27772. #ifdef WOLFSSL_HASH_FLAGS
  27773. wc_HashAlg hash;
  27774. int ret = 0;
  27775. word32 flags = 0;
  27776. int i, j;
  27777. int notSupportedLen;
  27778. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27779. enum wc_HashType enumArray[] = {
  27780. #ifndef NO_MD5
  27781. WC_HASH_TYPE_MD5,
  27782. #endif
  27783. #ifndef NO_SHA
  27784. WC_HASH_TYPE_SHA,
  27785. #endif
  27786. #ifdef WOLFSSL_SHA224
  27787. WC_HASH_TYPE_SHA224,
  27788. #endif
  27789. #ifndef NO_SHA256
  27790. WC_HASH_TYPE_SHA256,
  27791. #endif
  27792. #ifdef WOLFSSL_SHA384
  27793. WC_HASH_TYPE_SHA384,
  27794. #endif
  27795. #ifdef WOLFSSL_SHA512
  27796. WC_HASH_TYPE_SHA512,
  27797. #endif
  27798. #ifdef WOLFSSL_SHA3
  27799. WC_HASH_TYPE_SHA3_224,
  27800. #endif
  27801. };
  27802. enum wc_HashType notSupported[] = {
  27803. WC_HASH_TYPE_MD5_SHA,
  27804. WC_HASH_TYPE_MD2,
  27805. WC_HASH_TYPE_MD4,
  27806. WC_HASH_TYPE_BLAKE2B,
  27807. WC_HASH_TYPE_BLAKE2S,
  27808. WC_HASH_TYPE_NONE,
  27809. };
  27810. /* dynamically finds the length */
  27811. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27812. /* For loop to test various arguments... */
  27813. for (i = 0; i < enumlen; i++) {
  27814. ret = wc_HashInit(&hash, enumArray[i]);
  27815. if (ret == 0) {
  27816. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  27817. }
  27818. if (ret == 0) {
  27819. if (flags & WC_HASH_FLAG_ISCOPY) {
  27820. ret = 0;
  27821. }
  27822. }
  27823. if (ret == 0) {
  27824. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  27825. if (ret == BAD_FUNC_ARG) {
  27826. ret = 0;
  27827. }
  27828. }
  27829. wc_HashFree(&hash, enumArray[i]);
  27830. }
  27831. /* For loop to test not supported cases */
  27832. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27833. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27834. ret = wc_HashInit(&hash, notSupported[j]);
  27835. if (ret == 0) {
  27836. ret = -1;
  27837. }
  27838. else if (ret == BAD_FUNC_ARG) {
  27839. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  27840. if (ret == 0) {
  27841. ret = -1;
  27842. }
  27843. else if (ret == BAD_FUNC_ARG) {
  27844. ret = 0;
  27845. }
  27846. }
  27847. if (ret == 0) {
  27848. ret = wc_HashFree(&hash, notSupported[j]);
  27849. if (ret == 0) {
  27850. ret = -1;
  27851. }
  27852. else if (ret == BAD_FUNC_ARG) {
  27853. ret = 0;
  27854. }
  27855. }
  27856. }
  27857. res = TEST_RES_CHECK(ret == 0);
  27858. #endif
  27859. return res;
  27860. } /* END test_wc_HashSetFlags */
  27861. /*
  27862. * Unit test function for wc_HashGetFlags()
  27863. */
  27864. static int test_wc_HashGetFlags(void)
  27865. {
  27866. int res = TEST_SKIPPED;
  27867. #ifdef WOLFSSL_HASH_FLAGS
  27868. wc_HashAlg hash;
  27869. int ret = 0;
  27870. word32 flags = 0;
  27871. int i, j;
  27872. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27873. enum wc_HashType enumArray[] = {
  27874. #ifndef NO_MD5
  27875. WC_HASH_TYPE_MD5,
  27876. #endif
  27877. #ifndef NO_SHA
  27878. WC_HASH_TYPE_SHA,
  27879. #endif
  27880. #ifdef WOLFSSL_SHA224
  27881. WC_HASH_TYPE_SHA224,
  27882. #endif
  27883. #ifndef NO_SHA256
  27884. WC_HASH_TYPE_SHA256,
  27885. #endif
  27886. #ifdef WOLFSSL_SHA384
  27887. WC_HASH_TYPE_SHA384,
  27888. #endif
  27889. #ifdef WOLFSSL_SHA512
  27890. WC_HASH_TYPE_SHA512,
  27891. #endif
  27892. #ifdef WOLFSSL_SHA3
  27893. WC_HASH_TYPE_SHA3_224,
  27894. #endif
  27895. };
  27896. enum wc_HashType notSupported[] = {
  27897. WC_HASH_TYPE_MD5_SHA,
  27898. WC_HASH_TYPE_MD2,
  27899. WC_HASH_TYPE_MD4,
  27900. WC_HASH_TYPE_BLAKE2B,
  27901. WC_HASH_TYPE_BLAKE2S,
  27902. WC_HASH_TYPE_NONE,
  27903. };
  27904. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27905. int notSupportedLen;
  27906. /* For loop to test various arguments... */
  27907. for (i = 0; i < enumlen; i++) {
  27908. ret = wc_HashInit(&hash, enumArray[i]);
  27909. if (ret == 0) {
  27910. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  27911. }
  27912. if (ret == 0) {
  27913. if (flags & WC_HASH_FLAG_ISCOPY) {
  27914. ret = 0;
  27915. }
  27916. }
  27917. if (ret == 0) {
  27918. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  27919. if (ret == BAD_FUNC_ARG) {
  27920. ret = 0;
  27921. }
  27922. }
  27923. wc_HashFree(&hash, enumArray[i]);
  27924. if (ret != 0) {
  27925. break;
  27926. }
  27927. }
  27928. /* For loop to test not supported cases */
  27929. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27930. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27931. ret = wc_HashInit(&hash, notSupported[j]);
  27932. if (ret == 0) {
  27933. ret = -1;
  27934. }
  27935. else if (ret == BAD_FUNC_ARG) {
  27936. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  27937. if (ret == 0) {
  27938. ret = -1;
  27939. }
  27940. else if (ret == BAD_FUNC_ARG) {
  27941. ret = 0;
  27942. }
  27943. }
  27944. if (ret == 0) {
  27945. ret = wc_HashFree(&hash, notSupported[j]);
  27946. if (ret == 0) {
  27947. ret = -1;
  27948. }
  27949. if (ret == BAD_FUNC_ARG) {
  27950. ret = 0;
  27951. }
  27952. }
  27953. }
  27954. res = TEST_RES_CHECK(ret == 0);
  27955. #endif
  27956. return res;
  27957. } /* END test_wc_HashGetFlags */
  27958. /*----------------------------------------------------------------------------*
  27959. | Compatibility Tests
  27960. *----------------------------------------------------------------------------*/
  27961. /*----------------------------------------------------------------------------*
  27962. | ASN.1 Tests
  27963. *----------------------------------------------------------------------------*/
  27964. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27965. {
  27966. int res = TEST_SKIPPED;
  27967. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27968. EXPECT_DECLS;
  27969. ASN1_BIT_STRING* str = NULL;
  27970. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27971. /* Empty data testing. */
  27972. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27973. ASN1_BIT_STRING_free(str);
  27974. str = NULL;
  27975. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27976. /* Invalid parameter testing. */
  27977. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27978. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27979. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27980. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27981. /* No bit string - bit is always 0. */
  27982. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27983. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27984. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27985. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27986. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27987. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27988. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27989. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27990. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27991. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27992. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27993. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27994. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27995. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27996. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27997. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27998. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27999. ASN1_BIT_STRING_free(str);
  28000. ASN1_BIT_STRING_free(NULL);
  28001. res = EXPECT_RESULT();
  28002. #endif
  28003. return res;
  28004. }
  28005. static int test_wolfSSL_ASN1_INTEGER(void)
  28006. {
  28007. int res = TEST_SKIPPED;
  28008. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28009. EXPECT_DECLS;
  28010. ASN1_INTEGER* a = NULL;
  28011. ASN1_INTEGER* dup = NULL;
  28012. const unsigned char invalidLenDer[] = {
  28013. 0x02, 0x20, 0x00
  28014. };
  28015. const unsigned char longDer[] = {
  28016. 0x02, 0x20,
  28017. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28018. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28019. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28020. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  28021. };
  28022. const unsigned char* p;
  28023. /* Invalid parameter testing. */
  28024. ASN1_INTEGER_free(NULL);
  28025. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  28026. ExpectNotNull(a = ASN1_INTEGER_new());
  28027. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28028. ASN1_INTEGER_free(dup);
  28029. dup = NULL;
  28030. ASN1_INTEGER_free(a);
  28031. a = NULL;
  28032. p = longDer;
  28033. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  28034. p = longDer;
  28035. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  28036. ExpectPtrNE(p, longDer);
  28037. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28038. ASN1_INTEGER_free(dup);
  28039. ASN1_INTEGER_free(a);
  28040. res = EXPECT_RESULT();
  28041. #endif
  28042. return res;
  28043. }
  28044. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  28045. {
  28046. int res = TEST_SKIPPED;
  28047. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28048. EXPECT_DECLS;
  28049. ASN1_INTEGER* a = NULL;
  28050. ASN1_INTEGER* b = NULL;
  28051. ExpectNotNull(a = ASN1_INTEGER_new());
  28052. ExpectNotNull(b = ASN1_INTEGER_new());
  28053. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  28054. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28055. /* Invalid parameter testing. */
  28056. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  28057. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  28058. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  28059. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28060. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  28061. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28062. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  28063. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28064. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28065. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28066. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  28067. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  28068. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28069. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  28070. ASN1_INTEGER_free(b);
  28071. ASN1_INTEGER_free(a);
  28072. res = EXPECT_RESULT();
  28073. #endif
  28074. return res;
  28075. }
  28076. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  28077. {
  28078. int res = TEST_SKIPPED;
  28079. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28080. EXPECT_DECLS;
  28081. ASN1_INTEGER* ai = NULL;
  28082. ASN1_INTEGER* ai2 = NULL;
  28083. BIGNUM* bn = NULL;
  28084. BIGNUM* bn2 = NULL;
  28085. ExpectNotNull(ai = ASN1_INTEGER_new());
  28086. ExpectNotNull(bn2 = BN_new());
  28087. /* Invalid parameter testing. */
  28088. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  28089. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  28090. /* at the moment hard setting since no set function */
  28091. if (ai != NULL) {
  28092. ai->data[0] = 0xff; /* No DER encoding. */
  28093. ai->length = 1;
  28094. }
  28095. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28096. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28097. BN_free(bn);
  28098. bn = NULL;
  28099. #else
  28100. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28101. #endif
  28102. if (ai != NULL) {
  28103. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28104. ai->data[1] = 0x04; /* bad length of integer */
  28105. ai->data[2] = 0x03;
  28106. ai->length = 3;
  28107. }
  28108. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28109. /* Interpreted as a number 0x020403. */
  28110. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28111. BN_free(bn);
  28112. bn = NULL;
  28113. #else
  28114. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28115. #endif
  28116. if (ai != NULL) {
  28117. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28118. ai->data[1] = 0x01; /* length of integer */
  28119. ai->data[2] = 0x03;
  28120. ai->length = 3;
  28121. }
  28122. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28123. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  28124. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28125. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28126. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28127. if (ai != NULL) {
  28128. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28129. ai->data[1] = 0x02; /* length of integer */
  28130. ai->data[2] = 0x00; /* padding byte to ensure positive */
  28131. ai->data[3] = 0xff;
  28132. ai->length = 4;
  28133. }
  28134. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28135. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28136. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28137. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28138. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28139. if (ai != NULL) {
  28140. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28141. ai->data[1] = 0x01; /* length of integer */
  28142. ai->data[2] = 0x00;
  28143. ai->length = 3;
  28144. }
  28145. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28146. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28147. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28148. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28149. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28150. if (ai != NULL) {
  28151. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28152. ai->data[1] = 0x01; /* length of integer */
  28153. ai->data[2] = 0x01;
  28154. ai->length = 3;
  28155. ai->negative = 1;
  28156. }
  28157. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28158. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28159. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28160. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28161. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28162. BN_free(bn2);
  28163. BN_free(bn);
  28164. ASN1_INTEGER_free(ai2);
  28165. ASN1_INTEGER_free(ai);
  28166. res = EXPECT_RESULT();
  28167. #endif
  28168. return res;
  28169. }
  28170. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  28171. {
  28172. int res = TEST_SKIPPED;
  28173. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28174. EXPECT_DECLS;
  28175. ASN1_INTEGER *a = NULL;
  28176. long val;
  28177. ExpectNotNull(a = ASN1_INTEGER_new());
  28178. /* Invalid parameter testing. */
  28179. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  28180. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28181. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  28182. #else
  28183. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  28184. #endif
  28185. ASN1_INTEGER_free(a);
  28186. a = NULL;
  28187. ExpectNotNull(a = ASN1_INTEGER_new());
  28188. val = 0;
  28189. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  28190. ASN1_INTEGER_free(a);
  28191. a = NULL;
  28192. /* 0 */
  28193. ExpectNotNull(a = ASN1_INTEGER_new());
  28194. val = 0;
  28195. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28196. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28197. ASN1_INTEGER_free(a);
  28198. a = NULL;
  28199. /* 40 */
  28200. ExpectNotNull(a = ASN1_INTEGER_new());
  28201. val = 40;
  28202. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28203. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28204. ASN1_INTEGER_free(a);
  28205. a = NULL;
  28206. /* -40 */
  28207. ExpectNotNull(a = ASN1_INTEGER_new());
  28208. val = -40;
  28209. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28210. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28211. ASN1_INTEGER_free(a);
  28212. a = NULL;
  28213. /* 128 */
  28214. ExpectNotNull(a = ASN1_INTEGER_new());
  28215. val = 128;
  28216. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28217. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28218. ASN1_INTEGER_free(a);
  28219. a = NULL;
  28220. /* -128 */
  28221. ExpectNotNull(a = ASN1_INTEGER_new());
  28222. val = -128;
  28223. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28224. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28225. ASN1_INTEGER_free(a);
  28226. a = NULL;
  28227. /* 200 */
  28228. ExpectNotNull(a = ASN1_INTEGER_new());
  28229. val = 200;
  28230. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28231. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28232. ASN1_INTEGER_free(a);
  28233. a = NULL;
  28234. /* int max (2147483647) */
  28235. ExpectNotNull(a = ASN1_INTEGER_new());
  28236. val = 2147483647;
  28237. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28238. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28239. ASN1_INTEGER_free(a);
  28240. a = NULL;
  28241. /* int min (-2147483648) */
  28242. ExpectNotNull(a = ASN1_INTEGER_new());
  28243. val = -2147483647 - 1;
  28244. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28245. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28246. ASN1_INTEGER_free(a);
  28247. a = NULL;
  28248. /* long max positive */
  28249. ExpectNotNull(a = ASN1_INTEGER_new());
  28250. val = (long)(((unsigned long)-1) >> 1);
  28251. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28252. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28253. ASN1_INTEGER_free(a);
  28254. res = EXPECT_RESULT();
  28255. #endif
  28256. return res;
  28257. }
  28258. #if defined(OPENSSL_EXTRA)
  28259. typedef struct ASN1IntTestVector {
  28260. const byte* der;
  28261. const size_t derSz;
  28262. const long value;
  28263. } ASN1IntTestVector;
  28264. #endif
  28265. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  28266. {
  28267. int res = TEST_SKIPPED;
  28268. #if defined(OPENSSL_EXTRA)
  28269. EXPECT_DECLS;
  28270. size_t i;
  28271. WOLFSSL_ASN1_INTEGER* a = NULL;
  28272. WOLFSSL_ASN1_INTEGER* b = NULL;
  28273. WOLFSSL_ASN1_INTEGER* c = NULL;
  28274. const byte* p = NULL;
  28275. byte* p2 = NULL;
  28276. byte* reEncoded = NULL;
  28277. int reEncodedSz;
  28278. static const byte zeroDer[] = {
  28279. 0x02, 0x01, 0x00
  28280. };
  28281. static const byte oneDer[] = {
  28282. 0x02, 0x01, 0x01
  28283. };
  28284. static const byte negativeDer[] = {
  28285. 0x02, 0x03, 0xC1, 0x16, 0x0D
  28286. };
  28287. static const byte positiveDer[] = {
  28288. 0x02, 0x03, 0x01, 0x00, 0x01
  28289. };
  28290. static const byte primeDer[] = {
  28291. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  28292. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  28293. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  28294. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  28295. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  28296. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  28297. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  28298. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  28299. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  28300. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  28301. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  28302. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  28303. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  28304. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  28305. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  28306. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  28307. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  28308. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  28309. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  28310. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  28311. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  28312. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  28313. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  28314. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  28315. };
  28316. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  28317. static const ASN1IntTestVector testVectors[] = {
  28318. {zeroDer, sizeof(zeroDer), 0},
  28319. {oneDer, sizeof(oneDer), 1},
  28320. {negativeDer, sizeof(negativeDer), -4123123},
  28321. {positiveDer, sizeof(positiveDer), 65537},
  28322. {primeDer, sizeof(primeDer), 0}
  28323. };
  28324. static const size_t NUM_TEST_VECTORS =
  28325. sizeof(testVectors)/sizeof(testVectors[0]);
  28326. /* Check d2i error conditions */
  28327. /* NULL pointer to input. */
  28328. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  28329. ExpectNull(b);
  28330. /* NULL input. */
  28331. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  28332. ExpectNull(b);
  28333. /* 0 length. */
  28334. p = testVectors[0].der;
  28335. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  28336. ExpectNull(b);
  28337. /* Negative length. */
  28338. p = testVectors[0].der;
  28339. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  28340. ExpectNull(b);
  28341. /* Garbage DER input. */
  28342. p = garbageDer;
  28343. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  28344. ExpectNull(b);
  28345. /* Check i2d error conditions */
  28346. /* NULL input. */
  28347. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  28348. /* 0 length input data buffer (a->length == 0). */
  28349. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  28350. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  28351. if (a != NULL)
  28352. a->data = NULL;
  28353. /* NULL input data buffer. */
  28354. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  28355. if (a != NULL) {
  28356. /* Reset a->data. */
  28357. a->data = a->intData;
  28358. }
  28359. /* Set a to valid value. */
  28360. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  28361. /* NULL output buffer. */
  28362. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  28363. wolfSSL_ASN1_INTEGER_free(a);
  28364. a = NULL;
  28365. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  28366. p = testVectors[i].der;
  28367. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  28368. testVectors[i].derSz));
  28369. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28370. if (testVectors[i].derSz <= sizeof(long)) {
  28371. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  28372. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  28373. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  28374. wolfSSL_ASN1_INTEGER_free(c);
  28375. c = NULL;
  28376. }
  28377. /* Convert to DER without a pre-allocated output buffer. */
  28378. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  28379. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  28380. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  28381. /* Convert to DER with a pre-allocated output buffer. In this case, the
  28382. * output buffer pointer should be incremented just past the end of the
  28383. * encoded data. */
  28384. p2 = reEncoded;
  28385. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  28386. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  28387. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  28388. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  28389. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  28390. reEncoded = NULL;
  28391. wolfSSL_ASN1_INTEGER_free(a);
  28392. a = NULL;
  28393. }
  28394. res = EXPECT_RESULT();
  28395. #endif /* OPENSSL_EXTRA */
  28396. return res;
  28397. }
  28398. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  28399. {
  28400. int res = TEST_SKIPPED;
  28401. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28402. EXPECT_DECLS;
  28403. BIO* bio = NULL;
  28404. BIO* out = NULL;
  28405. BIO* fixed = NULL;
  28406. ASN1_INTEGER* ai = NULL;
  28407. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  28408. char tmp[1024];
  28409. int tmpSz;
  28410. const char expected1[] = "123456";
  28411. const char expected2[] = "112345678912345678901234567890";
  28412. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  28413. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  28414. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  28415. ExpectNotNull(ai = ASN1_INTEGER_new());
  28416. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  28417. /* Invalid parameter testing. */
  28418. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  28419. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  28420. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  28421. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  28422. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  28423. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  28424. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  28425. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  28426. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  28427. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  28428. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  28429. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  28430. /* No data to read from BIO. */
  28431. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  28432. /* read first line */
  28433. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  28434. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  28435. XMEMSET(tmp, 0, 1024);
  28436. tmpSz = BIO_read(out, tmp, 1024);
  28437. ExpectIntEQ(tmpSz, 6);
  28438. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  28439. /* fail on second line (not % 2) */
  28440. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  28441. /* read 3rd long line */
  28442. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  28443. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  28444. XMEMSET(tmp, 0, 1024);
  28445. tmpSz = BIO_read(out, tmp, 1024);
  28446. ExpectIntEQ(tmpSz, 30);
  28447. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  28448. /* fail on empty line */
  28449. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  28450. BIO_free(bio);
  28451. bio = NULL;
  28452. /* Make long integer, requiring dynamic memory, even longer. */
  28453. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  28454. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  28455. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  28456. XMEMSET(tmp, 0, 1024);
  28457. tmpSz = BIO_read(out, tmp, 1024);
  28458. ExpectIntEQ(tmpSz, 48);
  28459. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  28460. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  28461. XMEMSET(tmp, 0, 1024);
  28462. tmpSz = BIO_read(out, tmp, 1024);
  28463. ExpectIntEQ(tmpSz, 56);
  28464. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  28465. BIO_free(bio);
  28466. BIO_free(out);
  28467. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28468. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28469. /* Ensure there is 0 bytes avaialble to write into. */
  28470. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  28471. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  28472. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28473. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  28474. BIO_free(fixed);
  28475. ASN1_INTEGER_free(ai);
  28476. res = EXPECT_RESULT();
  28477. #endif
  28478. return res;
  28479. }
  28480. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  28481. {
  28482. int res = TEST_SKIPPED;
  28483. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28484. EXPECT_DECLS;
  28485. ASN1_INTEGER *a = NULL;
  28486. unsigned char *pp,*tpp;
  28487. int ret;
  28488. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  28489. /* Invalid parameter testing. */
  28490. /* Set pp to an invalid value. */
  28491. pp = NULL;
  28492. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  28493. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  28494. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  28495. /* 40 */
  28496. if (a != NULL) {
  28497. a->intData[0] = ASN_INTEGER;
  28498. a->intData[1] = 1;
  28499. a->intData[2] = 40;
  28500. }
  28501. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28502. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28503. DYNAMIC_TYPE_TMP_BUFFER));
  28504. tpp = pp;
  28505. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28506. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  28507. tpp--;
  28508. ExpectIntEQ(*tpp, 40);
  28509. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28510. pp = NULL;
  28511. /* 128 */
  28512. if (a != NULL) {
  28513. a->intData[0] = ASN_INTEGER;
  28514. a->intData[1] = 1;
  28515. a->intData[2] = 128;
  28516. }
  28517. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28518. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28519. DYNAMIC_TYPE_TMP_BUFFER));
  28520. tpp = pp;
  28521. if (tpp != NULL) {
  28522. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28523. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  28524. tpp--;
  28525. ExpectIntEQ(*(tpp--), 128);
  28526. ExpectIntEQ(*tpp, 0);
  28527. }
  28528. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28529. pp = NULL;
  28530. /* -40 */
  28531. if (a != NULL) {
  28532. a->intData[0] = ASN_INTEGER;
  28533. a->intData[1] = 1;
  28534. a->intData[2] = 40;
  28535. a->negative = 1;
  28536. }
  28537. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28538. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28539. DYNAMIC_TYPE_TMP_BUFFER));
  28540. tpp = pp;
  28541. if (tpp != NULL) {
  28542. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28543. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  28544. tpp--;
  28545. ExpectIntEQ(*tpp, 216);
  28546. }
  28547. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28548. pp = NULL;
  28549. /* -128 */
  28550. if (a != NULL) {
  28551. a->intData[0] = ASN_INTEGER;
  28552. a->intData[1] = 1;
  28553. a->intData[2] = 128;
  28554. a->negative = 1;
  28555. }
  28556. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28557. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28558. DYNAMIC_TYPE_TMP_BUFFER));
  28559. tpp = pp;
  28560. if (tpp != NULL) {
  28561. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28562. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  28563. tpp--;
  28564. ExpectIntEQ(*tpp, 128);
  28565. }
  28566. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28567. pp = NULL;
  28568. /* -200 */
  28569. if (a != NULL) {
  28570. a->intData[0] = ASN_INTEGER;
  28571. a->intData[1] = 1;
  28572. a->intData[2] = 200;
  28573. a->negative = 1;
  28574. }
  28575. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28576. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28577. DYNAMIC_TYPE_TMP_BUFFER));
  28578. tpp = pp;
  28579. if (tpp != NULL) {
  28580. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28581. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  28582. tpp--;
  28583. ExpectIntEQ(*(tpp--), 56);
  28584. ExpectIntEQ(*tpp, 255);
  28585. }
  28586. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28587. pp = NULL;
  28588. /* Empty */
  28589. if (a != NULL) {
  28590. a->intData[0] = ASN_INTEGER;
  28591. a->intData[1] = 0;
  28592. a->negative = 0;
  28593. }
  28594. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28595. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28596. DYNAMIC_TYPE_TMP_BUFFER));
  28597. tpp = pp;
  28598. if (tpp != NULL) {
  28599. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28600. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  28601. tpp--;
  28602. ExpectIntEQ(*tpp, 0);
  28603. }
  28604. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28605. pp = NULL;
  28606. /* 0 */
  28607. if (a != NULL) {
  28608. a->intData[0] = ASN_INTEGER;
  28609. a->intData[1] = 1;
  28610. a->intData[2] = 0;
  28611. a->negative = 1;
  28612. }
  28613. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28614. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28615. DYNAMIC_TYPE_TMP_BUFFER));
  28616. if (tpp != NULL) {
  28617. tpp = pp;
  28618. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28619. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  28620. tpp--;
  28621. ExpectIntEQ(*tpp, 0);
  28622. }
  28623. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28624. pp = NULL;
  28625. /* 0x100 */
  28626. if (a != NULL) {
  28627. a->intData[0] = ASN_INTEGER;
  28628. a->intData[1] = 2;
  28629. a->intData[2] = 0x01;
  28630. a->intData[3] = 0x00;
  28631. a->negative = 0;
  28632. }
  28633. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28634. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28635. DYNAMIC_TYPE_TMP_BUFFER));
  28636. if (tpp != NULL) {
  28637. tpp = pp;
  28638. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28639. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  28640. tpp -= 2;
  28641. ExpectIntEQ(tpp[0], 0x01);
  28642. ExpectIntEQ(tpp[1], 0x00);
  28643. }
  28644. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28645. pp = NULL;
  28646. /* -0x8000 => 0x8000 */
  28647. if (a != NULL) {
  28648. a->intData[0] = ASN_INTEGER;
  28649. a->intData[1] = 2;
  28650. a->intData[2] = 0x80;
  28651. a->intData[3] = 0x00;
  28652. a->negative = 1;
  28653. }
  28654. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28655. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28656. DYNAMIC_TYPE_TMP_BUFFER));
  28657. tpp = pp;
  28658. if (tpp != NULL) {
  28659. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28660. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  28661. tpp -= 2;
  28662. ExpectIntEQ(tpp[0], 0x80);
  28663. ExpectIntEQ(tpp[1], 0x00);
  28664. }
  28665. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28666. pp = NULL;
  28667. /* -0x8001 => 0xFF7FFF */
  28668. if (a != NULL) {
  28669. a->intData[0] = ASN_INTEGER;
  28670. a->intData[1] = 2;
  28671. a->intData[2] = 0x80;
  28672. a->intData[3] = 0x01;
  28673. a->negative = 1;
  28674. }
  28675. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  28676. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28677. DYNAMIC_TYPE_TMP_BUFFER));
  28678. tpp = pp;
  28679. if (tpp != NULL) {
  28680. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  28681. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  28682. tpp -= 3;
  28683. ExpectIntEQ(tpp[0], 0xFF);
  28684. ExpectIntEQ(tpp[1], 0x7F);
  28685. ExpectIntEQ(tpp[2], 0xFF);
  28686. }
  28687. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28688. wolfSSL_ASN1_INTEGER_free(a);
  28689. res = EXPECT_RESULT();
  28690. #endif /* OPENSSL_EXTRA && !NO_ASN */
  28691. return res;
  28692. }
  28693. static int test_wolfSSL_ASN1_OBJECT(void)
  28694. {
  28695. int res = TEST_SKIPPED;
  28696. #if defined(OPENSSL_EXTRA)
  28697. EXPECT_DECLS;
  28698. ASN1_OBJECT* a = NULL;
  28699. ASN1_OBJECT s;
  28700. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  28701. /* Invalid parameter testing. */
  28702. ASN1_OBJECT_free(NULL);
  28703. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  28704. /* Test that a static ASN1_OBJECT can be freed. */
  28705. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28706. ASN1_OBJECT_free(&s);
  28707. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28708. ASN1_OBJECT_free(a);
  28709. a = NULL;
  28710. s.obj = der;
  28711. s.objSz = sizeof(der);
  28712. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28713. ASN1_OBJECT_free(a);
  28714. ASN1_OBJECT_free(&s);
  28715. res = EXPECT_RESULT();
  28716. #endif /* OPENSSL_EXTRA */
  28717. return res;
  28718. }
  28719. static int test_wolfSSL_ASN1_get_object(void)
  28720. {
  28721. int res = TEST_SKIPPED;
  28722. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28723. EXPECT_DECLS;
  28724. const unsigned char* derBuf = cliecc_cert_der_256;
  28725. const unsigned char* nullPtr = NULL;
  28726. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  28727. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  28728. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  28729. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  28730. const unsigned char* p;
  28731. unsigned char objDer[8];
  28732. unsigned char* der;
  28733. unsigned char* derPtr;
  28734. int len = sizeof_cliecc_cert_der_256;
  28735. long asnLen = 0;
  28736. int tag = 0;
  28737. int cls = 0;
  28738. ASN1_OBJECT* a = NULL;
  28739. ASN1_OBJECT s;
  28740. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28741. /* Invalid encoding at length. */
  28742. p = objDerInvalidLen;
  28743. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28744. 0x80);
  28745. p = objDerBadLen;
  28746. /* Error = 0x80, Constructed = 0x20 */
  28747. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28748. 0x80 | 0x20);
  28749. /* Read a couple TLV triplets and make sure they match the expected values
  28750. */
  28751. /* SEQUENCE */
  28752. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  28753. ExpectIntEQ(asnLen, 862);
  28754. ExpectIntEQ(tag, 0x10);
  28755. ExpectIntEQ(cls, 0);
  28756. /* SEQUENCE */
  28757. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28758. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28759. ExpectIntEQ(asnLen, 772);
  28760. ExpectIntEQ(tag, 0x10);
  28761. ExpectIntEQ(cls, 0);
  28762. /* [0] */
  28763. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28764. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28765. ExpectIntEQ(asnLen, 3);
  28766. ExpectIntEQ(tag, 0);
  28767. ExpectIntEQ(cls, 0x80);
  28768. /* INTEGER */
  28769. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28770. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28771. ExpectIntEQ(asnLen, 1);
  28772. ExpectIntEQ(tag, 0x2);
  28773. ExpectIntEQ(cls, 0);
  28774. derBuf += asnLen;
  28775. /* INTEGER */
  28776. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28777. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28778. ExpectIntEQ(asnLen, 20);
  28779. ExpectIntEQ(tag, 0x2);
  28780. ExpectIntEQ(cls, 0);
  28781. derBuf += asnLen;
  28782. /* SEQUENCE */
  28783. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28784. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28785. ExpectIntEQ(asnLen, 10);
  28786. ExpectIntEQ(tag, 0x10);
  28787. ExpectIntEQ(cls, 0);
  28788. /* Found OBJECT_ID. */
  28789. /* Invalid parameter testing. */
  28790. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  28791. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  28792. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  28793. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  28794. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  28795. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  28796. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  28797. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  28798. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  28799. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  28800. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28801. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28802. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  28803. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  28804. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  28805. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  28806. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  28807. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28808. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28809. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  28810. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  28811. /* Invalid encoding at length. */
  28812. p = objDerInvalidLen;
  28813. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  28814. p = objDerBadLen;
  28815. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  28816. p = objDerNotObj;
  28817. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  28818. p = objDerNoData;
  28819. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  28820. /* Create an ASN OBJECT from content */
  28821. p = derBuf + 2;
  28822. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  28823. ASN1_OBJECT_free(a);
  28824. a = NULL;
  28825. /* Create an ASN OBJECT from DER */
  28826. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  28827. /* Invalid parameter testing. */
  28828. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  28829. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  28830. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  28831. der = NULL;
  28832. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  28833. derPtr = objDer;
  28834. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  28835. ExpectPtrNE(derPtr, objDer);
  28836. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  28837. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28838. ASN1_OBJECT_free(a);
  28839. res = EXPECT_RESULT();
  28840. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28841. return res;
  28842. }
  28843. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  28844. {
  28845. int res = TEST_SKIPPED;
  28846. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28847. EXPECT_DECLS;
  28848. ASN1_OBJECT* obj = NULL;
  28849. ASN1_OBJECT* a = NULL;
  28850. BIO *bio = NULL;
  28851. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  28852. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  28853. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  28854. const unsigned char* p;
  28855. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  28856. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  28857. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  28858. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  28859. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  28860. /* No DER encoding in ASN1_OBJECT. */
  28861. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  28862. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28863. ASN1_OBJECT_free(a);
  28864. a = NULL;
  28865. /* DER encoding - not OBJECT_ID */
  28866. p = notObjDer;
  28867. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28868. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28869. ASN1_OBJECT_free(a);
  28870. a = NULL;
  28871. /* Bad length encoding. */
  28872. p = badLenDer;
  28873. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28874. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28875. ASN1_OBJECT_free(a);
  28876. a = NULL;
  28877. /* Good encoding - but unknown. */
  28878. p = goodDer;
  28879. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28880. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28881. ASN1_OBJECT_free(a);
  28882. BIO_free(bio);
  28883. ASN1_OBJECT_free(obj);
  28884. res = EXPECT_RESULT();
  28885. #endif
  28886. return res;
  28887. }
  28888. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28889. {
  28890. int res = TEST_SKIPPED;
  28891. #if defined(OPENSSL_EXTRA) && \
  28892. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28893. EXPECT_DECLS;
  28894. char buf[50] = {0};
  28895. ASN1_OBJECT* obj;
  28896. const char* oid = "2.5.29.19";
  28897. const char* ln = "X509v3 Basic Constraints";
  28898. obj = NULL;
  28899. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28900. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28901. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28902. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  28903. XMEMSET(buf, 0, sizeof(buf));
  28904. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28905. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28906. ASN1_OBJECT_free(obj);
  28907. res = EXPECT_RESULT();
  28908. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28909. return res;
  28910. }
  28911. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28912. {
  28913. int res = TEST_SKIPPED;
  28914. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28915. EXPECT_DECLS;
  28916. WOLFSSL_STACK* sk = NULL;
  28917. WOLFSSL_ASN1_OBJECT* obj;
  28918. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28919. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28920. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28921. sk = NULL;
  28922. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28923. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28924. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28925. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28926. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28927. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28928. sk = NULL;
  28929. /* obj freed in pop_free call. */
  28930. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28931. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28932. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28933. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28934. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28935. wolfSSL_ASN1_OBJECT_free(obj);
  28936. res = EXPECT_RESULT();
  28937. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28938. return res;
  28939. }
  28940. static int test_wolfSSL_ASN1_STRING(void)
  28941. {
  28942. int res = TEST_SKIPPED;
  28943. #if defined(OPENSSL_EXTRA)
  28944. EXPECT_DECLS;
  28945. ASN1_STRING* str = NULL;
  28946. ASN1_STRING* c = NULL;
  28947. const char data[] = "hello wolfSSL";
  28948. const char data2[] = "Same len data";
  28949. const char longData[] =
  28950. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28951. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28952. ASN1_STRING_free(str);
  28953. str = NULL;
  28954. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28955. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28956. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28957. /* Check setting to NULL works. */
  28958. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28959. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28960. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28961. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28962. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28963. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28964. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28965. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28966. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28967. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28968. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28969. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28970. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28971. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28972. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28973. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28974. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28975. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28976. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28977. /* Check setting back to smaller size frees dynamic data. */
  28978. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28979. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28980. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28981. ExpectNull(ASN1_STRING_get0_data(NULL));
  28982. ExpectNotNull(ASN1_STRING_get0_data(str));
  28983. ExpectNull(ASN1_STRING_data(NULL));
  28984. ExpectNotNull(ASN1_STRING_data(str));
  28985. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28986. ExpectIntGT(ASN1_STRING_length(str), 0);
  28987. ASN1_STRING_free(c);
  28988. ASN1_STRING_free(str);
  28989. ASN1_STRING_free(NULL);
  28990. #ifndef NO_WOLFSSL_STUB
  28991. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28992. #endif
  28993. res = EXPECT_RESULT();
  28994. #endif
  28995. return res;
  28996. }
  28997. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28998. {
  28999. int res = TEST_SKIPPED;
  29000. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  29001. !defined(NO_FILESYSTEM)
  29002. EXPECT_DECLS;
  29003. WOLFSSL_X509* x509 = NULL;
  29004. WOLFSSL_X509_NAME* subject = NULL;
  29005. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  29006. WOLFSSL_ASN1_STRING* a = NULL;
  29007. FILE* file = XBADFILE;
  29008. int idx = 0;
  29009. char targetOutput[16] = "www.wolfssl.com";
  29010. unsigned char* actual_output = NULL;
  29011. int len = 0;
  29012. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  29013. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  29014. if (file != NULL)
  29015. fclose(file);
  29016. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  29017. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  29018. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  29019. NID_commonName, -1)), 5);
  29020. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  29021. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  29022. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  29023. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  29024. a = NULL;
  29025. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  29026. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  29027. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  29028. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  29029. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  29030. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  29031. wolfSSL_X509_free(x509);
  29032. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29033. ExpectNotNull(a = ASN1_STRING_new());
  29034. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  29035. ASN1_STRING_free(a);
  29036. res = EXPECT_RESULT();
  29037. #endif
  29038. return res;
  29039. }
  29040. static int test_wolfSSL_i2s_ASN1_STRING(void)
  29041. {
  29042. int res = TEST_SKIPPED;
  29043. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29044. EXPECT_DECLS;
  29045. WOLFSSL_ASN1_STRING* str = NULL;
  29046. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  29047. char* ret = NULL;
  29048. ExpectNotNull(str = ASN1_STRING_new());
  29049. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  29050. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29051. ret = NULL;
  29052. /* No data. */
  29053. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29054. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29055. ret = NULL;
  29056. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  29057. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29058. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29059. ret = NULL;
  29060. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  29061. /* No type. */
  29062. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29063. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29064. ASN1_STRING_free(str);
  29065. res = EXPECT_RESULT();
  29066. #endif
  29067. return res;
  29068. }
  29069. static int test_wolfSSL_ASN1_STRING_canon(void)
  29070. {
  29071. int res = TEST_SKIPPED;
  29072. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  29073. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  29074. defined(OPENSSL_EXTRA_X509_SMALL))
  29075. EXPECT_DECLS;
  29076. WOLFSSL_ASN1_STRING* orig = NULL;
  29077. WOLFSSL_ASN1_STRING* canon = NULL;
  29078. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  29079. const char* whitespaceOnly = "\t\r\n";
  29080. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  29081. const char* canonData = "\x01 \x02 \xff tt";
  29082. const char longData[] =
  29083. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  29084. ExpectNotNull(orig = ASN1_STRING_new());
  29085. ExpectNotNull(canon = ASN1_STRING_new());
  29086. /* Invalid parameter testing. */
  29087. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  29088. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  29089. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  29090. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29091. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29092. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  29093. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29094. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29095. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  29096. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29097. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29098. ASN1_STRING_free(orig);
  29099. orig = NULL;
  29100. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29101. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  29102. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29103. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  29104. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29105. ASN1_STRING_free(orig);
  29106. orig = NULL;
  29107. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  29108. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  29109. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29110. ASN1_STRING_free(orig);
  29111. orig = NULL;
  29112. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29113. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29114. ASN1_STRING_free(orig);
  29115. ASN1_STRING_free(canon);
  29116. res = EXPECT_RESULT();
  29117. #endif
  29118. #endif
  29119. return res;
  29120. }
  29121. static int test_wolfSSL_ASN1_STRING_print(void)
  29122. {
  29123. int res = TEST_SKIPPED;
  29124. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  29125. !defined(NO_BIO)
  29126. EXPECT_DECLS;
  29127. ASN1_STRING* asnStr = NULL;
  29128. const char HELLO_DATA[]= \
  29129. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  29130. #define MAX_UNPRINTABLE_CHAR 32
  29131. #define MAX_BUF 255
  29132. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  29133. unsigned char expected[sizeof(unprintableData)+1];
  29134. unsigned char rbuf[MAX_BUF];
  29135. BIO *bio = NULL;
  29136. int p_len;
  29137. int i;
  29138. /* setup */
  29139. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  29140. unprintableData[i] = HELLO_DATA[i];
  29141. expected[i] = HELLO_DATA[i];
  29142. }
  29143. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  29144. unprintableData[sizeof(HELLO_DATA)+i] = i;
  29145. if (i == (int)'\n' || i == (int)'\r')
  29146. expected[sizeof(HELLO_DATA)+i] = i;
  29147. else
  29148. expected[sizeof(HELLO_DATA)+i] = '.';
  29149. }
  29150. unprintableData[sizeof(unprintableData)-1] = '\0';
  29151. expected[sizeof(expected)-1] = '\0';
  29152. XMEMSET(rbuf, 0, MAX_BUF);
  29153. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29154. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  29155. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29156. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  29157. (int)sizeof(unprintableData)), 1);
  29158. /* test */
  29159. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  29160. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  29161. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  29162. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  29163. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  29164. ExpectStrEQ((char*)rbuf, (const char*)expected);
  29165. BIO_free(bio);
  29166. bio = NULL;
  29167. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29168. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29169. /* Ensure there is 0 bytes avaialble to write into. */
  29170. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  29171. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29172. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29173. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29174. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  29175. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29176. BIO_free(bio);
  29177. ASN1_STRING_free(asnStr);
  29178. res = EXPECT_RESULT();
  29179. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  29180. return res;
  29181. }
  29182. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  29183. {
  29184. int res = TEST_SKIPPED;
  29185. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  29186. EXPECT_DECLS;
  29187. ASN1_STRING* asn_str = NULL;
  29188. const char data[] = "Hello wolfSSL!";
  29189. ASN1_STRING* esc_str = NULL;
  29190. const char esc_data[] = "a+;<>";
  29191. ASN1_STRING* neg_int = NULL;
  29192. const char neg_int_data[] = "\xff";
  29193. ASN1_STRING* neg_enum = NULL;
  29194. const char neg_enum_data[] = "\xff";
  29195. BIO *bio = NULL;
  29196. BIO *fixed = NULL;
  29197. unsigned long flags;
  29198. int p_len;
  29199. unsigned char rbuf[255];
  29200. /* setup */
  29201. XMEMSET(rbuf, 0, 255);
  29202. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29203. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  29204. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29205. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29206. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  29207. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29208. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  29209. sizeof(esc_data)), 1);
  29210. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  29211. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  29212. sizeof(neg_int_data) - 1), 1);
  29213. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  29214. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  29215. sizeof(neg_enum_data) - 1), 1);
  29216. /* Invalid parameter testing. */
  29217. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  29218. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  29219. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  29220. /* no flags */
  29221. XMEMSET(rbuf, 0, 255);
  29222. flags = 0;
  29223. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  29224. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  29225. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  29226. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29227. /* Ensure there is 0 bytes avaialble to write into. */
  29228. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29229. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29230. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29231. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29232. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  29233. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29234. /* RFC2253 Escape */
  29235. XMEMSET(rbuf, 0, 255);
  29236. flags = ASN1_STRFLGS_ESC_2253;
  29237. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  29238. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  29239. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  29240. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29241. /* Ensure there is 0 bytes avaialble to write into. */
  29242. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29243. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29244. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29245. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29246. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  29247. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29248. /* Show type */
  29249. XMEMSET(rbuf, 0, 255);
  29250. flags = ASN1_STRFLGS_SHOW_TYPE;
  29251. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  29252. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  29253. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  29254. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29255. /* Ensure there is 0 bytes avaialble to write into. */
  29256. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29257. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29258. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29259. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29260. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  29261. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29262. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  29263. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29264. /* Dump All */
  29265. XMEMSET(rbuf, 0, 255);
  29266. flags = ASN1_STRFLGS_DUMP_ALL;
  29267. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  29268. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  29269. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  29270. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29271. /* Ensure there is 0 bytes avaialble to write into. */
  29272. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29273. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29274. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29275. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29276. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29277. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29278. /* Dump Der */
  29279. XMEMSET(rbuf, 0, 255);
  29280. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  29281. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  29282. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  29283. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  29284. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29285. /* Ensure there is 0 bytes avaialble to write into. */
  29286. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29287. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29288. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29289. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29290. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  29291. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29292. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29293. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29294. /* Dump All + Show type */
  29295. XMEMSET(rbuf, 0, 255);
  29296. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29297. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  29298. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  29299. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  29300. /* Dump All + Show type - Negative Integer. */
  29301. XMEMSET(rbuf, 0, 255);
  29302. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29303. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  29304. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  29305. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  29306. /* Dump All + Show type - Negative Enumerated. */
  29307. XMEMSET(rbuf, 0, 255);
  29308. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29309. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  29310. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  29311. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  29312. BIO_free(fixed);
  29313. BIO_free(bio);
  29314. ASN1_STRING_free(asn_str);
  29315. ASN1_STRING_free(esc_str);
  29316. ASN1_STRING_free(neg_int);
  29317. ASN1_STRING_free(neg_enum);
  29318. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  29319. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  29320. res = EXPECT_RESULT();
  29321. #endif
  29322. return res;
  29323. }
  29324. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  29325. {
  29326. int res = TEST_SKIPPED;
  29327. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  29328. EXPECT_DECLS;
  29329. ASN1_STRING* asn1str_test = NULL;
  29330. ASN1_STRING* asn1str_answer = NULL;
  29331. /* Each character is encoded using 4 bytes */
  29332. char input[] = {
  29333. 0, 0, 0, 'T',
  29334. 0, 0, 0, 'e',
  29335. 0, 0, 0, 's',
  29336. 0, 0, 0, 't',
  29337. };
  29338. char output[] = "Test";
  29339. char badInput[] = {
  29340. 1, 0, 0, 'T',
  29341. 0, 1, 0, 'e',
  29342. 0, 0, 1, 's',
  29343. };
  29344. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  29345. /* Test wrong type. */
  29346. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29347. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29348. ASN1_STRING_free(asn1str_test);
  29349. asn1str_test = NULL;
  29350. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  29351. /* Test bad length. */
  29352. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  29353. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29354. /* Test bad input. */
  29355. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  29356. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29357. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  29358. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29359. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  29360. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29361. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  29362. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  29363. ExpectNotNull(
  29364. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  29365. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  29366. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  29367. ASN1_STRING_free(asn1str_test);
  29368. ASN1_STRING_free(asn1str_answer);
  29369. res = EXPECT_RESULT();
  29370. #endif /* OPENSSL_ALL && !NO_ASN */
  29371. return res;
  29372. }
  29373. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  29374. {
  29375. int res = TEST_SKIPPED;
  29376. #if defined(OPENSSL_EXTRA)
  29377. EXPECT_DECLS;
  29378. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  29379. unsigned char nullstr[32];
  29380. XMEMSET(nullstr, 0, 32);
  29381. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  29382. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  29383. if (asn1_gtime != NULL) {
  29384. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  29385. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  29386. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  29387. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29388. }
  29389. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  29390. res = EXPECT_RESULT();
  29391. #endif /* OPENSSL_EXTRA */
  29392. return res;
  29393. }
  29394. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  29395. {
  29396. int res = TEST_SKIPPED;
  29397. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  29398. EXPECT_DECLS;
  29399. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  29400. BIO* bio = NULL;
  29401. unsigned char buf[24];
  29402. int i;
  29403. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29404. BIO_set_write_buf_size(bio, 24);
  29405. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  29406. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  29407. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  29408. /* Type not set. */
  29409. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  29410. gtime.type = V_ASN1_GENERALIZEDTIME;
  29411. /* Invalid parameters testing. */
  29412. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  29413. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  29414. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  29415. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  29416. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  29417. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  29418. BIO_free(bio);
  29419. bio = NULL;
  29420. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29421. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29422. /* Ensure there is 0 bytes avaialble to write into. */
  29423. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  29424. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  29425. for (i = 1; i < 20; i++) {
  29426. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  29427. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  29428. }
  29429. BIO_free(bio);
  29430. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  29431. res = EXPECT_RESULT();
  29432. #endif /* OPENSSL_EXTRA */
  29433. return res;
  29434. }
  29435. static int test_wolfSSL_ASN1_TIME(void)
  29436. {
  29437. int res = TEST_SKIPPED;
  29438. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  29439. EXPECT_DECLS;
  29440. WOLFSSL_ASN1_TIME* asn_time = NULL;
  29441. unsigned char *data;
  29442. ExpectNotNull(asn_time = ASN1_TIME_new());
  29443. #ifndef NO_WOLFSSL_STUB
  29444. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  29445. #endif
  29446. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  29447. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  29448. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  29449. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  29450. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  29451. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  29452. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  29453. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  29454. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  29455. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  29456. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  29457. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  29458. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  29459. ASN1_TIME_free(asn_time);
  29460. ASN1_TIME_free(NULL);
  29461. res = EXPECT_RESULT();
  29462. #endif
  29463. return res;
  29464. }
  29465. static int test_wolfSSL_ASN1_TIME_to_string(void)
  29466. {
  29467. int res = TEST_SKIPPED;
  29468. #ifndef NO_ASN_TIME
  29469. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29470. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  29471. EXPECT_DECLS;
  29472. WOLFSSL_ASN1_TIME* t = NULL;
  29473. char buf[ASN_GENERALIZED_TIME_SIZE];
  29474. ExpectNotNull((t = ASN1_TIME_new()));
  29475. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  29476. /* Invalid parameter testing. */
  29477. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  29478. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  29479. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  29480. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  29481. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  29482. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  29483. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  29484. /* Buffer needs to be longer than minimum of 5 characters. */
  29485. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  29486. ASN1_TIME_free(t);
  29487. res = EXPECT_RESULT();
  29488. #endif
  29489. #endif /* NO_ASN_TIME */
  29490. return res;
  29491. }
  29492. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  29493. {
  29494. int res = TEST_SKIPPED;
  29495. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  29496. EXPECT_DECLS;
  29497. ASN1_TIME* fromTime = NULL;
  29498. ASN1_TIME* closeToTime = NULL;
  29499. ASN1_TIME* toTime = NULL;
  29500. ASN1_TIME* invalidTime = NULL;
  29501. int daysDiff;
  29502. int secsDiff;
  29503. ExpectNotNull((fromTime = ASN1_TIME_new()));
  29504. /* Feb 22, 2003, 21:15:15 */
  29505. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  29506. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  29507. /* Feb 22, 2003, 21:16:15 */
  29508. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  29509. ExpectNotNull((toTime = ASN1_TIME_new()));
  29510. /* Dec 19, 2010, 18:10:11 */
  29511. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  29512. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  29513. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  29514. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  29515. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  29516. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  29517. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  29518. /* Error conditions. */
  29519. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  29520. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  29521. /* If both times are NULL, difference is 0. */
  29522. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  29523. ExpectIntEQ(daysDiff, 0);
  29524. ExpectIntEQ(secsDiff, 0);
  29525. /* If one time is NULL, it defaults to the current time. */
  29526. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  29527. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  29528. /* Normal operation. Both times non-NULL. */
  29529. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  29530. ExpectIntEQ(daysDiff, 2856);
  29531. ExpectIntEQ(secsDiff, 75296);
  29532. /* Swapping the times should return negative values. */
  29533. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  29534. ExpectIntEQ(daysDiff, -2856);
  29535. ExpectIntEQ(secsDiff, -75296);
  29536. /* Compare with invalid time string. */
  29537. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  29538. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  29539. /* Compare with days difference of 0. */
  29540. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  29541. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  29542. /* Days and seconds differences not 0. */
  29543. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  29544. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  29545. /* Same time. */
  29546. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  29547. /* Compare regression test: No seconds difference, just difference in days.
  29548. */
  29549. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  29550. ASN1_TIME_set_string(toTime, "19800101000000Z");
  29551. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  29552. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  29553. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  29554. /* Edge case with Unix epoch. */
  29555. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  29556. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  29557. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  29558. ExpectIntEQ(daysDiff, 3652);
  29559. ExpectIntEQ(secsDiff, 0);
  29560. /* Edge case with year > 2038 (year 2038 problem). */
  29561. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  29562. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  29563. ExpectIntEQ(daysDiff, 2932896);
  29564. ExpectIntEQ(secsDiff, 86399);
  29565. ASN1_TIME_free(fromTime);
  29566. ASN1_TIME_free(closeToTime);
  29567. ASN1_TIME_free(toTime);
  29568. ASN1_TIME_free(invalidTime);
  29569. res = EXPECT_RESULT();
  29570. #endif
  29571. return res;
  29572. }
  29573. static int test_wolfSSL_ASN1_TIME_adj(void)
  29574. {
  29575. int res = TEST_SKIPPED;
  29576. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  29577. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  29578. EXPECT_DECLS;
  29579. const int year = 365*24*60*60;
  29580. const int day = 24*60*60;
  29581. const int hour = 60*60;
  29582. const int mini = 60;
  29583. const byte asn_utc_time = ASN_UTC_TIME;
  29584. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  29585. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  29586. #endif
  29587. WOLFSSL_ASN1_TIME* asn_time = NULL;
  29588. WOLFSSL_ASN1_TIME* s = NULL;
  29589. int offset_day;
  29590. long offset_sec;
  29591. char date_str[CTC_DATE_SIZE + 1];
  29592. time_t t;
  29593. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  29594. /* UTC notation test */
  29595. /* 2000/2/15 20:30:00 */
  29596. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  29597. offset_day = 7;
  29598. offset_sec = 45 * mini;
  29599. /* offset_sec = -45 * min;*/
  29600. ExpectNotNull(asn_time =
  29601. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  29602. ExpectTrue(asn_time->type == asn_utc_time);
  29603. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  29604. CTC_DATE_SIZE));
  29605. date_str[CTC_DATE_SIZE] = '\0';
  29606. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  29607. /* negative offset */
  29608. offset_sec = -45 * mini;
  29609. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  29610. ExpectNotNull(asn_time);
  29611. ExpectTrue(asn_time->type == asn_utc_time);
  29612. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  29613. CTC_DATE_SIZE));
  29614. date_str[CTC_DATE_SIZE] = '\0';
  29615. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  29616. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  29617. s = NULL;
  29618. XMEMSET(date_str, 0, sizeof(date_str));
  29619. /* Generalized time will overflow time_t if not long */
  29620. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  29621. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  29622. DYNAMIC_TYPE_OPENSSL);
  29623. /* GeneralizedTime notation test */
  29624. /* 2055/03/01 09:00:00 */
  29625. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  29626. offset_day = 12;
  29627. offset_sec = 10 * mini;
  29628. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  29629. offset_sec));
  29630. ExpectTrue(asn_time->type == asn_gen_time);
  29631. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  29632. CTC_DATE_SIZE));
  29633. date_str[CTC_DATE_SIZE] = '\0';
  29634. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  29635. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  29636. s = NULL;
  29637. XMEMSET(date_str, 0, sizeof(date_str));
  29638. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  29639. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  29640. s = NULL;
  29641. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  29642. offset_day = 7;
  29643. offset_sec = 45 * mini;
  29644. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  29645. offset_sec));
  29646. ExpectTrue(asn_time->type == asn_utc_time);
  29647. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  29648. CTC_DATE_SIZE));
  29649. date_str[CTC_DATE_SIZE] = '\0';
  29650. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  29651. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  29652. asn_time = NULL;
  29653. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  29654. offset_sec));
  29655. ExpectTrue(asn_time->type == asn_utc_time);
  29656. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  29657. CTC_DATE_SIZE));
  29658. date_str[CTC_DATE_SIZE] = '\0';
  29659. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  29660. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  29661. res = EXPECT_RESULT();
  29662. #endif
  29663. return res;
  29664. }
  29665. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  29666. {
  29667. int res = TEST_SKIPPED;
  29668. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29669. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29670. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  29671. EXPECT_DECLS;
  29672. ASN1_TIME asnTime;
  29673. struct tm tm;
  29674. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  29675. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  29676. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  29677. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  29678. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29679. ExpectIntEQ(tm.tm_sec, 15);
  29680. ExpectIntEQ(tm.tm_min, 15);
  29681. ExpectIntEQ(tm.tm_hour, 21);
  29682. ExpectIntEQ(tm.tm_mday, 22);
  29683. ExpectIntEQ(tm.tm_mon, 1);
  29684. ExpectIntEQ(tm.tm_year, 100);
  29685. ExpectIntEQ(tm.tm_isdst, 0);
  29686. #ifdef XMKTIME
  29687. ExpectIntEQ(tm.tm_wday, 2);
  29688. ExpectIntEQ(tm.tm_yday, 52);
  29689. #endif
  29690. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  29691. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29692. ExpectIntEQ(tm.tm_year, 50);
  29693. /* Get current time. */
  29694. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  29695. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  29696. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  29697. /* 0 length. */
  29698. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29699. /* No type. */
  29700. asnTime.length = 1;
  29701. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29702. /* Not UTCTIME length. */
  29703. asnTime.type = V_ASN1_UTCTIME;
  29704. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29705. /* Not GENERALIZEDTIME length. */
  29706. asnTime.type = V_ASN1_GENERALIZEDTIME;
  29707. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29708. /* Not Zulu timezone. */
  29709. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  29710. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29711. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  29712. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29713. #ifdef XMKTIME
  29714. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  29715. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29716. ExpectIntEQ(tm.tm_sec, 47);
  29717. ExpectIntEQ(tm.tm_min, 22);
  29718. ExpectIntEQ(tm.tm_hour, 21);
  29719. ExpectIntEQ(tm.tm_mday, 12);
  29720. ExpectIntEQ(tm.tm_mon, 4);
  29721. ExpectIntEQ(tm.tm_year, 123);
  29722. ExpectIntEQ(tm.tm_wday, 5);
  29723. ExpectIntEQ(tm.tm_yday, 131);
  29724. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  29725. fields are zeroed out as expected. */
  29726. ExpectIntEQ(tm.tm_isdst, 0);
  29727. #endif
  29728. res = EXPECT_RESULT();
  29729. #endif
  29730. return res;
  29731. }
  29732. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  29733. {
  29734. int res = TEST_SKIPPED;
  29735. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  29736. EXPECT_DECLS;
  29737. WOLFSSL_ASN1_TIME *t = NULL;
  29738. WOLFSSL_ASN1_TIME *out = NULL;
  29739. WOLFSSL_ASN1_TIME *gtime = NULL;
  29740. int tlen = 0;
  29741. unsigned char *data = NULL;
  29742. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  29743. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  29744. /* type not set. */
  29745. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29746. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29747. t = NULL;
  29748. /* UTC Time test */
  29749. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  29750. if (t != NULL) {
  29751. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  29752. t->type = ASN_UTC_TIME;
  29753. t->length = ASN_UTC_TIME_SIZE;
  29754. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29755. }
  29756. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  29757. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  29758. out = NULL;
  29759. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29760. wolfSSL_ASN1_TIME_free(gtime);
  29761. gtime = NULL;
  29762. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  29763. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29764. ExpectPtrEq(gtime, out);
  29765. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29766. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29767. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  29768. /* Generalized Time test */
  29769. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  29770. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  29771. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  29772. if (t != NULL) {
  29773. t->type = ASN_GENERALIZED_TIME;
  29774. t->length = ASN_GENERALIZED_TIME_SIZE;
  29775. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  29776. }
  29777. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  29778. ASN_GENERALIZED_TIME_SIZE);
  29779. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  29780. "20050727123456Z");
  29781. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29782. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29783. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29784. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  29785. /* UTC Time to Generalized Time 1900's test */
  29786. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  29787. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  29788. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  29789. if (t != NULL) {
  29790. t->type = ASN_UTC_TIME;
  29791. t->length = ASN_UTC_TIME_SIZE;
  29792. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  29793. }
  29794. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29795. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29796. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29797. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  29798. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29799. /* Null parameter test */
  29800. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  29801. gtime = NULL;
  29802. out = NULL;
  29803. if (t != NULL) {
  29804. t->type = ASN_UTC_TIME;
  29805. t->length = ASN_UTC_TIME_SIZE;
  29806. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29807. }
  29808. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  29809. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29810. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29811. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  29812. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29813. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29814. res = EXPECT_RESULT();
  29815. #endif
  29816. return res;
  29817. }
  29818. static int test_wolfSSL_ASN1_TIME_print(void)
  29819. {
  29820. int res = TEST_SKIPPED;
  29821. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  29822. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29823. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29824. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  29825. !defined(NO_ASN_TIME)
  29826. EXPECT_DECLS;
  29827. BIO* bio = NULL;
  29828. BIO* fixed = NULL;
  29829. X509* x509 = NULL;
  29830. const unsigned char* der = client_cert_der_2048;
  29831. ASN1_TIME* notAfter;
  29832. ASN1_TIME* notBefore;
  29833. unsigned char buf[25];
  29834. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29835. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29836. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  29837. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  29838. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  29839. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  29840. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  29841. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  29842. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  29843. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29844. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  29845. /* Test BIO_write fails. */
  29846. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29847. /* Ensure there is 0 bytes avaialble to write into. */
  29848. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  29849. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29850. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29851. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29852. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  29853. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29854. /* create a bad time and test results */
  29855. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  29856. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  29857. if (EXPECT_SUCCESS()) {
  29858. notAfter->data[8] = 0;
  29859. notAfter->data[3] = 0;
  29860. }
  29861. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  29862. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29863. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29864. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  29865. BIO_free(bio);
  29866. BIO_free(fixed);
  29867. X509_free(x509);
  29868. res = EXPECT_RESULT();
  29869. #endif
  29870. return res;
  29871. }
  29872. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  29873. {
  29874. int res = TEST_SKIPPED;
  29875. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  29876. EXPECT_DECLS;
  29877. BIO* bio = NULL;
  29878. ASN1_UTCTIME* utc = NULL;
  29879. unsigned char buf[25];
  29880. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  29881. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  29882. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  29883. /* Valid date */
  29884. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29885. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  29886. DYNAMIC_TYPE_ASN1));
  29887. if (utc != NULL) {
  29888. utc->type = ASN_UTC_TIME;
  29889. utc->length = ASN_UTC_TIME_SIZE;
  29890. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  29891. }
  29892. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  29893. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  29894. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  29895. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  29896. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29897. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  29898. XMEMSET(buf, 0, sizeof(buf));
  29899. BIO_free(bio);
  29900. bio = NULL;
  29901. /* Invalid format */
  29902. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29903. if (utc != NULL) {
  29904. utc->type = ASN_UTC_TIME;
  29905. utc->length = ASN_UTC_TIME_SIZE;
  29906. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  29907. }
  29908. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29909. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29910. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29911. /* Invalid type */
  29912. if (utc != NULL) {
  29913. utc->type = ASN_GENERALIZED_TIME;
  29914. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29915. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29916. }
  29917. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29918. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29919. BIO_free(bio);
  29920. res = EXPECT_RESULT();
  29921. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29922. return res;
  29923. }
  29924. static int test_wolfSSL_ASN1_TYPE(void)
  29925. {
  29926. int res = TEST_SKIPPED;
  29927. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29928. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29929. EXPECT_DECLS;
  29930. WOLFSSL_ASN1_TYPE* t = NULL;
  29931. WOLFSSL_ASN1_OBJECT* obj = NULL;
  29932. #ifndef NO_ASN_TIME
  29933. WOLFSSL_ASN1_TIME* time = NULL;
  29934. #endif
  29935. WOLFSSL_ASN1_STRING* str = NULL;
  29936. unsigned char data[] = { 0x00 };
  29937. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29938. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29939. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29940. wolfSSL_ASN1_TYPE_free(t);
  29941. t = NULL;
  29942. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29943. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29944. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29945. wolfSSL_ASN1_TYPE_free(t);
  29946. t = NULL;
  29947. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29948. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29949. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29950. wolfSSL_ASN1_TYPE_free(t);
  29951. t = NULL;
  29952. #ifndef NO_ASN_TIME
  29953. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29954. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29955. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29956. wolfSSL_ASN1_TYPE_free(t);
  29957. t = NULL;
  29958. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29959. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29960. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29961. wolfSSL_ASN1_TYPE_free(t);
  29962. t = NULL;
  29963. #endif
  29964. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29965. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29966. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29967. wolfSSL_ASN1_TYPE_free(t);
  29968. t = NULL;
  29969. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29970. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29971. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29972. wolfSSL_ASN1_TYPE_free(t);
  29973. t = NULL;
  29974. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29975. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29976. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29977. wolfSSL_ASN1_TYPE_free(t);
  29978. t = NULL;
  29979. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29980. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29981. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29982. wolfSSL_ASN1_TYPE_free(t);
  29983. t = NULL;
  29984. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29985. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29986. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29987. wolfSSL_ASN1_TYPE_free(t);
  29988. t = NULL;
  29989. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29990. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29991. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29992. wolfSSL_ASN1_TYPE_free(t);
  29993. t = NULL;
  29994. res = EXPECT_RESULT();
  29995. #endif
  29996. return res;
  29997. }
  29998. /* Testing code used in dpp.c in hostap */
  29999. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30000. typedef struct {
  30001. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  30002. * as an OID identifying the curve */
  30003. X509_ALGOR *alg;
  30004. /* Compressed format public key per ANSI X9.63 */
  30005. ASN1_BIT_STRING *pub_key;
  30006. } DPP_BOOTSTRAPPING_KEY;
  30007. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  30008. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  30009. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  30010. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  30011. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  30012. typedef struct {
  30013. ASN1_INTEGER *integer;
  30014. } TEST_ASN1;
  30015. ASN1_SEQUENCE(TEST_ASN1) = {
  30016. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  30017. } ASN1_SEQUENCE_END(TEST_ASN1)
  30018. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  30019. typedef struct {
  30020. ASN1_OCTET_STRING *octet_string;
  30021. } TEST_FAIL_ASN1;
  30022. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  30023. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  30024. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  30025. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  30026. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  30027. #endif
  30028. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  30029. {
  30030. int res = TEST_SKIPPED;
  30031. /* Testing code used in dpp.c in hostap */
  30032. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30033. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  30034. EXPECT_DECLS;
  30035. EC_KEY *eckey = NULL;
  30036. EVP_PKEY *key = NULL;
  30037. size_t len;
  30038. unsigned char *der = NULL;
  30039. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  30040. const unsigned char *in = ecc_clikey_der_256;
  30041. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  30042. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  30043. const EC_GROUP *group = NULL;
  30044. const EC_POINT *point = NULL;
  30045. int nid;
  30046. TEST_ASN1 *test_asn1 = NULL;
  30047. TEST_FAIL_ASN1 test_fail_asn1;
  30048. const unsigned char badObjDer[] = { 0x06, 0x00 };
  30049. const unsigned char goodObjDer[] = {
  30050. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  30051. };
  30052. WOLFSSL_ASN1_ITEM emptyTemplate;
  30053. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  30054. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30055. der = NULL;
  30056. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  30057. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  30058. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30059. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  30060. (long)sizeof_ecc_clikey_der_256));
  30061. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  30062. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  30063. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  30064. nid = EC_GROUP_get_curve_name(group);
  30065. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  30066. group_obj = OBJ_nid2obj(nid);
  30067. if ((ec_obj != NULL) && (group_obj != NULL)) {
  30068. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  30069. group_obj), 1);
  30070. if (EXPECT_SUCCESS()) {
  30071. ec_obj = NULL;
  30072. group_obj = NULL;
  30073. }
  30074. }
  30075. wolfSSL_ASN1_OBJECT_free(group_obj);
  30076. wolfSSL_ASN1_OBJECT_free(ec_obj);
  30077. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  30078. #ifdef HAVE_COMP_KEY
  30079. ExpectIntGT((len = EC_POINT_point2oct(
  30080. group, point, POINT_CONVERSION_COMPRESSED,
  30081. NULL, 0, NULL)), 0);
  30082. #else
  30083. ExpectIntGT((len = EC_POINT_point2oct(
  30084. group, point, POINT_CONVERSION_UNCOMPRESSED,
  30085. NULL, 0, NULL)), 0);
  30086. #endif
  30087. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  30088. #ifdef HAVE_COMP_KEY
  30089. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30090. der, len-1, NULL), 0);
  30091. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30092. der, len, NULL), len);
  30093. #else
  30094. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30095. der, len-1, NULL), 0);
  30096. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30097. der, len, NULL), len);
  30098. #endif
  30099. if (EXPECT_SUCCESS()) {
  30100. bootstrap->pub_key->data = der;
  30101. bootstrap->pub_key->length = (int)len;
  30102. /* Not actually used */
  30103. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30104. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30105. }
  30106. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  30107. der = NULL;
  30108. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30109. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30110. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30111. EVP_PKEY_free(key);
  30112. EC_KEY_free(eckey);
  30113. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30114. bootstrap = NULL;
  30115. DPP_BOOTSTRAPPING_KEY_free(NULL);
  30116. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  30117. * a NULL and an empty BIT_STRING. */
  30118. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30119. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  30120. if (EXPECT_SUCCESS()) {
  30121. bootstrap->alg->algorithm->obj = badObjDer;
  30122. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  30123. }
  30124. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  30125. if (EXPECT_SUCCESS()) {
  30126. bootstrap->alg->parameter->type = V_ASN1_NULL;
  30127. bootstrap->alg->parameter->value.ptr = NULL;
  30128. bootstrap->pub_key->data = NULL;
  30129. bootstrap->pub_key->length = 0;
  30130. /* Not actually used */
  30131. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30132. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30133. }
  30134. /* Encode with bad OBJECT_ID. */
  30135. der = NULL;
  30136. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30137. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  30138. if (EXPECT_SUCCESS()) {
  30139. bootstrap->alg->algorithm->obj = goodObjDer;
  30140. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  30141. bootstrap->alg->algorithm->grp = 2;
  30142. }
  30143. der = NULL;
  30144. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  30145. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  30146. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30147. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30148. /* Test integer */
  30149. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  30150. der = NULL;
  30151. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  30152. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  30153. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30154. TEST_ASN1_free(test_asn1);
  30155. /* Test integer cases. */
  30156. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30157. TEST_ASN1_free(NULL);
  30158. /* Test error cases. */
  30159. ExpectNull(TEST_FAIL_ASN1_new());
  30160. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30161. TEST_FAIL_ASN1_free(NULL);
  30162. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  30163. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  30164. res = EXPECT_RESULT();
  30165. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  30166. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  30167. return res;
  30168. }
  30169. static int test_wolfSSL_lhash(void)
  30170. {
  30171. int res = TEST_SKIPPED;
  30172. #ifdef OPENSSL_ALL
  30173. EXPECT_DECLS;
  30174. const char testStr[] = "Like a true nature's child\n"
  30175. "We were born\n"
  30176. "Born to be wild";
  30177. #ifdef NO_SHA
  30178. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  30179. #else
  30180. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  30181. #endif
  30182. res = EXPECT_RESULT();
  30183. #endif
  30184. return res;
  30185. }
  30186. static int test_wolfSSL_X509_NAME(void)
  30187. {
  30188. int res = TEST_SKIPPED;
  30189. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30190. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30191. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  30192. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  30193. defined(OPENSSL_EXTRA))
  30194. EXPECT_DECLS;
  30195. X509* x509 = NULL;
  30196. const unsigned char* c;
  30197. unsigned char buf[4096];
  30198. int bytes;
  30199. XFILE f = XBADFILE;
  30200. const X509_NAME* a = NULL;
  30201. const X509_NAME* b = NULL;
  30202. X509_NAME* d2i_name = NULL;
  30203. int sz = 0;
  30204. unsigned char* tmp;
  30205. char file[] = "./certs/ca-cert.der";
  30206. #ifndef OPENSSL_EXTRA_X509_SMALL
  30207. byte empty[] = { /* CN=empty emailAddress= */
  30208. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  30209. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  30210. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  30211. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  30212. 0x01, 0x16, 0x00
  30213. };
  30214. #endif
  30215. #ifndef OPENSSL_EXTRA_X509_SMALL
  30216. /* test compile of deprecated function, returns 0 */
  30217. ExpectIntEQ(CRYPTO_thread_id(), 0);
  30218. #endif
  30219. ExpectNotNull(a = X509_NAME_new());
  30220. X509_NAME_free((X509_NAME*)a);
  30221. a = NULL;
  30222. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30223. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  30224. if (f != XBADFILE)
  30225. XFCLOSE(f);
  30226. c = buf;
  30227. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  30228. /* test cmp function */
  30229. ExpectNotNull(a = X509_get_issuer_name(x509));
  30230. ExpectNotNull(b = X509_get_subject_name(x509));
  30231. #ifndef OPENSSL_EXTRA_X509_SMALL
  30232. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  30233. #endif
  30234. tmp = buf;
  30235. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  30236. if (sz > 0 && tmp == buf) {
  30237. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  30238. __LINE__);
  30239. fprintf(stderr, " Expected pointer to be incremented\n");
  30240. abort();
  30241. }
  30242. #ifndef OPENSSL_EXTRA_X509_SMALL
  30243. tmp = buf;
  30244. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30245. #endif
  30246. /* if output parameter is NULL, should still return required size. */
  30247. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  30248. /* retry but with the function creating a buffer */
  30249. tmp = NULL;
  30250. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30251. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30252. tmp = NULL;
  30253. #ifdef WOLFSSL_CERT_NAME_ALL
  30254. /* test for givenName and name */
  30255. {
  30256. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  30257. const byte gName[] = "test-given-name";
  30258. const byte name[] = "test-name";
  30259. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30260. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  30261. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30262. 1);
  30263. wolfSSL_X509_NAME_ENTRY_free(entry);
  30264. entry = NULL;
  30265. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30266. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  30267. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30268. 1);
  30269. wolfSSL_X509_NAME_ENTRY_free(entry);
  30270. tmp = NULL;
  30271. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30272. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30273. }
  30274. #endif
  30275. b = NULL;
  30276. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  30277. #ifndef OPENSSL_EXTRA_X509_SMALL
  30278. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  30279. #endif
  30280. X509_NAME_free((X509_NAME*)b);
  30281. X509_NAME_free(d2i_name);
  30282. d2i_name = NULL;
  30283. X509_free(x509);
  30284. #ifndef OPENSSL_EXTRA_X509_SMALL
  30285. /* test with an empty domain component */
  30286. tmp = empty;
  30287. sz = sizeof(empty);
  30288. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30289. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  30290. /* size of empty emailAddress will be 0 */
  30291. tmp = buf;
  30292. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  30293. (char*)tmp, sizeof(buf)), 0);
  30294. /* should contain no organization name */
  30295. tmp = buf;
  30296. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  30297. (char*)tmp, sizeof(buf)), -1);
  30298. X509_NAME_free(d2i_name);
  30299. #endif
  30300. res = EXPECT_RESULT();
  30301. #endif
  30302. return res;
  30303. }
  30304. static int test_wolfSSL_X509_NAME_hash(void)
  30305. {
  30306. int res = TEST_SKIPPED;
  30307. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  30308. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  30309. EXPECT_DECLS;
  30310. BIO* bio = NULL;
  30311. X509* x509 = NULL;
  30312. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30313. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30314. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30315. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  30316. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  30317. X509_free(x509);
  30318. BIO_free(bio);
  30319. res = EXPECT_RESULT();
  30320. #endif
  30321. return res;
  30322. }
  30323. static int test_wolfSSL_X509_NAME_print_ex(void)
  30324. {
  30325. int res = TEST_SKIPPED;
  30326. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  30327. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  30328. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  30329. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  30330. !defined(NO_BIO) && !defined(NO_RSA)
  30331. EXPECT_DECLS;
  30332. int memSz;
  30333. byte* mem = NULL;
  30334. BIO* bio = NULL;
  30335. BIO* membio = NULL;
  30336. X509* x509 = NULL;
  30337. X509_NAME* name = NULL;
  30338. const char* expNormal = "C=US, CN=wolfssl.com";
  30339. const char* expReverse = "CN=wolfssl.com, C=US";
  30340. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  30341. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  30342. const char* expRFC5523 =
  30343. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  30344. /* Test with real cert (svrCertFile) first */
  30345. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30346. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30347. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30348. ExpectNotNull(name = X509_get_subject_name(x509));
  30349. /* Test without flags */
  30350. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30351. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30352. BIO_free(membio);
  30353. membio = NULL;
  30354. /* Test flag: XN_FLAG_RFC2253 */
  30355. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30356. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30357. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30358. BIO_free(membio);
  30359. membio = NULL;
  30360. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  30361. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30362. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30363. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30364. BIO_free(membio);
  30365. membio = NULL;
  30366. X509_free(x509);
  30367. BIO_free(bio);
  30368. name = NULL;
  30369. /* Test normal case without escaped characters */
  30370. {
  30371. /* Create name: "/C=US/CN=wolfssl.com" */
  30372. ExpectNotNull(name = X509_NAME_new());
  30373. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  30374. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  30375. WOLFSSL_SUCCESS);
  30376. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  30377. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  30378. WOLFSSL_SUCCESS);
  30379. /* Test without flags */
  30380. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30381. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30382. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30383. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  30384. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  30385. BIO_free(membio);
  30386. membio = NULL;
  30387. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  30388. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30389. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30390. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30391. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30392. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  30393. BIO_free(membio);
  30394. membio = NULL;
  30395. /* Test flags: XN_FLAG_DN_REV - reversed */
  30396. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30397. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30398. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30399. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30400. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  30401. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  30402. BIO_free(membio);
  30403. membio = NULL;
  30404. X509_NAME_free(name);
  30405. name = NULL;
  30406. }
  30407. /* Test RFC2253 characters are escaped with backslashes */
  30408. {
  30409. ExpectNotNull(name = X509_NAME_new());
  30410. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  30411. /* space at beginning and end, and: ,+"\ */
  30412. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  30413. WOLFSSL_SUCCESS);
  30414. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  30415. /* # at beginning, and: <>;*/
  30416. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  30417. WOLFSSL_SUCCESS);
  30418. /* Test without flags */
  30419. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30420. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30421. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30422. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  30423. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  30424. XSTRLEN(expNotEscaped)), 0);
  30425. BIO_free(membio);
  30426. membio = NULL;
  30427. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  30428. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30429. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30430. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30431. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30432. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  30433. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  30434. BIO_free(membio);
  30435. membio = NULL;
  30436. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  30437. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30438. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30439. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30440. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30441. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  30442. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  30443. XSTRLEN(expNotEscapedRev)), 0);
  30444. BIO_free(membio);
  30445. X509_NAME_free(name);
  30446. }
  30447. res = EXPECT_RESULT();
  30448. #endif
  30449. return res;
  30450. }
  30451. #ifndef NO_BIO
  30452. static int test_wolfSSL_X509_INFO_multiple_info(void)
  30453. {
  30454. int res = TEST_SKIPPED;
  30455. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  30456. EXPECT_DECLS;
  30457. STACK_OF(X509_INFO) *info_stack = NULL;
  30458. X509_INFO *info = NULL;
  30459. int len;
  30460. int i;
  30461. const char* files[] = {
  30462. cliCertFile,
  30463. cliKeyFile,
  30464. /* This needs to be the order as svrCertFile contains the
  30465. * intermediate cert as well. */
  30466. svrKeyFile,
  30467. svrCertFile,
  30468. NULL,
  30469. };
  30470. const char** curFile;
  30471. BIO *fileBIO = NULL;
  30472. BIO *concatBIO = NULL;
  30473. byte tmp[FOURK_BUF];
  30474. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  30475. * to group objects together. */
  30476. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  30477. for (curFile = files; *curFile != NULL; curFile++) {
  30478. int fileLen;
  30479. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  30480. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  30481. if (EXPECT_SUCCESS()) {
  30482. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  30483. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  30484. fileLen -= len;
  30485. }
  30486. /* Make sure we read the entire file */
  30487. ExpectIntEQ(fileLen, 0);
  30488. }
  30489. BIO_free(fileBIO);
  30490. fileBIO = NULL;
  30491. }
  30492. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  30493. NULL));
  30494. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  30495. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  30496. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  30497. ExpectNotNull(info->x509);
  30498. ExpectNull(info->crl);
  30499. if (i != 0) {
  30500. ExpectNotNull(info->x_pkey);
  30501. ExpectIntEQ(X509_check_private_key(info->x509,
  30502. info->x_pkey->dec_pkey), 1);
  30503. }
  30504. else {
  30505. ExpectNull(info->x_pkey);
  30506. }
  30507. }
  30508. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  30509. BIO_free(concatBIO);
  30510. res = EXPECT_RESULT();
  30511. #endif
  30512. return res;
  30513. }
  30514. #endif
  30515. #ifndef NO_BIO
  30516. static int test_wolfSSL_X509_INFO(void)
  30517. {
  30518. int res = TEST_SKIPPED;
  30519. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  30520. EXPECT_DECLS;
  30521. STACK_OF(X509_INFO) *info_stack = NULL;
  30522. X509_INFO *info = NULL;
  30523. BIO *cert = NULL;
  30524. int i;
  30525. /* PEM in hex format to avoid null terminator */
  30526. byte data[] = {
  30527. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  30528. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  30529. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  30530. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  30531. 0x2d, 0x2d, 0x2d
  30532. };
  30533. /* PEM in hex format to avoid null terminator */
  30534. byte data2[] = {
  30535. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  30536. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  30537. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  30538. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  30539. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  30540. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  30541. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  30542. };
  30543. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  30544. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  30545. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  30546. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  30547. ExpectNotNull(info->x509);
  30548. ExpectNull(info->crl);
  30549. ExpectNull(info->x_pkey);
  30550. }
  30551. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  30552. info_stack = NULL;
  30553. BIO_free(cert);
  30554. cert = NULL;
  30555. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  30556. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  30557. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  30558. info_stack = NULL;
  30559. BIO_free(cert);
  30560. cert = NULL;
  30561. /* This case should fail due to invalid input. */
  30562. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  30563. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  30564. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  30565. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  30566. info_stack = NULL;
  30567. BIO_free(cert);
  30568. cert = NULL;
  30569. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  30570. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  30571. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  30572. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  30573. BIO_free(cert);
  30574. res = EXPECT_RESULT();
  30575. #endif
  30576. return res;
  30577. }
  30578. #endif
  30579. static int test_wolfSSL_X509_subject_name_hash(void)
  30580. {
  30581. int res = TEST_SKIPPED;
  30582. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  30583. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  30584. EXPECT_DECLS;
  30585. X509* x509 = NULL;
  30586. X509_NAME* subjectName = NULL;
  30587. unsigned long ret1 = 0;
  30588. unsigned long ret2 = 0;
  30589. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30590. SSL_FILETYPE_PEM));
  30591. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  30592. /* These two
  30593. * - X509_subject_name_hash(x509)
  30594. * - X509_NAME_hash(X509_get_subject_name(x509))
  30595. * should give the same hash, if !defined(NO_SHA) is true. */
  30596. ret1 = X509_subject_name_hash(x509);
  30597. ExpectIntNE(ret1, 0);
  30598. #if !defined(NO_SHA)
  30599. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  30600. ExpectIntNE(ret2, 0);
  30601. ExpectIntEQ(ret1, ret2);
  30602. #else
  30603. (void) ret2;
  30604. #endif
  30605. X509_free(x509);
  30606. res = EXPECT_RESULT();
  30607. #endif
  30608. return res;
  30609. }
  30610. static int test_wolfSSL_X509_issuer_name_hash(void)
  30611. {
  30612. int res = TEST_SKIPPED;
  30613. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  30614. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  30615. EXPECT_DECLS;
  30616. X509* x509 = NULL;
  30617. X509_NAME* issuertName = NULL;
  30618. unsigned long ret1 = 0;
  30619. unsigned long ret2 = 0;
  30620. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30621. SSL_FILETYPE_PEM));
  30622. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  30623. /* These two
  30624. * - X509_issuer_name_hash(x509)
  30625. * - X509_NAME_hash(X509_get_issuer_name(x509))
  30626. * should give the same hash, if !defined(NO_SHA) is true. */
  30627. ret1 = X509_issuer_name_hash(x509);
  30628. ExpectIntNE(ret1, 0);
  30629. #if !defined(NO_SHA)
  30630. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  30631. ExpectIntNE(ret2, 0);
  30632. ExpectIntEQ(ret1, ret2);
  30633. #else
  30634. (void) ret2;
  30635. #endif
  30636. X509_free(x509);
  30637. res = EXPECT_RESULT();
  30638. #endif
  30639. return res;
  30640. }
  30641. static int test_wolfSSL_X509_check_host(void)
  30642. {
  30643. int res = TEST_SKIPPED;
  30644. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  30645. && !defined(NO_SHA) && !defined(NO_RSA)
  30646. EXPECT_DECLS;
  30647. X509* x509 = NULL;
  30648. const char altName[] = "example.com";
  30649. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30650. SSL_FILETYPE_PEM));
  30651. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  30652. WOLFSSL_SUCCESS);
  30653. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  30654. WOLFSSL_FAILURE);
  30655. X509_free(x509);
  30656. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  30657. WOLFSSL_FAILURE);
  30658. res = EXPECT_RESULT();
  30659. #endif
  30660. return res;
  30661. }
  30662. static int test_wolfSSL_X509_check_email(void)
  30663. {
  30664. int res = TEST_SKIPPED;
  30665. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  30666. EXPECT_DECLS;
  30667. X509* x509 = NULL;
  30668. const char goodEmail[] = "info@wolfssl.com";
  30669. const char badEmail[] = "disinfo@wolfssl.com";
  30670. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30671. SSL_FILETYPE_PEM));
  30672. /* Should fail on non-matching email address */
  30673. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  30674. WOLFSSL_FAILURE);
  30675. /* Should succeed on matching email address */
  30676. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  30677. WOLFSSL_SUCCESS);
  30678. /* Should compute length internally when not provided */
  30679. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  30680. WOLFSSL_SUCCESS);
  30681. /* Should fail when email address is NULL */
  30682. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  30683. WOLFSSL_FAILURE);
  30684. X509_free(x509);
  30685. /* Should fail when x509 is NULL */
  30686. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  30687. WOLFSSL_FAILURE);
  30688. res = EXPECT_RESULT();
  30689. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  30690. return res;
  30691. }
  30692. static int test_wolfSSL_DES(void)
  30693. {
  30694. int res = TEST_SKIPPED;
  30695. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  30696. EXPECT_DECLS;
  30697. const_DES_cblock myDes;
  30698. DES_cblock iv;
  30699. DES_key_schedule key;
  30700. word32 i;
  30701. DES_LONG dl;
  30702. unsigned char msg[] = "hello wolfssl";
  30703. DES_check_key(1);
  30704. DES_set_key(&myDes, &key);
  30705. /* check, check of odd parity */
  30706. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  30707. myDes[0] = 6; /*set even parity*/
  30708. XMEMSET(key, 5, sizeof(DES_key_schedule));
  30709. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  30710. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  30711. /* set odd parity for success case */
  30712. DES_set_odd_parity(&myDes);
  30713. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  30714. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  30715. myDes[3]);
  30716. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  30717. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  30718. ExpectIntEQ(key[i], myDes[i]);
  30719. }
  30720. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  30721. /* check weak key */
  30722. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  30723. XMEMSET(key, 5, sizeof(DES_key_schedule));
  30724. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  30725. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  30726. /* now do unchecked copy of a weak key over */
  30727. DES_set_key_unchecked(&myDes, &key);
  30728. /* compare arrays, should be the same */
  30729. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  30730. ExpectIntEQ(key[i], myDes[i]);
  30731. }
  30732. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  30733. /* check DES_key_sched API */
  30734. XMEMSET(key, 1, sizeof(DES_key_schedule));
  30735. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  30736. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  30737. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  30738. /* compare arrays, should be the same */
  30739. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  30740. ExpectIntEQ(key[i], myDes[i]);
  30741. }
  30742. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  30743. * DES_cbc_encrypt on the input */
  30744. XMEMSET(iv, 0, sizeof(DES_cblock));
  30745. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  30746. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  30747. ExpectIntEQ(dl, 480052723);
  30748. res = EXPECT_RESULT();
  30749. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  30750. return res;
  30751. }
  30752. static int test_wc_PemToDer(void)
  30753. {
  30754. int res = TEST_SKIPPED;
  30755. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  30756. EXPECT_DECLS;
  30757. int ret;
  30758. DerBuffer* pDer = NULL;
  30759. const char* ca_cert = "./certs/server-cert.pem";
  30760. byte* cert_buf = NULL;
  30761. size_t cert_sz = 0;
  30762. int eccKey = 0;
  30763. EncryptedInfo info;
  30764. XMEMSET(&info, 0, sizeof(info));
  30765. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30766. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  30767. &info, &eccKey), 0);
  30768. wc_FreeDer(&pDer);
  30769. pDer = NULL;
  30770. if (cert_buf != NULL) {
  30771. free(cert_buf);
  30772. cert_buf = NULL;
  30773. }
  30774. #ifdef HAVE_ECC
  30775. {
  30776. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  30777. byte key_buf[256] = {0};
  30778. /* Test fail of loading a key with cert type */
  30779. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  30780. key_buf[0] = '\n';
  30781. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  30782. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  30783. &pDer, NULL, &info, &eccKey)), 0);
  30784. #ifdef OPENSSL_EXTRA
  30785. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  30786. &pDer, NULL, &info, &eccKey)), 0);
  30787. #endif
  30788. wc_FreeDer(&pDer);
  30789. if (cert_buf != NULL)
  30790. free(cert_buf);
  30791. }
  30792. #endif
  30793. res = EXPECT_RESULT();
  30794. #endif
  30795. return res;
  30796. }
  30797. static int test_wc_AllocDer(void)
  30798. {
  30799. int res = TEST_SKIPPED;
  30800. #if !defined(NO_CERTS)
  30801. EXPECT_DECLS;
  30802. DerBuffer* pDer = NULL;
  30803. word32 testSize = 1024;
  30804. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  30805. BAD_FUNC_ARG);
  30806. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  30807. ExpectNotNull(pDer);
  30808. wc_FreeDer(&pDer);
  30809. res = EXPECT_RESULT();
  30810. #endif
  30811. return res;
  30812. }
  30813. static int test_wc_CertPemToDer(void)
  30814. {
  30815. int res = TEST_SKIPPED;
  30816. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  30817. EXPECT_DECLS;
  30818. const char* ca_cert = "./certs/ca-cert.pem";
  30819. byte* cert_buf = NULL;
  30820. size_t cert_sz = 0;
  30821. size_t cert_dersz = 0;
  30822. byte* cert_der = NULL;
  30823. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30824. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30825. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30826. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30827. (int)cert_dersz, CERT_TYPE), 0);
  30828. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  30829. BAD_FUNC_ARG);
  30830. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  30831. BAD_FUNC_ARG);
  30832. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  30833. BAD_FUNC_ARG);
  30834. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  30835. CERT_TYPE), BAD_FUNC_ARG);
  30836. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  30837. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  30838. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  30839. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  30840. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  30841. CERT_TYPE), BAD_FUNC_ARG);
  30842. if (cert_der != NULL)
  30843. free(cert_der);
  30844. if (cert_buf != NULL)
  30845. free(cert_buf);
  30846. res = EXPECT_RESULT();
  30847. #endif
  30848. return res;
  30849. }
  30850. static int test_wc_KeyPemToDer(void)
  30851. {
  30852. int res = TEST_SKIPPED;
  30853. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30854. EXPECT_DECLS;
  30855. int ret;
  30856. const byte cert_buf[] = \
  30857. "-----BEGIN PRIVATE KEY-----\n"
  30858. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  30859. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  30860. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  30861. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  30862. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  30863. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  30864. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  30865. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  30866. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  30867. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  30868. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  30869. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  30870. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  30871. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  30872. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  30873. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  30874. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  30875. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  30876. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  30877. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  30878. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  30879. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  30880. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  30881. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  30882. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  30883. "AQue5o6oVfhgLiJzMdo/77gw\n"
  30884. "-----END PRIVATE KEY-----\n";
  30885. const int cert_sz = sizeof(cert_buf);
  30886. const char cert_pw[] = "password";
  30887. int cert_dersz = 0;
  30888. byte* cert_der = NULL;
  30889. /* Bad arg: Cert buffer is NULL */
  30890. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  30891. BAD_FUNC_ARG);
  30892. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  30893. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  30894. BAD_FUNC_ARG);
  30895. /* Test normal operation */
  30896. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30897. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30898. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  30899. cert_pw), 0);
  30900. ExpectIntLE(ret, cert_sz);
  30901. if (cert_der != NULL) {
  30902. free(cert_der);
  30903. cert_der = NULL;
  30904. }
  30905. /* Test NULL for DER buffer to return needed DER buffer size */
  30906. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  30907. ExpectIntLE(ret, cert_sz);
  30908. if (EXPECT_SUCCESS())
  30909. cert_dersz = ret;
  30910. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30911. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  30912. cert_pw), 0);
  30913. ExpectIntLE(ret, cert_sz);
  30914. if (cert_der != NULL)
  30915. free(cert_der);
  30916. res = EXPECT_RESULT();
  30917. #endif
  30918. return res;
  30919. }
  30920. static int test_wc_PubKeyPemToDer(void)
  30921. {
  30922. int res = TEST_SKIPPED;
  30923. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  30924. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30925. EXPECT_DECLS;
  30926. int ret = 0;
  30927. const char* key = "./certs/ecc-client-keyPub.pem";
  30928. byte* cert_buf = NULL;
  30929. size_t cert_sz = 0, cert_dersz = 0;
  30930. byte* cert_der = NULL;
  30931. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  30932. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  30933. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  30934. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30935. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30936. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30937. (int)cert_dersz), 0);
  30938. if (cert_der != NULL) {
  30939. free(cert_der);
  30940. cert_der = NULL;
  30941. }
  30942. /* Test NULL for DER buffer to return needed DER buffer size */
  30943. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  30944. ExpectIntLE(ret, cert_sz);
  30945. cert_dersz = ret;
  30946. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30947. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30948. (int)cert_dersz), 0);
  30949. if (cert_der != NULL) {
  30950. free(cert_der);
  30951. }
  30952. if (cert_buf != NULL) {
  30953. free(cert_buf);
  30954. }
  30955. res = EXPECT_RESULT();
  30956. #endif
  30957. return res;
  30958. }
  30959. static int test_wc_PemPubKeyToDer(void)
  30960. {
  30961. int res = TEST_SKIPPED;
  30962. #if !defined(NO_FILESYSTEM) && \
  30963. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30964. EXPECT_DECLS;
  30965. const char* key = "./certs/ecc-client-keyPub.pem";
  30966. size_t cert_dersz = 1024;
  30967. byte* cert_der = NULL;
  30968. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  30969. BAD_FUNC_ARG);
  30970. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30971. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  30972. if (cert_der != NULL) {
  30973. free(cert_der);
  30974. }
  30975. res = EXPECT_RESULT();
  30976. #endif
  30977. return res;
  30978. }
  30979. static int test_wc_GetPubKeyDerFromCert(void)
  30980. {
  30981. int res = TEST_SKIPPED;
  30982. #if !defined(NO_RSA) || defined(HAVE_ECC)
  30983. EXPECT_DECLS;
  30984. int ret;
  30985. word32 idx = 0;
  30986. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30987. word32 keyDerSz = (word32)sizeof(keyDer);
  30988. DecodedCert decoded;
  30989. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30990. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  30991. word32 certBufSz = sizeof(certBuf);
  30992. #endif
  30993. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  30994. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30995. XFILE fp = XBADFILE;
  30996. #endif
  30997. #ifndef NO_RSA
  30998. RsaKey rsaKey;
  30999. #if defined(USE_CERT_BUFFERS_2048)
  31000. byte* rsaCertDer = (byte*)client_cert_der_2048;
  31001. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  31002. #elif defined(USE_CERT_BUFFERS_1024)
  31003. byte* rsaCertDer = (byte*)client_cert_der_1024;
  31004. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  31005. #else
  31006. unsigned char rsaCertDer[TWOK_BUF];
  31007. word32 rsaCertDerSz;
  31008. #endif
  31009. #endif
  31010. #ifdef HAVE_ECC
  31011. ecc_key eccKey;
  31012. #if defined(USE_CERT_BUFFERS_256)
  31013. byte* eccCert = (byte*)cliecc_cert_der_256;
  31014. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  31015. #else
  31016. unsigned char eccCert[ONEK_BUF];
  31017. word32 eccCertSz;
  31018. XFILE fp2 = XBADFILE;
  31019. #endif
  31020. #endif
  31021. #ifndef NO_RSA
  31022. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  31023. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  31024. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  31025. fp), 0);
  31026. if (fp != XBADFILE) {
  31027. XFCLOSE(fp);
  31028. fp = XBADFILE;
  31029. }
  31030. #endif
  31031. /* good test case - RSA DER cert */
  31032. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  31033. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31034. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31035. ExpectIntGT(keyDerSz, 0);
  31036. /* sanity check, verify we can import DER public key */
  31037. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31038. ExpectIntEQ(ret, 0);
  31039. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31040. if (ret == 0) {
  31041. wc_FreeRsaKey(&rsaKey);
  31042. }
  31043. /* test LENGTH_ONLY_E case */
  31044. keyDerSz = 0;
  31045. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31046. LENGTH_ONLY_E);
  31047. ExpectIntGT(keyDerSz, 0);
  31048. /* bad args: DecodedCert NULL */
  31049. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  31050. /* bad args: output key buff size */
  31051. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  31052. /* bad args: zero size output key buffer */
  31053. keyDerSz = 0;
  31054. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31055. BAD_FUNC_ARG);
  31056. wc_FreeDecodedCert(&decoded);
  31057. /* Certificate Request Tests */
  31058. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  31059. {
  31060. XMEMSET(certBuf, 0, sizeof(certBuf));
  31061. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  31062. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  31063. if (fp != XBADFILE) {
  31064. XFCLOSE(fp);
  31065. }
  31066. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  31067. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  31068. /* good test case - RSA DER certificate request */
  31069. keyDerSz = sizeof(keyDer);
  31070. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31071. 0);
  31072. ExpectIntGT(keyDerSz, 0);
  31073. /* sanity check, verify we can import DER public key */
  31074. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31075. ExpectIntEQ(ret, 0);
  31076. idx = 0;
  31077. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31078. if (ret == 0) {
  31079. wc_FreeRsaKey(&rsaKey);
  31080. }
  31081. wc_FreeDecodedCert(&decoded);
  31082. }
  31083. #endif /* WOLFSSL_CERT_REQ */
  31084. #endif /* NO_RSA */
  31085. #ifdef HAVE_ECC
  31086. #ifndef USE_CERT_BUFFERS_256
  31087. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  31088. XBADFILE);
  31089. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  31090. if (fp2 != XBADFILE) {
  31091. XFCLOSE(fp2);
  31092. }
  31093. #endif
  31094. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  31095. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31096. /* good test case - ECC */
  31097. XMEMSET(keyDer, 0, sizeof(keyDer));
  31098. keyDerSz = sizeof(keyDer);
  31099. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31100. ExpectIntGT(keyDerSz, 0);
  31101. /* sanity check, verify we can import DER public key */
  31102. ret = wc_ecc_init(&eccKey);
  31103. ExpectIntEQ(ret, 0);
  31104. idx = 0; /* reset idx to 0, used above in RSA case */
  31105. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  31106. if (ret == 0) {
  31107. wc_ecc_free(&eccKey);
  31108. }
  31109. /* test LENGTH_ONLY_E case */
  31110. keyDerSz = 0;
  31111. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31112. LENGTH_ONLY_E);
  31113. ExpectIntGT(keyDerSz, 0);
  31114. wc_FreeDecodedCert(&decoded);
  31115. #endif
  31116. res = EXPECT_RESULT();
  31117. #endif /* !NO_RSA || HAVE_ECC */
  31118. return res;
  31119. }
  31120. static int test_wc_CheckCertSigPubKey(void)
  31121. {
  31122. int res = TEST_SKIPPED;
  31123. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31124. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  31125. EXPECT_DECLS;
  31126. int ret;
  31127. const char* ca_cert = "./certs/ca-cert.pem";
  31128. byte* cert_buf = NULL;
  31129. size_t cert_sz = 0;
  31130. byte* cert_der = NULL;
  31131. word32 cert_dersz = 0;
  31132. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  31133. word32 keyDerSz = (word32)sizeof(keyDer);
  31134. DecodedCert decoded;
  31135. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31136. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  31137. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31138. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  31139. (int)cert_dersz, CERT_TYPE), 0);
  31140. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  31141. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31142. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31143. ExpectIntGT(keyDerSz, 0);
  31144. /* Good test case. */
  31145. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31146. keyDerSz, RSAk), 0);
  31147. /* No certificate. */
  31148. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  31149. ECDSAk), BAD_FUNC_ARG);
  31150. /* Bad cert size. */
  31151. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  31152. RSAk), 0);
  31153. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  31154. /* No public key. */
  31155. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  31156. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  31157. /* Bad public key size. */
  31158. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  31159. RSAk), BAD_FUNC_ARG);
  31160. /* Wrong aglo. */
  31161. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31162. keyDerSz, ECDSAk), ASN_PARSE_E);
  31163. wc_FreeDecodedCert(&decoded);
  31164. if (cert_der != NULL)
  31165. free(cert_der);
  31166. if (cert_buf != NULL)
  31167. free(cert_buf);
  31168. res = EXPECT_RESULT();
  31169. #endif
  31170. return res;
  31171. }
  31172. static int test_wolfSSL_certs(void)
  31173. {
  31174. int res = TEST_SKIPPED;
  31175. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31176. !defined(NO_RSA)
  31177. EXPECT_DECLS;
  31178. X509* x509ext = NULL;
  31179. #ifdef OPENSSL_ALL
  31180. X509* x509 = NULL;
  31181. WOLFSSL_X509_EXTENSION* ext = NULL;
  31182. ASN1_OBJECT* obj = NULL;
  31183. #endif
  31184. WOLFSSL* ssl = NULL;
  31185. WOLFSSL_CTX* ctx = NULL;
  31186. STACK_OF(ASN1_OBJECT)* sk = NULL;
  31187. ASN1_STRING* asn1_str = NULL;
  31188. AUTHORITY_KEYID* akey = NULL;
  31189. BASIC_CONSTRAINTS* bc = NULL;
  31190. int crit;
  31191. #ifndef NO_WOLFSSL_SERVER
  31192. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31193. #else
  31194. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31195. #endif
  31196. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31197. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31198. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31199. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31200. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  31201. #endif
  31202. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31203. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31204. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  31205. #endif
  31206. ExpectNotNull(ssl = SSL_new(ctx));
  31207. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31208. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31209. #endif
  31210. #ifdef HAVE_PK_CALLBACKS
  31211. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  31212. #endif /* HAVE_PK_CALLBACKS */
  31213. /* create and use x509 */
  31214. #ifdef OPENSSL_ALL
  31215. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31216. WOLFSSL_FILETYPE_PEM));
  31217. #endif
  31218. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  31219. WOLFSSL_FILETYPE_PEM));
  31220. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  31221. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31222. /* with loading in a new cert the check on private key should now fail */
  31223. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31224. #endif
  31225. #if defined(USE_CERT_BUFFERS_2048)
  31226. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  31227. (unsigned char*)server_cert_der_2048,
  31228. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  31229. #endif
  31230. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31231. /************* Get Digest of Certificate ******************/
  31232. {
  31233. byte digest[64]; /* max digest size */
  31234. word32 digestSz;
  31235. XMEMSET(digest, 0, sizeof(digest));
  31236. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  31237. WOLFSSL_SUCCESS);
  31238. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  31239. WOLFSSL_SUCCESS);
  31240. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  31241. WOLFSSL_FAILURE);
  31242. }
  31243. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  31244. /* test and checkout X509 extensions */
  31245. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  31246. NID_basic_constraints, &crit, NULL));
  31247. ExpectIntEQ(crit, 0);
  31248. #ifdef OPENSSL_ALL
  31249. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  31250. X509_EXTENSION_free(ext);
  31251. ext = NULL;
  31252. ExpectNotNull(ext = X509_EXTENSION_new());
  31253. X509_EXTENSION_set_critical(ext, 1);
  31254. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  31255. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  31256. ASN1_OBJECT_free(obj);
  31257. obj = NULL;
  31258. X509_EXTENSION_free(ext);
  31259. ext = NULL;
  31260. ExpectNotNull(ext = X509_EXTENSION_new());
  31261. X509_EXTENSION_set_critical(ext, 0);
  31262. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  31263. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  31264. NULL);
  31265. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  31266. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  31267. * and X509_get_ext_d2i has created new */
  31268. asn1_str = NULL;
  31269. X509_EXTENSION_free(ext);
  31270. ext = NULL;
  31271. #endif
  31272. BASIC_CONSTRAINTS_free(bc);
  31273. bc = NULL;
  31274. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  31275. NID_key_usage, &crit, NULL));
  31276. ExpectIntEQ(crit, 1);
  31277. ExpectIntEQ(asn1_str->type, NID_key_usage);
  31278. #ifdef OPENSSL_ALL
  31279. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  31280. X509_EXTENSION_free(ext);
  31281. ext = NULL;
  31282. #endif
  31283. ASN1_STRING_free(asn1_str);
  31284. asn1_str = NULL;
  31285. #ifdef OPENSSL_ALL
  31286. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  31287. NID_ext_key_usage, &crit, NULL));
  31288. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  31289. X509_EXTENSION_free(ext);
  31290. ext = NULL;
  31291. sk_ASN1_OBJECT_pop_free(sk, NULL);
  31292. sk = NULL;
  31293. #else
  31294. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  31295. &crit, NULL);
  31296. ExpectNull(sk);
  31297. #endif
  31298. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  31299. NID_authority_key_identifier, &crit, NULL));
  31300. #ifdef OPENSSL_ALL
  31301. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  31302. akey));
  31303. X509_EXTENSION_free(ext);
  31304. ext = NULL;
  31305. #endif
  31306. wolfSSL_AUTHORITY_KEYID_free(akey);
  31307. akey = NULL;
  31308. /* NID not yet supported */
  31309. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31310. NID_private_key_usage_period, &crit, NULL));
  31311. ExpectIntEQ(crit, -1);
  31312. sk_ASN1_OBJECT_free(sk);
  31313. sk = NULL;
  31314. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  31315. NID_subject_alt_name, &crit, NULL));
  31316. {
  31317. int i;
  31318. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  31319. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  31320. ExpectIntEQ(gen->type, GEN_DNS);
  31321. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  31322. }
  31323. }
  31324. sk_GENERAL_NAME_free(sk);
  31325. sk = NULL;
  31326. /* NID not yet supported */
  31327. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31328. NID_issuer_alt_name, &crit, NULL));
  31329. ExpectIntEQ(crit, -1);
  31330. sk_ASN1_OBJECT_free(sk);
  31331. sk = NULL;
  31332. /* NID not yet supported */
  31333. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31334. NID_info_access, &crit, NULL));
  31335. sk_ASN1_OBJECT_free(sk);
  31336. sk = NULL;
  31337. /* NID not yet supported */
  31338. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31339. NID_sinfo_access, &crit, NULL));
  31340. ExpectIntEQ(crit, -1);
  31341. sk_ASN1_OBJECT_free(sk);
  31342. sk = NULL;
  31343. /* NID not yet supported */
  31344. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31345. NID_name_constraints, &crit, NULL));
  31346. ExpectIntEQ(crit, -1);
  31347. sk_ASN1_OBJECT_free(sk);
  31348. sk = NULL;
  31349. /* no cert policy set */
  31350. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31351. NID_certificate_policies, &crit, NULL));
  31352. sk_ASN1_OBJECT_free(sk);
  31353. sk = NULL;
  31354. /* NID not yet supported */
  31355. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31356. NID_policy_mappings, &crit, NULL));
  31357. ExpectIntEQ(crit, -1);
  31358. sk_ASN1_OBJECT_free(sk);
  31359. sk = NULL;
  31360. /* NID not yet supported */
  31361. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31362. NID_policy_constraints, &crit, NULL));
  31363. ExpectIntEQ(crit, -1);
  31364. sk_ASN1_OBJECT_free(sk);
  31365. sk = NULL;
  31366. /* NID not yet supported */
  31367. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31368. NID_inhibit_any_policy, &crit, NULL));
  31369. ExpectIntEQ(crit, -1);
  31370. sk_ASN1_OBJECT_free(sk);
  31371. sk = NULL;
  31372. /* NID not yet supported */
  31373. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31374. NID_tlsfeature, &crit, NULL));
  31375. ExpectIntEQ(crit, -1);
  31376. sk_ASN1_OBJECT_free(sk);
  31377. sk = NULL;
  31378. /* test invalid cases */
  31379. crit = 0;
  31380. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  31381. NULL));
  31382. ExpectIntEQ(crit, -1);
  31383. /* NULL passed for criticality. */
  31384. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  31385. NID_tlsfeature, NULL, NULL));
  31386. ExpectIntEQ(SSL_get_hit(ssl), 0);
  31387. #ifdef OPENSSL_ALL
  31388. X509_free(x509);
  31389. #endif
  31390. X509_free(x509ext);
  31391. SSL_free(ssl);
  31392. SSL_CTX_free(ctx);
  31393. res = EXPECT_RESULT();
  31394. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  31395. return res;
  31396. }
  31397. static int test_wolfSSL_X509_check_private_key(void)
  31398. {
  31399. int res = TEST_SKIPPED;
  31400. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  31401. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  31402. EXPECT_DECLS;
  31403. X509* x509 = NULL;
  31404. EVP_PKEY* pkey = NULL;
  31405. const byte* key;
  31406. /* Check with correct key */
  31407. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  31408. SSL_FILETYPE_PEM)));
  31409. key = client_key_der_2048;
  31410. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31411. (long)sizeof_client_key_der_2048));
  31412. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  31413. EVP_PKEY_free(pkey);
  31414. pkey = NULL;
  31415. /* Check with wrong key */
  31416. key = server_key_der_2048;
  31417. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31418. (long)sizeof_server_key_der_2048));
  31419. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  31420. /* test for incorrect parameter */
  31421. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  31422. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  31423. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  31424. EVP_PKEY_free(pkey);
  31425. X509_free(x509);
  31426. res = EXPECT_RESULT();
  31427. #endif
  31428. return res;
  31429. }
  31430. static int test_wolfSSL_private_keys(void)
  31431. {
  31432. int res = TEST_SKIPPED;
  31433. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31434. !defined(NO_FILESYSTEM)
  31435. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31436. EXPECT_DECLS;
  31437. WOLFSSL* ssl = NULL;
  31438. WOLFSSL_CTX* ctx = NULL;
  31439. EVP_PKEY* pkey = NULL;
  31440. OpenSSL_add_all_digests();
  31441. OpenSSL_add_all_algorithms();
  31442. #ifndef NO_RSA
  31443. #ifndef NO_WOLFSSL_SERVER
  31444. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31445. #else
  31446. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31447. #endif
  31448. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  31449. /* Have to load a cert before you can check the private key against that
  31450. * certificates public key! */
  31451. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31452. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  31453. #endif
  31454. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  31455. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31456. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31457. #endif
  31458. ExpectNotNull(ssl = SSL_new(ctx));
  31459. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31460. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31461. #endif
  31462. #ifdef USE_CERT_BUFFERS_2048
  31463. {
  31464. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31465. unsigned char buf[FOURK_BUF];
  31466. word32 bufSz;
  31467. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  31468. (unsigned char*)client_key_der_2048,
  31469. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  31470. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31471. /* Should mismatch now that a different private key loaded */
  31472. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31473. #endif
  31474. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  31475. (unsigned char*)server_key,
  31476. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  31477. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31478. /* After loading back in DER format of original key, should match */
  31479. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31480. #endif
  31481. /* test loading private key to the WOLFSSL_CTX */
  31482. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  31483. (unsigned char*)client_key_der_2048,
  31484. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  31485. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31486. /* Should mismatch now that a different private key loaded */
  31487. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31488. #endif
  31489. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  31490. (unsigned char*)server_key,
  31491. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  31492. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31493. /* After loading back in DER format of original key, should match */
  31494. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31495. #endif
  31496. /* pkey not set yet, expecting to fail */
  31497. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  31498. /* set PKEY and test again */
  31499. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31500. &server_key, (long)sizeof_server_key_der_2048));
  31501. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  31502. /* reuse PKEY structure and test
  31503. * this should be checked with a memory management sanity checker */
  31504. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  31505. server_key = (const unsigned char*)server_key_der_2048;
  31506. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31507. &server_key, (long)sizeof_server_key_der_2048));
  31508. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  31509. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  31510. bufSz = FOURK_BUF;
  31511. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  31512. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  31513. RSAk, NULL, 0)), 0);
  31514. server_key = (const unsigned char*)buf;
  31515. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  31516. (long)bufSz));
  31517. }
  31518. #endif
  31519. EVP_PKEY_free(pkey);
  31520. pkey = NULL;
  31521. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  31522. ssl = NULL;
  31523. SSL_CTX_free(ctx);
  31524. ctx = NULL;
  31525. #endif /* end of RSA private key match tests */
  31526. #ifdef HAVE_ECC
  31527. #ifndef NO_WOLFSSL_SERVER
  31528. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31529. #else
  31530. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31531. #endif
  31532. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  31533. WOLFSSL_FILETYPE_PEM));
  31534. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  31535. WOLFSSL_FILETYPE_PEM));
  31536. ExpectNotNull(ssl = SSL_new(ctx));
  31537. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31538. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31539. #endif
  31540. SSL_free(ssl);
  31541. ssl = NULL;
  31542. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  31543. WOLFSSL_FILETYPE_PEM));
  31544. ExpectNotNull(ssl = SSL_new(ctx));
  31545. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  31546. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31547. #endif
  31548. SSL_free(ssl);
  31549. ssl = NULL;
  31550. SSL_CTX_free(ctx);
  31551. ctx = NULL;
  31552. #endif /* end of ECC private key match tests */
  31553. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  31554. #ifndef NO_WOLFSSL_SERVER
  31555. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31556. #else
  31557. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31558. #endif
  31559. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  31560. WOLFSSL_FILETYPE_PEM));
  31561. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  31562. WOLFSSL_FILETYPE_PEM));
  31563. ExpectNotNull(ssl = SSL_new(ctx));
  31564. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31565. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31566. #endif
  31567. SSL_free(ssl);
  31568. ssl = NULL;
  31569. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  31570. WOLFSSL_FILETYPE_PEM));
  31571. ExpectNotNull(ssl = SSL_new(ctx));
  31572. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31573. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31574. #endif
  31575. SSL_free(ssl);
  31576. ssl = NULL;
  31577. SSL_CTX_free(ctx);
  31578. ctx = NULL;
  31579. #endif /* end of Ed25519 private key match tests */
  31580. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  31581. #ifndef NO_WOLFSSL_SERVER
  31582. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31583. #else
  31584. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31585. #endif
  31586. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  31587. WOLFSSL_FILETYPE_PEM));
  31588. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  31589. WOLFSSL_FILETYPE_PEM));
  31590. ExpectNotNull(ssl = SSL_new(ctx));
  31591. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31592. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31593. #endif
  31594. SSL_free(ssl);
  31595. ssl = NULL;
  31596. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  31597. WOLFSSL_FILETYPE_PEM));
  31598. ExpectNotNull(ssl = SSL_new(ctx));
  31599. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  31600. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31601. #endif
  31602. SSL_free(ssl);
  31603. ssl = NULL;
  31604. SSL_CTX_free(ctx);
  31605. ctx = NULL;
  31606. #endif /* end of Ed448 private key match tests */
  31607. EVP_cleanup();
  31608. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  31609. CONF_modules_free();
  31610. ENGINE_cleanup();
  31611. CONF_modules_unload();
  31612. (void)ssl;
  31613. (void)ctx;
  31614. (void)pkey;
  31615. res = EXPECT_RESULT();
  31616. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31617. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31618. return res;
  31619. }
  31620. static int test_wolfSSL_PEM_read_PrivateKey(void)
  31621. {
  31622. int res = TEST_SKIPPED;
  31623. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  31624. && !defined(NO_FILESYSTEM)
  31625. EXPECT_DECLS;
  31626. XFILE file = XBADFILE;
  31627. const char* fname = "./certs/server-key.pem";
  31628. EVP_PKEY* pkey = NULL;
  31629. RSA* rsa = NULL;
  31630. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  31631. unsigned char* sig = NULL;
  31632. size_t sigLen;
  31633. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  31634. size_t tbsLen = sizeof(tbs);
  31635. /* Check error case. */
  31636. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  31637. /* Read in an RSA key. */
  31638. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31639. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  31640. if (file != XBADFILE)
  31641. XFCLOSE(file);
  31642. /* Make sure the key is usable by signing some data with it. */
  31643. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  31644. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  31645. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  31646. DYNAMIC_TYPE_TMP_BUFFER));
  31647. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  31648. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  31649. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  31650. WOLFSSL_SUCCESS);
  31651. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31652. EVP_PKEY_CTX_free(ctx);
  31653. EVP_PKEY_free(pkey);
  31654. res = EXPECT_RESULT();
  31655. #endif
  31656. return res;
  31657. }
  31658. static int test_wolfSSL_PEM_read_PUBKEY(void)
  31659. {
  31660. int res = TEST_SKIPPED;
  31661. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  31662. && !defined(NO_FILESYSTEM)
  31663. EXPECT_DECLS;
  31664. XFILE file = XBADFILE;
  31665. const char* fname = "./certs/client-keyPub.pem";
  31666. EVP_PKEY* pkey = NULL;
  31667. /* Check error case. */
  31668. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  31669. /* Read in an RSA key. */
  31670. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31671. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  31672. EVP_PKEY_free(pkey);
  31673. if (file != XBADFILE)
  31674. XFCLOSE(file);
  31675. res = EXPECT_RESULT();
  31676. #endif
  31677. return res;
  31678. }
  31679. static int test_wolfSSL_PEM_PrivateKey(void)
  31680. {
  31681. int res = TEST_SKIPPED;
  31682. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31683. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31684. EXPECT_DECLS;
  31685. #ifndef NO_BIO
  31686. BIO* bio = NULL;
  31687. #endif
  31688. EVP_PKEY* pkey = NULL;
  31689. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31690. #ifndef NO_BIO
  31691. /* test creating new EVP_PKEY with bad arg */
  31692. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31693. /* test loading RSA key using BIO */
  31694. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  31695. {
  31696. XFILE file = XBADFILE;
  31697. const char* fname = "./certs/server-key.pem";
  31698. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  31699. size_t sz;
  31700. byte* buf = NULL;
  31701. EVP_PKEY* pkey2 = NULL;
  31702. EVP_PKEY* pkey3 = NULL;
  31703. RSA* rsa_key = NULL;
  31704. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31705. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31706. ExpectIntGT(sz = XFTELL(file), 0);
  31707. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31708. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31709. if (buf != NULL) {
  31710. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31711. }
  31712. if (file != XBADFILE) {
  31713. XFCLOSE(file);
  31714. file = XBADFILE;
  31715. }
  31716. /* Test using BIO new mem and loading PEM private key */
  31717. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31718. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31719. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31720. buf = NULL;
  31721. BIO_free(bio);
  31722. bio = NULL;
  31723. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31724. if (pkey2 != NULL) {
  31725. pkey2->type = EVP_PKEY_RSA;
  31726. }
  31727. /* Test parameter copy */
  31728. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  31729. EVP_PKEY_free(pkey2);
  31730. EVP_PKEY_free(pkey);
  31731. pkey = NULL;
  31732. /* Qt unit test case : rsa pkcs8 key */
  31733. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  31734. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31735. ExpectIntGT(sz = XFTELL(file), 0);
  31736. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31737. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31738. if (buf) {
  31739. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31740. }
  31741. if (file != XBADFILE) {
  31742. XFCLOSE(file);
  31743. file = XBADFILE;
  31744. }
  31745. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31746. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31747. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31748. buf = NULL;
  31749. BIO_free(bio);
  31750. bio = NULL;
  31751. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31752. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  31753. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  31754. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31755. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31756. #else
  31757. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31758. #endif
  31759. RSA_free(rsa_key);
  31760. EVP_PKEY_free(pkey3);
  31761. EVP_PKEY_free(pkey);
  31762. pkey = NULL;
  31763. }
  31764. #endif
  31765. /* test loading ECC key using BIO */
  31766. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31767. {
  31768. XFILE file = XBADFILE;
  31769. const char* fname = "./certs/ecc-key.pem";
  31770. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  31771. size_t sz = 0;
  31772. byte* buf = NULL;
  31773. EVP_PKEY* pkey2 = NULL;
  31774. EVP_PKEY* pkey3 = NULL;
  31775. EC_KEY* ec_key = NULL;
  31776. int nid = 0;
  31777. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31778. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31779. ExpectIntGT(sz = XFTELL(file), 0);
  31780. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31781. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31782. if (buf) {
  31783. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31784. }
  31785. if (file != XBADFILE) {
  31786. XFCLOSE(file);
  31787. file = XBADFILE;
  31788. }
  31789. /* Test using BIO new mem and loading PEM private key */
  31790. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31791. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31792. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31793. buf = NULL;
  31794. BIO_free(bio);
  31795. bio = NULL;
  31796. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31797. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31798. if (pkey2 != NULL) {
  31799. pkey2->type = EVP_PKEY_EC;
  31800. }
  31801. /* Test parameter copy */
  31802. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  31803. /* Qt unit test case 1*/
  31804. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31805. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31806. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31807. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31808. #else
  31809. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31810. #endif
  31811. /* Test default digest */
  31812. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  31813. ExpectIntEQ(nid, NID_sha256);
  31814. EC_KEY_free(ec_key);
  31815. ec_key = NULL;
  31816. EVP_PKEY_free(pkey3);
  31817. pkey3 = NULL;
  31818. EVP_PKEY_free(pkey2);
  31819. pkey2 = NULL;
  31820. EVP_PKEY_free(pkey);
  31821. pkey = NULL;
  31822. /* Qt unit test case ec pkcs8 key */
  31823. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  31824. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31825. ExpectIntGT(sz = XFTELL(file), 0);
  31826. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31827. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31828. if (buf) {
  31829. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31830. }
  31831. if (file != XBADFILE) {
  31832. XFCLOSE(file);
  31833. file = XBADFILE;
  31834. }
  31835. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31836. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31837. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31838. buf = NULL;
  31839. BIO_free(bio);
  31840. bio = NULL;
  31841. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31842. /* Qt unit test case */
  31843. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31844. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31845. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31846. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31847. #else
  31848. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31849. #endif
  31850. EC_KEY_free(ec_key);
  31851. EVP_PKEY_free(pkey3);
  31852. EVP_PKEY_free(pkey);
  31853. pkey = NULL;
  31854. }
  31855. #endif
  31856. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  31857. defined(WOLFSSL_CERT_GEN))
  31858. {
  31859. #define BIO_PEM_TEST_CHAR 'a'
  31860. EVP_PKEY* pkey2 = NULL;
  31861. unsigned char extra[10];
  31862. int i;
  31863. BIO* pub_bio = NULL;
  31864. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  31865. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31866. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  31867. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31868. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  31869. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  31870. &server_key, (long)sizeof_server_key_der_2048));
  31871. ExpectNull(pkey);
  31872. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31873. &server_key, (long)sizeof_server_key_der_2048));
  31874. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  31875. NULL), WOLFSSL_FAILURE);
  31876. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31877. NULL), WOLFSSL_FAILURE);
  31878. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31879. NULL), WOLFSSL_SUCCESS);
  31880. ExpectIntGT(BIO_pending(bio), 0);
  31881. ExpectIntEQ(BIO_pending(bio), 1679);
  31882. /* Check if the pubkey API writes only the public key */
  31883. #ifdef WOLFSSL_KEY_GEN
  31884. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  31885. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  31886. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  31887. ExpectIntGT(BIO_pending(pub_bio), 0);
  31888. /* Previously both the private key and the pubkey calls would write
  31889. * out the private key and the PEM header was the only difference.
  31890. * The public PEM should be significantly shorter than the
  31891. * private key versison. */
  31892. ExpectIntEQ(BIO_pending(pub_bio), 451);
  31893. #endif
  31894. /* test creating new EVP_PKEY with good args */
  31895. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31896. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  31897. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  31898. /* test of reuse of EVP_PKEY */
  31899. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31900. ExpectIntEQ(BIO_pending(bio), 0);
  31901. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31902. SSL_SUCCESS);
  31903. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  31904. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31905. ExpectNotNull(pkey);
  31906. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  31907. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  31908. }
  31909. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  31910. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  31911. for (i = 0; i < 10; i++) {
  31912. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  31913. }
  31914. BIO_free(pub_bio);
  31915. BIO_free(bio);
  31916. bio = NULL;
  31917. EVP_PKEY_free(pkey);
  31918. pkey = NULL;
  31919. EVP_PKEY_free(pkey2);
  31920. }
  31921. #endif
  31922. /* key is DES encrypted */
  31923. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31924. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31925. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  31926. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  31927. {
  31928. XFILE f = XBADFILE;
  31929. wc_pem_password_cb* passwd_cb = NULL;
  31930. void* passwd_cb_userdata;
  31931. SSL_CTX* ctx = NULL;
  31932. char passwd[] = "bad password";
  31933. #ifndef WOLFSSL_NO_TLS12
  31934. #ifndef NO_WOLFSSL_SERVER
  31935. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31936. #else
  31937. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31938. #endif
  31939. #else
  31940. #ifndef NO_WOLFSSL_SERVER
  31941. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31942. #else
  31943. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31944. #endif
  31945. #endif
  31946. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31947. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31948. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31949. ExpectNull(passwd_cb_userdata =
  31950. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31951. /* fail case with password call back */
  31952. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31953. (void*)passwd));
  31954. BIO_free(bio);
  31955. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31956. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31957. (void*)passwd));
  31958. BIO_free(bio);
  31959. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31960. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31961. if ((bio == NULL) && (f != XBADFILE)) {
  31962. XFCLOSE(f);
  31963. }
  31964. /* use callback that works */
  31965. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31966. (void*)"yassl123"));
  31967. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31968. EVP_PKEY_free(pkey);
  31969. pkey = NULL;
  31970. BIO_free(bio);
  31971. bio = NULL;
  31972. SSL_CTX_free(ctx);
  31973. }
  31974. #endif /* !defined(NO_DES3) */
  31975. #endif /* !NO_BIO */
  31976. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31977. {
  31978. unsigned char buf[2048];
  31979. size_t bytes = 0;
  31980. XFILE f = XBADFILE;
  31981. SSL_CTX* ctx = NULL;
  31982. #ifndef WOLFSSL_NO_TLS12
  31983. #ifndef NO_WOLFSSL_SERVER
  31984. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31985. #else
  31986. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31987. #endif
  31988. #else
  31989. #ifndef NO_WOLFSSL_SERVER
  31990. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31991. #else
  31992. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31993. #endif
  31994. #endif
  31995. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31996. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31997. if (f != XBADFILE)
  31998. XFCLOSE(f);
  31999. server_key = buf;
  32000. pkey = NULL;
  32001. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  32002. ExpectNull(pkey);
  32003. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  32004. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  32005. EVP_PKEY_free(pkey);
  32006. pkey = NULL;
  32007. SSL_CTX_free(ctx);
  32008. server_key = NULL;
  32009. }
  32010. #endif
  32011. res = EXPECT_RESULT();
  32012. #ifndef NO_BIO
  32013. (void)bio;
  32014. #endif
  32015. (void)pkey;
  32016. (void)server_key;
  32017. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  32018. return res;
  32019. }
  32020. static int test_wolfSSL_PEM_file_RSAKey(void)
  32021. {
  32022. int res = TEST_SKIPPED;
  32023. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32024. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32025. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32026. EXPECT_DECLS;
  32027. RSA* rsa = NULL;
  32028. XFILE fp = XBADFILE;
  32029. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  32030. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  32031. if (fp != XBADFILE)
  32032. XFCLOSE(fp);
  32033. ExpectIntEQ(RSA_size(rsa), 256);
  32034. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  32035. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  32036. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  32037. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  32038. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  32039. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  32040. RSA_free(rsa);
  32041. res = EXPECT_RESULT();
  32042. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32043. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32044. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32045. return res;
  32046. }
  32047. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  32048. {
  32049. int res = TEST_SKIPPED;
  32050. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  32051. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  32052. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  32053. EXPECT_DECLS;
  32054. RSA* rsa = NULL;
  32055. XFILE f = NULL;
  32056. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  32057. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32058. ExpectIntEQ(RSA_size(rsa), 256);
  32059. if (f != XBADFILE) {
  32060. XFCLOSE(f);
  32061. f = XBADFILE;
  32062. }
  32063. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  32064. NULL), WOLFSSL_FAILURE);
  32065. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  32066. NULL), WOLFSSL_FAILURE);
  32067. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  32068. WOLFSSL_SUCCESS);
  32069. RSA_free(rsa);
  32070. #ifdef HAVE_ECC
  32071. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  32072. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32073. if (f != XBADFILE)
  32074. XFCLOSE(f);
  32075. #endif /* HAVE_ECC */
  32076. res = EXPECT_RESULT();
  32077. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32078. return res;
  32079. }
  32080. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  32081. {
  32082. int res = TEST_SKIPPED;
  32083. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32084. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32085. EXPECT_DECLS;
  32086. XFILE file = XBADFILE;
  32087. const char* fname = "./certs/client-keyPub.pem";
  32088. RSA *rsa = NULL;
  32089. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  32090. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32091. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  32092. ExpectIntEQ(RSA_size(rsa), 256);
  32093. RSA_free(rsa);
  32094. if (file != XBADFILE)
  32095. XFCLOSE(file);
  32096. res = EXPECT_RESULT();
  32097. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32098. return res;
  32099. }
  32100. #ifndef NO_BIO
  32101. static int test_wolfSSL_PEM_bio_RSAKey(void)
  32102. {
  32103. int res = TEST_SKIPPED;
  32104. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32105. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32106. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32107. EXPECT_DECLS;
  32108. RSA* rsa = NULL;
  32109. BIO* bio = NULL;
  32110. /* PrivateKey */
  32111. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32112. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  32113. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  32114. ExpectNotNull(rsa);
  32115. ExpectIntEQ(RSA_size(rsa), 256);
  32116. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  32117. NULL), WOLFSSL_FAILURE);
  32118. BIO_free(bio);
  32119. bio = NULL;
  32120. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32121. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  32122. NULL), WOLFSSL_SUCCESS);
  32123. BIO_free(bio);
  32124. bio = NULL;
  32125. RSA_free(rsa);
  32126. rsa = NULL;
  32127. /* PUBKEY */
  32128. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  32129. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32130. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32131. ExpectIntEQ(RSA_size(rsa), 256);
  32132. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32133. BIO_free(bio);
  32134. bio = NULL;
  32135. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32136. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  32137. BIO_free(bio);
  32138. bio = NULL;
  32139. RSA_free(rsa);
  32140. rsa = NULL;
  32141. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  32142. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  32143. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32144. BIO_free(bio);
  32145. bio = NULL;
  32146. RSA_free(rsa);
  32147. rsa = NULL;
  32148. #ifdef HAVE_ECC
  32149. /* ensure that non-rsa keys do not work */
  32150. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32151. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32152. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32153. BIO_free(bio);
  32154. bio = NULL;
  32155. RSA_free(rsa);
  32156. rsa = NULL;
  32157. #endif /* HAVE_ECC */
  32158. res = EXPECT_RESULT();
  32159. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32160. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32161. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32162. return res;
  32163. }
  32164. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  32165. {
  32166. int res = TEST_SKIPPED;
  32167. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32168. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32169. EXPECT_DECLS;
  32170. RSA* rsa = NULL;
  32171. RSA* rsa_dup = NULL;
  32172. BIO* bio = NULL;
  32173. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32174. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32175. ExpectIntEQ(RSA_size(rsa), 256);
  32176. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  32177. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  32178. /* Test duplicating empty key. */
  32179. ExpectNotNull(rsa_dup = RSA_new());
  32180. ExpectNull(RSAPublicKey_dup(rsa_dup));
  32181. RSA_free(rsa_dup);
  32182. rsa_dup = NULL;
  32183. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  32184. ExpectPtrNE(rsa_dup, rsa);
  32185. #endif
  32186. /* test if valgrind complains about unreleased memory */
  32187. RSA_up_ref(rsa);
  32188. RSA_free(rsa);
  32189. BIO_free(bio);
  32190. bio = NULL;
  32191. RSA_free(rsa);
  32192. rsa = NULL;
  32193. RSA_free(rsa_dup);
  32194. rsa_dup = NULL;
  32195. #ifdef HAVE_ECC
  32196. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  32197. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32198. BIO_free(bio);
  32199. #endif /* HAVE_ECC */
  32200. res = EXPECT_RESULT();
  32201. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32202. return res;
  32203. }
  32204. static int test_wolfSSL_PEM_bio_DSAKey(void)
  32205. {
  32206. int res = TEST_SKIPPED;
  32207. #ifndef HAVE_SELFTEST
  32208. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  32209. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  32210. EXPECT_DECLS;
  32211. DSA* dsa = NULL;
  32212. BIO* bio = NULL;
  32213. /* PrivateKey */
  32214. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  32215. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  32216. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32217. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32218. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  32219. NULL), WOLFSSL_FAILURE);
  32220. BIO_free(bio);
  32221. bio = NULL;
  32222. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32223. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  32224. NULL), WOLFSSL_SUCCESS);
  32225. BIO_free(bio);
  32226. bio = NULL;
  32227. DSA_free(dsa);
  32228. dsa = NULL;
  32229. /* PUBKEY */
  32230. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  32231. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32232. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32233. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32234. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32235. BIO_free(bio);
  32236. bio = NULL;
  32237. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32238. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  32239. BIO_free(bio);
  32240. bio = NULL;
  32241. DSA_free(dsa);
  32242. dsa = NULL;
  32243. #ifdef HAVE_ECC
  32244. /* ensure that non-dsa keys do not work */
  32245. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32246. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32247. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32248. BIO_free(bio);
  32249. bio = NULL;
  32250. DSA_free(dsa);
  32251. dsa = NULL;
  32252. #endif /* HAVE_ECC */
  32253. res = TEST_RES_CHECK(1);
  32254. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  32255. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  32256. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  32257. #endif /* HAVE_SELFTEST */
  32258. return res;
  32259. }
  32260. static int test_wolfSSL_PEM_bio_ECKey(void)
  32261. {
  32262. int res = TEST_SKIPPED;
  32263. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32264. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  32265. EXPECT_DECLS;
  32266. EC_KEY* ec = NULL;
  32267. EC_KEY* ec2;
  32268. BIO* bio = NULL;
  32269. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  32270. unsigned char* pem = NULL;
  32271. int pLen;
  32272. #endif
  32273. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  32274. "MAA=\n"
  32275. "-----END PUBLIC KEY-----";
  32276. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  32277. "MAA=\n"
  32278. "-----END EC PRIVATE KEY-----";
  32279. /* PrivateKey */
  32280. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  32281. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  32282. ec2 = NULL;
  32283. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  32284. ExpectIntEQ(ec == ec2, 1);
  32285. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  32286. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  32287. NULL), WOLFSSL_FAILURE);
  32288. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  32289. NULL), WOLFSSL_FAILURE);
  32290. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  32291. WOLFSSL_FAILURE);
  32292. BIO_free(bio);
  32293. bio = NULL;
  32294. /* Public key data - fail. */
  32295. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  32296. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  32297. BIO_free(bio);
  32298. bio = NULL;
  32299. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32300. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  32301. NULL), WOLFSSL_SUCCESS);
  32302. BIO_free(bio);
  32303. bio = NULL;
  32304. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  32305. NULL),WOLFSSL_FAILURE);
  32306. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  32307. WOLFSSL_FAILURE);
  32308. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  32309. WOLFSSL_FAILURE);
  32310. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  32311. WOLFSSL_SUCCESS);
  32312. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  32313. NULL), 0);
  32314. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  32315. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  32316. NULL), 0);
  32317. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  32318. NULL), 0);
  32319. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  32320. &pLen), 0);
  32321. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  32322. &pLen), 0);
  32323. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  32324. &pLen), 0);
  32325. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  32326. NULL), 0);
  32327. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  32328. &pLen), 1);
  32329. ExpectIntGT(pLen, 0);
  32330. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32331. #endif
  32332. EC_KEY_free(ec);
  32333. ec = NULL;
  32334. /* PUBKEY */
  32335. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  32336. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  32337. ec2 = NULL;
  32338. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  32339. ExpectIntEQ(ec == ec2, 1);
  32340. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  32341. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32342. BIO_free(bio);
  32343. bio = NULL;
  32344. /* Test 0x30, 0x00 fails. */
  32345. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  32346. sizeof(ec_key_bad_1)));
  32347. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  32348. BIO_free(bio);
  32349. bio = NULL;
  32350. /* Private key data - fail. */
  32351. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  32352. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  32353. BIO_free(bio);
  32354. bio = NULL;
  32355. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32356. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  32357. BIO_free(bio);
  32358. bio = NULL;
  32359. /* Same test as above, but with a file pointer rather than a BIO. */
  32360. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32361. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  32362. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  32363. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  32364. EC_KEY_free(ec);
  32365. ec = NULL;
  32366. #ifndef NO_RSA
  32367. /* ensure that non-ec keys do not work */
  32368. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  32369. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  32370. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  32371. BIO_free(bio);
  32372. bio = NULL;
  32373. EC_KEY_free(ec);
  32374. ec = NULL;
  32375. #endif /* !NO_RSA */
  32376. /* Test 0x30, 0x00 fails. */
  32377. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  32378. sizeof(ec_priv_key_bad_1)));
  32379. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  32380. BIO_free(bio);
  32381. bio = NULL;
  32382. res = EXPECT_RESULT();
  32383. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32384. return res;
  32385. }
  32386. static int test_wolfSSL_PEM_PUBKEY(void)
  32387. {
  32388. int res = TEST_SKIPPED;
  32389. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  32390. EXPECT_DECLS;
  32391. BIO* bio = NULL;
  32392. EVP_PKEY* pkey = NULL;
  32393. /* test creating new EVP_PKEY with bad arg */
  32394. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  32395. /* test loading ECC key using BIO */
  32396. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  32397. {
  32398. XFILE file = XBADFILE;
  32399. const char* fname = "./certs/ecc-client-keyPub.pem";
  32400. size_t sz;
  32401. byte* buf = NULL;
  32402. EVP_PKEY* pkey2 = NULL;
  32403. EC_KEY* ec_key = NULL;
  32404. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32405. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  32406. ExpectIntGT(sz = XFTELL(file), 0);
  32407. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  32408. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32409. if (buf != NULL) {
  32410. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32411. }
  32412. if (file != XBADFILE) {
  32413. XFCLOSE(file);
  32414. }
  32415. /* Test using BIO new mem and loading PEM private key */
  32416. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32417. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  32418. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32419. BIO_free(bio);
  32420. bio = NULL;
  32421. /* Qt unit test case*/
  32422. ExpectNotNull(pkey2 = EVP_PKEY_new());
  32423. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  32424. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  32425. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32426. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  32427. #else
  32428. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  32429. #endif
  32430. EC_KEY_free(ec_key);
  32431. EVP_PKEY_free(pkey2);
  32432. EVP_PKEY_free(pkey);
  32433. pkey = NULL;
  32434. }
  32435. #endif
  32436. (void)bio;
  32437. (void)pkey;
  32438. res = EXPECT_RESULT();
  32439. #endif
  32440. return res;
  32441. }
  32442. #endif /* !NO_BIO */
  32443. static int test_DSA_do_sign_verify(void)
  32444. {
  32445. int res = TEST_SKIPPED;
  32446. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  32447. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  32448. !defined(NO_DSA)
  32449. EXPECT_DECLS;
  32450. unsigned char digest[WC_SHA_DIGEST_SIZE];
  32451. DSA_SIG* sig = NULL;
  32452. DSA* dsa = NULL;
  32453. word32 bytes;
  32454. byte sigBin[DSA_SIG_SIZE];
  32455. int dsacheck;
  32456. #ifdef USE_CERT_BUFFERS_1024
  32457. byte tmp[ONEK_BUF];
  32458. XMEMSET(tmp, 0, sizeof(tmp));
  32459. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  32460. bytes = sizeof_dsa_key_der_1024;
  32461. #elif defined(USE_CERT_BUFFERS_2048)
  32462. byte tmp[TWOK_BUF];
  32463. XMEMSET(tmp, 0, sizeof(tmp));
  32464. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  32465. bytes = sizeof_dsa_key_der_2048;
  32466. #else
  32467. byte tmp[TWOK_BUF];
  32468. XFILE fp = XBADFILE;
  32469. XMEMSET(tmp, 0, sizeof(tmp));
  32470. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  32471. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  32472. if (fp != XBADFILE)
  32473. XFCLOSE(fp);
  32474. #endif /* END USE_CERT_BUFFERS_1024 */
  32475. XMEMSET(digest, 202, sizeof(digest));
  32476. ExpectNotNull(dsa = DSA_new());
  32477. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  32478. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  32479. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  32480. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  32481. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  32482. DSA_SIG_free(sig);
  32483. DSA_free(dsa);
  32484. res = EXPECT_RESULT();
  32485. #endif
  32486. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  32487. return res;
  32488. }
  32489. static int test_wolfSSL_tmp_dh(void)
  32490. {
  32491. int res = TEST_SKIPPED;
  32492. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32493. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  32494. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32495. EXPECT_DECLS;
  32496. byte buff[6000];
  32497. char file[] = "./certs/dsaparams.pem";
  32498. XFILE f = XBADFILE;
  32499. int bytes = 0;
  32500. DSA* dsa = NULL;
  32501. DH* dh = NULL;
  32502. #if defined(WOLFSSL_DH_EXTRA) && \
  32503. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  32504. DH* dh2 = NULL;
  32505. #endif
  32506. BIO* bio = NULL;
  32507. SSL* ssl = NULL;
  32508. SSL_CTX* ctx = NULL;
  32509. #ifndef NO_WOLFSSL_SERVER
  32510. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32511. #else
  32512. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32513. #endif
  32514. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32515. WOLFSSL_FILETYPE_PEM));
  32516. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32517. WOLFSSL_FILETYPE_PEM));
  32518. ExpectNotNull(ssl = SSL_new(ctx));
  32519. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  32520. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  32521. if (f != XBADFILE)
  32522. XFCLOSE(f);
  32523. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32524. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  32525. ExpectNotNull(dsa);
  32526. dh = wolfSSL_DSA_dup_DH(dsa);
  32527. ExpectNotNull(dh);
  32528. #if defined(WOLFSSL_DH_EXTRA) && \
  32529. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  32530. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  32531. #endif
  32532. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  32533. #ifndef NO_WOLFSSL_SERVER
  32534. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  32535. #else
  32536. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  32537. #endif
  32538. BIO_free(bio);
  32539. DSA_free(dsa);
  32540. DH_free(dh);
  32541. #if defined(WOLFSSL_DH_EXTRA) && \
  32542. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  32543. DH_free(dh2);
  32544. #endif
  32545. SSL_free(ssl);
  32546. SSL_CTX_free(ctx);
  32547. res = EXPECT_RESULT();
  32548. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32549. #endif
  32550. return res;
  32551. }
  32552. static int test_wolfSSL_ctrl(void)
  32553. {
  32554. int res = TEST_SKIPPED;
  32555. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  32556. EXPECT_DECLS;
  32557. byte buff[6000];
  32558. BIO* bio = NULL;
  32559. int bytes;
  32560. BUF_MEM* ptr = NULL;
  32561. XMEMSET(buff, 0, sizeof(buff));
  32562. bytes = sizeof(buff);
  32563. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32564. ExpectNotNull(BIO_s_socket());
  32565. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  32566. /* needs tested after stubs filled out @TODO
  32567. SSL_ctrl
  32568. SSL_CTX_ctrl
  32569. */
  32570. BIO_free(bio);
  32571. res = EXPECT_RESULT();
  32572. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  32573. return res;
  32574. }
  32575. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  32576. {
  32577. int res = TEST_SKIPPED;
  32578. #ifdef OPENSSL_EXTRA
  32579. EXPECT_DECLS;
  32580. static const unsigned char pw[] = "password";
  32581. static const int pwSz = sizeof(pw) - 1;
  32582. size_t checkPwSz = 0;
  32583. const unsigned char* checkPw = NULL;
  32584. WOLFSSL_EVP_PKEY* key = NULL;
  32585. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  32586. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  32587. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  32588. pwSz));
  32589. if (key != NULL) {
  32590. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  32591. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  32592. ExpectIntEQ(key->pkey_sz, pwSz);
  32593. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  32594. }
  32595. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  32596. ExpectIntEQ((int)checkPwSz, pwSz);
  32597. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  32598. wolfSSL_EVP_PKEY_free(key);
  32599. key = NULL;
  32600. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  32601. 0));
  32602. ExpectIntEQ(key->pkey_sz, 0);
  32603. if (EXPECT_SUCCESS()) {
  32604. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  32605. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  32606. }
  32607. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  32608. ExpectIntEQ((int)checkPwSz, 0);
  32609. wolfSSL_EVP_PKEY_free(key);
  32610. key = NULL;
  32611. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  32612. 0));
  32613. ExpectIntEQ(key->pkey_sz, 0);
  32614. if (EXPECT_SUCCESS()) {
  32615. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  32616. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  32617. }
  32618. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  32619. ExpectIntEQ((int)checkPwSz, 0);
  32620. wolfSSL_EVP_PKEY_free(key);
  32621. key = NULL;
  32622. res = EXPECT_RESULT();
  32623. #endif /* OPENSSL_EXTRA */
  32624. return res;
  32625. }
  32626. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  32627. {
  32628. int res = TEST_SKIPPED;
  32629. #ifdef OPENSSL_EXTRA
  32630. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  32631. EXPECT_DECLS;
  32632. const char *priv = "ABCDEFGHIJKLMNOP";
  32633. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  32634. WOLFSSL_EVP_PKEY* key = NULL;
  32635. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  32636. NULL, NULL, AES_128_KEY_SIZE, cipher));
  32637. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  32638. NULL, (const unsigned char *)priv, 0, cipher));
  32639. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  32640. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  32641. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  32642. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  32643. wolfSSL_EVP_PKEY_free(key);
  32644. res = EXPECT_RESULT();
  32645. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  32646. #endif /* OPENSSL_EXTRA */
  32647. return res;
  32648. }
  32649. static int test_wolfSSL_EVP_Digest(void)
  32650. {
  32651. int res = TEST_SKIPPED;
  32652. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  32653. EXPECT_DECLS;
  32654. const char* in = "abc";
  32655. int inLen = (int)XSTRLEN(in);
  32656. byte out[WC_SHA256_DIGEST_SIZE];
  32657. unsigned int outLen;
  32658. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  32659. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  32660. "\x15\xAD";
  32661. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  32662. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  32663. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  32664. res = EXPECT_RESULT();
  32665. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  32666. return res;
  32667. }
  32668. static int test_wolfSSL_EVP_Digest_all(void)
  32669. {
  32670. int res = TEST_SKIPPED;
  32671. #ifdef OPENSSL_EXTRA
  32672. EXPECT_DECLS;
  32673. const char* digests[] = {
  32674. #ifndef NO_MD5
  32675. "MD5",
  32676. #endif
  32677. #ifndef NO_SHA
  32678. "SHA",
  32679. #endif
  32680. #ifdef WOLFSSL_SHA224
  32681. "SHA224",
  32682. #endif
  32683. #ifndef NO_SHA256
  32684. "SHA256",
  32685. #endif
  32686. #ifdef WOLFSSL_SHA384
  32687. "SHA384",
  32688. #endif
  32689. #ifdef WOLFSSL_SHA512
  32690. "SHA512",
  32691. #endif
  32692. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  32693. "SHA512_224",
  32694. #endif
  32695. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  32696. "SHA512_256",
  32697. #endif
  32698. #ifdef WOLFSSL_SHA3
  32699. #ifndef WOLFSSL_NOSHA3_224
  32700. "SHA3_224",
  32701. #endif
  32702. #ifndef WOLFSSL_NOSHA3_256
  32703. "SHA3_256",
  32704. #endif
  32705. "SHA3_384",
  32706. #ifndef WOLFSSL_NOSHA3_512
  32707. "SHA3_512",
  32708. #endif
  32709. #endif /* WOLFSSL_SHA3 */
  32710. NULL
  32711. };
  32712. const char** d;
  32713. const unsigned char in[] = "abc";
  32714. int inLen = XSTR_SIZEOF(in);
  32715. byte out[WC_MAX_DIGEST_SIZE];
  32716. unsigned int outLen;
  32717. for (d = digests; *d != NULL; d++) {
  32718. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  32719. ExpectIntGT(outLen, 0);
  32720. ExpectIntEQ(EVP_MD_size(*d), outLen);
  32721. }
  32722. res = EXPECT_RESULT();
  32723. #endif
  32724. return res;
  32725. }
  32726. static int test_wolfSSL_EVP_MD_size(void)
  32727. {
  32728. int res = TEST_SKIPPED;
  32729. #ifdef OPENSSL_EXTRA
  32730. EXPECT_DECLS;
  32731. WOLFSSL_EVP_MD_CTX mdCtx;
  32732. #ifdef WOLFSSL_SHA3
  32733. #ifndef WOLFSSL_NOSHA3_224
  32734. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32735. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  32736. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  32737. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  32738. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32739. #endif
  32740. #ifndef WOLFSSL_NOSHA3_256
  32741. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32742. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  32743. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  32744. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  32745. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32746. #endif
  32747. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32748. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  32749. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  32750. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  32751. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32752. #ifndef WOLFSSL_NOSHA3_512
  32753. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32754. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  32755. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  32756. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  32757. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32758. #endif
  32759. #endif /* WOLFSSL_SHA3 */
  32760. #ifndef NO_SHA256
  32761. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32762. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  32763. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32764. WC_SHA256_DIGEST_SIZE);
  32765. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32766. WC_SHA256_BLOCK_SIZE);
  32767. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  32768. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  32769. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32770. #endif
  32771. #ifndef NO_MD5
  32772. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32773. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  32774. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32775. WC_MD5_DIGEST_SIZE);
  32776. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32777. WC_MD5_BLOCK_SIZE);
  32778. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  32779. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  32780. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32781. #endif
  32782. #ifdef WOLFSSL_SHA224
  32783. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32784. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  32785. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32786. WC_SHA224_DIGEST_SIZE);
  32787. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32788. WC_SHA224_BLOCK_SIZE);
  32789. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  32790. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  32791. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32792. #endif
  32793. #ifdef WOLFSSL_SHA384
  32794. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32795. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  32796. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32797. WC_SHA384_DIGEST_SIZE);
  32798. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32799. WC_SHA384_BLOCK_SIZE);
  32800. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  32801. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  32802. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32803. #endif
  32804. #ifdef WOLFSSL_SHA512
  32805. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32806. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  32807. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32808. WC_SHA512_DIGEST_SIZE);
  32809. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32810. WC_SHA512_BLOCK_SIZE);
  32811. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  32812. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  32813. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32814. #endif
  32815. #ifndef NO_SHA
  32816. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32817. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  32818. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32819. WC_SHA_DIGEST_SIZE);
  32820. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32821. WC_SHA_BLOCK_SIZE);
  32822. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32823. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32824. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32825. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32826. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  32827. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32828. WC_SHA_DIGEST_SIZE);
  32829. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32830. WC_SHA_BLOCK_SIZE);
  32831. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32832. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32833. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32834. #endif
  32835. /* error case */
  32836. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32837. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  32838. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32839. BAD_FUNC_ARG);
  32840. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  32841. /* Cleanup is valid on uninit'ed struct */
  32842. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32843. res = EXPECT_RESULT();
  32844. #endif /* OPENSSL_EXTRA */
  32845. return res;
  32846. }
  32847. static int test_wolfSSL_EVP_MD_pkey_type(void)
  32848. {
  32849. int res = TEST_SKIPPED;
  32850. #ifdef OPENSSL_EXTRA
  32851. EXPECT_DECLS;
  32852. const WOLFSSL_EVP_MD* md;
  32853. #ifndef NO_MD5
  32854. ExpectNotNull(md = EVP_md5());
  32855. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  32856. #endif
  32857. #ifndef NO_SHA
  32858. ExpectNotNull(md = EVP_sha1());
  32859. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  32860. #endif
  32861. #ifdef WOLFSSL_SHA224
  32862. ExpectNotNull(md = EVP_sha224());
  32863. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  32864. #endif
  32865. ExpectNotNull(md = EVP_sha256());
  32866. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  32867. #ifdef WOLFSSL_SHA384
  32868. ExpectNotNull(md = EVP_sha384());
  32869. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  32870. #endif
  32871. #ifdef WOLFSSL_SHA512
  32872. ExpectNotNull(md = EVP_sha512());
  32873. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  32874. #endif
  32875. res = EXPECT_RESULT();
  32876. #endif
  32877. return res;
  32878. }
  32879. #ifdef OPENSSL_EXTRA
  32880. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32881. size_t testKeySz, const char* testData, size_t testDataSz,
  32882. const byte* testResult, size_t testResultSz)
  32883. {
  32884. EXPECT_DECLS;
  32885. unsigned char check[WC_MAX_DIGEST_SIZE];
  32886. size_t checkSz = -1;
  32887. WOLFSSL_EVP_PKEY* key = NULL;
  32888. WOLFSSL_EVP_MD_CTX mdCtx;
  32889. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32890. testKey, (int)testKeySz));
  32891. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32892. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32893. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32894. (unsigned int)testDataSz), 1);
  32895. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32896. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32897. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32898. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32899. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32900. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32901. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32902. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32903. (unsigned int)testDataSz), 1);
  32904. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32905. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32906. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32907. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32908. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32909. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32910. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32911. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32912. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32913. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32914. (unsigned int)testDataSz - 4), 1);
  32915. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32916. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32917. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32918. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32919. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32920. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32921. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32922. (unsigned int)testDataSz - 4), 1);
  32923. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32924. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32925. wolfSSL_EVP_PKEY_free(key);
  32926. return EXPECT_RESULT();
  32927. }
  32928. #endif
  32929. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32930. {
  32931. int res = TEST_SKIPPED;
  32932. #ifdef OPENSSL_EXTRA
  32933. EXPECT_DECLS;
  32934. static const unsigned char testKey[] =
  32935. {
  32936. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32937. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32938. 0x0b, 0x0b, 0x0b, 0x0b
  32939. };
  32940. static const char testData[] = "Hi There";
  32941. #ifdef WOLFSSL_SHA224
  32942. static const unsigned char testResultSha224[] =
  32943. {
  32944. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32945. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32946. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32947. 0x53, 0x68, 0x4b, 0x22
  32948. };
  32949. #endif
  32950. #ifndef NO_SHA256
  32951. static const unsigned char testResultSha256[] =
  32952. {
  32953. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32954. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32955. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32956. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32957. };
  32958. #endif
  32959. #ifdef WOLFSSL_SHA384
  32960. static const unsigned char testResultSha384[] =
  32961. {
  32962. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32963. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32964. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32965. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32966. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32967. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32968. };
  32969. #endif
  32970. #ifdef WOLFSSL_SHA512
  32971. static const unsigned char testResultSha512[] =
  32972. {
  32973. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32974. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32975. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32976. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32977. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32978. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32979. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32980. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32981. };
  32982. #endif
  32983. #ifdef WOLFSSL_SHA3
  32984. #ifndef WOLFSSL_NOSHA3_224
  32985. static const unsigned char testResultSha3_224[] =
  32986. {
  32987. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32988. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32989. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32990. 0xf3, 0xc8, 0x60, 0xf7
  32991. };
  32992. #endif
  32993. #ifndef WOLFSSL_NOSHA3_256
  32994. static const unsigned char testResultSha3_256[] =
  32995. {
  32996. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32997. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32998. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32999. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  33000. };
  33001. #endif
  33002. #ifndef WOLFSSL_NOSHA3_384
  33003. static const unsigned char testResultSha3_384[] =
  33004. {
  33005. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  33006. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  33007. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  33008. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  33009. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  33010. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  33011. };
  33012. #endif
  33013. #ifndef WOLFSSL_NOSHA3_512
  33014. static const unsigned char testResultSha3_512[] =
  33015. {
  33016. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  33017. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  33018. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  33019. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  33020. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  33021. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  33022. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  33023. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  33024. };
  33025. #endif
  33026. #endif
  33027. #ifndef NO_SHA256
  33028. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  33029. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  33030. sizeof(testResultSha256)), TEST_SUCCESS);
  33031. #endif
  33032. #ifdef WOLFSSL_SHA224
  33033. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  33034. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  33035. sizeof(testResultSha224)), TEST_SUCCESS);
  33036. #endif
  33037. #ifdef WOLFSSL_SHA384
  33038. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  33039. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  33040. sizeof(testResultSha384)), TEST_SUCCESS);
  33041. #endif
  33042. #ifdef WOLFSSL_SHA512
  33043. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  33044. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  33045. sizeof(testResultSha512)), TEST_SUCCESS);
  33046. #endif
  33047. #ifdef WOLFSSL_SHA3
  33048. #ifndef WOLFSSL_NOSHA3_224
  33049. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  33050. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  33051. sizeof(testResultSha3_224)), TEST_SUCCESS);
  33052. #endif
  33053. #ifndef WOLFSSL_NOSHA3_256
  33054. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  33055. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  33056. sizeof(testResultSha3_256)), TEST_SUCCESS);
  33057. #endif
  33058. #ifndef WOLFSSL_NOSHA3_384
  33059. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  33060. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  33061. sizeof(testResultSha3_384)), TEST_SUCCESS);
  33062. #endif
  33063. #ifndef WOLFSSL_NOSHA3_512
  33064. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  33065. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  33066. sizeof(testResultSha3_512)), TEST_SUCCESS);
  33067. #endif
  33068. #endif
  33069. res = EXPECT_RESULT();
  33070. #endif /* OPENSSL_EXTRA */
  33071. return res;
  33072. }
  33073. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  33074. {
  33075. int res = TEST_SKIPPED;
  33076. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  33077. defined(USE_CERT_BUFFERS_2048)
  33078. EXPECT_DECLS;
  33079. WOLFSSL_EVP_PKEY* privKey = NULL;
  33080. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33081. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  33082. const char testData[] = "Hi There";
  33083. WOLFSSL_EVP_MD_CTX mdCtx;
  33084. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  33085. int ret;
  33086. size_t checkSz = -1;
  33087. int sz = 2048 / 8;
  33088. const unsigned char* cp;
  33089. const unsigned char* p;
  33090. unsigned char check[2048/8];
  33091. size_t i;
  33092. int paddings[] = {
  33093. RSA_PKCS1_PADDING,
  33094. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  33095. RSA_PKCS1_PSS_PADDING,
  33096. #endif
  33097. };
  33098. cp = client_key_der_2048;
  33099. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  33100. sizeof_client_key_der_2048)));
  33101. p = client_keypub_der_2048;
  33102. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33103. sizeof_client_keypub_der_2048)));
  33104. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33105. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  33106. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33107. NULL, privKey), 1);
  33108. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33109. (unsigned int)XSTRLEN(testData)), 1);
  33110. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33111. ExpectIntEQ((int)checkSz, sz);
  33112. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33113. ExpectIntEQ((int)checkSz,sz);
  33114. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33115. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33116. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  33117. ExpectIntEQ(ret, 1);
  33118. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33119. ExpectIntEQ(ret, 1);
  33120. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33121. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33122. NULL, pubKey), 1);
  33123. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33124. (unsigned int)XSTRLEN(testData)),
  33125. 1);
  33126. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33127. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33128. ExpectIntEQ(ret, 1);
  33129. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33130. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33131. NULL, privKey), 1);
  33132. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33133. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33134. ExpectIntEQ((int)checkSz, sz);
  33135. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33136. ExpectIntEQ((int)checkSz, sz);
  33137. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33138. (unsigned int)XSTRLEN(testData) - 4), 1);
  33139. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33140. ExpectIntEQ((int)checkSz, sz);
  33141. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33142. ExpectIntEQ(ret, 1);
  33143. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33144. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33145. NULL, pubKey), 1);
  33146. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33147. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33148. (unsigned int)XSTRLEN(testData) - 4),
  33149. 1);
  33150. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33151. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33152. ExpectIntEQ(ret, 1);
  33153. /* Check all signing padding types */
  33154. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  33155. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33156. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  33157. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  33158. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33159. paddings[i]), 1);
  33160. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33161. (unsigned int)XSTRLEN(testData)), 1);
  33162. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33163. ExpectIntEQ((int)checkSz, sz);
  33164. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33165. ExpectIntEQ((int)checkSz,sz);
  33166. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33167. ExpectIntEQ(ret, 1);
  33168. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33169. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  33170. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  33171. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33172. paddings[i]), 1);
  33173. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33174. (unsigned int)XSTRLEN(testData)), 1);
  33175. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33176. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33177. ExpectIntEQ(ret, 1);
  33178. }
  33179. wolfSSL_EVP_PKEY_free(pubKey);
  33180. wolfSSL_EVP_PKEY_free(privKey);
  33181. res = EXPECT_RESULT();
  33182. #endif
  33183. return res;
  33184. }
  33185. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  33186. {
  33187. int res = TEST_SKIPPED;
  33188. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  33189. EXPECT_DECLS;
  33190. WOLFSSL_EVP_PKEY* privKey = NULL;
  33191. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33192. const char testData[] = "Hi There";
  33193. WOLFSSL_EVP_MD_CTX mdCtx;
  33194. int ret;
  33195. size_t checkSz = -1;
  33196. const unsigned char* cp;
  33197. const unsigned char* p;
  33198. unsigned char check[2048/8];
  33199. cp = ecc_clikey_der_256;
  33200. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  33201. sizeof_ecc_clikey_der_256));
  33202. p = ecc_clikeypub_der_256;
  33203. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33204. sizeof_ecc_clikeypub_der_256)));
  33205. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33206. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33207. NULL, privKey), 1);
  33208. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33209. (unsigned int)XSTRLEN(testData)), 1);
  33210. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33211. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33212. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33213. ExpectIntEQ(ret, 1);
  33214. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33215. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33216. NULL, pubKey), 1);
  33217. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33218. (unsigned int)XSTRLEN(testData)),
  33219. 1);
  33220. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33221. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33222. ExpectIntEQ(ret, 1);
  33223. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33224. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33225. NULL, privKey), 1);
  33226. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33227. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33228. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33229. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33230. (unsigned int)XSTRLEN(testData) - 4), 1);
  33231. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33232. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33233. ExpectIntEQ(ret, 1);
  33234. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33235. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33236. NULL, pubKey), 1);
  33237. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33238. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33239. (unsigned int)XSTRLEN(testData) - 4),
  33240. 1);
  33241. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33242. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33243. ExpectIntEQ(ret, 1);
  33244. wolfSSL_EVP_PKEY_free(pubKey);
  33245. wolfSSL_EVP_PKEY_free(privKey);
  33246. res = EXPECT_RESULT();
  33247. #endif
  33248. return res;
  33249. }
  33250. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  33251. {
  33252. int res = TEST_SKIPPED;
  33253. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33254. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  33255. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33256. EXPECT_DECLS;
  33257. char caFile[] = "./certs/client-ca.pem";
  33258. char clientFile[] = "./certs/client-cert.pem";
  33259. SSL_CTX* ctx = NULL;
  33260. X509* x509 = NULL;
  33261. BIO *bio = NULL;
  33262. X509 *cert = NULL;
  33263. X509 *ca = NULL;
  33264. STACK_OF(X509) *chain = NULL;
  33265. STACK_OF(X509) *chain2 = NULL;
  33266. #ifndef NO_WOLFSSL_SERVER
  33267. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33268. #else
  33269. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33270. #endif
  33271. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  33272. WOLFSSL_FILETYPE_PEM));
  33273. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  33274. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  33275. WOLFSSL_FILETYPE_PEM));
  33276. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  33277. /* additional test of getting EVP_PKEY key size from X509
  33278. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  33279. * allowed with user RSA */
  33280. {
  33281. EVP_PKEY* pkey = NULL;
  33282. #if defined(HAVE_ECC)
  33283. X509* ecX509 = NULL;
  33284. #endif /* HAVE_ECC */
  33285. ExpectNotNull(pkey = X509_get_pubkey(x509));
  33286. /* current RSA key is 2048 bit (256 bytes) */
  33287. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  33288. EVP_PKEY_free(pkey);
  33289. pkey = NULL;
  33290. #if defined(HAVE_ECC)
  33291. #if defined(USE_CERT_BUFFERS_256)
  33292. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  33293. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  33294. SSL_FILETYPE_ASN1));
  33295. #else
  33296. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  33297. cliEccCertFile, SSL_FILETYPE_PEM));
  33298. #endif
  33299. pkey = X509_get_pubkey(ecX509);
  33300. ExpectNotNull(pkey);
  33301. /* current ECC key is 256 bit (32 bytes) */
  33302. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  33303. X509_free(ecX509);
  33304. ecX509 = NULL;
  33305. EVP_PKEY_free(pkey);
  33306. pkey = NULL;
  33307. #endif /* HAVE_ECC */
  33308. }
  33309. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  33310. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  33311. if (EXPECT_SUCCESS()) {
  33312. x509 = NULL;
  33313. }
  33314. #ifdef WOLFSSL_ENCRYPTED_KEYS
  33315. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  33316. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  33317. #endif
  33318. SSL_CTX_free(ctx);
  33319. ctx = NULL;
  33320. #ifndef NO_WOLFSSL_SERVER
  33321. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33322. #else
  33323. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33324. #endif
  33325. /* Test haproxy use case */
  33326. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  33327. /* Read Certificate */
  33328. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33329. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  33330. ExpectNotNull(chain = sk_X509_new_null());
  33331. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  33332. if (EXPECT_SUCCESS()) {
  33333. ca = NULL;
  33334. }
  33335. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  33336. ExpectNotNull(ca = sk_X509_shift(chain2));
  33337. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  33338. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  33339. if (EXPECT_SUCCESS()) {
  33340. ca = NULL;
  33341. }
  33342. BIO_free(bio);
  33343. X509_free(cert);
  33344. X509_free(ca);
  33345. X509_free(x509);
  33346. sk_X509_pop_free(chain, X509_free);
  33347. sk_X509_pop_free(chain2, X509_free);
  33348. SSL_CTX_free(ctx);
  33349. res = EXPECT_RESULT();
  33350. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33351. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33352. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  33353. return res;
  33354. }
  33355. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  33356. static int test_wolfSSL_ERR_peek_last_error_line(void)
  33357. {
  33358. int res = TEST_SKIPPED;
  33359. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33360. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  33361. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  33362. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  33363. EXPECT_DECLS;
  33364. callback_functions client_cb;
  33365. callback_functions server_cb;
  33366. int line = 0;
  33367. int flag = ERR_TXT_STRING;
  33368. const char* file = NULL;
  33369. const char* data = NULL;
  33370. /* create a failed connection and inspect the error */
  33371. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  33372. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33373. client_cb.method = wolfTLSv1_1_client_method;
  33374. server_cb.method = wolfTLSv1_2_server_method;
  33375. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  33376. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  33377. ExpectNotNull(data);
  33378. /* check clearing error state */
  33379. ERR_remove_state(0);
  33380. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  33381. ERR_peek_last_error_line(NULL, &line);
  33382. ExpectIntEQ(line, 0);
  33383. ERR_peek_last_error_line(&file, NULL);
  33384. ExpectNull(file);
  33385. /* retry connection to fill error queue */
  33386. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  33387. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33388. client_cb.method = wolfTLSv1_1_client_method;
  33389. server_cb.method = wolfTLSv1_2_server_method;
  33390. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  33391. /* check that error code was stored */
  33392. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  33393. ERR_peek_last_error_line(NULL, &line);
  33394. ExpectIntNE(line, 0);
  33395. ERR_peek_last_error_line(&file, NULL);
  33396. ExpectNotNull(file);
  33397. fprintf(stderr, "\nTesting error print out\n");
  33398. ERR_print_errors_fp(stderr);
  33399. fprintf(stderr, "Done testing print out\n\n");
  33400. res = EXPECT_RESULT();
  33401. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33402. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  33403. return res;
  33404. }
  33405. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  33406. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33407. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33408. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  33409. {
  33410. (void) ok;
  33411. (void) ctx;
  33412. fprintf(stderr, "ENTER verify_cb\n");
  33413. return SSL_SUCCESS;
  33414. }
  33415. #endif
  33416. static int test_wolfSSL_X509_Name_canon(void)
  33417. {
  33418. int res = TEST_SKIPPED;
  33419. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33420. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  33421. defined(WOLFSSL_CERT_GEN) && \
  33422. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  33423. EXPECT_DECLS;
  33424. const long ex_hash1 = 0x0fdb2da4;
  33425. const long ex_hash2 = 0x9f3e8c9e;
  33426. X509_NAME *name = NULL;
  33427. X509 *x509 = NULL;
  33428. XFILE file = XBADFILE;
  33429. unsigned long hash = 0;
  33430. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  33431. byte *pbuf = NULL;
  33432. word32 len = 0;
  33433. (void) ex_hash2;
  33434. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  33435. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  33436. ExpectNotNull(name = X509_get_issuer_name(x509));
  33437. /* When output buffer is NULL, should return necessary output buffer
  33438. * length.*/
  33439. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  33440. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  33441. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  33442. hash = (((unsigned long)digest[3] << 24) |
  33443. ((unsigned long)digest[2] << 16) |
  33444. ((unsigned long)digest[1] << 8) |
  33445. ((unsigned long)digest[0]));
  33446. ExpectIntEQ(hash, ex_hash1);
  33447. if (file != XBADFILE) {
  33448. XFCLOSE(file);
  33449. file = XBADFILE;
  33450. }
  33451. X509_free(x509);
  33452. x509 = NULL;
  33453. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  33454. pbuf = NULL;
  33455. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  33456. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  33457. ExpectNotNull(name = X509_get_issuer_name(x509));
  33458. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  33459. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  33460. hash = (((unsigned long)digest[3] << 24) |
  33461. ((unsigned long)digest[2] << 16) |
  33462. ((unsigned long)digest[1] << 8) |
  33463. ((unsigned long)digest[0]));
  33464. ExpectIntEQ(hash, ex_hash2);
  33465. if (file != XBADFILE)
  33466. XFCLOSE(file);
  33467. X509_free(x509);
  33468. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  33469. res = EXPECT_RESULT();
  33470. #endif
  33471. return res;
  33472. }
  33473. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  33474. {
  33475. int res = TEST_SKIPPED;
  33476. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  33477. EXPECT_DECLS;
  33478. const int MAX_DIR = 4;
  33479. const char paths[][32] = {
  33480. "./certs/ed25519",
  33481. "./certs/ecc",
  33482. "./certs/crl",
  33483. "./certs/",
  33484. };
  33485. char CertCrl_path[MAX_FILENAME_SZ];
  33486. char *p;
  33487. X509_STORE* str = NULL;
  33488. X509_LOOKUP* lookup = NULL;
  33489. WOLFSSL_STACK* sk = NULL;
  33490. int len, total_len, i;
  33491. (void)sk;
  33492. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  33493. /* illegal string */
  33494. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33495. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33496. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  33497. SSL_FILETYPE_PEM,NULL), 0);
  33498. /* free store */
  33499. X509_STORE_free(str);
  33500. str = NULL;
  33501. /* short folder string */
  33502. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33503. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33504. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  33505. SSL_FILETYPE_PEM,NULL), 1);
  33506. #if defined(WOLFSSL_INT_H)
  33507. /* only available when including internal.h */
  33508. ExpectNotNull(sk = lookup->dirs->dir_entry);
  33509. #endif
  33510. /* free store */
  33511. X509_STORE_free(str);
  33512. str = NULL;
  33513. /* typical function check */
  33514. p = &CertCrl_path[0];
  33515. total_len = 0;
  33516. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  33517. len = (int)XSTRLEN((const char*)&paths[i]);
  33518. total_len += len;
  33519. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  33520. p += len;
  33521. if (i != 0) *(p++) = SEPARATOR_CHAR;
  33522. }
  33523. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33524. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33525. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  33526. SSL_FILETYPE_PEM,NULL), 1);
  33527. #if defined(WOLFSSL_INT_H)
  33528. /* only available when including internal.h */
  33529. ExpectNotNull(sk = lookup->dirs->dir_entry);
  33530. #endif
  33531. X509_STORE_free(str);
  33532. res = EXPECT_RESULT();
  33533. #endif
  33534. return res;
  33535. }
  33536. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  33537. {
  33538. int res = TEST_SKIPPED;
  33539. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33540. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33541. defined(WOLFSSL_SIGNER_DER_CERT)
  33542. EXPECT_DECLS;
  33543. X509_STORE_CTX* ctx = NULL;
  33544. X509_STORE* str = NULL;
  33545. X509_LOOKUP* lookup = NULL;
  33546. X509* cert1 = NULL;
  33547. X509* x509Ca = NULL;
  33548. X509* x509Svr = NULL;
  33549. X509* issuer = NULL;
  33550. WOLFSSL_STACK* sk = NULL;
  33551. X509_NAME* caName = NULL;
  33552. X509_NAME* issuerName = NULL;
  33553. XFILE file1 = XBADFILE;
  33554. int i;
  33555. int cert_count = 0;
  33556. int cmp;
  33557. char der[] = "certs/ca-cert.der";
  33558. #ifdef HAVE_CRL
  33559. char pem[][100] = {
  33560. "./certs/crl/crl.pem",
  33561. "./certs/crl/crl2.pem",
  33562. "./certs/crl/caEccCrl.pem",
  33563. "./certs/crl/eccCliCRL.pem",
  33564. "./certs/crl/eccSrvCRL.pem",
  33565. ""
  33566. };
  33567. #endif
  33568. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  33569. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  33570. if (file1 != XBADFILE)
  33571. XFCLOSE(file1);
  33572. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33573. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33574. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33575. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  33576. SSL_FILETYPE_PEM,NULL), 1);
  33577. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  33578. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  33579. /* check if CA cert is loaded into the store */
  33580. for (i = 0; i < cert_count; i++) {
  33581. x509Ca = sk_X509_value(sk, i);
  33582. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  33583. }
  33584. ExpectNotNull((x509Svr =
  33585. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  33586. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  33587. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  33588. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  33589. ExpectNotNull(issuer);
  33590. caName = X509_get_subject_name(x509Ca);
  33591. ExpectNotNull(caName);
  33592. issuerName = X509_get_subject_name(issuer);
  33593. ExpectNotNull(issuerName);
  33594. cmp = X509_NAME_cmp(caName, issuerName);
  33595. ExpectIntEQ(cmp, 0);
  33596. /* load der format */
  33597. X509_free(issuer);
  33598. issuer = NULL;
  33599. X509_STORE_CTX_free(ctx);
  33600. ctx = NULL;
  33601. X509_STORE_free(str);
  33602. str = NULL;
  33603. sk_X509_pop_free(sk, NULL);
  33604. sk = NULL;
  33605. X509_free(x509Svr);
  33606. x509Svr = NULL;
  33607. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33608. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33609. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  33610. SSL_FILETYPE_ASN1,NULL), 1);
  33611. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  33612. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  33613. /* check if CA cert is loaded into the store */
  33614. for (i = 0; i < cert_count; i++) {
  33615. x509Ca = sk_X509_value(sk, i);
  33616. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  33617. }
  33618. X509_STORE_free(str);
  33619. str = NULL;
  33620. sk_X509_pop_free(sk, NULL);
  33621. sk = NULL;
  33622. X509_free(cert1);
  33623. cert1 = NULL;
  33624. #ifdef HAVE_CRL
  33625. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  33626. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  33627. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  33628. SSL_FILETYPE_PEM,NULL), 1);
  33629. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  33630. "certs/server-revoked-cert.pem",
  33631. SSL_FILETYPE_PEM,NULL), 1);
  33632. if (str) {
  33633. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  33634. WOLFSSL_FILETYPE_PEM), 1);
  33635. /* since store hasn't yet known the revoked cert*/
  33636. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  33637. "certs/server-revoked-cert.pem",
  33638. WOLFSSL_FILETYPE_PEM), 1);
  33639. }
  33640. for (i = 0; pem[i][0] != '\0'; i++)
  33641. {
  33642. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  33643. SSL_FILETYPE_PEM, NULL), 1);
  33644. }
  33645. if (str) {
  33646. /* since store knows crl list */
  33647. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  33648. "certs/server-revoked-cert.pem",
  33649. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  33650. }
  33651. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  33652. X509_STORE_free(str);
  33653. #endif
  33654. res = EXPECT_RESULT();
  33655. #endif
  33656. return res;
  33657. }
  33658. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  33659. {
  33660. int res = TEST_SKIPPED;
  33661. #if defined(OPENSSL_EXTRA)
  33662. X509_STORE_CTX_cleanup(NULL);
  33663. X509_STORE_CTX_trusted_stack(NULL, NULL);
  33664. res = TEST_SUCCESS;
  33665. #endif
  33666. return res;
  33667. }
  33668. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  33669. {
  33670. int res = TEST_SKIPPED;
  33671. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33672. EXPECT_DECLS;
  33673. X509_STORE_CTX* ctx = NULL;
  33674. X509_STORE* str = NULL;
  33675. X509* x509Ca = NULL;
  33676. X509* x509Svr = NULL;
  33677. X509* issuer = NULL;
  33678. X509_NAME* caName = NULL;
  33679. X509_NAME* issuerName = NULL;
  33680. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33681. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33682. ExpectNotNull((x509Ca =
  33683. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  33684. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  33685. ExpectNotNull((x509Svr =
  33686. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  33687. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  33688. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  33689. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  33690. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  33691. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  33692. #ifdef WOLFSSL_SIGNER_DER_CERT
  33693. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  33694. #endif
  33695. X509_free(issuer);
  33696. X509_STORE_CTX_free(ctx);
  33697. X509_free(x509Svr);
  33698. X509_STORE_free(str);
  33699. X509_free(x509Ca);
  33700. res = EXPECT_RESULT();
  33701. #endif
  33702. return res;
  33703. }
  33704. static int test_wolfSSL_PKCS7_certs(void)
  33705. {
  33706. int res = TEST_SKIPPED;
  33707. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  33708. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  33709. EXPECT_DECLS;
  33710. STACK_OF(X509)* sk = NULL;
  33711. STACK_OF(X509_INFO)* info_sk = NULL;
  33712. PKCS7 *p7 = NULL;
  33713. BIO* bio = NULL;
  33714. const byte* p = NULL;
  33715. int buflen = 0;
  33716. int i;
  33717. /* Test twice. Once with d2i and once without to test
  33718. * that everything is free'd correctly. */
  33719. for (i = 0; i < 2; i++) {
  33720. ExpectNotNull(p7 = PKCS7_new());
  33721. if (p7 != NULL) {
  33722. p7->version = 1;
  33723. #ifdef NO_SHA
  33724. p7->hashOID = SHA256h;
  33725. #else
  33726. p7->hashOID = SHAh;
  33727. #endif
  33728. }
  33729. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  33730. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  33731. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  33732. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  33733. ExpectNotNull(sk = sk_X509_new_null());
  33734. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  33735. X509_INFO* info = NULL;
  33736. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  33737. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  33738. if (EXPECT_SUCCESS() && (info != NULL)) {
  33739. info->x509 = NULL;
  33740. }
  33741. X509_INFO_free(info);
  33742. }
  33743. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  33744. info_sk = NULL;
  33745. BIO_free(bio);
  33746. bio = NULL;
  33747. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33748. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  33749. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  33750. sk_X509_pop_free(sk, X509_free);
  33751. }
  33752. sk = NULL;
  33753. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  33754. if (i == 0) {
  33755. PKCS7_free(p7);
  33756. p7 = NULL;
  33757. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  33758. if (p7 != NULL) {
  33759. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  33760. * them */
  33761. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  33762. }
  33763. /* PKCS7_free free's the certs */
  33764. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  33765. }
  33766. BIO_free(bio);
  33767. bio = NULL;
  33768. PKCS7_free(p7);
  33769. p7 = NULL;
  33770. }
  33771. res = EXPECT_RESULT();
  33772. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33773. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  33774. return res;
  33775. }
  33776. static int test_wolfSSL_X509_STORE_CTX(void)
  33777. {
  33778. int res = TEST_SKIPPED;
  33779. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33780. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33781. EXPECT_DECLS;
  33782. X509_STORE_CTX* ctx = NULL;
  33783. X509_STORE* str = NULL;
  33784. X509* x509 = NULL;
  33785. #ifdef OPENSSL_ALL
  33786. X509* x5092 = NULL;
  33787. STACK_OF(X509) *sk = NULL;
  33788. STACK_OF(X509) *sk2 = NULL;
  33789. STACK_OF(X509) *sk3 = NULL;
  33790. #endif
  33791. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33792. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33793. ExpectNotNull((x509 =
  33794. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  33795. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  33796. #ifdef OPENSSL_ALL
  33797. /* sk_X509_new only in OPENSSL_ALL */
  33798. sk = sk_X509_new_null();
  33799. ExpectNotNull(sk);
  33800. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  33801. #else
  33802. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  33803. #endif
  33804. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  33805. X509_STORE_CTX_set_error(ctx, -5);
  33806. X509_STORE_CTX_set_error(NULL, -5);
  33807. X509_STORE_CTX_free(ctx);
  33808. ctx = NULL;
  33809. #ifdef OPENSSL_ALL
  33810. sk_X509_pop_free(sk, NULL);
  33811. sk = NULL;
  33812. #endif
  33813. X509_STORE_free(str);
  33814. str = NULL;
  33815. X509_free(x509);
  33816. x509 = NULL;
  33817. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33818. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  33819. X509_STORE_CTX_free(ctx);
  33820. ctx = NULL;
  33821. #ifdef OPENSSL_ALL
  33822. /* test X509_STORE_CTX_get(1)_chain */
  33823. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  33824. SSL_FILETYPE_PEM)));
  33825. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  33826. SSL_FILETYPE_PEM)));
  33827. ExpectNotNull((sk = sk_X509_new_null()));
  33828. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  33829. if (EXPECT_FAIL()) {
  33830. X509_free(x509);
  33831. x509 = NULL;
  33832. }
  33833. ExpectNotNull((str = X509_STORE_new()));
  33834. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  33835. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  33836. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  33837. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  33838. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  33839. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  33840. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  33841. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  33842. X509_STORE_CTX_free(ctx);
  33843. ctx = NULL;
  33844. X509_STORE_free(str);
  33845. str = NULL;
  33846. /* CTX certs not freed yet */
  33847. X509_free(x5092);
  33848. x5092 = NULL;
  33849. sk_X509_pop_free(sk, NULL);
  33850. sk = NULL;
  33851. /* sk3 is dup so free here */
  33852. sk_X509_pop_free(sk3, NULL);
  33853. sk3 = NULL;
  33854. #endif
  33855. /* test X509_STORE_CTX_get/set_ex_data */
  33856. {
  33857. int i = 0, tmpData = 5;
  33858. void* tmpDataRet;
  33859. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33860. #ifdef HAVE_EX_DATA
  33861. for (i = 0; i < MAX_EX_DATA; i++) {
  33862. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33863. WOLFSSL_SUCCESS);
  33864. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33865. ExpectNotNull(tmpDataRet);
  33866. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33867. }
  33868. #else
  33869. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33870. WOLFSSL_FAILURE);
  33871. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33872. ExpectNull(tmpDataRet);
  33873. #endif
  33874. X509_STORE_CTX_free(ctx);
  33875. ctx = NULL;
  33876. }
  33877. /* test X509_STORE_get/set_ex_data */
  33878. {
  33879. int i = 0, tmpData = 99;
  33880. void* tmpDataRet;
  33881. ExpectNotNull(str = X509_STORE_new());
  33882. #ifdef HAVE_EX_DATA
  33883. for (i = 0; i < MAX_EX_DATA; i++) {
  33884. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33885. WOLFSSL_SUCCESS);
  33886. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33887. ExpectNotNull(tmpDataRet);
  33888. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33889. }
  33890. #else
  33891. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33892. WOLFSSL_FAILURE);
  33893. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33894. ExpectNull(tmpDataRet);
  33895. #endif
  33896. X509_STORE_free(str);
  33897. str = NULL;
  33898. }
  33899. res = EXPECT_RESULT();
  33900. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33901. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33902. return res;
  33903. }
  33904. static int test_wolfSSL_X509_STORE_set_flags(void)
  33905. {
  33906. int res = TEST_SKIPPED;
  33907. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33908. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33909. EXPECT_DECLS;
  33910. X509_STORE* store = NULL;
  33911. X509* x509 = NULL;
  33912. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33913. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33914. WOLFSSL_FILETYPE_PEM)));
  33915. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33916. #ifdef HAVE_CRL
  33917. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33918. WOLFSSL_SUCCESS);
  33919. #else
  33920. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33921. NOT_COMPILED_IN);
  33922. #endif
  33923. wolfSSL_X509_free(x509);
  33924. wolfSSL_X509_STORE_free(store);
  33925. res = EXPECT_RESULT();
  33926. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33927. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33928. return res;
  33929. }
  33930. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33931. {
  33932. int res = TEST_SKIPPED;
  33933. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33934. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33935. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33936. EXPECT_DECLS;
  33937. WOLFSSL_X509_STORE* store = NULL;
  33938. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33939. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33940. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33941. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33942. X509_FILETYPE_PEM), 1);
  33943. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33944. X509_FILETYPE_PEM), 1);
  33945. if (store != NULL) {
  33946. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33947. WOLFSSL_FILETYPE_PEM), 1);
  33948. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33949. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33950. }
  33951. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33952. X509_FILETYPE_PEM), 1);
  33953. if (store != NULL) {
  33954. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33955. WOLFSSL_FILETYPE_PEM), 1);
  33956. }
  33957. wolfSSL_X509_STORE_free(store);
  33958. res = EXPECT_RESULT();
  33959. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33960. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33961. return res;
  33962. }
  33963. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33964. {
  33965. int res = TEST_SKIPPED;
  33966. #if defined(OPENSSL_EXTRA)
  33967. EXPECT_DECLS;
  33968. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33969. time_t c_time;
  33970. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33971. c_time = 365*24*60*60;
  33972. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33973. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33974. WOLFSSL_USE_CHECK_TIME);
  33975. ExpectTrue(ctx->param->check_time == c_time);
  33976. wolfSSL_X509_STORE_CTX_free(ctx);
  33977. res = EXPECT_RESULT();
  33978. #endif /* OPENSSL_EXTRA */
  33979. return res;
  33980. }
  33981. static int test_wolfSSL_CTX_get0_set1_param(void)
  33982. {
  33983. int res = TEST_SKIPPED;
  33984. #if defined(OPENSSL_EXTRA)
  33985. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33986. EXPECT_DECLS;
  33987. SSL_CTX* ctx = NULL;
  33988. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33989. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33990. char testIPv4[] = "127.0.0.1";
  33991. char testhostName[] = "foo.hoge.com";
  33992. #ifndef NO_WOLFSSL_SERVER
  33993. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33994. #else
  33995. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33996. #endif
  33997. ExpectNull(SSL_CTX_get0_param(NULL));
  33998. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33999. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  34000. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  34001. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  34002. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  34003. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  34004. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  34005. WOLFSSL_SUCCESS);
  34006. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  34007. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  34008. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  34009. (int)XSTRLEN(testhostName)));
  34010. ExpectIntEQ(0x01, pParam->hostFlags);
  34011. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34012. /* test for incorrect patameter */
  34013. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  34014. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  34015. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  34016. SSL_CTX_free(ctx);
  34017. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  34018. res = EXPECT_RESULT();
  34019. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34020. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34021. return res;
  34022. }
  34023. static int test_wolfSSL_get0_param(void)
  34024. {
  34025. int res = TEST_SKIPPED;
  34026. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34027. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34028. EXPECT_DECLS;
  34029. SSL_CTX* ctx = NULL;
  34030. SSL* ssl = NULL;
  34031. #ifndef NO_WOLFSSL_SERVER
  34032. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34033. #else
  34034. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34035. #endif
  34036. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34037. SSL_FILETYPE_PEM));
  34038. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34039. ExpectNotNull(ssl = SSL_new(ctx));
  34040. ExpectNotNull(SSL_get0_param(ssl));
  34041. SSL_free(ssl);
  34042. SSL_CTX_free(ctx);
  34043. res = EXPECT_RESULT();
  34044. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34045. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34046. return res;
  34047. }
  34048. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  34049. {
  34050. int res = TEST_SKIPPED;
  34051. #if defined(OPENSSL_EXTRA)
  34052. EXPECT_DECLS;
  34053. const char host[] = "www.example.com";
  34054. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34055. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  34056. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34057. if (pParam != NULL) {
  34058. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34059. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  34060. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34061. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34062. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34063. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34064. }
  34065. res = EXPECT_RESULT();
  34066. #endif /* OPENSSL_EXTRA */
  34067. return res;
  34068. }
  34069. static int test_wolfSSL_set1_host(void)
  34070. {
  34071. int res = TEST_SKIPPED;
  34072. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34073. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34074. EXPECT_DECLS;
  34075. const char host[] = "www.test_wolfSSL_set1_host.com";
  34076. const char emptyStr[] = "";
  34077. SSL_CTX* ctx = NULL;
  34078. SSL* ssl = NULL;
  34079. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34080. #ifndef NO_WOLFSSL_SERVER
  34081. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34082. #else
  34083. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34084. #endif
  34085. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34086. SSL_FILETYPE_PEM));
  34087. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34088. ExpectNotNull(ssl = SSL_new(ctx));
  34089. pParam = SSL_get0_param(ssl);
  34090. /* we should get back host string */
  34091. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34092. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34093. /* we should get back empty string */
  34094. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  34095. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34096. /* we should get back host string */
  34097. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34098. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34099. /* we should get back empty string */
  34100. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  34101. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34102. SSL_free(ssl);
  34103. SSL_CTX_free(ctx);
  34104. res = EXPECT_RESULT();
  34105. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34106. #endif /* OPENSSL_EXTRA */
  34107. return res;
  34108. }
  34109. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  34110. {
  34111. int res = TEST_SKIPPED;
  34112. #if defined(OPENSSL_EXTRA)
  34113. EXPECT_DECLS;
  34114. unsigned char buf[16] = {0};
  34115. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  34116. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  34117. /* test 127.0.0.1 */
  34118. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  34119. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  34120. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  34121. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  34122. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34123. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  34124. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  34125. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  34126. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34127. ExpectIntEQ(XSTRNCMP(param->ipasc,
  34128. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  34129. /* test 2001:db8:: */
  34130. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34131. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34132. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34133. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  34134. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34135. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  34136. /* test ::1234:5678 */
  34137. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  34138. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34139. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34140. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34141. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34142. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  34143. /* test 2001:db8::1234:5678 */
  34144. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34145. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34146. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34147. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34148. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34149. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  34150. sizeof(param->ipasc)), 0);
  34151. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  34152. /* 2001:db8:1::ab9:c0a8:102 */
  34153. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34154. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  34155. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  34156. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  34157. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34158. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  34159. sizeof(param->ipasc)), 0);
  34160. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34161. res = EXPECT_RESULT();
  34162. #endif /* OPENSSL_EXTRA */
  34163. return res;
  34164. }
  34165. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  34166. {
  34167. int res = TEST_SKIPPED;
  34168. #if defined(OPENSSL_EXTRA)
  34169. EXPECT_DECLS;
  34170. X509_STORE* store = NULL;
  34171. X509_STORE_CTX* ctx = NULL;
  34172. X509_STORE_CTX* ctx_no_init = NULL;
  34173. ExpectNotNull((store = X509_STORE_new()));
  34174. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34175. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  34176. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  34177. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  34178. /* should return NULL if ctx has not bee initialized */
  34179. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  34180. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  34181. wolfSSL_X509_STORE_CTX_free(ctx);
  34182. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  34183. X509_STORE_free(store);
  34184. res = EXPECT_RESULT();
  34185. #endif /* OPENSSL_EXTRA */
  34186. return res;
  34187. }
  34188. static int test_wolfSSL_CTX_set_client_CA_list(void)
  34189. {
  34190. int res = TEST_SKIPPED;
  34191. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34192. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  34193. EXPECT_DECLS;
  34194. WOLFSSL_CTX* ctx = NULL;
  34195. WOLFSSL* ssl = NULL;
  34196. X509_NAME* name = NULL;
  34197. STACK_OF(X509_NAME)* names = NULL;
  34198. STACK_OF(X509_NAME)* ca_list = NULL;
  34199. int names_len = 0;
  34200. int i;
  34201. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34202. /* Send two X501 names in cert request */
  34203. names = SSL_load_client_CA_file(cliCertFile);
  34204. ExpectNotNull(names);
  34205. ca_list = SSL_load_client_CA_file(caCertFile);
  34206. ExpectNotNull(ca_list);
  34207. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  34208. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  34209. if (EXPECT_FAIL()) {
  34210. wolfSSL_X509_NAME_free(name);
  34211. name = NULL;
  34212. }
  34213. SSL_CTX_set_client_CA_list(ctx, names);
  34214. /* This should only free the stack structure */
  34215. sk_X509_NAME_free(ca_list);
  34216. ca_list = NULL;
  34217. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  34218. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  34219. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  34220. for (i = 0; i < names_len; i++) {
  34221. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  34222. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  34223. }
  34224. /* Needed to be able to create ssl object */
  34225. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34226. SSL_FILETYPE_PEM));
  34227. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34228. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34229. /* load again as old names are responsibility of ctx to free*/
  34230. names = SSL_load_client_CA_file(cliCertFile);
  34231. ExpectNotNull(names);
  34232. SSL_set_client_CA_list(ssl, names);
  34233. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  34234. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  34235. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  34236. for (i = 0; i < names_len; i++) {
  34237. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  34238. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  34239. }
  34240. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  34241. {
  34242. tcp_ready ready;
  34243. func_args server_args;
  34244. callback_functions server_cb;
  34245. THREAD_TYPE serverThread;
  34246. WOLFSSL* ssl_client = NULL;
  34247. WOLFSSL_CTX* ctx_client = NULL;
  34248. SOCKET_T sockfd = 0;
  34249. /* wolfSSL_get_client_CA_list() with handshake */
  34250. StartTCP();
  34251. InitTcpReady(&ready);
  34252. XMEMSET(&server_args, 0, sizeof(func_args));
  34253. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  34254. server_args.signal = &ready;
  34255. server_args.callbacks = &server_cb;
  34256. /* we are responsible for free'ing WOLFSSL_CTX */
  34257. server_cb.ctx = ctx;
  34258. server_cb.isSharedCtx = 1;
  34259. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  34260. cliCertFile, 0));
  34261. start_thread(test_server_nofail, &server_args, &serverThread);
  34262. wait_tcp_ready(&server_args);
  34263. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34264. ExpectNotNull(ctx_client =
  34265. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  34266. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  34267. ctx_client, caCertFile, 0));
  34268. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  34269. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  34270. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  34271. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  34272. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  34273. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  34274. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  34275. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  34276. /* We are expecting two cert names to be sent */
  34277. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  34278. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  34279. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  34280. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  34281. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  34282. }
  34283. wolfSSL_shutdown(ssl_client);
  34284. wolfSSL_free(ssl_client);
  34285. wolfSSL_CTX_free(ctx_client);
  34286. CloseSocket(sockfd);
  34287. join_thread(serverThread);
  34288. FreeTcpReady(&ready);
  34289. }
  34290. #endif
  34291. wolfSSL_free(ssl);
  34292. wolfSSL_CTX_free(ctx);
  34293. res = EXPECT_RESULT();
  34294. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  34295. * !NO_BIO */
  34296. return res;
  34297. }
  34298. static int test_wolfSSL_CTX_add_client_CA(void)
  34299. {
  34300. int res = TEST_SKIPPED;
  34301. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34302. !defined(NO_WOLFSSL_CLIENT)
  34303. EXPECT_DECLS;
  34304. WOLFSSL_CTX* ctx = NULL;
  34305. WOLFSSL_X509* x509 = NULL;
  34306. WOLFSSL_X509* x509_a = NULL;
  34307. STACK_OF(X509_NAME)* ca_list = NULL;
  34308. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34309. /* Add client cert */
  34310. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  34311. SSL_FILETYPE_PEM));
  34312. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  34313. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  34314. /* Add another client cert */
  34315. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  34316. SSL_FILETYPE_PEM));
  34317. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  34318. /* test for incorrect parameter */
  34319. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  34320. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  34321. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  34322. X509_free(x509);
  34323. X509_free(x509_a);
  34324. SSL_CTX_free(ctx);
  34325. res = EXPECT_RESULT();
  34326. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  34327. return res;
  34328. }
  34329. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  34330. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  34331. {
  34332. callback_functions* callbacks = ((func_args*)args)->callbacks;
  34333. WOLFSSL_CTX* ctx = callbacks->ctx;
  34334. WOLFSSL* ssl = NULL;
  34335. SOCKET_T sfd = 0;
  34336. SOCKET_T cfd = 0;
  34337. word16 port;
  34338. char input[1024];
  34339. int idx;
  34340. int ret, err = 0;
  34341. const char* privateName = "ech-private-name.com";
  34342. int privateNameLen = (int)XSTRLEN(privateName);
  34343. ((func_args*)args)->return_code = TEST_FAIL;
  34344. port = ((func_args*)args)->signal->port;
  34345. AssertIntEQ(WOLFSSL_SUCCESS,
  34346. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  34347. AssertIntEQ(WOLFSSL_SUCCESS,
  34348. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34349. WOLFSSL_FILETYPE_PEM));
  34350. AssertIntEQ(WOLFSSL_SUCCESS,
  34351. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34352. WOLFSSL_FILETYPE_PEM));
  34353. if (callbacks->ctx_ready)
  34354. callbacks->ctx_ready(ctx);
  34355. ssl = wolfSSL_new(ctx);
  34356. /* set the sni for the server */
  34357. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  34358. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  34359. CloseSocket(sfd);
  34360. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  34361. if (callbacks->ssl_ready)
  34362. callbacks->ssl_ready(ssl);
  34363. do {
  34364. err = 0; /* Reset error */
  34365. ret = wolfSSL_accept(ssl);
  34366. if (ret != WOLFSSL_SUCCESS) {
  34367. err = wolfSSL_get_error(ssl, 0);
  34368. }
  34369. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  34370. if (ret != WOLFSSL_SUCCESS) {
  34371. char buff[WOLFSSL_MAX_ERROR_SZ];
  34372. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  34373. }
  34374. else {
  34375. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  34376. input[idx] = 0;
  34377. printf("Client message: %s\n", input);
  34378. }
  34379. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  34380. privateNameLen));
  34381. ((func_args*)args)->return_code = TEST_SUCCESS;
  34382. }
  34383. if (callbacks->on_result)
  34384. callbacks->on_result(ssl);
  34385. wolfSSL_shutdown(ssl);
  34386. wolfSSL_free(ssl);
  34387. wolfSSL_CTX_free(ctx);
  34388. CloseSocket(cfd);
  34389. #ifdef FP_ECC
  34390. wc_ecc_fp_free();
  34391. #endif
  34392. return 0;
  34393. }
  34394. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  34395. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  34396. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  34397. {
  34398. AssertNotNull(ssl);
  34399. AssertNotNull(line);
  34400. XFILE fp;
  34401. const byte lf = '\n';
  34402. fp = XFOPEN("./MyKeyLog.txt", "a");
  34403. XFWRITE( line, 1, strlen(line),fp);
  34404. XFWRITE( (void*)&lf,1,1,fp);
  34405. XFCLOSE(fp);
  34406. }
  34407. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  34408. static int test_wolfSSL_CTX_set_keylog_callback(void)
  34409. {
  34410. int res = TEST_SKIPPED;
  34411. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  34412. !defined(NO_WOLFSSL_CLIENT)
  34413. EXPECT_DECLS;
  34414. SSL_CTX* ctx = NULL;
  34415. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34416. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  34417. SSL_CTX_free(ctx);
  34418. SSL_CTX_set_keylog_callback(NULL, NULL);
  34419. res = EXPECT_RESULT();
  34420. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  34421. return res;
  34422. }
  34423. static int test_wolfSSL_CTX_get_keylog_callback(void)
  34424. {
  34425. int res = TEST_SKIPPED;
  34426. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  34427. !defined(NO_WOLFSSL_CLIENT)
  34428. EXPECT_DECLS;
  34429. SSL_CTX* ctx = NULL;
  34430. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34431. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  34432. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  34433. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  34434. SSL_CTX_set_keylog_callback(ctx, NULL );
  34435. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  34436. SSL_CTX_free(ctx);
  34437. res = EXPECT_RESULT();
  34438. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  34439. return res;
  34440. }
  34441. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  34442. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  34443. {
  34444. /* set keylog callback */
  34445. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  34446. return TEST_SUCCESS;
  34447. }
  34448. #endif
  34449. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  34450. {
  34451. int res = TEST_SKIPPED;
  34452. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  34453. /* This test is intended for checking whether keylog callback is called
  34454. * in client during TLS handshake between the client and a server.
  34455. */
  34456. EXPECT_DECLS;
  34457. test_ssl_cbf server_cbf;
  34458. test_ssl_cbf client_cbf;
  34459. XFILE fp = XBADFILE;
  34460. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  34461. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  34462. server_cbf.method = wolfTLSv1_2_server_method;
  34463. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  34464. /* clean up keylog file */
  34465. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  34466. if (fp != XBADFILE) {
  34467. XFCLOSE(fp);
  34468. fp = XBADFILE;
  34469. }
  34470. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34471. &server_cbf, NULL), TEST_SUCCESS);
  34472. /* check if the keylog file exists */
  34473. char buff[300] = {0};
  34474. int found = 0;
  34475. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  34476. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  34477. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  34478. found = 1;
  34479. break;
  34480. }
  34481. }
  34482. if (fp != XBADFILE) {
  34483. XFCLOSE(fp);
  34484. }
  34485. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  34486. ExpectIntEQ(found, 1);
  34487. res = EXPECT_RESULT();
  34488. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  34489. return res;
  34490. }
  34491. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  34492. defined(HAVE_SECRET_CALLBACK)
  34493. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  34494. {
  34495. /* set keylog callback */
  34496. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  34497. return TEST_SUCCESS;
  34498. }
  34499. #endif
  34500. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  34501. {
  34502. int res = TEST_SKIPPED;
  34503. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  34504. defined(HAVE_SECRET_CALLBACK)
  34505. /* This test is intended for checking whether keylog callback is called
  34506. * in client during TLS handshake between the client and a server.
  34507. */
  34508. EXPECT_DECLS;
  34509. test_ssl_cbf server_cbf;
  34510. test_ssl_cbf client_cbf;
  34511. XFILE fp = XBADFILE;
  34512. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  34513. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  34514. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  34515. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  34516. /* clean up keylog file */
  34517. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  34518. if (fp != XBADFILE) {
  34519. XFCLOSE(fp);
  34520. fp = XBADFILE;
  34521. }
  34522. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34523. &server_cbf, NULL), TEST_SUCCESS);
  34524. /* check if the keylog file exists */
  34525. {
  34526. char buff[300] = {0};
  34527. int found[4] = {0};
  34528. int numfnd = 0;
  34529. int i;
  34530. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  34531. while (EXPECT_SUCCESS() &&
  34532. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  34533. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  34534. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  34535. found[0] = 1;
  34536. continue;
  34537. }
  34538. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  34539. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  34540. found[1] = 1;
  34541. continue;
  34542. }
  34543. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  34544. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  34545. found[2] = 1;
  34546. continue;
  34547. }
  34548. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  34549. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  34550. found[3] = 1;
  34551. continue;
  34552. }
  34553. }
  34554. if (fp != XBADFILE)
  34555. XFCLOSE(fp);
  34556. for (i = 0; i < 4; i++) {
  34557. if (found[i] != 0)
  34558. numfnd++;
  34559. }
  34560. ExpectIntEQ(numfnd, 4);
  34561. }
  34562. res = EXPECT_RESULT();
  34563. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  34564. return res;
  34565. }
  34566. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  34567. static int test_wolfSSL_Tls13_ECH_params(void)
  34568. {
  34569. EXPECT_DECLS;
  34570. #if !defined(NO_WOLFSSL_CLIENT)
  34571. word32 outputLen = 0;
  34572. byte testBuf[72];
  34573. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  34574. WOLFSSL *ssl = wolfSSL_new(ctx);
  34575. ExpectNotNull(ctx);
  34576. ExpectNotNull(ssl);
  34577. /* invalid ctx */
  34578. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  34579. "ech-public-name.com", 0, 0, 0));
  34580. /* invalid public name */
  34581. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  34582. 0, 0));
  34583. /* invalid algorithms */
  34584. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  34585. "ech-public-name.com", 1000, 1000, 1000));
  34586. /* invalid ctx */
  34587. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  34588. &outputLen));
  34589. /* invalid output len */
  34590. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  34591. /* invalid ssl */
  34592. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  34593. (char*)testBuf, sizeof(testBuf)));
  34594. /* invalid configs64 */
  34595. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  34596. sizeof(testBuf)));
  34597. /* invalid size */
  34598. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  34599. (char*)testBuf, 0));
  34600. /* invalid ssl */
  34601. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  34602. sizeof(testBuf)));
  34603. /* invalid configs */
  34604. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  34605. sizeof(testBuf)));
  34606. /* invalid size */
  34607. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  34608. /* invalid ssl */
  34609. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  34610. /* invalid size */
  34611. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  34612. wolfSSL_free(ssl);
  34613. wolfSSL_CTX_free(ctx);
  34614. #endif /* !NO_WOLFSSL_CLIENT */
  34615. return EXPECT_RESULT();
  34616. }
  34617. static int test_wolfSSL_Tls13_ECH(void)
  34618. {
  34619. EXPECT_DECLS;
  34620. tcp_ready ready;
  34621. func_args client_args;
  34622. func_args server_args;
  34623. THREAD_TYPE serverThread;
  34624. callback_functions server_cbf;
  34625. callback_functions client_cbf;
  34626. SOCKET_T sockfd = 0;
  34627. WOLFSSL_CTX* ctx = NULL;
  34628. WOLFSSL* ssl = NULL;
  34629. const char* publicName = "ech-public-name.com";
  34630. const char* privateName = "ech-private-name.com";
  34631. int privateNameLen = 20;
  34632. char reply[1024];
  34633. int replyLen = 0;
  34634. byte rawEchConfig[128];
  34635. word32 rawEchConfigLen = sizeof(rawEchConfig);
  34636. InitTcpReady(&ready);
  34637. ready.port = 22222;
  34638. XMEMSET(&client_args, 0, sizeof(func_args));
  34639. XMEMSET(&server_args, 0, sizeof(func_args));
  34640. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  34641. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  34642. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  34643. /* create the server context here so we can get the ech config */
  34644. ExpectNotNull(server_cbf.ctx =
  34645. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  34646. /* generate ech config */
  34647. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  34648. publicName, 0, 0, 0));
  34649. /* get the config for the client to use */
  34650. ExpectIntEQ(WOLFSSL_SUCCESS,
  34651. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  34652. &rawEchConfigLen));
  34653. server_args.callbacks = &server_cbf;
  34654. server_args.signal = &ready;
  34655. /* start server task */
  34656. start_thread(server_task_ech, &server_args, &serverThread);
  34657. wait_tcp_ready(&server_args);
  34658. /* run as a TLS1.3 client */
  34659. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  34660. ExpectIntEQ(WOLFSSL_SUCCESS,
  34661. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34662. ExpectIntEQ(WOLFSSL_SUCCESS,
  34663. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34664. ExpectIntEQ(WOLFSSL_SUCCESS,
  34665. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34666. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34667. /* get connected the server task */
  34668. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34669. /* set the ech configs for the client */
  34670. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  34671. rawEchConfigLen));
  34672. /* set the sni for the client */
  34673. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  34674. privateName, privateNameLen));
  34675. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34676. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34677. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  34678. privateNameLen);
  34679. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  34680. /* add th null terminator for string compare */
  34681. reply[replyLen] = 0;
  34682. /* check that the server replied with the private name */
  34683. ExpectStrEQ(privateName, reply);
  34684. wolfSSL_free(ssl);
  34685. wolfSSL_CTX_free(ctx);
  34686. CloseSocket(sockfd);
  34687. join_thread(serverThread);
  34688. FreeTcpReady(&ready);
  34689. return EXPECT_RESULT();
  34690. }
  34691. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  34692. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34693. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34694. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34695. static int post_auth_version_cb(WOLFSSL* ssl)
  34696. {
  34697. EXPECT_DECLS;
  34698. /* do handshake and then test version error */
  34699. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34700. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34701. return EXPECT_RESULT();
  34702. }
  34703. static int post_auth_version_client_cb(WOLFSSL* ssl)
  34704. {
  34705. EXPECT_DECLS;
  34706. /* do handshake and then test version error */
  34707. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34708. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34709. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  34710. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  34711. /* check was added to error queue */
  34712. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  34713. /* check the string matches expected string */
  34714. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  34715. "WRONG_SSL_VERSION");
  34716. #endif
  34717. return EXPECT_RESULT();
  34718. }
  34719. static int post_auth_cb(WOLFSSL* ssl)
  34720. {
  34721. EXPECT_DECLS;
  34722. WOLFSSL_X509* x509 = NULL;
  34723. /* do handshake and then test version error */
  34724. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34725. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  34726. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  34727. wolfSSL_X509_free(x509);
  34728. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  34729. return EXPECT_RESULT();
  34730. }
  34731. static int set_post_auth_cb(WOLFSSL* ssl)
  34732. {
  34733. EXPECT_DECLS;
  34734. if (!wolfSSL_is_server(ssl)) {
  34735. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  34736. }
  34737. else {
  34738. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  34739. }
  34740. return EXPECT_RESULT();
  34741. }
  34742. #endif
  34743. static int test_wolfSSL_Tls13_postauth(void)
  34744. {
  34745. int res = TEST_SKIPPED;
  34746. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34747. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34748. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34749. EXPECT_DECLS;
  34750. test_ssl_cbf server_cbf;
  34751. test_ssl_cbf client_cbf;
  34752. /* test version failure doing post auth with TLS 1.2 connection */
  34753. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  34754. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  34755. server_cbf.method = wolfTLSv1_2_server_method;
  34756. server_cbf.ssl_ready = set_post_auth_cb;
  34757. server_cbf.on_result = post_auth_version_cb;
  34758. client_cbf.ssl_ready = set_post_auth_cb;
  34759. client_cbf.on_result = post_auth_version_client_cb;
  34760. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34761. &server_cbf, NULL), TEST_SUCCESS);
  34762. /* tests on post auth with TLS 1.3 */
  34763. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  34764. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  34765. server_cbf.method = wolfTLSv1_3_server_method;
  34766. server_cbf.ssl_ready = set_post_auth_cb;
  34767. client_cbf.ssl_ready = set_post_auth_cb;
  34768. server_cbf.on_result = post_auth_cb;
  34769. client_cbf.on_result = NULL;
  34770. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34771. &server_cbf, NULL), TEST_SUCCESS);
  34772. res = EXPECT_RESULT();
  34773. #endif
  34774. return res;
  34775. }
  34776. static int test_wolfSSL_X509_NID(void)
  34777. {
  34778. int res = TEST_SKIPPED;
  34779. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  34780. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  34781. EXPECT_DECLS;
  34782. int sigType;
  34783. int nameSz;
  34784. X509* cert = NULL;
  34785. EVP_PKEY* pubKeyTmp = NULL;
  34786. X509_NAME* name = NULL;
  34787. char commonName[80];
  34788. char countryName[80];
  34789. char localityName[80];
  34790. char stateName[80];
  34791. char orgName[80];
  34792. char orgUnit[80];
  34793. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  34794. /* convert cert from DER to internal WOLFSSL_X509 struct */
  34795. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  34796. sizeof_client_cert_der_2048));
  34797. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  34798. /* extract PUBLIC KEY from cert */
  34799. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  34800. /* extract signatureType */
  34801. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  34802. /* extract subjectName info */
  34803. ExpectNotNull(name = X509_get_subject_name(cert));
  34804. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  34805. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34806. NULL, 0)), 0);
  34807. ExpectIntEQ(nameSz, 15);
  34808. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34809. commonName, sizeof(commonName))), 0);
  34810. ExpectIntEQ(nameSz, 15);
  34811. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  34812. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34813. commonName, 9)), 0);
  34814. ExpectIntEQ(nameSz, 8);
  34815. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  34816. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  34817. countryName, sizeof(countryName))), 0);
  34818. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  34819. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  34820. localityName, sizeof(localityName))), 0);
  34821. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  34822. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34823. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  34824. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  34825. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  34826. orgName, sizeof(orgName))), 0);
  34827. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  34828. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34829. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34830. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34831. EVP_PKEY_free(pubKeyTmp);
  34832. X509_free(cert);
  34833. res = EXPECT_RESULT();
  34834. #endif
  34835. return res;
  34836. }
  34837. static int test_wolfSSL_CTX_set_srp_username(void)
  34838. {
  34839. int res = TEST_SKIPPED;
  34840. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34841. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34842. EXPECT_DECLS;
  34843. WOLFSSL_CTX* ctx = NULL;
  34844. WOLFSSL* ssl = NULL;
  34845. const char *username = "TESTUSER";
  34846. const char *password = "TESTPASSWORD";
  34847. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34848. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34849. SSL_SUCCESS);
  34850. wolfSSL_CTX_free(ctx);
  34851. ctx = NULL;
  34852. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34853. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34854. SSL_SUCCESS);
  34855. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34856. SSL_SUCCESS);
  34857. ExpectNotNull(ssl = SSL_new(ctx));
  34858. ExpectNotNull(SSL_get_srp_username(ssl));
  34859. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34860. wolfSSL_free(ssl);
  34861. wolfSSL_CTX_free(ctx);
  34862. res = EXPECT_RESULT();
  34863. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34864. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34865. return res;
  34866. }
  34867. static int test_wolfSSL_CTX_set_srp_password(void)
  34868. {
  34869. int res = TEST_SKIPPED;
  34870. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34871. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34872. EXPECT_DECLS;
  34873. WOLFSSL_CTX* ctx = NULL;
  34874. const char *username = "TESTUSER";
  34875. const char *password = "TESTPASSWORD";
  34876. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34877. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34878. SSL_SUCCESS);
  34879. wolfSSL_CTX_free(ctx);
  34880. ctx = NULL;
  34881. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34882. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34883. SSL_SUCCESS);
  34884. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34885. SSL_SUCCESS);
  34886. wolfSSL_CTX_free(ctx);
  34887. res = EXPECT_RESULT();
  34888. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34889. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34890. return res;
  34891. }
  34892. static int test_wolfSSL_X509_STORE(void)
  34893. {
  34894. int res = TEST_SKIPPED;
  34895. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34896. EXPECT_DECLS;
  34897. X509_STORE *store = NULL;
  34898. #ifdef HAVE_CRL
  34899. X509_STORE_CTX *storeCtx = NULL;
  34900. X509_CRL *crl = NULL;
  34901. X509 *ca = NULL;
  34902. X509 *cert = NULL;
  34903. const char crlPem[] = "./certs/crl/crl.revoked";
  34904. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34905. const char caCert[] = "./certs/ca-cert.pem";
  34906. XFILE fp = XBADFILE;
  34907. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34908. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34909. SSL_FILETYPE_PEM)));
  34910. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34911. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34912. SSL_FILETYPE_PEM)));
  34913. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34914. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34915. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34916. X509_STORE_free(store);
  34917. store = NULL;
  34918. X509_STORE_CTX_free(storeCtx);
  34919. storeCtx = NULL;
  34920. X509_free(cert);
  34921. cert = NULL;
  34922. X509_free(ca);
  34923. ca = NULL;
  34924. /* should fail to verify now after adding in CRL */
  34925. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34926. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34927. SSL_FILETYPE_PEM)));
  34928. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34929. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34930. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34931. NULL, NULL));
  34932. if (fp != XBADFILE)
  34933. XFCLOSE(fp);
  34934. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34935. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34936. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34937. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34938. SSL_FILETYPE_PEM)));
  34939. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34940. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34941. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  34942. X509_CRL_free(crl);
  34943. crl = NULL;
  34944. X509_STORE_free(store);
  34945. store = NULL;
  34946. X509_STORE_CTX_free(storeCtx);
  34947. storeCtx = NULL;
  34948. X509_free(cert);
  34949. cert = NULL;
  34950. X509_free(ca);
  34951. ca = NULL;
  34952. #endif /* HAVE_CRL */
  34953. #ifndef WOLFCRYPT_ONLY
  34954. {
  34955. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34956. SSL_CTX* ctx = NULL;
  34957. SSL* ssl = NULL;
  34958. int i;
  34959. for (i = 0; i < 2; i++) {
  34960. #ifndef NO_WOLFSSL_SERVER
  34961. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34962. #else
  34963. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34964. #endif
  34965. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34966. SSL_CTX_set_cert_store(ctx, store);
  34967. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34968. SSL_CTX_set_cert_store(ctx, store);
  34969. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34970. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34971. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34972. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34973. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34974. ExpectNotNull(ssl = SSL_new(ctx));
  34975. if (i == 0) {
  34976. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34977. SSL_SUCCESS);
  34978. }
  34979. else {
  34980. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store),
  34981. SSL_SUCCESS);
  34982. }
  34983. if (EXPECT_FAIL() || (i == 1)) {
  34984. X509_STORE_free(store);
  34985. store = NULL;
  34986. }
  34987. SSL_free(ssl);
  34988. ssl = NULL;
  34989. SSL_CTX_free(ctx);
  34990. ctx = NULL;
  34991. }
  34992. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34993. }
  34994. #endif
  34995. res = EXPECT_RESULT();
  34996. #endif
  34997. return res;
  34998. }
  34999. static int test_wolfSSL_X509_STORE_load_locations(void)
  35000. {
  35001. int res = TEST_SKIPPED;
  35002. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35003. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35004. EXPECT_DECLS;
  35005. SSL_CTX *ctx = NULL;
  35006. X509_STORE *store = NULL;
  35007. const char ca_file[] = "./certs/ca-cert.pem";
  35008. const char client_pem_file[] = "./certs/client-cert.pem";
  35009. const char client_der_file[] = "./certs/client-cert.der";
  35010. const char ecc_file[] = "./certs/ecc-key.pem";
  35011. const char certs_path[] = "./certs/";
  35012. const char bad_path[] = "./bad-path/";
  35013. #ifdef HAVE_CRL
  35014. const char crl_path[] = "./certs/crl/";
  35015. const char crl_file[] = "./certs/crl/crl.pem";
  35016. #endif
  35017. #ifndef NO_WOLFSSL_SERVER
  35018. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35019. #else
  35020. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35021. #endif
  35022. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35023. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  35024. WOLFSSL_SUCCESS);
  35025. /* Test bad arguments */
  35026. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  35027. WOLFSSL_FAILURE);
  35028. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  35029. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  35030. WOLFSSL_FAILURE);
  35031. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  35032. WOLFSSL_FAILURE);
  35033. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  35034. WOLFSSL_FAILURE);
  35035. #ifdef HAVE_CRL
  35036. /* Test with CRL */
  35037. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  35038. WOLFSSL_SUCCESS);
  35039. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  35040. WOLFSSL_SUCCESS);
  35041. #endif
  35042. /* Test with CA */
  35043. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  35044. WOLFSSL_SUCCESS);
  35045. /* Test with client_cert and certs path */
  35046. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  35047. WOLFSSL_SUCCESS);
  35048. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  35049. WOLFSSL_SUCCESS);
  35050. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35051. /* Clear nodes */
  35052. ERR_clear_error();
  35053. #endif
  35054. SSL_CTX_free(ctx);
  35055. res = EXPECT_RESULT();
  35056. #endif
  35057. return res;
  35058. }
  35059. static int test_X509_STORE_get0_objects(void)
  35060. {
  35061. int res = TEST_SKIPPED;
  35062. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  35063. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35064. EXPECT_DECLS;
  35065. X509_STORE *store = NULL;
  35066. X509_STORE *store_cpy = NULL;
  35067. SSL_CTX *ctx = NULL;
  35068. X509_OBJECT *obj = NULL;
  35069. STACK_OF(X509_OBJECT) *objs = NULL;
  35070. int i;
  35071. /* Setup store */
  35072. #ifndef NO_WOLFSSL_SERVER
  35073. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35074. #else
  35075. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35076. #endif
  35077. ExpectNotNull(store_cpy = X509_STORE_new());
  35078. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35079. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  35080. WOLFSSL_SUCCESS);
  35081. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  35082. WOLFSSL_SUCCESS);
  35083. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  35084. WOLFSSL_SUCCESS);
  35085. #ifdef HAVE_CRL
  35086. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  35087. WOLFSSL_SUCCESS);
  35088. #endif
  35089. /* Store ready */
  35090. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  35091. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  35092. #ifdef HAVE_CRL
  35093. #ifdef WOLFSSL_SIGNER_DER_CERT
  35094. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  35095. #else
  35096. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  35097. #endif
  35098. #else
  35099. #ifdef WOLFSSL_SIGNER_DER_CERT
  35100. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  35101. #else
  35102. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  35103. #endif
  35104. #endif
  35105. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  35106. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  35107. switch (X509_OBJECT_get_type(obj)) {
  35108. case X509_LU_X509:
  35109. {
  35110. WOLFSSL_X509* x509;
  35111. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  35112. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  35113. break;
  35114. }
  35115. case X509_LU_CRL:
  35116. #ifdef HAVE_CRL
  35117. {
  35118. WOLFSSL_CRL* crl = NULL;
  35119. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  35120. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  35121. break;
  35122. }
  35123. #endif
  35124. case X509_LU_NONE:
  35125. default:
  35126. Fail(("X509_OBJECT_get_type should return x509 or crl "
  35127. "(when built with crl support)"),
  35128. ("Unrecognized X509_OBJECT type or none"));
  35129. }
  35130. }
  35131. X509_STORE_free(store_cpy);
  35132. SSL_CTX_free(ctx);
  35133. res = EXPECT_RESULT();
  35134. #endif
  35135. return res;
  35136. }
  35137. static int test_wolfSSL_BN_CTX(void)
  35138. {
  35139. int res = TEST_SKIPPED;
  35140. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35141. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35142. EXPECT_DECLS;
  35143. WOLFSSL_BN_CTX* bn_ctx = NULL;
  35144. WOLFSSL_BIGNUM* t = NULL;
  35145. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  35146. /* No implementation. */
  35147. BN_CTX_init(NULL);
  35148. ExpectNotNull(t = BN_CTX_get(NULL));
  35149. BN_free(t);
  35150. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  35151. BN_free(t);
  35152. #ifndef NO_WOLFSSL_STUB
  35153. /* No implementation. */
  35154. BN_CTX_start(NULL);
  35155. BN_CTX_start(bn_ctx);
  35156. #endif
  35157. BN_CTX_free(NULL);
  35158. BN_CTX_free(bn_ctx);
  35159. res = EXPECT_RESULT();
  35160. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35161. return res;
  35162. }
  35163. static int test_wolfSSL_BN(void)
  35164. {
  35165. int res = TEST_SKIPPED;
  35166. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35167. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35168. EXPECT_DECLS;
  35169. BIGNUM* a = NULL;
  35170. BIGNUM* b = NULL;
  35171. BIGNUM* c = NULL;
  35172. BIGNUM* d = NULL;
  35173. BIGNUM emptyBN;
  35174. /* Setup */
  35175. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35176. /* internal not set emptyBN. */
  35177. ExpectNotNull(a = BN_new());
  35178. ExpectNotNull(b = BN_new());
  35179. ExpectNotNull(c = BN_dup(b));
  35180. ExpectNotNull(d = BN_new());
  35181. /* Invalid parameter testing. */
  35182. BN_free(NULL);
  35183. ExpectNull(BN_dup(NULL));
  35184. ExpectNull(BN_dup(&emptyBN));
  35185. ExpectNull(BN_copy(NULL, NULL));
  35186. ExpectNull(BN_copy(b, NULL));
  35187. ExpectNull(BN_copy(NULL, c));
  35188. ExpectNull(BN_copy(b, &emptyBN));
  35189. ExpectNull(BN_copy(&emptyBN, c));
  35190. BN_clear(NULL);
  35191. BN_clear(&emptyBN);
  35192. ExpectIntEQ(BN_num_bytes(NULL), 0);
  35193. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  35194. ExpectIntEQ(BN_num_bits(NULL), 0);
  35195. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  35196. ExpectIntEQ(BN_is_negative(NULL), 0);
  35197. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  35198. /* END Invalid Parameters */
  35199. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  35200. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  35201. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  35202. ExpectIntEQ(BN_num_bits(a), 2);
  35203. ExpectIntEQ(BN_num_bytes(a), 1);
  35204. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  35205. defined(WOLFSSL_SP_INT_NEGATIVE))
  35206. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  35207. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  35208. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  35209. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  35210. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  35211. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35212. {
  35213. /* Do additional tests on negative BN conversions. */
  35214. char* ret = NULL;
  35215. ASN1_INTEGER* asn1 = NULL;
  35216. BIGNUM* tmp = NULL;
  35217. /* Sanity check we have a negative BN. */
  35218. ExpectIntEQ(BN_is_negative(c), 1);
  35219. ExpectNotNull(ret = BN_bn2dec(c));
  35220. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35221. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35222. ret = NULL;
  35223. /* Convert to ASN1_INTEGER and back to BN. */
  35224. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  35225. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  35226. /* After converting back BN should be negative and correct. */
  35227. ExpectIntEQ(BN_is_negative(tmp), 1);
  35228. ExpectNotNull(ret = BN_bn2dec(tmp));
  35229. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35230. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35231. ASN1_INTEGER_free(asn1);
  35232. BN_free(tmp);
  35233. }
  35234. #endif
  35235. ExpectIntEQ(BN_get_word(c), 4);
  35236. #endif
  35237. ExpectIntEQ(BN_set_word(a, 3), 1);
  35238. ExpectIntEQ(BN_set_word(b, 3), 1);
  35239. ExpectIntEQ(BN_set_word(c, 4), 1);
  35240. /* NULL == NULL, NULL < num, num > NULL */
  35241. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  35242. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  35243. ExpectIntLT(BN_cmp(NULL, b), 0);
  35244. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  35245. ExpectIntGT(BN_cmp(a, NULL), 0);
  35246. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  35247. ExpectIntEQ(BN_cmp(a, b), 0);
  35248. ExpectIntLT(BN_cmp(a, c), 0);
  35249. ExpectIntGT(BN_cmp(c, b), 0);
  35250. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  35251. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  35252. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  35253. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  35254. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  35255. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  35256. BN_clear(a);
  35257. BN_free(a);
  35258. BN_free(b);
  35259. BN_free(c);
  35260. BN_clear_free(d);
  35261. res = EXPECT_RESULT();
  35262. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35263. return res;
  35264. }
  35265. static int test_wolfSSL_BN_init(void)
  35266. {
  35267. int res = TEST_SKIPPED;
  35268. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35269. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35270. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  35271. EXPECT_DECLS;
  35272. BIGNUM* ap = NULL;
  35273. BIGNUM bv;
  35274. BIGNUM cv;
  35275. BIGNUM dv;
  35276. ExpectNotNull(ap = BN_new());
  35277. BN_init(NULL);
  35278. XMEMSET(&bv, 0, sizeof(bv));
  35279. ExpectNull(BN_dup(&bv));
  35280. BN_init(&bv);
  35281. BN_init(&cv);
  35282. BN_init(&dv);
  35283. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  35284. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  35285. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  35286. /* a^b mod c = */
  35287. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  35288. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  35289. /* check result 3^2 mod 5 */
  35290. ExpectIntEQ(BN_get_word(&dv), 4);
  35291. /* a*b mod c = */
  35292. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  35293. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  35294. /* check result 3*2 mod 5 */
  35295. ExpectIntEQ(BN_get_word(&dv), 1);
  35296. BN_free(ap);
  35297. res = EXPECT_RESULT();
  35298. #endif
  35299. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35300. return res;
  35301. }
  35302. static int test_wolfSSL_BN_enc_dec(void)
  35303. {
  35304. int res = TEST_SKIPPED;
  35305. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  35306. EXPECT_DECLS;
  35307. BIGNUM* a = NULL;
  35308. BIGNUM* b = NULL;
  35309. BIGNUM* c = NULL;
  35310. BIGNUM emptyBN;
  35311. char* str = NULL;
  35312. const char* emptyStr = "";
  35313. const char* numberStr = "12345";
  35314. const char* badStr = "g12345";
  35315. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35316. const char* twoStr = "2";
  35317. #endif
  35318. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  35319. unsigned char outNum[5];
  35320. /* Setup */
  35321. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35322. ExpectNotNull(a = BN_new());
  35323. ExpectNotNull(b = BN_new());
  35324. ExpectIntEQ(BN_set_word(a, 2), 1);
  35325. /* Invalid parameters */
  35326. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  35327. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  35328. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  35329. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  35330. ExpectNull(BN_bn2hex(NULL));
  35331. ExpectNull(BN_bn2hex(&emptyBN));
  35332. ExpectNull(BN_bn2dec(NULL));
  35333. ExpectNull(BN_bn2dec(&emptyBN));
  35334. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  35335. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  35336. ExpectNull(BN_bin2bn(binNum, -1, a));
  35337. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  35338. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  35339. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  35340. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  35341. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  35342. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  35343. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  35344. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  35345. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  35346. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  35347. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  35348. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  35349. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  35350. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  35351. ExpectIntEQ(BN_set_word(a, 2), 1);
  35352. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  35353. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  35354. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  35355. ExpectIntEQ(BN_cmp(a, b), 0);
  35356. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  35357. ExpectIntEQ(BN_cmp(a, b), -1);
  35358. ExpectNotNull(str = BN_bn2hex(a));
  35359. ExpectNotNull(BN_hex2bn(&b, str));
  35360. ExpectIntEQ(BN_cmp(a, b), 0);
  35361. ExpectNotNull(BN_hex2bn(&b, numberStr));
  35362. ExpectIntEQ(BN_cmp(a, b), -1);
  35363. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  35364. str = NULL;
  35365. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35366. ExpectNotNull(str = BN_bn2dec(a));
  35367. ExpectStrEQ(str, twoStr);
  35368. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  35369. str = NULL;
  35370. #ifndef NO_RSA
  35371. ExpectNotNull(str = BN_bn2dec(a));
  35372. ExpectNotNull(BN_dec2bn(&b, str));
  35373. ExpectIntEQ(BN_cmp(a, b), 0);
  35374. ExpectNotNull(BN_dec2bn(&b, numberStr));
  35375. ExpectIntEQ(BN_cmp(a, b), -1);
  35376. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  35377. str = NULL;
  35378. #else
  35379. /* No implementation - fail with good parameters. */
  35380. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  35381. #endif
  35382. #endif
  35383. BN_free(b);
  35384. BN_free(a);
  35385. res = EXPECT_RESULT();
  35386. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35387. return res;
  35388. }
  35389. static int test_wolfSSL_BN_word(void)
  35390. {
  35391. int res = TEST_SKIPPED;
  35392. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  35393. EXPECT_DECLS;
  35394. BIGNUM* a = NULL;
  35395. BIGNUM* b = NULL;
  35396. BIGNUM* c = NULL;
  35397. BIGNUM av;
  35398. ExpectNotNull(a = BN_new());
  35399. ExpectNotNull(b = BN_new());
  35400. ExpectNotNull(c = BN_new());
  35401. XMEMSET(&av, 0, sizeof(av));
  35402. /* Invalid parameter. */
  35403. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  35404. ExpectIntEQ(BN_add_word(&av, 3), 0);
  35405. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  35406. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  35407. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  35408. ExpectIntEQ(BN_set_word(&av, 3), 0);
  35409. ExpectIntEQ(BN_get_word(NULL), 0);
  35410. ExpectIntEQ(BN_get_word(&av), 0);
  35411. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  35412. ExpectIntEQ(BN_is_word(&av, 3), 0);
  35413. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  35414. !defined(NO_DSA))
  35415. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  35416. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  35417. #endif
  35418. ExpectIntEQ(BN_one(NULL), 0);
  35419. ExpectIntEQ(BN_one(&av), 0);
  35420. BN_zero(NULL);
  35421. BN_zero(&av);
  35422. ExpectIntEQ(BN_is_one(NULL), 0);
  35423. ExpectIntEQ(BN_is_one(&av), 0);
  35424. ExpectIntEQ(BN_is_zero(NULL), 0);
  35425. ExpectIntEQ(BN_is_zero(&av), 0);
  35426. ExpectIntEQ(BN_set_word(a, 3), 1);
  35427. ExpectIntEQ(BN_set_word(b, 2), 1);
  35428. ExpectIntEQ(BN_set_word(c, 5), 1);
  35429. /* a + 3 = */
  35430. ExpectIntEQ(BN_add_word(a, 3), 1);
  35431. /* check result 3 + 3*/
  35432. ExpectIntEQ(BN_get_word(a), 6);
  35433. ExpectIntEQ(BN_is_word(a, 6), 1);
  35434. ExpectIntEQ(BN_is_word(a, 5), 0);
  35435. /* set a back to 3 */
  35436. ExpectIntEQ(BN_set_word(a, 3), 1);
  35437. /* a - 3 = */
  35438. ExpectIntEQ(BN_sub_word(a, 3), 1);
  35439. /* check result 3 - 3*/
  35440. ExpectIntEQ(BN_get_word(a), 0);
  35441. ExpectIntEQ(BN_one(a), 1);
  35442. ExpectIntEQ(BN_is_word(a, 1), 1);
  35443. ExpectIntEQ(BN_is_word(a, 0), 0);
  35444. ExpectIntEQ(BN_is_one(a), 1);
  35445. ExpectIntEQ(BN_is_zero(a), 0);
  35446. BN_zero(a);
  35447. ExpectIntEQ(BN_is_word(a, 0), 1);
  35448. ExpectIntEQ(BN_is_word(a, 1), 0);
  35449. ExpectIntEQ(BN_is_zero(a), 1);
  35450. ExpectIntEQ(BN_is_one(a), 0);
  35451. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  35452. !defined(NO_DSA))
  35453. ExpectIntEQ(BN_set_word(a, 5), 1);
  35454. ExpectIntEQ(BN_mod_word(a, 3), 2);
  35455. ExpectIntEQ(BN_mod_word(a, 0), -1);
  35456. #endif
  35457. BN_free(c);
  35458. BN_free(b);
  35459. BN_free(a);
  35460. res = EXPECT_RESULT();
  35461. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35462. return res;
  35463. }
  35464. static int test_wolfSSL_BN_bits(void)
  35465. {
  35466. int res = TEST_SKIPPED;
  35467. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35468. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35469. EXPECT_DECLS;
  35470. BIGNUM* a = NULL;
  35471. BIGNUM emptyBN;
  35472. /* Setup */
  35473. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35474. ExpectNotNull(a = BN_new());
  35475. /* Invalid parameters. */
  35476. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  35477. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  35478. ExpectIntEQ(BN_set_bit(a, -1), 0);
  35479. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  35480. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  35481. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  35482. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  35483. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  35484. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  35485. ExpectIntEQ(BN_is_odd(NULL), 0);
  35486. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  35487. ExpectIntEQ(BN_set_word(a, 0), 1);
  35488. ExpectIntEQ(BN_is_zero(a), 1);
  35489. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  35490. ExpectIntEQ(BN_is_zero(a), 0);
  35491. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  35492. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  35493. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  35494. ExpectIntEQ(BN_is_zero(a), 1);
  35495. ExpectIntEQ(BN_set_bit(a, 0), 1);
  35496. ExpectIntEQ(BN_is_odd(a), 1);
  35497. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35498. ExpectIntEQ(BN_is_odd(a), 0);
  35499. ExpectIntEQ(BN_set_bit(a, 1), 1);
  35500. ExpectIntEQ(BN_is_odd(a), 0);
  35501. ExpectIntEQ(BN_set_bit(a, 129), 1);
  35502. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  35503. #ifndef NO_WOLFSSL_STUB
  35504. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  35505. #endif
  35506. BN_free(a);
  35507. res = EXPECT_RESULT();
  35508. #endif
  35509. return res;
  35510. }
  35511. static int test_wolfSSL_BN_shift(void)
  35512. {
  35513. int res = TEST_SKIPPED;
  35514. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35515. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35516. EXPECT_DECLS;
  35517. BIGNUM* a = NULL;
  35518. BIGNUM* b = NULL;
  35519. BIGNUM emptyBN;
  35520. /* Setup */
  35521. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35522. ExpectNotNull(a = BN_new());
  35523. ExpectNotNull(b = BN_new());
  35524. /* Invalid parameters. */
  35525. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  35526. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  35527. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  35528. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  35529. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  35530. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  35531. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  35532. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  35533. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  35534. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  35535. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  35536. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  35537. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  35538. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  35539. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  35540. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  35541. ExpectIntEQ(BN_set_word(a, 1), 1);
  35542. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  35543. ExpectIntEQ(BN_is_word(b, 2), 1);
  35544. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  35545. ExpectIntEQ(BN_is_word(a, 2), 1);
  35546. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  35547. ExpectIntEQ(BN_is_word(b, 1), 1);
  35548. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  35549. ExpectIntEQ(BN_is_word(a, 1), 1);
  35550. BN_free(b);
  35551. BN_free(a);
  35552. res = EXPECT_RESULT();
  35553. #endif
  35554. return res;
  35555. }
  35556. static int test_wolfSSL_BN_math(void)
  35557. {
  35558. int res = TEST_SKIPPED;
  35559. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35560. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35561. EXPECT_DECLS;
  35562. BIGNUM* a = NULL;
  35563. BIGNUM* b = NULL;
  35564. BIGNUM* r = NULL;
  35565. BIGNUM* rem = NULL;
  35566. BIGNUM emptyBN;
  35567. BN_ULONG val1;
  35568. BN_ULONG val2;
  35569. /* Setup */
  35570. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35571. ExpectNotNull(a = BN_new());
  35572. ExpectNotNull(b = BN_new());
  35573. ExpectNotNull(r = BN_new());
  35574. ExpectNotNull(rem = BN_new());
  35575. /* Invalid parameters. */
  35576. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  35577. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  35578. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  35579. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  35580. ExpectIntEQ(BN_add(r, a, NULL), 0);
  35581. ExpectIntEQ(BN_add(r, NULL, b), 0);
  35582. ExpectIntEQ(BN_add(NULL, a, b), 0);
  35583. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  35584. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  35585. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  35586. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  35587. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  35588. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  35589. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  35590. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  35591. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  35592. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  35593. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  35594. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  35595. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  35596. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  35597. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  35598. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  35599. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  35600. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  35601. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  35602. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  35603. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  35604. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  35605. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  35606. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  35607. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  35608. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  35609. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  35610. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  35611. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35612. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  35613. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  35614. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  35615. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  35616. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  35617. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  35618. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  35619. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  35620. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  35621. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  35622. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  35623. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  35624. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  35625. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  35626. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  35627. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35628. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35629. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  35630. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  35631. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  35632. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  35633. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  35634. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  35635. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  35636. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  35637. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  35638. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  35639. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  35640. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  35641. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  35642. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  35643. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35644. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  35645. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  35646. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  35647. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  35648. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  35649. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  35650. /* END Invalid parameters. */
  35651. val1 = 8;
  35652. val2 = 3;
  35653. ExpectIntEQ(BN_set_word(a, val1), 1);
  35654. ExpectIntEQ(BN_set_word(b, val2), 1);
  35655. ExpectIntEQ(BN_add(r, a, b), 1);
  35656. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  35657. ExpectIntEQ(BN_sub(r, a, b), 1);
  35658. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  35659. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  35660. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  35661. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  35662. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  35663. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  35664. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  35665. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  35666. BN_free(rem);
  35667. BN_free(r);
  35668. BN_free(b);
  35669. BN_free(a);
  35670. res = EXPECT_RESULT();
  35671. #endif
  35672. return res;
  35673. }
  35674. static int test_wolfSSL_BN_math_mod(void)
  35675. {
  35676. int res = TEST_SKIPPED;
  35677. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35678. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35679. EXPECT_DECLS;
  35680. BIGNUM* a = NULL;
  35681. BIGNUM* b = NULL;
  35682. BIGNUM* m = NULL;
  35683. BIGNUM* r = NULL;
  35684. BIGNUM* t = NULL;
  35685. BIGNUM emptyBN;
  35686. BN_ULONG val1;
  35687. BN_ULONG val2;
  35688. BN_ULONG val3;
  35689. /* Setup */
  35690. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35691. ExpectNotNull(a = BN_new());
  35692. ExpectNotNull(b = BN_new());
  35693. ExpectNotNull(m = BN_new());
  35694. ExpectNotNull(r = BN_new());
  35695. /* Invalid parameters. */
  35696. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  35697. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  35698. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  35699. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  35700. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  35701. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  35702. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  35703. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  35704. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  35705. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35706. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35707. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35708. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35709. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35710. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  35711. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  35712. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  35713. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  35714. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  35715. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  35716. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  35717. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  35718. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  35719. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  35720. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  35721. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  35722. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  35723. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35724. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35725. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35726. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35727. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35728. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  35729. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  35730. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  35731. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  35732. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  35733. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  35734. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  35735. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  35736. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  35737. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  35738. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  35739. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  35740. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  35741. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35742. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35743. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35744. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35745. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35746. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  35747. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  35748. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  35749. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  35750. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  35751. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35752. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35753. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35754. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35755. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  35756. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  35757. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  35758. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  35759. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  35760. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  35761. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  35762. /* END Invalid parameters. */
  35763. val1 = 9;
  35764. val2 = 13;
  35765. val3 = 5;
  35766. ExpectIntEQ(BN_set_word(a, val1), 1);
  35767. ExpectIntEQ(BN_set_word(b, val2), 1);
  35768. ExpectIntEQ(BN_set_word(m, val3), 1);
  35769. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  35770. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  35771. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  35772. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  35773. ExpectIntEQ(BN_set_word(a, 2), 1);
  35774. ExpectIntEQ(BN_set_word(b, 3), 1);
  35775. ExpectIntEQ(BN_set_word(m, 5), 1);
  35776. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  35777. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  35778. ExpectIntEQ(BN_is_word(r, 3), 1);
  35779. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  35780. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  35781. ExpectIntEQ(BN_is_word(r, 3), 1);
  35782. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  35783. ExpectIntEQ(BN_is_word(t, 3), 1);
  35784. BN_free(t);
  35785. /* No inverse case. No inverse when a divides b. */
  35786. ExpectIntEQ(BN_set_word(a, 3), 1);
  35787. ExpectIntEQ(BN_set_word(m, 9), 1);
  35788. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  35789. BN_free(r);
  35790. BN_free(m);
  35791. BN_free(b);
  35792. BN_free(a);
  35793. res = EXPECT_RESULT();
  35794. #endif
  35795. return res;
  35796. }
  35797. static int test_wolfSSL_BN_math_other(void)
  35798. {
  35799. int res = TEST_SKIPPED;
  35800. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35801. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35802. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35803. EXPECT_DECLS;
  35804. BIGNUM* a = NULL;
  35805. BIGNUM* b = NULL;
  35806. BIGNUM* r = NULL;
  35807. BIGNUM emptyBN;
  35808. /* Setup */
  35809. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35810. ExpectNotNull(a = BN_new());
  35811. ExpectNotNull(b = BN_new());
  35812. ExpectNotNull(r = BN_new());
  35813. /* Invalid parameters. */
  35814. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  35815. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  35816. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  35817. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  35818. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  35819. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  35820. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  35821. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35822. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  35823. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  35824. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  35825. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  35826. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  35827. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  35828. /* END Invalid parameters. */
  35829. /* No comman factors between 2 and 3. */
  35830. ExpectIntEQ(BN_set_word(a, 2), 1);
  35831. ExpectIntEQ(BN_set_word(b, 3), 1);
  35832. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35833. ExpectIntEQ(BN_is_word(r, 1), 1);
  35834. /* 3 is largest value that divides both 6 and 9. */
  35835. ExpectIntEQ(BN_set_word(a, 6), 1);
  35836. ExpectIntEQ(BN_set_word(b, 9), 1);
  35837. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35838. ExpectIntEQ(BN_is_word(r, 3), 1);
  35839. /* GCD of 0 and 0 is undefined. */
  35840. ExpectIntEQ(BN_set_word(a, 0), 1);
  35841. ExpectIntEQ(BN_set_word(b, 0), 1);
  35842. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  35843. /* Teardown */
  35844. BN_free(r);
  35845. BN_free(b);
  35846. BN_free(a);
  35847. res = EXPECT_RESULT();
  35848. #endif
  35849. #endif
  35850. return res;
  35851. }
  35852. static int test_wolfSSL_BN_rand(void)
  35853. {
  35854. int res = TEST_SKIPPED;
  35855. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35856. EXPECT_DECLS;
  35857. BIGNUM* bn = NULL;
  35858. BIGNUM* range = NULL;
  35859. BIGNUM emptyBN;
  35860. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35861. ExpectNotNull(bn = BN_new());
  35862. ExpectNotNull(range = BN_new());
  35863. /* Invalid parameters. */
  35864. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35865. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35866. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35867. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35868. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35869. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35870. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35871. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35872. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35873. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35874. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35875. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35876. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35877. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35878. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35879. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35880. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35881. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35882. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35883. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35884. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35885. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35886. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35887. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35888. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35889. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35890. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35891. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35892. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35893. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35894. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35895. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35896. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35897. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35898. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35899. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35900. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35901. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35902. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35903. /* 1 bit random value must have no more than one top bit set. */
  35904. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35905. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35906. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35907. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35908. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35909. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35910. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35911. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35912. /* END Invalid parameters. */
  35913. /* 0 bit random: 0. */
  35914. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35915. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35916. ExpectIntEQ(BN_is_zero(bn), 1);
  35917. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35918. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35919. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35920. ExpectIntEQ(BN_is_zero(bn), 1);
  35921. /* 1 bit random: 0 or 1. */
  35922. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35923. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35924. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35925. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35926. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35927. ExpectIntEQ(BN_get_word(bn), 1);
  35928. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35929. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35930. ExpectIntEQ(BN_get_word(bn), 1);
  35931. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35932. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35933. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35934. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35935. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35936. ExpectIntEQ(BN_get_word(bn), 1);
  35937. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35938. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35939. ExpectIntEQ(BN_get_word(bn), 1);
  35940. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35941. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35942. ExpectIntEQ(BN_num_bits(bn), 8);
  35943. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35944. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35945. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35946. ExpectIntEQ(BN_num_bits(bn), 8);
  35947. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35948. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35949. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35950. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35951. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35952. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35953. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35954. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35955. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35956. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35957. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35958. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35959. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35960. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35961. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35962. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35963. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35964. * case, requesting a 13-bit random number would actually return a 16-bit
  35965. * random number. */
  35966. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35967. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35968. ExpectIntEQ(BN_num_bits(bn), 13);
  35969. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35970. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35971. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35972. ExpectIntEQ(BN_set_word(range, 0), 1);
  35973. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35974. ExpectIntEQ(BN_set_word(range, 1), 1);
  35975. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35976. BN_free(bn);
  35977. BN_free(range);
  35978. res = EXPECT_RESULT();
  35979. #endif
  35980. return res;
  35981. }
  35982. static int test_wolfSSL_BN_prime(void)
  35983. {
  35984. int res = TEST_SKIPPED;
  35985. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35986. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35987. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35988. EXPECT_DECLS;
  35989. BIGNUM* a = NULL;
  35990. BIGNUM* add = NULL;
  35991. BIGNUM* rem = NULL;
  35992. BIGNUM emptyBN;
  35993. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35994. ExpectNotNull(a = BN_new());
  35995. ExpectNotNull(add = BN_new());
  35996. ExpectNotNull(rem = BN_new());
  35997. /* Invalid parameters. */
  35998. /* BN_generate_prime_ex()
  35999. * prime - must have valid BIGNUM
  36000. * bits - Greater then 0
  36001. * safe - not supported, must be 0
  36002. * add - not supported, must be NULL
  36003. * rem - not supported, must be NULL
  36004. * cb - anything
  36005. */
  36006. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  36007. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  36008. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  36009. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  36010. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  36011. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  36012. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  36013. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  36014. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  36015. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  36016. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  36017. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  36018. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  36019. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  36020. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  36021. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  36022. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  36023. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  36024. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  36025. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  36026. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  36027. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  36028. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  36029. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  36030. /* END Invalid parameters. */
  36031. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  36032. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  36033. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  36034. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  36035. BN_free(rem);
  36036. BN_free(add);
  36037. BN_free(a);
  36038. res = EXPECT_RESULT();
  36039. #endif
  36040. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36041. return res;
  36042. }
  36043. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36044. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36045. #define TEST_ARG 0x1234
  36046. static void msg_cb(int write_p, int version, int content_type,
  36047. const void *buf, size_t len, SSL *ssl, void *arg)
  36048. {
  36049. (void)write_p;
  36050. (void)version;
  36051. (void)content_type;
  36052. (void)buf;
  36053. (void)len;
  36054. (void)ssl;
  36055. AssertTrue(arg == (void*)TEST_ARG);
  36056. }
  36057. #endif
  36058. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36059. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36060. #if defined(SESSION_CERTS)
  36061. #include "wolfssl/internal.h"
  36062. #endif
  36063. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  36064. {
  36065. EXPECT_DECLS;
  36066. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36067. STACK_OF(X509)* sk = NULL;
  36068. X509* x509 = NULL;
  36069. int i, num;
  36070. BIO* bio = NULL;
  36071. #endif
  36072. (void) ctx;
  36073. fprintf(stderr, "\n===== msgcb called ====\n");
  36074. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  36075. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  36076. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  36077. ExpectNotNull(SSL_get0_verified_chain(ssl));
  36078. #else
  36079. (void) ssl;
  36080. #endif
  36081. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36082. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  36083. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  36084. if (sk == NULL) {
  36085. BIO_free(bio);
  36086. return TEST_FAIL;
  36087. }
  36088. num = sk_X509_num(sk);
  36089. ExpectTrue(num > 0);
  36090. for (i = 0; i < num; i++) {
  36091. ExpectNotNull(x509 = sk_X509_value(sk,i));
  36092. if (x509 == NULL)
  36093. break;
  36094. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  36095. X509_print(bio,x509);
  36096. fprintf(stderr, "\n\n");
  36097. }
  36098. BIO_free(bio);
  36099. #endif
  36100. return EXPECT_RESULT();
  36101. }
  36102. #endif
  36103. static int test_wolfSSL_msgCb(void)
  36104. {
  36105. int res = TEST_SKIPPED;
  36106. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36107. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36108. EXPECT_DECLS;
  36109. test_ssl_cbf client_cb;
  36110. test_ssl_cbf server_cb;
  36111. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  36112. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  36113. #ifndef WOLFSSL_NO_TLS12
  36114. client_cb.method = wolfTLSv1_2_client_method;
  36115. server_cb.method = wolfTLSv1_2_server_method;
  36116. #else
  36117. client_cb.method = wolfTLSv1_3_client_method;
  36118. server_cb.method = wolfTLSv1_3_server_method;
  36119. #endif
  36120. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36121. &server_cb, msgCb), TEST_SUCCESS);
  36122. res = EXPECT_RESULT();
  36123. #endif
  36124. return res;
  36125. }
  36126. static int test_wolfSSL_either_side(void)
  36127. {
  36128. int res = TEST_SKIPPED;
  36129. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36130. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36131. EXPECT_DECLS;
  36132. test_ssl_cbf client_cb;
  36133. test_ssl_cbf server_cb;
  36134. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36135. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36136. /* Use different CTX for client and server */
  36137. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36138. ExpectNotNull(client_cb.ctx);
  36139. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36140. ExpectNotNull(server_cb.ctx);
  36141. /* we are responsible for free'ing WOLFSSL_CTX */
  36142. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36143. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36144. &server_cb, NULL), TEST_SUCCESS);
  36145. wolfSSL_CTX_free(client_cb.ctx);
  36146. wolfSSL_CTX_free(server_cb.ctx);
  36147. res = EXPECT_RESULT();
  36148. #endif
  36149. return res;
  36150. }
  36151. static int test_wolfSSL_DTLS_either_side(void)
  36152. {
  36153. int res = TEST_SKIPPED;
  36154. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36155. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  36156. EXPECT_DECLS;
  36157. test_ssl_cbf client_cb;
  36158. test_ssl_cbf server_cb;
  36159. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36160. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36161. /* Use different CTX for client and server */
  36162. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36163. ExpectNotNull(client_cb.ctx);
  36164. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36165. ExpectNotNull(server_cb.ctx);
  36166. /* we are responsible for free'ing WOLFSSL_CTX */
  36167. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36168. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36169. &server_cb, NULL), TEST_SUCCESS);
  36170. wolfSSL_CTX_free(client_cb.ctx);
  36171. wolfSSL_CTX_free(server_cb.ctx);
  36172. res = EXPECT_RESULT();
  36173. #endif
  36174. return res;
  36175. }
  36176. static int test_generate_cookie(void)
  36177. {
  36178. int res = TEST_SKIPPED;
  36179. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  36180. EXPECT_DECLS;
  36181. SSL_CTX* ctx = NULL;
  36182. SSL* ssl = NULL;
  36183. byte buf[FOURK_BUF] = {0};
  36184. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  36185. ExpectNotNull(ssl = SSL_new(ctx));
  36186. /* Test unconnected */
  36187. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  36188. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  36189. wolfSSL_SetCookieCtx(ssl, ctx);
  36190. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  36191. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  36192. SSL_free(ssl);
  36193. SSL_CTX_free(ctx);
  36194. res = EXPECT_RESULT();
  36195. #endif
  36196. return res;
  36197. }
  36198. static int test_wolfSSL_set_options(void)
  36199. {
  36200. int res = TEST_SKIPPED;
  36201. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36202. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36203. EXPECT_DECLS;
  36204. WOLFSSL* ssl = NULL;
  36205. WOLFSSL_CTX* ctx = NULL;
  36206. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36207. char appData[] = "extra msg";
  36208. #endif
  36209. #ifdef OPENSSL_EXTRA
  36210. unsigned char protos[] = {
  36211. 7, 't', 'l', 's', '/', '1', '.', '2',
  36212. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  36213. };
  36214. unsigned int len = sizeof(protos);
  36215. void *arg = (void *)TEST_ARG;
  36216. #endif
  36217. #ifndef NO_WOLFSSL_SERVER
  36218. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36219. #else
  36220. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36221. #endif
  36222. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36223. WOLFSSL_FILETYPE_PEM));
  36224. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36225. WOLFSSL_FILETYPE_PEM));
  36226. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  36227. == WOLFSSL_OP_NO_TLSv1);
  36228. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  36229. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  36230. WOLFSSL_OP_NO_SSLv2)), 0);
  36231. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  36232. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  36233. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  36234. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  36235. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36236. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  36237. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36238. WOLFSSL_OP_NO_COMPRESSION));
  36239. wolfSSL_CTX_free(ctx);
  36240. ctx = NULL;
  36241. #ifndef NO_WOLFSSL_SERVER
  36242. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36243. #else
  36244. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36245. #endif
  36246. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36247. WOLFSSL_FILETYPE_PEM));
  36248. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36249. WOLFSSL_FILETYPE_PEM));
  36250. #ifdef OPENSSL_EXTRA
  36251. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  36252. #endif
  36253. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36254. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36255. #ifdef HAVE_EX_DATA
  36256. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  36257. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  36258. if (ssl != NULL) {
  36259. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  36260. appData, sizeof(appData)), 0);
  36261. }
  36262. #else
  36263. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  36264. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  36265. #endif
  36266. #endif
  36267. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  36268. WOLFSSL_OP_NO_TLSv1);
  36269. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  36270. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  36271. WOLFSSL_OP_NO_SSLv2)), 0);
  36272. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  36273. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  36274. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  36275. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  36276. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  36277. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  36278. #ifdef OPENSSL_EXTRA
  36279. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  36280. WOLFSSL_OP_NO_COMPRESSION));
  36281. #endif
  36282. #ifdef OPENSSL_EXTRA
  36283. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  36284. wolfSSL_set_msg_callback_arg(ssl, arg);
  36285. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  36286. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  36287. #else
  36288. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  36289. #endif
  36290. #endif
  36291. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  36292. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  36293. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  36294. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  36295. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  36296. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  36297. #else
  36298. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  36299. #endif
  36300. #endif /* HAVE_ALPN && !NO_BIO */
  36301. #endif
  36302. wolfSSL_free(ssl);
  36303. wolfSSL_CTX_free(ctx);
  36304. res = EXPECT_RESULT();
  36305. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  36306. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  36307. return res;
  36308. }
  36309. static int test_wolfSSL_sk_SSL_CIPHER(void)
  36310. {
  36311. int res = TEST_SKIPPED;
  36312. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  36313. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36314. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36315. EXPECT_DECLS;
  36316. SSL* ssl = NULL;
  36317. SSL_CTX* ctx = NULL;
  36318. STACK_OF(SSL_CIPHER) *sk = NULL;
  36319. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  36320. #ifndef NO_WOLFSSL_SERVER
  36321. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36322. #else
  36323. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36324. #endif
  36325. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  36326. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  36327. ExpectNotNull(ssl = SSL_new(ctx));
  36328. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  36329. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  36330. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  36331. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  36332. /* error case because connection has not been established yet */
  36333. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  36334. sk_SSL_CIPHER_free(dupSk);
  36335. /* sk is pointer to internal struct that should be free'd in SSL_free */
  36336. SSL_free(ssl);
  36337. SSL_CTX_free(ctx);
  36338. res = EXPECT_RESULT();
  36339. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  36340. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36341. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  36342. return res;
  36343. }
  36344. static int test_wolfSSL_set1_curves_list(void)
  36345. {
  36346. int res = TEST_SKIPPED;
  36347. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  36348. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36349. EXPECT_DECLS;
  36350. SSL* ssl = NULL;
  36351. SSL_CTX* ctx = NULL;
  36352. #ifndef NO_WOLFSSL_SERVER
  36353. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36354. #else
  36355. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36356. #endif
  36357. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  36358. SSL_FILETYPE_PEM));
  36359. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  36360. ExpectNotNull(ssl = SSL_new(ctx));
  36361. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  36362. #ifdef HAVE_ECC
  36363. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  36364. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  36365. #endif
  36366. #ifdef HAVE_CURVE25519
  36367. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  36368. #else
  36369. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  36370. #endif
  36371. #ifdef HAVE_CURVE448
  36372. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  36373. #else
  36374. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  36375. #endif
  36376. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  36377. #ifdef HAVE_ECC
  36378. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  36379. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  36380. #endif
  36381. #ifdef HAVE_CURVE25519
  36382. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  36383. #else
  36384. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  36385. #endif
  36386. #ifdef HAVE_CURVE448
  36387. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  36388. #else
  36389. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  36390. #endif
  36391. SSL_free(ssl);
  36392. SSL_CTX_free(ctx);
  36393. res = EXPECT_RESULT();
  36394. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  36395. #endif
  36396. return res;
  36397. }
  36398. static int test_wolfSSL_set1_sigalgs_list(void)
  36399. {
  36400. int res = TEST_SKIPPED;
  36401. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36402. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36403. EXPECT_DECLS;
  36404. SSL* ssl = NULL;
  36405. SSL_CTX* ctx = NULL;
  36406. #ifndef NO_WOLFSSL_SERVER
  36407. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36408. #else
  36409. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36410. #endif
  36411. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  36412. SSL_FILETYPE_PEM));
  36413. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  36414. ExpectNotNull(ssl = SSL_new(ctx));
  36415. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  36416. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  36417. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  36418. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  36419. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  36420. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  36421. #ifndef NO_RSA
  36422. #ifndef NO_SHA256
  36423. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  36424. WOLFSSL_FAILURE);
  36425. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  36426. WOLFSSL_FAILURE);
  36427. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  36428. WOLFSSL_SUCCESS);
  36429. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  36430. WOLFSSL_SUCCESS);
  36431. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  36432. WOLFSSL_FAILURE);
  36433. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  36434. WOLFSSL_FAILURE);
  36435. #ifdef WC_RSA_PSS
  36436. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  36437. WOLFSSL_SUCCESS);
  36438. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  36439. WOLFSSL_SUCCESS);
  36440. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  36441. WOLFSSL_SUCCESS);
  36442. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  36443. WOLFSSL_SUCCESS);
  36444. #endif
  36445. #ifdef WOLFSSL_SHA512
  36446. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  36447. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  36448. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  36449. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  36450. #elif defined(WOLFSSL_SHA384)
  36451. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  36452. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  36453. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  36454. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  36455. #endif
  36456. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  36457. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  36458. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  36459. WOLFSSL_FAILURE);
  36460. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  36461. WOLFSSL_FAILURE);
  36462. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  36463. WOLFSSL_FAILURE);
  36464. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  36465. WOLFSSL_FAILURE);
  36466. #endif
  36467. #endif
  36468. #ifdef HAVE_ECC
  36469. #ifndef NO_SHA256
  36470. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  36471. WOLFSSL_SUCCESS);
  36472. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  36473. WOLFSSL_SUCCESS);
  36474. #ifdef WOLFSSL_SHA512
  36475. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  36476. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  36477. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  36478. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  36479. #elif defined(WOLFSSL_SHA384)
  36480. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  36481. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  36482. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  36483. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  36484. #endif
  36485. #endif
  36486. #endif
  36487. #ifdef HAVE_ED25519
  36488. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  36489. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  36490. #endif
  36491. #ifdef HAVE_ED448
  36492. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  36493. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  36494. #endif
  36495. #ifndef NO_DSA
  36496. #ifndef NO_SHA256
  36497. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  36498. WOLFSSL_SUCCESS);
  36499. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  36500. WOLFSSL_SUCCESS);
  36501. #endif
  36502. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  36503. defined(WOLFSSL_ALLOW_TLS_SHA1))
  36504. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  36505. WOLFSSL_SUCCESS);
  36506. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  36507. WOLFSSL_SUCCESS);
  36508. #endif
  36509. #endif
  36510. SSL_free(ssl);
  36511. SSL_CTX_free(ctx);
  36512. res = EXPECT_RESULT();
  36513. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  36514. #endif
  36515. return res;
  36516. }
  36517. /* Testing wolfSSL_set_tlsext_status_type function.
  36518. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  36519. */
  36520. static int test_wolfSSL_set_tlsext_status_type(void)
  36521. {
  36522. int res = TEST_SKIPPED;
  36523. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  36524. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  36525. EXPECT_DECLS;
  36526. SSL* ssl = NULL;
  36527. SSL_CTX* ctx = NULL;
  36528. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36529. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  36530. SSL_FILETYPE_PEM));
  36531. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  36532. ExpectNotNull(ssl = SSL_new(ctx));
  36533. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  36534. SSL_SUCCESS);
  36535. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  36536. SSL_free(ssl);
  36537. SSL_CTX_free(ctx);
  36538. res = EXPECT_RESULT();
  36539. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  36540. return res;
  36541. }
  36542. #ifndef NO_BIO
  36543. static int test_wolfSSL_PEM_read_bio(void)
  36544. {
  36545. int res = TEST_SKIPPED;
  36546. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36547. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36548. EXPECT_DECLS;
  36549. byte buff[6000];
  36550. XFILE f = XBADFILE;
  36551. int bytes;
  36552. X509* x509 = NULL;
  36553. BIO* bio = NULL;
  36554. BUF_MEM* buf = NULL;
  36555. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  36556. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  36557. if (f != XBADFILE)
  36558. XFCLOSE(f);
  36559. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  36560. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  36561. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  36562. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  36563. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  36564. /* BIO should return the set EOF value */
  36565. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  36566. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  36567. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  36568. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  36569. BIO_free(bio);
  36570. BUF_MEM_free(buf);
  36571. X509_free(x509);
  36572. res = EXPECT_RESULT();
  36573. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  36574. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  36575. return res;
  36576. }
  36577. #if defined(OPENSSL_EXTRA)
  36578. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  36579. long argl, long ret)
  36580. {
  36581. (void)bio;
  36582. (void)cmd;
  36583. (void)argp;
  36584. (void)argi;
  36585. (void)argl;
  36586. return ret;
  36587. }
  36588. #endif
  36589. static int test_wolfSSL_BIO(void)
  36590. {
  36591. int res = TEST_SKIPPED;
  36592. #if defined(OPENSSL_EXTRA)
  36593. EXPECT_DECLS;
  36594. const unsigned char* p = NULL;
  36595. byte buff[20];
  36596. BIO* bio1 = NULL;
  36597. BIO* bio2 = NULL;
  36598. BIO* bio3 = NULL;
  36599. char* bufPt = NULL;
  36600. int i;
  36601. for (i = 0; i < 20; i++) {
  36602. buff[i] = i;
  36603. }
  36604. /* test BIO_free with NULL */
  36605. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  36606. /* Creating and testing type BIO_s_bio */
  36607. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  36608. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  36609. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  36610. /* read/write before set up */
  36611. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  36612. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  36613. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  36614. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  36615. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  36616. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  36617. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  36618. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  36619. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  36620. /* write buffer full */
  36621. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  36622. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  36623. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  36624. /* write the other direction with pair */
  36625. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  36626. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  36627. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  36628. /* try read */
  36629. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  36630. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  36631. /* try read using ctrl function */
  36632. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  36633. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  36634. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  36635. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  36636. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  36637. for (i = 0; i < 20; i++) {
  36638. ExpectIntEQ((int)bufPt[i], i);
  36639. }
  36640. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  36641. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  36642. for (i = 0; i < 8; i++) {
  36643. ExpectIntEQ((int)bufPt[i], i);
  36644. }
  36645. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  36646. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  36647. /* new pair */
  36648. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  36649. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  36650. bio2 = NULL;
  36651. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  36652. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36653. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  36654. /* test wrap around... */
  36655. ExpectIntEQ(BIO_reset(bio1), 0);
  36656. ExpectIntEQ(BIO_reset(bio3), 0);
  36657. /* fill write buffer, read only small amount then write again */
  36658. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36659. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36660. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  36661. for (i = 0; i < 4; i++) {
  36662. ExpectIntEQ(bufPt[i], i);
  36663. }
  36664. /* try writing over read index */
  36665. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  36666. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  36667. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  36668. /* read and write 0 bytes */
  36669. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36670. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  36671. /* should read only to end of write buffer then need to read again */
  36672. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  36673. for (i = 0; i < 16; i++) {
  36674. ExpectIntEQ(bufPt[i], buff[4 + i]);
  36675. }
  36676. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  36677. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  36678. for (i = 0; i < 4; i++) {
  36679. ExpectIntEQ(bufPt[i], 0);
  36680. }
  36681. /* read index should not have advanced with nread0 */
  36682. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  36683. for (i = 0; i < 4; i++) {
  36684. ExpectIntEQ(bufPt[i], 0);
  36685. }
  36686. /* write and fill up buffer checking reset of index state */
  36687. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36688. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36689. /* test reset on data in bio1 write buffer */
  36690. ExpectIntEQ(BIO_reset(bio1), 0);
  36691. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36692. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  36693. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36694. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  36695. ExpectNotNull(p);
  36696. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36697. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  36698. for (i = 0; i < 6; i++) {
  36699. ExpectIntEQ(bufPt[i], i);
  36700. }
  36701. /* test case of writing twice with offset read index */
  36702. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  36703. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  36704. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36705. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36706. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36707. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  36708. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  36709. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36710. BIO_free(bio1);
  36711. bio1 = NULL;
  36712. BIO_free(bio3);
  36713. bio3 = NULL;
  36714. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  36715. {
  36716. BIO* bioA = NULL;
  36717. BIO* bioB = NULL;
  36718. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  36719. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  36720. BIO_free(bioA);
  36721. bioA = NULL;
  36722. BIO_free(bioB);
  36723. bioB = NULL;
  36724. }
  36725. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36726. /* BIOs with file pointers */
  36727. #if !defined(NO_FILESYSTEM)
  36728. {
  36729. XFILE f1 = XBADFILE;
  36730. XFILE f2 = XBADFILE;
  36731. BIO* f_bio1 = NULL;
  36732. BIO* f_bio2 = NULL;
  36733. unsigned char cert[300];
  36734. char testFile[] = "tests/bio_write_test.txt";
  36735. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  36736. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  36737. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  36738. /* Failure due to wrong BIO type */
  36739. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36740. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  36741. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  36742. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  36743. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  36744. WOLFSSL_SUCCESS);
  36745. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36746. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  36747. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  36748. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  36749. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  36750. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  36751. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  36752. ExpectIntEQ(BIO_reset(f_bio2), 0);
  36753. ExpectIntEQ(BIO_tell(NULL),-1);
  36754. ExpectIntEQ(BIO_tell(f_bio2),0);
  36755. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  36756. ExpectIntEQ(BIO_tell(f_bio2),4);
  36757. BIO_free(f_bio1);
  36758. f_bio1 = NULL;
  36759. BIO_free(f_bio2);
  36760. f_bio2 = NULL;
  36761. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  36762. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36763. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36764. BIO_free(f_bio1);
  36765. f_bio1 = NULL;
  36766. }
  36767. #endif /* !defined(NO_FILESYSTEM) */
  36768. /* BIO info callback */
  36769. {
  36770. const char* testArg = "test";
  36771. BIO* cb_bio = NULL;
  36772. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  36773. BIO_set_callback(cb_bio, bioCallback);
  36774. ExpectNotNull(BIO_get_callback(cb_bio));
  36775. BIO_set_callback(cb_bio, NULL);
  36776. ExpectNull(BIO_get_callback(cb_bio));
  36777. BIO_set_callback_arg(cb_bio, (char*)testArg);
  36778. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  36779. ExpectNull(BIO_get_callback_arg(NULL));
  36780. BIO_free(cb_bio);
  36781. cb_bio = NULL;
  36782. }
  36783. /* BIO_vfree */
  36784. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  36785. BIO_vfree(NULL);
  36786. BIO_vfree(bio1);
  36787. res = EXPECT_RESULT();
  36788. #endif
  36789. return res;
  36790. }
  36791. #endif /* !NO_BIO */
  36792. static int test_wolfSSL_a2i_IPADDRESS(void)
  36793. {
  36794. int res = TEST_SKIPPED;
  36795. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  36796. EXPECT_DECLS;
  36797. const unsigned char* data = NULL;
  36798. int dataSz = 0;
  36799. ASN1_OCTET_STRING *st = NULL;
  36800. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  36801. const unsigned char ipv6_exp[] = {
  36802. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36803. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  36804. };
  36805. const unsigned char ipv6_home[] = {
  36806. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36807. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  36808. };
  36809. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  36810. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  36811. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36812. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  36813. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  36814. ASN1_STRING_free(st);
  36815. st = NULL;
  36816. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  36817. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36818. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36819. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36820. ASN1_STRING_free(st);
  36821. st = NULL;
  36822. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36823. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36824. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36825. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36826. ASN1_STRING_free(st);
  36827. res = EXPECT_RESULT();
  36828. #endif
  36829. return res;
  36830. }
  36831. static int test_wolfSSL_DES_ecb_encrypt(void)
  36832. {
  36833. int res = TEST_SKIPPED;
  36834. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  36835. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  36836. WOLFSSL_DES_key_schedule key;
  36837. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  36838. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  36839. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  36840. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  36841. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  36842. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  36843. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  36844. /* Encrypt messages */
  36845. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  36846. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  36847. {
  36848. /* Decrypt messages */
  36849. int ret1 = 0;
  36850. int ret2 = 0;
  36851. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  36852. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  36853. AssertIntEQ(ret1,0);
  36854. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  36855. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  36856. AssertIntEQ(ret2,0);
  36857. }
  36858. res = TEST_RES_CHECK(1);
  36859. #endif
  36860. return res;
  36861. }
  36862. static int test_wolfSSL_X509_cmp_time(void)
  36863. {
  36864. int res = TEST_SKIPPED;
  36865. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36866. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36867. EXPECT_DECLS;
  36868. WOLFSSL_ASN1_TIME asn_time;
  36869. time_t t;
  36870. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36871. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36872. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36873. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36874. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36875. res = EXPECT_RESULT();
  36876. #endif
  36877. return res;
  36878. }
  36879. static int test_wolfSSL_X509_time_adj(void)
  36880. {
  36881. int res = TEST_SKIPPED;
  36882. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36883. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36884. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36885. !defined(NO_ASN_TIME)
  36886. EXPECT_DECLS;
  36887. X509* x509 = NULL;
  36888. time_t t;
  36889. time_t not_before;
  36890. time_t not_after;
  36891. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36892. client_cert_der_2048, sizeof_client_cert_der_2048,
  36893. WOLFSSL_FILETYPE_ASN1));
  36894. t = 0;
  36895. not_before = wc_Time(0);
  36896. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36897. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36898. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36899. /* Check X509_gmtime_adj, too. */
  36900. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36901. X509_free(x509);
  36902. res = EXPECT_RESULT();
  36903. #endif
  36904. return res;
  36905. }
  36906. static int test_wolfSSL_X509(void)
  36907. {
  36908. int res = TEST_SKIPPED;
  36909. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36910. !defined(NO_RSA)
  36911. EXPECT_DECLS;
  36912. X509* x509 = NULL;
  36913. #ifndef NO_BIO
  36914. BIO* bio = NULL;
  36915. X509_STORE_CTX* ctx = NULL;
  36916. X509_STORE* store = NULL;
  36917. #endif
  36918. char der[] = "certs/ca-cert.der";
  36919. XFILE fp = XBADFILE;
  36920. ExpectNotNull(x509 = X509_new());
  36921. X509_free(x509);
  36922. x509 = NULL;
  36923. #ifndef NO_BIO
  36924. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36925. SSL_FILETYPE_PEM));
  36926. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36927. #ifdef WOLFSSL_CERT_GEN
  36928. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36929. #endif
  36930. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36931. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36932. ExpectNotNull(store = X509_STORE_new());
  36933. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36934. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36935. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36936. X509_STORE_CTX_free(ctx);
  36937. X509_STORE_free(store);
  36938. X509_free(x509);
  36939. x509 = NULL;
  36940. BIO_free(bio);
  36941. #endif
  36942. /** d2i_X509_fp test **/
  36943. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36944. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36945. ExpectNotNull(x509);
  36946. X509_free(x509);
  36947. x509 = NULL;
  36948. if (fp != XBADFILE) {
  36949. XFCLOSE(fp);
  36950. fp = XBADFILE;
  36951. }
  36952. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36953. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36954. ExpectNotNull(x509);
  36955. X509_free(x509);
  36956. if (fp != XBADFILE)
  36957. XFCLOSE(fp);
  36958. /* X509_up_ref test */
  36959. ExpectIntEQ(X509_up_ref(NULL), 0);
  36960. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36961. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36962. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36963. X509_free(x509); /* refCount = 2 */
  36964. X509_free(x509); /* refCount = 1 */
  36965. X509_free(x509); /* refCount = 0, free */
  36966. res = EXPECT_RESULT();
  36967. #endif
  36968. return res;
  36969. }
  36970. static int test_wolfSSL_X509_get_ext_count(void)
  36971. {
  36972. int res = TEST_SKIPPED;
  36973. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36974. !defined(NO_RSA)
  36975. int ret = 0;
  36976. WOLFSSL_X509* x509;
  36977. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36978. FILE* f;
  36979. /* NULL parameter check */
  36980. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36981. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36982. SSL_FILETYPE_PEM));
  36983. AssertIntEQ(X509_get_ext_count(x509), 5);
  36984. wolfSSL_X509_free(x509);
  36985. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36986. SSL_FILETYPE_PEM));
  36987. AssertIntEQ(X509_get_ext_count(x509), 5);
  36988. wolfSSL_X509_free(x509);
  36989. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  36990. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36991. fclose(f);
  36992. /* wolfSSL_X509_get_ext_count() valid input */
  36993. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36994. /* wolfSSL_X509_get_ext_count() NULL argument */
  36995. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36996. wolfSSL_X509_free(x509);
  36997. res = TEST_RES_CHECK(1);
  36998. #endif
  36999. return res;
  37000. }
  37001. static int test_wolfSSL_X509_sign2(void)
  37002. {
  37003. int res = TEST_SKIPPED;
  37004. /* test requires WOLFSSL_AKID_NAME to match expected output */
  37005. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  37006. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  37007. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  37008. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  37009. defined(WOLFSSL_IP_ALT_NAME))
  37010. EXPECT_DECLS;
  37011. WOLFSSL_X509 *x509 = NULL;
  37012. WOLFSSL_X509 *ca = NULL;
  37013. const unsigned char *der = NULL;
  37014. const unsigned char *pt = NULL;
  37015. WOLFSSL_EVP_PKEY *priv = NULL;
  37016. WOLFSSL_X509_NAME *name = NULL;
  37017. int derSz;
  37018. #ifndef NO_ASN_TIME
  37019. WOLFSSL_ASN1_TIME *notBefore = NULL;
  37020. WOLFSSL_ASN1_TIME *notAfter = NULL;
  37021. const int year = 365*24*60*60;
  37022. const int day = 24*60*60;
  37023. const int hour = 60*60;
  37024. const int mini = 60;
  37025. time_t t;
  37026. #endif
  37027. const unsigned char expected[] = {
  37028. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  37029. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  37030. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  37031. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  37032. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  37033. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  37034. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  37035. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  37036. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  37037. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  37038. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  37039. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  37040. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  37041. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  37042. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  37043. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  37044. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  37045. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  37046. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  37047. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  37048. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  37049. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  37050. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  37051. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  37052. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  37053. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  37054. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  37055. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  37056. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  37057. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  37058. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  37059. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  37060. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  37061. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  37062. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  37063. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  37064. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  37065. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  37066. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  37067. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  37068. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  37069. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  37070. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  37071. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  37072. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  37073. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  37074. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  37075. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  37076. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  37077. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  37078. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  37079. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  37080. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  37081. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  37082. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  37083. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  37084. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  37085. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  37086. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  37087. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  37088. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  37089. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  37090. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  37091. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  37092. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  37093. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  37094. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  37095. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  37096. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  37097. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  37098. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  37099. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  37100. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  37101. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  37102. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  37103. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  37104. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  37105. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  37106. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  37107. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  37108. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  37109. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  37110. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  37111. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  37112. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  37113. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  37114. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  37115. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  37116. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  37117. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  37118. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  37119. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  37120. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  37121. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  37122. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  37123. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  37124. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  37125. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  37126. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  37127. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  37128. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  37129. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  37130. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  37131. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  37132. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  37133. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  37134. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  37135. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  37136. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  37137. };
  37138. pt = ca_key_der_2048;
  37139. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  37140. sizeof_ca_key_der_2048));
  37141. pt = client_cert_der_2048;
  37142. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  37143. sizeof_client_cert_der_2048));
  37144. pt = ca_cert_der_2048;
  37145. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  37146. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  37147. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  37148. #ifndef NO_ASN_TIME
  37149. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  37150. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  37151. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  37152. ExpectIntEQ(notAfter->length, 13);
  37153. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  37154. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  37155. #endif
  37156. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  37157. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  37158. ExpectIntEQ(derSz, sizeof(expected));
  37159. #ifndef NO_ASN_TIME
  37160. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  37161. #endif
  37162. wolfSSL_X509_free(ca);
  37163. wolfSSL_X509_free(x509);
  37164. wolfSSL_EVP_PKEY_free(priv);
  37165. #ifndef NO_ASN_TIME
  37166. wolfSSL_ASN1_TIME_free(notBefore);
  37167. wolfSSL_ASN1_TIME_free(notAfter);
  37168. #endif
  37169. res = EXPECT_RESULT();
  37170. #endif
  37171. return res;
  37172. }
  37173. static int test_wolfSSL_X509_sign(void)
  37174. {
  37175. int res = TEST_SKIPPED;
  37176. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  37177. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  37178. EXPECT_DECLS;
  37179. int ret;
  37180. char *cn = NULL;
  37181. word32 cnSz;
  37182. X509_NAME *name = NULL;
  37183. X509 *x509 = NULL;
  37184. X509 *ca = NULL;
  37185. DecodedCert dCert;
  37186. EVP_PKEY *pub = NULL;
  37187. EVP_PKEY *priv = NULL;
  37188. EVP_MD_CTX *mctx = NULL;
  37189. #if defined(USE_CERT_BUFFERS_1024)
  37190. const unsigned char* rsaPriv = client_key_der_1024;
  37191. const unsigned char* rsaPub = client_keypub_der_1024;
  37192. const unsigned char* certIssuer = client_cert_der_1024;
  37193. long clientKeySz = (long)sizeof_client_key_der_1024;
  37194. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  37195. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  37196. #elif defined(USE_CERT_BUFFERS_2048)
  37197. const unsigned char* rsaPriv = client_key_der_2048;
  37198. const unsigned char* rsaPub = client_keypub_der_2048;
  37199. const unsigned char* certIssuer = client_cert_der_2048;
  37200. long clientKeySz = (long)sizeof_client_key_der_2048;
  37201. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  37202. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  37203. #endif
  37204. byte sn[16];
  37205. int snSz = sizeof(sn);
  37206. /* Set X509_NAME fields */
  37207. ExpectNotNull(name = X509_NAME_new());
  37208. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  37209. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  37210. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  37211. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  37212. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  37213. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  37214. /* Get private and public keys */
  37215. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  37216. clientKeySz));
  37217. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  37218. ExpectNotNull(x509 = X509_new());
  37219. /* Set version 3 */
  37220. ExpectIntNE(X509_set_version(x509, 2L), 0);
  37221. /* Set subject name, add pubkey, and sign certificate */
  37222. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  37223. X509_NAME_free(name);
  37224. name = NULL;
  37225. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  37226. #ifdef WOLFSSL_ALT_NAMES
  37227. /* Add some subject alt names */
  37228. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  37229. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  37230. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37231. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  37232. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37233. "sphygmomanometer",
  37234. ASN_DNS_TYPE), SSL_SUCCESS);
  37235. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37236. "supercalifragilisticexpialidocious",
  37237. ASN_DNS_TYPE), SSL_SUCCESS);
  37238. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37239. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  37240. ASN_DNS_TYPE), SSL_SUCCESS);
  37241. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37242. {
  37243. unsigned char ip4_type[] = {127,128,0,255};
  37244. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  37245. 0xff, 0xee, 0x99, 0x88,
  37246. 0x77, 0x66, 0x55, 0x44,
  37247. 0x00, 0x33, 0x22, 0x11};
  37248. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  37249. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  37250. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  37251. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  37252. }
  37253. #endif
  37254. #endif /* WOLFSSL_ALT_NAMES */
  37255. /* test valid sign case */
  37256. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  37257. /* test valid X509_sign_ctx case */
  37258. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37259. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  37260. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  37261. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  37262. ExpectIntEQ(X509_get_ext_count(x509), 1);
  37263. #endif
  37264. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  37265. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  37266. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  37267. #endif
  37268. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  37269. WOLFSSL_SUCCESS);
  37270. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  37271. /* Variation in size depends on ASN.1 encoding when MSB is set.
  37272. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  37273. * with the MSB set. See GenerateInteger in asn.c */
  37274. #ifndef USE_CERT_BUFFERS_1024
  37275. #ifndef WOLFSSL_ALT_NAMES
  37276. /* Valid case - size should be 781-786 with 16 byte serial number */
  37277. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  37278. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37279. /* Valid case - size should be 955-960 with 16 byte serial number */
  37280. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  37281. #else
  37282. /* Valid case - size should be 926-931 with 16 byte serial number */
  37283. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  37284. #endif
  37285. #else
  37286. #ifndef WOLFSSL_ALT_NAMES
  37287. /* Valid case - size should be 537-542 with 16 byte serial number */
  37288. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  37289. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37290. /* Valid case - size should be 695-670 with 16 byte serial number */
  37291. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  37292. #else
  37293. /* Valid case - size should be 666-671 with 16 byte serial number */
  37294. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  37295. #endif
  37296. #endif
  37297. /* check that issuer name is as expected after signature */
  37298. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  37299. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  37300. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  37301. ExpectNotNull(name = X509_get_subject_name(ca));
  37302. cnSz = X509_NAME_get_sz(name);
  37303. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37304. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  37305. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  37306. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37307. cn = NULL;
  37308. #ifdef WOLFSSL_MULTI_ATTRIB
  37309. /* test adding multiple OU's to the signer */
  37310. ExpectNotNull(name = X509_get_subject_name(ca));
  37311. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37312. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  37313. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37314. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  37315. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  37316. #endif
  37317. ExpectNotNull(name = X509_get_subject_name(ca));
  37318. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  37319. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37320. ExpectNotNull(name = X509_get_issuer_name(x509));
  37321. cnSz = X509_NAME_get_sz(name);
  37322. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37323. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  37324. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  37325. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  37326. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37327. cn = NULL;
  37328. FreeDecodedCert(&dCert);
  37329. /* Test invalid parameters */
  37330. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  37331. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  37332. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  37333. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  37334. EVP_MD_CTX_free(mctx);
  37335. mctx = NULL;
  37336. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37337. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  37338. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  37339. /* test invalid version number */
  37340. #if defined(OPENSSL_ALL)
  37341. ExpectIntNE(X509_set_version(x509, 6L), 0);
  37342. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37343. /* uses ParseCert which fails on bad version number */
  37344. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  37345. #endif
  37346. EVP_MD_CTX_free(mctx);
  37347. EVP_PKEY_free(priv);
  37348. EVP_PKEY_free(pub);
  37349. X509_free(x509);
  37350. X509_free(ca);
  37351. res = EXPECT_RESULT();
  37352. #endif
  37353. return res;
  37354. }
  37355. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  37356. {
  37357. int res = TEST_SKIPPED;
  37358. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37359. EXPECT_DECLS;
  37360. X509* x509 = NULL;
  37361. const X509_ALGOR* alg;
  37362. ExpectNotNull(x509 = X509_new());
  37363. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  37364. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37365. X509_free(x509);
  37366. res = EXPECT_RESULT();
  37367. #endif
  37368. return res;
  37369. }
  37370. static int test_wolfSSL_X509_ALGOR_get0(void)
  37371. {
  37372. int res = TEST_SKIPPED;
  37373. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37374. !defined(NO_SHA256) && !defined(NO_RSA)
  37375. EXPECT_DECLS;
  37376. X509* x509 = NULL;
  37377. const ASN1_OBJECT* obj = NULL;
  37378. const X509_ALGOR* alg = NULL;
  37379. int pptype = 0;
  37380. const void *ppval = NULL;
  37381. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  37382. SSL_FILETYPE_PEM));
  37383. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37384. /* Invalid case */
  37385. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  37386. ExpectNull(obj);
  37387. /* Valid case */
  37388. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  37389. ExpectNotNull(obj);
  37390. ExpectNull(ppval);
  37391. ExpectIntNE(pptype, 0);
  37392. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  37393. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  37394. X509_free(x509);
  37395. res = EXPECT_RESULT();
  37396. #endif
  37397. return res;
  37398. }
  37399. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  37400. {
  37401. int res = TEST_SKIPPED;
  37402. #if defined(OPENSSL_EXTRA)
  37403. EXPECT_DECLS;
  37404. X509_VERIFY_PARAM *paramTo = NULL;
  37405. X509_VERIFY_PARAM *paramFrom = NULL;
  37406. char testIPv4[] = "127.0.0.1";
  37407. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  37408. char testhostName1[] = "foo.hoge.com";
  37409. char testhostName2[] = "foobar.hoge.com";
  37410. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  37411. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  37412. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  37413. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  37414. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  37415. (int)XSTRLEN(testhostName1)), 1);
  37416. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  37417. (int)XSTRLEN(testhostName1)));
  37418. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  37419. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  37420. ExpectIntEQ(0x01, paramFrom->hostFlags);
  37421. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  37422. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  37423. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37424. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  37425. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  37426. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37427. /* null pointer */
  37428. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  37429. /* in the case of "from" null, returns success */
  37430. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  37431. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  37432. /* inherit flags test : VPARAM_DEFAULT */
  37433. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37434. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37435. (int)XSTRLEN(testhostName1)));
  37436. ExpectIntEQ(0x01, paramTo->hostFlags);
  37437. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37438. /* inherit flags test : VPARAM OVERWRITE */
  37439. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37440. (int)XSTRLEN(testhostName2)), 1);
  37441. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37442. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37443. if (paramTo != NULL) {
  37444. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  37445. }
  37446. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37447. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37448. (int)XSTRLEN(testhostName1)));
  37449. ExpectIntEQ(0x01, paramTo->hostFlags);
  37450. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37451. /* inherit flags test : VPARAM_RESET_FLAGS */
  37452. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37453. (int)XSTRLEN(testhostName2)), 1);
  37454. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37455. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  37456. if (paramTo != NULL) {
  37457. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  37458. }
  37459. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37460. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37461. (int)XSTRLEN(testhostName1)));
  37462. ExpectIntEQ(0x01, paramTo->hostFlags);
  37463. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37464. /* inherit flags test : VPARAM_LOCKED */
  37465. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37466. (int)XSTRLEN(testhostName2)), 1);
  37467. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37468. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37469. if (paramTo != NULL) {
  37470. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  37471. }
  37472. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37473. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  37474. (int)XSTRLEN(testhostName2)));
  37475. ExpectIntEQ(0x00, paramTo->hostFlags);
  37476. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37477. /* test for incorrect parameters */
  37478. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  37479. 0);
  37480. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  37481. /* inherit flags test : VPARAM_ONCE, not testable yet */
  37482. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  37483. 1);
  37484. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  37485. X509_V_FLAG_CRL_CHECK_ALL);
  37486. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  37487. X509_V_FLAG_CRL_CHECK_ALL), 1);
  37488. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  37489. X509_VERIFY_PARAM_free(paramTo);
  37490. X509_VERIFY_PARAM_free(paramFrom);
  37491. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  37492. res = EXPECT_RESULT();
  37493. #endif
  37494. return res;
  37495. }
  37496. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  37497. static int test_wolfSSL_check_domain_verify_count = 0;
  37498. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  37499. WOLFSSL_X509_STORE_CTX* store)
  37500. {
  37501. EXPECT_DECLS;
  37502. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  37503. ExpectIntEQ(preverify, 1);
  37504. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  37505. return EXPECT_RESULT() == TEST_SUCCESS;
  37506. }
  37507. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  37508. {
  37509. EXPECT_DECLS;
  37510. X509_VERIFY_PARAM *param = NULL;
  37511. ExpectNotNull(param = SSL_get0_param(ssl));
  37512. /* Domain check should only be done on the leaf cert */
  37513. X509_VERIFY_PARAM_set_hostflags(param,
  37514. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  37515. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  37516. "wolfSSL Server Chain", 0), 1);
  37517. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  37518. test_wolfSSL_check_domain_verify_cb);
  37519. return EXPECT_RESULT();
  37520. }
  37521. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  37522. {
  37523. EXPECT_DECLS;
  37524. /* Use a cert with different domains in chain */
  37525. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  37526. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  37527. return EXPECT_RESULT();
  37528. }
  37529. static int test_wolfSSL_check_domain(void)
  37530. {
  37531. EXPECT_DECLS;
  37532. test_ssl_cbf func_cb_client;
  37533. test_ssl_cbf func_cb_server;
  37534. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37535. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37536. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  37537. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  37538. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37539. &func_cb_server, NULL), TEST_SUCCESS);
  37540. /* Should have been called once for each cert in sent chain */
  37541. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  37542. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  37543. #else
  37544. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  37545. #endif
  37546. return EXPECT_RESULT();
  37547. }
  37548. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  37549. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  37550. {
  37551. int res = TEST_SKIPPED;
  37552. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37553. EXPECT_DECLS;
  37554. X509* x509 = NULL;
  37555. X509_PUBKEY* pubKey;
  37556. ExpectNotNull(x509 = X509_new());
  37557. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  37558. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  37559. X509_free(x509);
  37560. res = EXPECT_RESULT();
  37561. #endif
  37562. return res;
  37563. }
  37564. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  37565. {
  37566. int res = TEST_SKIPPED;
  37567. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37568. !defined(NO_SHA256) && !defined(NO_RSA)
  37569. EXPECT_DECLS;
  37570. X509* x509 = NULL;
  37571. ASN1_OBJECT* obj = NULL;
  37572. const ASN1_OBJECT* pa_oid = NULL;
  37573. X509_PUBKEY* pubKey = NULL;
  37574. X509_PUBKEY* pubKey2 = NULL;
  37575. EVP_PKEY* evpKey = NULL;
  37576. const unsigned char *pk = NULL;
  37577. int ppklen;
  37578. int pptype;
  37579. X509_ALGOR *pa = NULL;
  37580. const void *pval;
  37581. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  37582. SSL_FILETYPE_PEM));
  37583. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37584. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37585. ExpectNotNull(pk);
  37586. ExpectNotNull(pa);
  37587. ExpectNotNull(pubKey);
  37588. ExpectIntGT(ppklen, 0);
  37589. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  37590. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37591. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37592. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37593. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37594. ExpectNotNull(pk);
  37595. ExpectNotNull(pa);
  37596. ExpectIntGT(ppklen, 0);
  37597. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37598. ExpectNotNull(pa_oid);
  37599. ExpectNull(pval);
  37600. ExpectIntEQ(pptype, V_ASN1_NULL);
  37601. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  37602. X509_PUBKEY_free(pubKey2);
  37603. X509_free(x509);
  37604. EVP_PKEY_free(evpKey);
  37605. res = EXPECT_RESULT();
  37606. #endif
  37607. return res;
  37608. }
  37609. static int test_wolfSSL_X509_PUBKEY_EC(void)
  37610. {
  37611. int res = TEST_SKIPPED;
  37612. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  37613. EXPECT_DECLS;
  37614. X509* x509 = NULL;
  37615. ASN1_OBJECT* obj = NULL;
  37616. ASN1_OBJECT* poid = NULL;
  37617. const ASN1_OBJECT* pa_oid = NULL;
  37618. X509_PUBKEY* pubKey = NULL;
  37619. X509_PUBKEY* pubKey2 = NULL;
  37620. EVP_PKEY* evpKey = NULL;
  37621. const unsigned char *pk = NULL;
  37622. int ppklen;
  37623. int pptype;
  37624. X509_ALGOR *pa = NULL;
  37625. const void *pval;
  37626. char buf[50];
  37627. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  37628. SSL_FILETYPE_PEM));
  37629. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37630. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37631. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37632. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37633. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37634. ExpectNotNull(pk);
  37635. ExpectNotNull(pa);
  37636. ExpectIntGT(ppklen, 0);
  37637. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37638. ExpectNotNull(pa_oid);
  37639. ExpectNotNull(pval);
  37640. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  37641. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  37642. poid = (ASN1_OBJECT *)pval;
  37643. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  37644. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  37645. X509_PUBKEY_free(pubKey2);
  37646. X509_free(x509);
  37647. EVP_PKEY_free(evpKey);
  37648. res = EXPECT_RESULT();
  37649. #endif
  37650. return res;
  37651. }
  37652. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  37653. {
  37654. int res = TEST_SKIPPED;
  37655. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  37656. EXPECT_DECLS;
  37657. word32 bytes;
  37658. #ifdef USE_CERT_BUFFERS_1024
  37659. byte tmp[ONEK_BUF];
  37660. #elif defined(USE_CERT_BUFFERS_2048)
  37661. byte tmp[TWOK_BUF];
  37662. #else
  37663. byte tmp[TWOK_BUF];
  37664. #endif /* END USE_CERT_BUFFERS_1024 */
  37665. const unsigned char* dsaKeyDer = tmp;
  37666. ASN1_OBJECT* obj = NULL;
  37667. ASN1_STRING* str;
  37668. const ASN1_OBJECT* pa_oid = NULL;
  37669. X509_PUBKEY* pubKey = NULL;
  37670. EVP_PKEY* evpKey = NULL;
  37671. const unsigned char *pk = NULL;
  37672. int ppklen, pptype;
  37673. X509_ALGOR *pa = NULL;
  37674. const void *pval;
  37675. #ifdef USE_CERT_BUFFERS_1024
  37676. XMEMSET(tmp, 0, sizeof(tmp));
  37677. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  37678. bytes = sizeof_dsa_key_der_1024;
  37679. #elif defined(USE_CERT_BUFFERS_2048)
  37680. XMEMSET(tmp, 0, sizeof(tmp));
  37681. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  37682. bytes = sizeof_dsa_key_der_2048;
  37683. #else
  37684. {
  37685. XFILE fp = XBADFILE;
  37686. XMEMSET(tmp, 0, sizeof(tmp));
  37687. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  37688. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  37689. if (fp != XBADFILE)
  37690. XFCLOSE(fp);
  37691. }
  37692. #endif
  37693. /* Initialize pkey with der format dsa key */
  37694. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  37695. ExpectNotNull(pubKey = X509_PUBKEY_new());
  37696. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  37697. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37698. ExpectNotNull(pk);
  37699. ExpectNotNull(pa);
  37700. ExpectIntGT(ppklen, 0);
  37701. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37702. ExpectNotNull(pa_oid);
  37703. ExpectNotNull(pval);
  37704. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  37705. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  37706. str = (ASN1_STRING *)pval;
  37707. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  37708. #ifdef USE_CERT_BUFFERS_1024
  37709. ExpectIntEQ(ASN1_STRING_length(str), 291);
  37710. #else
  37711. ExpectIntEQ(ASN1_STRING_length(str), 549);
  37712. #endif /* END USE_CERT_BUFFERS_1024 */
  37713. X509_PUBKEY_free(pubKey);
  37714. EVP_PKEY_free(evpKey);
  37715. res = EXPECT_RESULT();
  37716. #endif
  37717. return res;
  37718. }
  37719. static int test_wolfSSL_BUF(void)
  37720. {
  37721. int res = TEST_SKIPPED;
  37722. #if defined(OPENSSL_EXTRA)
  37723. EXPECT_DECLS;
  37724. BUF_MEM* buf = NULL;
  37725. ExpectNotNull(buf = BUF_MEM_new());
  37726. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  37727. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  37728. BUF_MEM_free(buf);
  37729. res = EXPECT_RESULT();
  37730. #endif
  37731. return res;
  37732. }
  37733. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37734. static int stub_rand_seed(const void *buf, int num)
  37735. {
  37736. (void)buf;
  37737. (void)num;
  37738. return 123;
  37739. }
  37740. static int stub_rand_bytes(unsigned char *buf, int num)
  37741. {
  37742. (void)buf;
  37743. (void)num;
  37744. return 456;
  37745. }
  37746. static byte* was_stub_rand_cleanup_called(void)
  37747. {
  37748. static byte was_called = 0;
  37749. return &was_called;
  37750. }
  37751. static void stub_rand_cleanup(void)
  37752. {
  37753. byte* was_called = was_stub_rand_cleanup_called();
  37754. *was_called = 1;
  37755. return;
  37756. }
  37757. static byte* was_stub_rand_add_called(void)
  37758. {
  37759. static byte was_called = 0;
  37760. return &was_called;
  37761. }
  37762. static int stub_rand_add(const void *buf, int num, double entropy)
  37763. {
  37764. byte* was_called = was_stub_rand_add_called();
  37765. (void)buf;
  37766. (void)num;
  37767. (void)entropy;
  37768. *was_called = 1;
  37769. return 0;
  37770. }
  37771. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  37772. {
  37773. (void)buf;
  37774. (void)num;
  37775. return 9876;
  37776. }
  37777. static int stub_rand_status(void)
  37778. {
  37779. return 5432;
  37780. }
  37781. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37782. static int test_wolfSSL_RAND_set_rand_method(void)
  37783. {
  37784. int res = TEST_SKIPPED;
  37785. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37786. EXPECT_DECLS;
  37787. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  37788. unsigned char* buf = NULL;
  37789. int num = 0;
  37790. double entropy = 0;
  37791. int ret;
  37792. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  37793. byte* was_add_called = was_stub_rand_add_called();
  37794. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  37795. DYNAMIC_TYPE_TMP_BUFFER));
  37796. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  37797. ExpectIntEQ(*was_cleanup_called, 0);
  37798. RAND_cleanup();
  37799. ExpectIntEQ(*was_cleanup_called, 0);
  37800. rand_methods.seed = &stub_rand_seed;
  37801. rand_methods.bytes = &stub_rand_bytes;
  37802. rand_methods.cleanup = &stub_rand_cleanup;
  37803. rand_methods.add = &stub_rand_add;
  37804. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37805. rand_methods.status = &stub_rand_status;
  37806. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37807. ExpectIntEQ(RAND_seed(buf, num), 123);
  37808. ExpectIntEQ(RAND_bytes(buf, num), 456);
  37809. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37810. ExpectIntEQ(RAND_status(), 5432);
  37811. ExpectIntEQ(*was_add_called, 0);
  37812. /* The function pointer for RAND_add returns int, but RAND_add itself
  37813. * returns void. */
  37814. RAND_add(buf, num, entropy);
  37815. ExpectIntEQ(*was_add_called, 1);
  37816. was_add_called = 0;
  37817. ExpectIntEQ(*was_cleanup_called, 0);
  37818. RAND_cleanup();
  37819. ExpectIntEQ(*was_cleanup_called, 1);
  37820. *was_cleanup_called = 0;
  37821. ret = RAND_set_rand_method(NULL);
  37822. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  37823. ExpectIntNE(RAND_status(), 5432);
  37824. ExpectIntEQ(*was_cleanup_called, 0);
  37825. RAND_cleanup();
  37826. ExpectIntEQ(*was_cleanup_called, 0);
  37827. RAND_set_rand_method(NULL);
  37828. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37829. res = EXPECT_RESULT();
  37830. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37831. return res;
  37832. }
  37833. static int test_wolfSSL_RAND_bytes(void)
  37834. {
  37835. int res = TEST_SKIPPED;
  37836. #if defined(OPENSSL_EXTRA)
  37837. EXPECT_DECLS;
  37838. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37839. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37840. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37841. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37842. int max_bufsize;
  37843. byte *my_buf = NULL;
  37844. /* sanity check */
  37845. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  37846. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  37847. max_bufsize = size4;
  37848. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37849. DYNAMIC_TYPE_TMP_BUFFER));
  37850. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  37851. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  37852. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  37853. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  37854. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  37855. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  37856. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  37857. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37858. res = EXPECT_RESULT();
  37859. #endif
  37860. return res;
  37861. }
  37862. static int test_wolfSSL_RAND(void)
  37863. {
  37864. int res = TEST_SKIPPED;
  37865. #if defined(OPENSSL_EXTRA)
  37866. EXPECT_DECLS;
  37867. byte seed[16];
  37868. XMEMSET(seed, 0, sizeof(seed));
  37869. /* No global methods set. */
  37870. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  37871. ExpectIntEQ(RAND_poll(), 1);
  37872. RAND_cleanup();
  37873. ExpectIntEQ(RAND_egd(NULL), -1);
  37874. #ifndef NO_FILESYSTEM
  37875. {
  37876. char fname[100];
  37877. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  37878. ExpectIntEQ(RAND_write_file(NULL), 0);
  37879. }
  37880. #endif
  37881. res = EXPECT_RESULT();
  37882. #endif
  37883. return res;
  37884. }
  37885. static int test_wolfSSL_PKCS8_Compat(void)
  37886. {
  37887. int res = TEST_SKIPPED;
  37888. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  37889. !defined(NO_BIO)
  37890. EXPECT_DECLS;
  37891. PKCS8_PRIV_KEY_INFO* pt = NULL;
  37892. BIO* bio = NULL;
  37893. XFILE f = XBADFILE;
  37894. int bytes;
  37895. char pkcs8_buffer[512];
  37896. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37897. EVP_PKEY *pkey = NULL;
  37898. #endif
  37899. /* file from wolfssl/certs/ directory */
  37900. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  37901. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  37902. 0);
  37903. if (f != XBADFILE)
  37904. XFCLOSE(f);
  37905. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37906. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37907. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37908. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  37909. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37910. /* gets PKCS8 pointer to pkey */
  37911. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  37912. EVP_PKEY_free(pkey);
  37913. #endif
  37914. BIO_free(bio);
  37915. PKCS8_PRIV_KEY_INFO_free(pt);
  37916. res = EXPECT_RESULT();
  37917. #endif
  37918. return res;
  37919. }
  37920. static int test_wolfSSL_PKCS8_d2i(void)
  37921. {
  37922. int res = TEST_SKIPPED;
  37923. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37924. EXPECT_DECLS;
  37925. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37926. * requires a 12 byte password in FIPS mode. This test ends up
  37927. * trying to use an 8 byte password. */
  37928. #ifndef NO_FILESYSTEM
  37929. unsigned char pkcs8_buffer[2048];
  37930. const unsigned char* p;
  37931. int bytes;
  37932. XFILE file = XBADFILE;
  37933. WOLFSSL_EVP_PKEY* pkey = NULL;
  37934. #ifndef NO_BIO
  37935. BIO* bio = NULL;
  37936. #if defined(OPENSSL_ALL) && \
  37937. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37938. defined(HAVE_ECC)) && \
  37939. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37940. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37941. #endif
  37942. #endif
  37943. #ifndef NO_RSA
  37944. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37945. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37946. #ifndef NO_DES3
  37947. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37948. #endif
  37949. #endif /* NO_RSA */
  37950. #ifdef HAVE_ECC
  37951. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37952. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37953. #ifndef NO_DES3
  37954. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37955. #endif
  37956. #endif /* HAVE_ECC */
  37957. #endif /* !NO_FILESYSTEM */
  37958. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37959. #ifndef NO_RSA
  37960. #ifdef USE_CERT_BUFFERS_1024
  37961. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37962. int rsaSz = sizeof_server_key_der_1024;
  37963. #else
  37964. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37965. int rsaSz = sizeof_server_key_der_2048;
  37966. #endif
  37967. #endif
  37968. #ifdef HAVE_ECC
  37969. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37970. int ecSz = sizeof_ecc_key_der_256;
  37971. #endif
  37972. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37973. #ifndef NO_FILESYSTEM
  37974. (void)pkcs8_buffer;
  37975. (void)p;
  37976. (void)bytes;
  37977. (void)file;
  37978. #ifndef NO_BIO
  37979. (void)bio;
  37980. #endif
  37981. #endif
  37982. #ifdef OPENSSL_ALL
  37983. #ifndef NO_RSA
  37984. /* Try to auto-detect normal RSA private key */
  37985. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37986. EVP_PKEY_free(pkey);
  37987. pkey = NULL;
  37988. #endif
  37989. #ifdef HAVE_ECC
  37990. /* Try to auto-detect normal EC private key */
  37991. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37992. EVP_PKEY_free(pkey);
  37993. pkey = NULL;
  37994. #endif
  37995. #endif /* OPENSSL_ALL */
  37996. #ifndef NO_FILESYSTEM
  37997. #ifndef NO_RSA
  37998. /* Get DER encoded RSA PKCS#8 data. */
  37999. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  38000. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  38001. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38002. file)), 0);
  38003. if (file != XBADFILE) {
  38004. XFCLOSE(file);
  38005. file = XBADFILE;
  38006. }
  38007. p = pkcs8_buffer;
  38008. #ifdef OPENSSL_ALL
  38009. /* Try to decode - auto-detect key type. */
  38010. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  38011. #else
  38012. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  38013. #endif
  38014. /* Get PEM encoded RSA PKCS#8 data. */
  38015. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  38016. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38017. file)), 0);
  38018. if (file != XBADFILE) {
  38019. XFCLOSE(file);
  38020. file = XBADFILE;
  38021. }
  38022. #if defined(OPENSSL_ALL) && \
  38023. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  38024. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38025. /* Write PKCS#8 PEM to BIO. */
  38026. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  38027. NULL), bytes);
  38028. /* Compare file and written data */
  38029. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  38030. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  38031. BIO_free(bio);
  38032. bio = NULL;
  38033. #if !defined(NO_DES3) && !defined(NO_SHA)
  38034. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38035. /* Write Encrypted PKCS#8 PEM to BIO. */
  38036. bytes = 1834;
  38037. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  38038. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  38039. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  38040. (void*)"yassl123"));
  38041. EVP_PKEY_free(evpPkey);
  38042. evpPkey = NULL;
  38043. BIO_free(bio);
  38044. bio = NULL;
  38045. #endif /* !NO_DES3 && !NO_SHA */
  38046. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  38047. EVP_PKEY_free(pkey);
  38048. pkey = NULL;
  38049. /* PKCS#8 encrypted RSA key */
  38050. #ifndef NO_DES3
  38051. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  38052. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  38053. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38054. file)), 0);
  38055. if (file != XBADFILE) {
  38056. XFCLOSE(file);
  38057. file = XBADFILE;
  38058. }
  38059. #if defined(OPENSSL_ALL) && \
  38060. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  38061. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  38062. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  38063. (void*)"yassl123"));
  38064. EVP_PKEY_free(pkey);
  38065. pkey = NULL;
  38066. BIO_free(bio);
  38067. bio = NULL;
  38068. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  38069. #endif /* !NO_DES3 */
  38070. #endif /* NO_RSA */
  38071. #ifdef HAVE_ECC
  38072. /* PKCS#8 encode EC key */
  38073. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  38074. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  38075. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38076. file)), 0);
  38077. if (file != XBADFILE) {
  38078. XFCLOSE(file);
  38079. file = XBADFILE;
  38080. }
  38081. p = pkcs8_buffer;
  38082. #ifdef OPENSSL_ALL
  38083. /* Try to decode - auto-detect key type. */
  38084. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  38085. #else
  38086. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  38087. #endif
  38088. /* Get PEM encoded RSA PKCS#8 data. */
  38089. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  38090. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  38091. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38092. file)), 0);
  38093. if (file != XBADFILE) {
  38094. XFCLOSE(file);
  38095. file = XBADFILE;
  38096. }
  38097. #if defined(OPENSSL_ALL) && \
  38098. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  38099. defined(HAVE_AES_CBC)
  38100. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38101. /* Write PKCS#8 PEM to BIO. */
  38102. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  38103. NULL), bytes);
  38104. /* Compare file and written data */
  38105. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  38106. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  38107. BIO_free(bio);
  38108. bio = NULL;
  38109. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38110. /* Write Encrypted PKCS#8 PEM to BIO. */
  38111. bytes = 379;
  38112. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  38113. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  38114. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  38115. (void*)"yassl123"));
  38116. EVP_PKEY_free(evpPkey);
  38117. evpPkey = NULL;
  38118. BIO_free(bio);
  38119. bio = NULL;
  38120. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  38121. EVP_PKEY_free(pkey);
  38122. pkey = NULL;
  38123. /* PKCS#8 encrypted EC key */
  38124. #ifndef NO_DES3
  38125. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  38126. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  38127. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  38128. file)), 0);
  38129. if (file != XBADFILE) {
  38130. XFCLOSE(file);
  38131. file = XBADFILE;
  38132. }
  38133. #if defined(OPENSSL_ALL) && \
  38134. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  38135. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  38136. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  38137. (void*)"yassl123"));
  38138. EVP_PKEY_free(pkey);
  38139. pkey = NULL;
  38140. BIO_free(bio);
  38141. bio = NULL;
  38142. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  38143. #endif /* !NO_DES3 */
  38144. #endif /* HAVE_ECC */
  38145. #endif /* !NO_FILESYSTEM */
  38146. res = EXPECT_RESULT();
  38147. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  38148. return res;
  38149. }
  38150. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  38151. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  38152. #define LOGGING_THREADS 5
  38153. #define ERROR_COUNT 10
  38154. /* copied from logging.c since this is not exposed otherwise */
  38155. #ifndef ERROR_QUEUE_MAX
  38156. #ifdef ERROR_QUEUE_PER_THREAD
  38157. #define ERROR_QUEUE_MAX 16
  38158. #else
  38159. /* this breaks from compat of unlimited error queue size */
  38160. #define ERROR_QUEUE_MAX 100
  38161. #endif
  38162. #endif
  38163. static volatile int loggingThreadsReady;
  38164. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  38165. {
  38166. const char* file;
  38167. int line;
  38168. unsigned long err;
  38169. int errorCount = 0;
  38170. int i;
  38171. (void)args;
  38172. while (!loggingThreadsReady);
  38173. for (i = 0; i < ERROR_COUNT; i++)
  38174. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38175. while ((err = ERR_get_error_line(&file, &line))) {
  38176. AssertIntEQ(err, 990 + errorCount);
  38177. errorCount++;
  38178. }
  38179. AssertIntEQ(errorCount, ERROR_COUNT);
  38180. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  38181. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  38182. errorCount = 0;
  38183. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  38184. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38185. while ((err = ERR_get_error_line(&file, &line))) {
  38186. AssertIntEQ(err, 990 + errorCount);
  38187. errorCount++;
  38188. }
  38189. /* test that the 3 errors over the max were dropped */
  38190. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  38191. return 0;
  38192. }
  38193. #endif
  38194. static int test_error_queue_per_thread(void)
  38195. {
  38196. int res = TEST_SKIPPED;
  38197. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  38198. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  38199. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  38200. int i;
  38201. ERR_clear_error(); /* clear out any error nodes */
  38202. loggingThreadsReady = 0;
  38203. for (i = 0; i < LOGGING_THREADS; i++)
  38204. start_thread(test_logging, NULL, &loggingThreads[i]);
  38205. loggingThreadsReady = 1;
  38206. for (i = 0; i < LOGGING_THREADS; i++)
  38207. join_thread(loggingThreads[i]);
  38208. res = TEST_SUCCESS;
  38209. #endif
  38210. return res;
  38211. }
  38212. static int test_wolfSSL_ERR_put_error(void)
  38213. {
  38214. int res = TEST_SKIPPED;
  38215. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38216. defined(DEBUG_WOLFSSL)
  38217. EXPECT_DECLS;
  38218. const char* file;
  38219. int line;
  38220. ERR_clear_error(); /* clear out any error nodes */
  38221. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38222. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38223. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  38224. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  38225. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  38226. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  38227. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  38228. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  38229. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  38230. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  38231. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  38232. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  38233. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  38234. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  38235. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  38236. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  38237. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  38238. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  38239. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  38240. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  38241. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  38242. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  38243. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  38244. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  38245. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  38246. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  38247. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  38248. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  38249. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  38250. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  38251. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  38252. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  38253. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  38254. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  38255. "this file", 100);
  38256. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  38257. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38258. ExpectIntEQ(line, 100);
  38259. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  38260. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38261. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  38262. #endif
  38263. /* try reading past end of error queue */
  38264. file = NULL;
  38265. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38266. ExpectNull(file);
  38267. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  38268. PEMerr(4,4);
  38269. ExpectIntEQ(ERR_get_error(), 4);
  38270. /* Empty and free up all error nodes */
  38271. ERR_clear_error();
  38272. /* Verify all nodes are cleared */
  38273. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38274. ERR_clear_error();
  38275. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38276. res = EXPECT_RESULT();
  38277. #endif
  38278. return res;
  38279. }
  38280. /*
  38281. * This is a regression test for a bug where the peek/get error functions were
  38282. * drawing from the end of the queue rather than the front.
  38283. */
  38284. static int test_wolfSSL_ERR_get_error_order(void)
  38285. {
  38286. int res = TEST_SKIPPED;
  38287. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  38288. EXPECT_DECLS;
  38289. /* Empty the queue. */
  38290. wolfSSL_ERR_clear_error();
  38291. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  38292. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  38293. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  38294. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  38295. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  38296. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  38297. res = EXPECT_RESULT();
  38298. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  38299. return res;
  38300. }
  38301. #ifndef NO_BIO
  38302. static int test_wolfSSL_ERR_print_errors(void)
  38303. {
  38304. int res = TEST_SKIPPED;
  38305. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38306. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  38307. EXPECT_DECLS;
  38308. BIO* bio = NULL;
  38309. char buf[1024];
  38310. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38311. ERR_clear_error(); /* clear out any error nodes */
  38312. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38313. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  38314. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  38315. ERR_print_errors(bio);
  38316. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  38317. ExpectIntEQ(XSTRNCMP(
  38318. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  38319. buf, 55), 0);
  38320. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  38321. ExpectIntEQ(XSTRNCMP(
  38322. "error:299:wolfSSL library:unknown error number:asn.c:100",
  38323. buf, 56), 0);
  38324. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  38325. ExpectIntEQ(buf[0], '\0');
  38326. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  38327. BIO_free(bio);
  38328. res = EXPECT_RESULT();
  38329. #endif
  38330. return res;
  38331. }
  38332. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38333. defined(DEBUG_WOLFSSL)
  38334. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  38335. {
  38336. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  38337. return 0;
  38338. }
  38339. #endif
  38340. static int test_wolfSSL_ERR_print_errors_cb(void)
  38341. {
  38342. int res = TEST_SKIPPED;
  38343. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38344. defined(DEBUG_WOLFSSL)
  38345. EXPECT_DECLS;
  38346. BIO* bio = NULL;
  38347. char buf[1024];
  38348. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38349. ERR_clear_error(); /* clear out any error nodes */
  38350. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38351. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  38352. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  38353. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  38354. ExpectIntEQ(XSTRNCMP(
  38355. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  38356. buf, 53), 0);
  38357. ExpectIntEQ(XSTRNCMP(
  38358. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  38359. buf + 53, 55), 0);
  38360. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  38361. BIO_free(bio);
  38362. res = EXPECT_RESULT();
  38363. #endif
  38364. return res;
  38365. }
  38366. /*
  38367. * Testing WOLFSSL_ERROR_MSG
  38368. */
  38369. static int test_WOLFSSL_ERROR_MSG(void)
  38370. {
  38371. int res = TEST_SKIPPED;
  38372. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  38373. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  38374. const char* msg = TEST_STRING;
  38375. WOLFSSL_ERROR_MSG(msg);
  38376. res = TEST_SUCCESS;
  38377. #endif
  38378. return res;
  38379. }/*End test_WOLFSSL_ERROR_MSG*/
  38380. /*
  38381. * Testing wc_ERR_remove_state
  38382. */
  38383. static int test_wc_ERR_remove_state(void)
  38384. {
  38385. int res = TEST_SKIPPED;
  38386. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  38387. wc_ERR_remove_state();
  38388. res = TEST_SUCCESS;
  38389. #endif
  38390. return res;
  38391. }/*End test_wc_ERR_remove_state*/
  38392. /*
  38393. * Testing wc_ERR_print_errors_fp
  38394. */
  38395. static int test_wc_ERR_print_errors_fp(void)
  38396. {
  38397. int res = TEST_SKIPPED;
  38398. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  38399. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  38400. EXPECT_DECLS;
  38401. long sz;
  38402. XFILE fp = XBADFILE;
  38403. WOLFSSL_ERROR(BAD_FUNC_ARG);
  38404. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  38405. XBADFILE);
  38406. wc_ERR_print_errors_fp(fp);
  38407. #if defined(DEBUG_WOLFSSL)
  38408. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  38409. #ifdef NO_ERROR_QUEUE
  38410. ExpectIntEQ(sz = XFTELL(fp), 0);
  38411. #else
  38412. ExpectIntNE(sz = XFTELL(fp), 0);
  38413. #endif
  38414. #endif
  38415. if (fp != XBADFILE)
  38416. XFCLOSE(fp);
  38417. (void)sz;
  38418. res = EXPECT_RESULT();
  38419. #endif
  38420. return res;
  38421. }/*End test_wc_ERR_print_errors_fp*/
  38422. #ifdef DEBUG_WOLFSSL
  38423. static void Logging_cb(const int logLevel, const char *const logMessage)
  38424. {
  38425. (void)logLevel;
  38426. (void)logMessage;
  38427. }
  38428. #endif
  38429. /*
  38430. * Testing wolfSSL_GetLoggingCb
  38431. */
  38432. static int test_wolfSSL_GetLoggingCb(void)
  38433. {
  38434. EXPECT_DECLS;
  38435. #ifdef DEBUG_WOLFSSL
  38436. /* Testing without wolfSSL_SetLoggingCb() */
  38437. ExpectNull(wolfSSL_GetLoggingCb());
  38438. /* Testing with wolfSSL_SetLoggingCb() */
  38439. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  38440. ExpectNotNull(wolfSSL_GetLoggingCb());
  38441. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  38442. #endif
  38443. ExpectNull(wolfSSL_GetLoggingCb());
  38444. return EXPECT_RESULT();
  38445. }/*End test_wolfSSL_GetLoggingCb*/
  38446. #endif /* !NO_BIO */
  38447. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38448. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38449. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38450. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38451. {
  38452. EXPECT_DECLS;
  38453. static const unsigned char key[] = "simple test key";
  38454. HMAC_CTX* hmac = NULL;
  38455. ENGINE* e = NULL;
  38456. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38457. unsigned int len;
  38458. ExpectNotNull(hmac = HMAC_CTX_new());
  38459. HMAC_CTX_init(hmac);
  38460. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  38461. SSL_SUCCESS);
  38462. /* re-using test key as data to hash */
  38463. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  38464. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  38465. ExpectIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  38466. ExpectIntEQ(len, md_len);
  38467. ExpectIntEQ(HMAC_size(hmac), md_len);
  38468. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38469. HMAC_cleanup(hmac);
  38470. HMAC_CTX_free(hmac);
  38471. len = 0;
  38472. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38473. ExpectIntEQ(len, md_len);
  38474. return EXPECT_RESULT();
  38475. }
  38476. #endif
  38477. static int test_wolfSSL_HMAC(void)
  38478. {
  38479. int res = TEST_SKIPPED;
  38480. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38481. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38482. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38483. EXPECT_DECLS;
  38484. #ifndef NO_SHA256
  38485. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38486. TEST_SUCCESS);
  38487. #endif
  38488. #ifdef WOLFSSL_SHA224
  38489. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38490. TEST_SUCCESS);
  38491. #endif
  38492. #ifdef WOLFSSL_SHA384
  38493. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38494. TEST_SUCCESS);
  38495. #endif
  38496. #ifdef WOLFSSL_SHA512
  38497. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38498. TEST_SUCCESS);
  38499. #endif
  38500. #ifdef WOLFSSL_SHA3
  38501. #ifndef WOLFSSL_NOSHA3_224
  38502. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38503. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38504. #endif
  38505. #ifndef WOLFSSL_NOSHA3_256
  38506. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38507. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38508. #endif
  38509. #ifndef WOLFSSL_NOSHA3_384
  38510. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38511. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38512. #endif
  38513. #ifndef WOLFSSL_NOSHA3_512
  38514. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38515. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38516. #endif
  38517. #endif
  38518. #ifndef NO_SHA
  38519. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38520. TEST_SUCCESS);
  38521. #endif
  38522. res = EXPECT_RESULT();
  38523. #endif
  38524. return res;
  38525. }
  38526. static int test_wolfSSL_CMAC(void)
  38527. {
  38528. int res = TEST_SKIPPED;
  38529. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38530. defined(WOLFSSL_AES_DIRECT)
  38531. EXPECT_DECLS;
  38532. int i;
  38533. byte key[AES_128_KEY_SIZE];
  38534. CMAC_CTX* cmacCtx = NULL;
  38535. byte out[AES_BLOCK_SIZE];
  38536. size_t outLen = AES_BLOCK_SIZE;
  38537. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  38538. key[i] = i;
  38539. }
  38540. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38541. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38542. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38543. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38544. NULL), SSL_SUCCESS);
  38545. /* re-using test key as data to hash */
  38546. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  38547. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  38548. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  38549. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38550. CMAC_CTX_free(cmacCtx);
  38551. /* give a key too small for the cipher, verify we get failure */
  38552. cmacCtx = NULL;
  38553. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38554. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38555. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38556. NULL), SSL_FAILURE);
  38557. CMAC_CTX_free(cmacCtx);
  38558. res = EXPECT_RESULT();
  38559. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38560. return res;
  38561. }
  38562. static int test_wolfSSL_OBJ(void)
  38563. {
  38564. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  38565. * mode
  38566. */
  38567. int res = TEST_SKIPPED;
  38568. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  38569. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  38570. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  38571. EXPECT_DECLS;
  38572. ASN1_OBJECT *obj = NULL;
  38573. ASN1_OBJECT *obj2 = NULL;
  38574. char buf[50];
  38575. XFILE fp = XBADFILE;
  38576. X509 *x509 = NULL;
  38577. X509_NAME *x509Name = NULL;
  38578. X509_NAME_ENTRY *x509NameEntry = NULL;
  38579. ASN1_OBJECT *asn1Name = NULL;
  38580. int numNames = 0;
  38581. BIO *bio = NULL;
  38582. int nid;
  38583. int i, j;
  38584. const char *f[] = {
  38585. #ifndef NO_RSA
  38586. "./certs/ca-cert.der",
  38587. #endif
  38588. #ifdef HAVE_ECC
  38589. "./certs/ca-ecc-cert.der",
  38590. "./certs/ca-ecc384-cert.der",
  38591. #endif
  38592. NULL};
  38593. ASN1_OBJECT *field_name_obj = NULL;
  38594. int lastpos = -1;
  38595. int tmp = -1;
  38596. ASN1_STRING *asn1 = NULL;
  38597. unsigned char *buf_dyn = NULL;
  38598. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  38599. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38600. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  38601. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  38602. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38603. ASN1_OBJECT_free(obj);
  38604. obj = NULL;
  38605. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  38606. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  38607. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  38608. #ifdef WOLFSSL_CERT_EXT
  38609. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  38610. #endif
  38611. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38612. ExpectNotNull(obj2 = OBJ_dup(obj));
  38613. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  38614. ASN1_OBJECT_free(obj);
  38615. obj = NULL;
  38616. ASN1_OBJECT_free(obj2);
  38617. obj2 = NULL;
  38618. for (i = 0; f[i] != NULL; i++)
  38619. {
  38620. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  38621. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  38622. if (fp != XBADFILE) {
  38623. XFCLOSE(fp);
  38624. fp = XBADFILE;
  38625. }
  38626. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  38627. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38628. /* Get the Common Name by using OBJ_txt2obj */
  38629. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  38630. do
  38631. {
  38632. lastpos = tmp;
  38633. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  38634. } while (tmp > -1);
  38635. ExpectIntNE(lastpos, -1);
  38636. ASN1_OBJECT_free(field_name_obj);
  38637. field_name_obj = NULL;
  38638. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  38639. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  38640. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  38641. /*
  38642. * All Common Names should be www.wolfssl.com
  38643. * This makes testing easier as we can test for the expected value.
  38644. */
  38645. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  38646. OPENSSL_free(buf_dyn);
  38647. buf_dyn = NULL;
  38648. bio = BIO_new(BIO_s_mem());
  38649. ExpectTrue(bio != NULL);
  38650. for (j = 0; j < numNames; j++)
  38651. {
  38652. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38653. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  38654. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38655. }
  38656. BIO_free(bio);
  38657. bio = NULL;
  38658. X509_free(x509);
  38659. x509 = NULL;
  38660. }
  38661. #ifdef HAVE_PKCS12
  38662. {
  38663. PKCS12 *p12 = NULL;
  38664. int boolRet;
  38665. EVP_PKEY *pkey = NULL;
  38666. const char *p12_f[] = {
  38667. #if !defined(NO_DES3) && !defined(NO_RSA)
  38668. "./certs/test-servercert.p12",
  38669. #endif
  38670. NULL};
  38671. for (i = 0; p12_f[i] != NULL; i++)
  38672. {
  38673. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  38674. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  38675. if (fp != XBADFILE) {
  38676. XFCLOSE(fp);
  38677. fp = XBADFILE;
  38678. }
  38679. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  38680. &pkey, &x509, NULL)) > 0);
  38681. wc_PKCS12_free(p12);
  38682. p12 = NULL;
  38683. EVP_PKEY_free(pkey);
  38684. x509Name = X509_get_issuer_name(x509);
  38685. ExpectNotNull(x509Name);
  38686. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38687. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  38688. for (j = 0; j < numNames; j++)
  38689. {
  38690. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38691. ExpectNotNull(asn1Name =
  38692. X509_NAME_ENTRY_get_object(x509NameEntry));
  38693. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38694. }
  38695. BIO_free(bio);
  38696. bio = NULL;
  38697. X509_free(x509);
  38698. x509 = NULL;
  38699. }
  38700. }
  38701. #endif /* HAVE_PKCS12 */
  38702. res = EXPECT_RESULT();
  38703. #endif
  38704. return res;
  38705. }
  38706. static int test_wolfSSL_OBJ_cmp(void)
  38707. {
  38708. int res = TEST_SKIPPED;
  38709. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38710. EXPECT_DECLS;
  38711. ASN1_OBJECT *obj = NULL;
  38712. ASN1_OBJECT *obj2 = NULL;
  38713. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38714. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  38715. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  38716. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  38717. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  38718. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  38719. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  38720. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  38721. ASN1_OBJECT_free(obj);
  38722. ASN1_OBJECT_free(obj2);
  38723. res = EXPECT_RESULT();
  38724. #endif
  38725. return res;
  38726. }
  38727. static int test_wolfSSL_OBJ_txt2nid(void)
  38728. {
  38729. int res = TEST_SKIPPED;
  38730. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  38731. EXPECT_DECLS;
  38732. int i;
  38733. static const struct {
  38734. const char* sn;
  38735. const char* ln;
  38736. const char* oid;
  38737. int nid;
  38738. } testVals[] = {
  38739. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  38740. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  38741. NID_id_on_dnsSRV },
  38742. { "msUPN", "Microsoft User Principal Name",
  38743. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  38744. { NULL, NULL, NULL, NID_undef }
  38745. };
  38746. /* Invalid cases */
  38747. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  38748. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  38749. /* Valid cases */
  38750. for (i = 0; testVals[i].sn != NULL; i++) {
  38751. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  38752. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  38753. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  38754. }
  38755. res = EXPECT_RESULT();
  38756. #endif
  38757. return res;
  38758. }
  38759. static int test_wolfSSL_OBJ_txt2obj(void)
  38760. {
  38761. int res = TEST_SKIPPED;
  38762. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  38763. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  38764. EXPECT_DECLS;
  38765. int i;
  38766. char buf[50];
  38767. ASN1_OBJECT* obj = NULL;
  38768. static const struct {
  38769. const char* oidStr;
  38770. const char* sn;
  38771. const char* ln;
  38772. } objs_list[] = {
  38773. #if defined(WOLFSSL_APACHE_HTTPD)
  38774. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  38775. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  38776. #endif
  38777. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  38778. { NULL, NULL, NULL }
  38779. };
  38780. static const struct {
  38781. const char* numeric;
  38782. const char* name;
  38783. } objs_named[] = {
  38784. /* In dictionary but not in normal list. */
  38785. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  38786. /* Made up OID. */
  38787. { "1.3.5.7", "1.3.5.7" },
  38788. { NULL, NULL }
  38789. };
  38790. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  38791. ASN1_OBJECT_free(obj);
  38792. obj = NULL;
  38793. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  38794. ASN1_OBJECT_free(obj);
  38795. obj = NULL;
  38796. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  38797. /* Test numerical value of oid (oidStr) */
  38798. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  38799. /* Convert object back to text to confirm oid is correct */
  38800. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38801. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38802. ASN1_OBJECT_free(obj);
  38803. obj = NULL;
  38804. XMEMSET(buf, 0, sizeof(buf));
  38805. /* Test short name (sn) */
  38806. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  38807. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  38808. /* Convert object back to text to confirm oid is correct */
  38809. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38810. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38811. ASN1_OBJECT_free(obj);
  38812. obj = NULL;
  38813. XMEMSET(buf, 0, sizeof(buf));
  38814. /* Test long name (ln) - should fail when no_name = 1 */
  38815. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  38816. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  38817. /* Convert object back to text to confirm oid is correct */
  38818. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38819. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38820. ASN1_OBJECT_free(obj);
  38821. obj = NULL;
  38822. XMEMSET(buf, 0, sizeof(buf));
  38823. }
  38824. for (i = 0; objs_named[i].numeric != NULL; i++) {
  38825. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  38826. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  38827. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  38828. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38829. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  38830. ASN1_OBJECT_free(obj);
  38831. obj = NULL;
  38832. }
  38833. res = EXPECT_RESULT();
  38834. #endif
  38835. return res;
  38836. }
  38837. static int test_wolfSSL_PEM_write_bio_X509(void)
  38838. {
  38839. int res = TEST_SKIPPED;
  38840. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  38841. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  38842. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  38843. !defined(NO_FILESYSTEM)
  38844. EXPECT_DECLS;
  38845. /* This test contains the hard coded expected
  38846. * lengths. Update if necessary */
  38847. XFILE fp = XBADFILE;
  38848. WOLFSSL_EVP_PKEY *priv = NULL;
  38849. BIO* input = NULL;
  38850. BIO* output = NULL;
  38851. X509* x509a = NULL;
  38852. X509* x509b = NULL;
  38853. ASN1_TIME* notBeforeA = NULL;
  38854. ASN1_TIME* notAfterA = NULL;
  38855. #ifndef NO_ASN_TIME
  38856. ASN1_TIME* notBeforeB = NULL;
  38857. ASN1_TIME* notAfterB = NULL;
  38858. #endif
  38859. int expectedLen;
  38860. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  38861. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  38862. if (fp != XBADFILE) {
  38863. XFCLOSE(fp);
  38864. fp = XBADFILE;
  38865. }
  38866. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  38867. "rb"));
  38868. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  38869. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  38870. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38871. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  38872. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  38873. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  38874. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38875. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38876. /* compare length against expected */
  38877. expectedLen = 2000;
  38878. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38879. #ifndef NO_ASN_TIME
  38880. /* read exported X509 PEM back into struct, sanity check on export,
  38881. * make sure notBefore/notAfter are the same and certs are identical. */
  38882. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38883. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  38884. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  38885. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38886. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38887. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38888. X509_free(x509b);
  38889. x509b = NULL;
  38890. #endif
  38891. /* Reset output buffer */
  38892. BIO_free(output);
  38893. output = NULL;
  38894. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38895. /* Test forcing the AKID to be generated just from KeyIdentifier */
  38896. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  38897. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  38898. x509a->authKeyId = x509a->authKeyIdSrc;
  38899. x509a->authKeyIdSrc = NULL;
  38900. x509a->authKeyIdSrcSz = 0;
  38901. }
  38902. /* Resign to re-generate the der */
  38903. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  38904. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38905. /* Check that we generate a smaller output since the AKID will
  38906. * only contain the KeyIdentifier without any additional
  38907. * information */
  38908. /* Here we copy the validity struct from the original */
  38909. expectedLen = 1688;
  38910. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38911. /* Reset buffers and x509 */
  38912. BIO_free(input);
  38913. input = NULL;
  38914. BIO_free(output);
  38915. output = NULL;
  38916. X509_free(x509a);
  38917. x509a = NULL;
  38918. /* test CA and basicConstSet values are encoded when
  38919. * the cert is a CA */
  38920. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  38921. /* read PEM into X509 struct */
  38922. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38923. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38924. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38925. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38926. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  38927. * values are maintained and certs are identical.*/
  38928. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38929. ExpectIntEQ(x509b->isCa, 1);
  38930. ExpectIntEQ(x509b->basicConstSet, 1);
  38931. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38932. X509_free(x509a);
  38933. x509a = NULL;
  38934. X509_free(x509b);
  38935. x509b = NULL;
  38936. BIO_free(input);
  38937. input = NULL;
  38938. BIO_free(output);
  38939. output = NULL;
  38940. /* test CA and basicConstSet values are encoded when
  38941. * the cert is not CA */
  38942. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  38943. /* read PEM into X509 struct */
  38944. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38945. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38946. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38947. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38948. /* read exported X509 PEM back into struct, ensure isCa and
  38949. * basicConstSet values are maintained and certs are identical */
  38950. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38951. ExpectIntEQ(x509b->isCa, 0);
  38952. ExpectIntEQ(x509b->basicConstSet, 1);
  38953. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38954. wolfSSL_EVP_PKEY_free(priv);
  38955. X509_free(x509a);
  38956. X509_free(x509b);
  38957. BIO_free(input);
  38958. BIO_free(output);
  38959. res = EXPECT_RESULT();
  38960. #endif
  38961. return res;
  38962. }
  38963. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38964. {
  38965. int res = TEST_SKIPPED;
  38966. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38967. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38968. EXPECT_DECLS;
  38969. X509* x509 = NULL;
  38970. #ifndef NO_BIO
  38971. BIO* bio = NULL;
  38972. #endif
  38973. X509_NAME* nm = NULL;
  38974. X509_NAME_ENTRY* entry = NULL;
  38975. unsigned char cn[] = "another name to add";
  38976. #ifdef OPENSSL_ALL
  38977. int i;
  38978. int names_len = 0;
  38979. #endif
  38980. ExpectNotNull(x509 =
  38981. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38982. #ifndef NO_BIO
  38983. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38984. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38985. #endif
  38986. #ifdef WOLFSSL_CERT_REQ
  38987. {
  38988. X509_REQ* req = NULL;
  38989. #ifndef NO_BIO
  38990. BIO* bReq = NULL;
  38991. #endif
  38992. ExpectNotNull(req =
  38993. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38994. #ifndef NO_BIO
  38995. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  38996. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38997. BIO_free(bReq);
  38998. #endif
  38999. X509_free(req);
  39000. }
  39001. #endif
  39002. ExpectNotNull(nm = X509_get_subject_name(x509));
  39003. /* Test add entry */
  39004. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39005. 0x0c, cn, (int)sizeof(cn)));
  39006. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39007. #ifdef WOLFSSL_CERT_EXT
  39008. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39009. (byte*)"support@wolfssl.com", 19, -1,
  39010. 1), WOLFSSL_SUCCESS);
  39011. #endif
  39012. X509_NAME_ENTRY_free(entry);
  39013. entry = NULL;
  39014. #ifdef WOLFSSL_CERT_REQ
  39015. {
  39016. unsigned char srv_pkcs9p[] = "Server";
  39017. unsigned char fvrtDrnk[] = "tequila";
  39018. unsigned char* der = NULL;
  39019. char* subject = NULL;
  39020. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39021. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39022. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39023. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39024. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39025. ExpectNotNull(der);
  39026. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39027. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39028. #ifdef DEBUG_WOLFSSL
  39029. if (subject != NULL) {
  39030. fprintf(stderr, "\n\t%s\n", subject);
  39031. }
  39032. #endif
  39033. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39034. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39035. }
  39036. #endif
  39037. /* Test add entry by text */
  39038. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39039. 0x0c, cn, (int)sizeof(cn)));
  39040. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39041. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39042. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39043. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39044. #endif
  39045. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39046. X509_NAME_ENTRY_free(entry);
  39047. entry = NULL;
  39048. /* Test add entry by NID */
  39049. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39050. cn, -1, -1, 0), SSL_SUCCESS);
  39051. #ifdef OPENSSL_ALL
  39052. /* stack of name entry */
  39053. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39054. for (i = 0; i < names_len; i++) {
  39055. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39056. }
  39057. #endif
  39058. #ifndef NO_BIO
  39059. BIO_free(bio);
  39060. #endif
  39061. X509_free(x509); /* free's nm */
  39062. res = EXPECT_RESULT();
  39063. #endif
  39064. return res;
  39065. }
  39066. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  39067. static int test_GENERAL_NAME_set0_othername(void) {
  39068. int res = TEST_SKIPPED;
  39069. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39070. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39071. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39072. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39073. defined(WOLFSSL_FPKI)
  39074. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39075. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39076. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39077. EXPECT_DECLS;
  39078. const char * cert_fname = "./certs/server-cert.der";
  39079. const char * key_fname = "./certs/server-key.der";
  39080. X509* x509 = NULL;
  39081. GENERAL_NAME* gn = NULL;
  39082. GENERAL_NAMES* gns = NULL;
  39083. ASN1_OBJECT* upn_oid = NULL;
  39084. ASN1_UTF8STRING *utf8str = NULL;
  39085. ASN1_TYPE *value = NULL;
  39086. X509_EXTENSION * ext = NULL;
  39087. byte* pt = NULL;
  39088. byte der[4096];
  39089. int derSz = 0;
  39090. EVP_PKEY* priv = NULL;
  39091. XFILE f = XBADFILE;
  39092. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39093. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39094. if (f != XBADFILE) {
  39095. XFCLOSE(f);
  39096. f = XBADFILE;
  39097. }
  39098. ExpectNotNull(gn = GENERAL_NAME_new());
  39099. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39100. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39101. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39102. ExpectNotNull(value = ASN1_TYPE_new());
  39103. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39104. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39105. wolfSSL_ASN1_STRING_free(utf8str);
  39106. }
  39107. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39108. if (EXPECT_FAIL()) {
  39109. ASN1_TYPE_free(value);
  39110. }
  39111. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39112. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39113. if (EXPECT_FAIL()) {
  39114. GENERAL_NAME_free(gn);
  39115. gn = NULL;
  39116. }
  39117. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39118. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39119. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39120. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39121. if (f != XBADFILE) {
  39122. XFCLOSE(f);
  39123. f = XBADFILE;
  39124. }
  39125. pt = der;
  39126. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39127. (const unsigned char**)&pt, derSz));
  39128. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39129. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39130. gns = NULL;
  39131. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39132. NULL));
  39133. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39134. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39135. ExpectIntEQ(gn->type, 0);
  39136. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39137. ASN1_OBJECT_free(upn_oid);
  39138. X509_EXTENSION_free(ext);
  39139. X509_free(x509);
  39140. EVP_PKEY_free(priv);
  39141. res = EXPECT_RESULT();
  39142. #endif
  39143. return res;
  39144. }
  39145. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  39146. static int test_othername_and_SID_ext(void) {
  39147. int res = TEST_SKIPPED;
  39148. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39149. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39150. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39151. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39152. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39153. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39154. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39155. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39156. EXPECT_DECLS;
  39157. const char* csr_fname = "./certs/csr.signed.der";
  39158. const char* key_fname = "./certs/server-key.der";
  39159. byte der[4096];
  39160. int derSz = 0;
  39161. X509_REQ* x509 = NULL;
  39162. STACK_OF(X509_EXTENSION) *exts = NULL;
  39163. X509_EXTENSION * san_ext = NULL;
  39164. X509_EXTENSION * ext = NULL;
  39165. GENERAL_NAME* gn = NULL;
  39166. GENERAL_NAMES* gns = NULL;
  39167. ASN1_OBJECT* upn_oid = NULL;
  39168. ASN1_UTF8STRING *utf8str = NULL;
  39169. ASN1_TYPE *value = NULL;
  39170. ASN1_STRING *extval = NULL;
  39171. /* SID extension. SID data format explained here:
  39172. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  39173. */
  39174. uint8_t SidExtension[] = {
  39175. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  39176. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  39177. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  39178. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  39179. uint8_t expectedAltName[] = {
  39180. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  39181. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  39182. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  39183. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  39184. X509_EXTENSION *sid_ext = NULL;
  39185. ASN1_OBJECT* sid_oid = NULL;
  39186. ASN1_OCTET_STRING *sid_data = NULL;
  39187. EVP_PKEY* priv = NULL;
  39188. XFILE f = XBADFILE;
  39189. byte* pt = NULL;
  39190. BIO* bio = NULL;
  39191. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  39192. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  39193. if (f != XBADFILE) {
  39194. XFCLOSE(f);
  39195. f = XBADFILE;
  39196. }
  39197. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  39198. ExpectNotNull(gn = GENERAL_NAME_new());
  39199. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39200. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39201. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39202. ExpectNotNull(value = ASN1_TYPE_new());
  39203. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39204. if (EXPECT_FAIL()) {
  39205. ASN1_UTF8STRING_free(utf8str);
  39206. }
  39207. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39208. if (EXPECT_FAIL()) {
  39209. ASN1_TYPE_free(value);
  39210. GENERAL_NAME_free(gn);
  39211. gn = NULL;
  39212. }
  39213. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39214. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39215. if (EXPECT_FAIL()) {
  39216. GENERAL_NAME_free(gn);
  39217. }
  39218. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39219. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  39220. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  39221. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  39222. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  39223. sid_data));
  39224. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  39225. /* Ensure an empty stack doesn't raise an error. */
  39226. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39227. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  39228. if (EXPECT_FAIL()) {
  39229. X509_EXTENSION_free(san_ext);
  39230. }
  39231. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  39232. if (EXPECT_FAIL()) {
  39233. X509_EXTENSION_free(sid_ext);
  39234. }
  39235. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39236. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39237. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39238. if (f != XBADFILE)
  39239. XFCLOSE(f);
  39240. pt = der;
  39241. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39242. (const unsigned char**)&pt, derSz));
  39243. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  39244. pt = der;
  39245. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  39246. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39247. gns = NULL;
  39248. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39249. exts = NULL;
  39250. ASN1_OBJECT_free(upn_oid);
  39251. ASN1_OBJECT_free(sid_oid);
  39252. ASN1_OCTET_STRING_free(sid_data);
  39253. X509_REQ_free(x509);
  39254. x509 = NULL;
  39255. EVP_PKEY_free(priv);
  39256. /* At this point everything used to generate what is in der is cleaned up.
  39257. * We now read back from der to confirm the extensions were inserted
  39258. * correctly. */
  39259. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  39260. ExpectNotNull(bio);
  39261. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  39262. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  39263. ExpectNotNull(x509);
  39264. BIO_free(bio);
  39265. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  39266. x509));
  39267. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  39268. /* Check the SID extension. */
  39269. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  39270. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39271. ExpectIntEQ(extval->length, sizeof(SidExtension));
  39272. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  39273. /* Check the AltNames extension. */
  39274. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  39275. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39276. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  39277. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  39278. 0);
  39279. /* Cleanup */
  39280. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39281. NULL));
  39282. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  39283. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  39284. ExpectIntEQ(gn->type, 0);
  39285. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39286. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39287. X509_REQ_free(x509);
  39288. res = EXPECT_RESULT();
  39289. #endif
  39290. return res;
  39291. }
  39292. static int test_wolfSSL_X509_set_name(void)
  39293. {
  39294. int res = TEST_SKIPPED;
  39295. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39296. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39297. EXPECT_DECLS;
  39298. X509* x509 = NULL;
  39299. X509_NAME* name = NULL;
  39300. ExpectNotNull(name = X509_NAME_new());
  39301. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  39302. (byte*)"wolfssl.com", 11, 0, 1),
  39303. WOLFSSL_SUCCESS);
  39304. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  39305. (byte*)"support@wolfssl.com", 19, -1,
  39306. 1), WOLFSSL_SUCCESS);
  39307. ExpectNotNull(x509 = X509_new());
  39308. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  39309. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  39310. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  39311. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  39312. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  39313. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  39314. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  39315. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  39316. X509_free(x509);
  39317. X509_NAME_free(name);
  39318. res = EXPECT_RESULT();
  39319. #endif /* OPENSSL_ALL && !NO_CERTS */
  39320. return res;
  39321. }
  39322. static int test_wolfSSL_X509_set_notAfter(void)
  39323. {
  39324. int res = TEST_SKIPPED;
  39325. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39326. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39327. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39328. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  39329. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  39330. /* Generalized time will overflow time_t if not long */
  39331. EXPECT_DECLS;
  39332. X509* x = NULL;
  39333. BIO* bio = NULL;
  39334. ASN1_TIME *asn_time = NULL;
  39335. ASN1_TIME *time_check = NULL;
  39336. const int year = 365*24*60*60;
  39337. const int day = 24*60*60;
  39338. const int hour = 60*60;
  39339. const int mini = 60;
  39340. int offset_day;
  39341. unsigned char buf[25];
  39342. time_t t;
  39343. /*
  39344. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39345. */
  39346. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  39347. offset_day = 7;
  39348. /*
  39349. * Free these.
  39350. */
  39351. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39352. ExpectNotNull(asn_time);
  39353. ExpectNotNull(x = X509_new());
  39354. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39355. /*
  39356. * Tests
  39357. */
  39358. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  39359. /* time_check is simply (ANS1_TIME*)x->notAfter */
  39360. ExpectNotNull(time_check = X509_get_notAfter(x));
  39361. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39362. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39363. /* Convert to human readable format and compare to intended date */
  39364. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39365. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39366. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  39367. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  39368. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  39369. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  39370. /*
  39371. * Cleanup
  39372. */
  39373. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39374. X509_free(x);
  39375. BIO_free(bio);
  39376. res = EXPECT_RESULT();
  39377. #endif
  39378. return res;
  39379. }
  39380. static int test_wolfSSL_X509_set_notBefore(void)
  39381. {
  39382. int res = TEST_SKIPPED;
  39383. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39384. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39385. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39386. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  39387. EXPECT_DECLS;
  39388. X509* x = NULL;
  39389. BIO* bio = NULL;
  39390. ASN1_TIME *asn_time = NULL;
  39391. ASN1_TIME *time_check = NULL;
  39392. const int year = 365*24*60*60;
  39393. const int day = 24*60*60;
  39394. const int hour = 60*60;
  39395. const int mini = 60;
  39396. int offset_day;
  39397. unsigned char buf[25];
  39398. time_t t;
  39399. /*
  39400. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39401. */
  39402. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  39403. offset_day = 7;
  39404. /*
  39405. * Free these.
  39406. */
  39407. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39408. ExpectNotNull(asn_time);
  39409. ExpectNotNull(x = X509_new());
  39410. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39411. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  39412. /*
  39413. * Main Tests
  39414. */
  39415. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  39416. /* time_check == (ANS1_TIME*)x->notBefore */
  39417. ExpectNotNull(time_check = X509_get_notBefore(x));
  39418. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39419. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39420. /* Convert to human readable format and compare to intended date */
  39421. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39422. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39423. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  39424. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  39425. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  39426. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  39427. /*
  39428. * Cleanup
  39429. */
  39430. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39431. X509_free(x);
  39432. BIO_free(bio);
  39433. res = EXPECT_RESULT();
  39434. #endif
  39435. return res;
  39436. }
  39437. static int test_wolfSSL_X509_set_version(void)
  39438. {
  39439. int res = TEST_SKIPPED;
  39440. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  39441. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39442. EXPECT_DECLS;
  39443. X509* x509 = NULL;
  39444. long v = 2L;
  39445. long maxInt = INT_MAX;
  39446. ExpectNotNull(x509 = X509_new());
  39447. /* These should pass. */
  39448. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  39449. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  39450. /* Fail Case: When v(long) is greater than x509->version(int). */
  39451. v = maxInt+1;
  39452. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  39453. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  39454. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  39455. /* Cleanup */
  39456. X509_free(x509);
  39457. res = EXPECT_RESULT();
  39458. #endif
  39459. return res;
  39460. }
  39461. #ifndef NO_BIO
  39462. static int test_wolfSSL_BIO_gets(void)
  39463. {
  39464. int res = TEST_SKIPPED;
  39465. #if defined(OPENSSL_EXTRA)
  39466. EXPECT_DECLS;
  39467. BIO* bio = NULL;
  39468. BIO* bio2 = NULL;
  39469. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  39470. char emp[] = "";
  39471. char bio_buffer[20];
  39472. int bufferSz = 20;
  39473. /* try with bad args */
  39474. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  39475. /* try with real msg */
  39476. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39477. XMEMSET(bio_buffer, 0, bufferSz);
  39478. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  39479. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  39480. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  39481. ExpectFalse(bio2 != BIO_next(bio));
  39482. /* make buffer filled with no terminating characters */
  39483. XMEMSET(bio_buffer, 1, bufferSz);
  39484. /* BIO_gets reads a line of data */
  39485. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39486. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39487. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39488. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39489. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39490. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39491. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39492. /* check not null terminated string */
  39493. BIO_free(bio);
  39494. bio = NULL;
  39495. msg[0] = 0x33;
  39496. msg[1] = 0x33;
  39497. msg[2] = 0x33;
  39498. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39499. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  39500. ExpectIntEQ(bio_buffer[0], msg[0]);
  39501. ExpectIntEQ(bio_buffer[1], msg[1]);
  39502. ExpectIntNE(bio_buffer[2], msg[2]);
  39503. BIO_free(bio);
  39504. bio = NULL;
  39505. msg[3] = 0x33;
  39506. bio_buffer[3] = 0x33;
  39507. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39508. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  39509. ExpectIntEQ(bio_buffer[0], msg[0]);
  39510. ExpectIntEQ(bio_buffer[1], msg[1]);
  39511. ExpectIntEQ(bio_buffer[2], msg[2]);
  39512. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  39513. /* check reading an empty string */
  39514. BIO_free(bio);
  39515. bio = NULL;
  39516. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  39517. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39518. ExpectStrEQ(emp, bio_buffer);
  39519. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39520. /* check error cases */
  39521. BIO_free(bio);
  39522. bio = NULL;
  39523. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  39524. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39525. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39526. #if !defined(NO_FILESYSTEM)
  39527. {
  39528. BIO* f_bio = NULL;
  39529. XFILE f = XBADFILE;
  39530. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  39531. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39532. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  39533. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  39534. if (EXPECT_FAIL() && (f != XBADFILE)) {
  39535. XFCLOSE(f);
  39536. }
  39537. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39538. BIO_free(f_bio);
  39539. f_bio = NULL;
  39540. }
  39541. #endif /* NO_FILESYSTEM */
  39542. BIO_free(bio);
  39543. bio = NULL;
  39544. BIO_free(bio2);
  39545. bio2 = NULL;
  39546. /* try with type BIO */
  39547. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  39548. sizeof(msg));
  39549. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39550. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39551. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  39552. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  39553. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  39554. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  39555. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  39556. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39557. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39558. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39559. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39560. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39561. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39562. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39563. BIO_free(bio);
  39564. bio = NULL;
  39565. BIO_free(bio2);
  39566. bio2 = NULL;
  39567. /* check reading an empty string */
  39568. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39569. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  39570. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39571. ExpectStrEQ(emp, bio_buffer);
  39572. BIO_free(bio);
  39573. bio = NULL;
  39574. res = EXPECT_RESULT();
  39575. #endif
  39576. return res;
  39577. }
  39578. static int test_wolfSSL_BIO_puts(void)
  39579. {
  39580. int res = TEST_SKIPPED;
  39581. #if defined(OPENSSL_EXTRA)
  39582. EXPECT_DECLS;
  39583. BIO* bio = NULL;
  39584. char input[] = "hello\0world\n.....ok\n\0";
  39585. char output[128];
  39586. XMEMSET(output, 0, sizeof(output));
  39587. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39588. ExpectIntEQ(BIO_puts(bio, input), 5);
  39589. ExpectIntEQ(BIO_pending(bio), 5);
  39590. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  39591. ExpectIntEQ(BIO_pending(bio), 19);
  39592. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  39593. ExpectStrEQ(output, "helloworld\n");
  39594. ExpectIntEQ(BIO_pending(bio), 8);
  39595. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  39596. ExpectStrEQ(output, ".....ok\n");
  39597. ExpectIntEQ(BIO_pending(bio), 0);
  39598. ExpectIntEQ(BIO_puts(bio, ""), -1);
  39599. BIO_free(bio);
  39600. res = EXPECT_RESULT();
  39601. #endif
  39602. return res;
  39603. }
  39604. static int test_wolfSSL_BIO_dump(void)
  39605. {
  39606. int res = TEST_SKIPPED;
  39607. #if defined(OPENSSL_EXTRA)
  39608. EXPECT_DECLS;
  39609. BIO* bio;
  39610. static const unsigned char data[] = {
  39611. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  39612. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  39613. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  39614. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  39615. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  39616. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  39617. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  39618. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  39619. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  39620. 0xB4
  39621. };
  39622. /* Generated with OpenSSL. */
  39623. static const char expected[] =
  39624. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  39625. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  39626. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  39627. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  39628. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  39629. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  39630. static const char expectedAll[] =
  39631. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  39632. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  39633. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  39634. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  39635. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  39636. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  39637. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  39638. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  39639. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  39640. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  39641. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  39642. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  39643. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  39644. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  39645. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  39646. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  39647. char output[16 * 80];
  39648. int i;
  39649. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39650. /* Example key dumped. */
  39651. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  39652. sizeof(expected) - 1);
  39653. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  39654. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  39655. /* Try every possible value for a character. */
  39656. for (i = 0; i < 256; i++)
  39657. output[i] = i;
  39658. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  39659. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  39660. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  39661. BIO_free(bio);
  39662. res = EXPECT_RESULT();
  39663. #endif
  39664. return res;
  39665. }
  39666. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39667. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39668. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39669. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  39670. {
  39671. (void)ssl;
  39672. (void)buf;
  39673. (void)sz;
  39674. (void)ctx;
  39675. return WOLFSSL_CBIO_ERR_WANT_READ;
  39676. }
  39677. #endif
  39678. static int test_wolfSSL_BIO_should_retry(void)
  39679. {
  39680. int res = TEST_SKIPPED;
  39681. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39682. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39683. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39684. EXPECT_DECLS;
  39685. tcp_ready ready;
  39686. func_args server_args;
  39687. THREAD_TYPE serverThread;
  39688. SOCKET_T sockfd = 0;
  39689. WOLFSSL_CTX* ctx = NULL;
  39690. WOLFSSL* ssl = NULL;
  39691. char msg[64] = "hello wolfssl!";
  39692. char reply[1024];
  39693. int msgSz = (int)XSTRLEN(msg);
  39694. int ret;
  39695. BIO* bio = NULL;
  39696. XMEMSET(&server_args, 0, sizeof(func_args));
  39697. #ifdef WOLFSSL_TIRTOS
  39698. fdOpenSession(Task_self());
  39699. #endif
  39700. StartTCP();
  39701. InitTcpReady(&ready);
  39702. #if defined(USE_WINDOWS_API)
  39703. /* use RNG to get random port if using windows */
  39704. ready.port = GetRandomPort();
  39705. #endif
  39706. server_args.signal = &ready;
  39707. start_thread(test_server_nofail, &server_args, &serverThread);
  39708. wait_tcp_ready(&server_args);
  39709. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39710. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  39711. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  39712. #endif
  39713. ExpectIntEQ(WOLFSSL_SUCCESS,
  39714. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39715. ExpectIntEQ(WOLFSSL_SUCCESS,
  39716. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39717. ExpectIntEQ(WOLFSSL_SUCCESS,
  39718. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39719. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  39720. /* force retry */
  39721. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39722. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39723. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  39724. ExpectNotNull(bio = BIO_new(BIO_f_ssl()));
  39725. ExpectIntEQ(BIO_set_ssl(bio, ssl, BIO_CLOSE), 1);
  39726. if (EXPECT_FAIL()) {
  39727. wolfSSL_free(ssl);
  39728. ssl = NULL;
  39729. }
  39730. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  39731. ExpectIntNE(BIO_should_retry(bio), 0);
  39732. /* now perform successful connection */
  39733. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  39734. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  39735. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  39736. ret = wolfSSL_get_error(ssl, -1);
  39737. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  39738. ExpectIntNE(BIO_should_retry(bio), 0);
  39739. }
  39740. else {
  39741. ExpectIntEQ(BIO_should_retry(bio), 0);
  39742. }
  39743. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  39744. XSTRLEN("I hear you fa shizzle!")), 0);
  39745. BIO_free(bio);
  39746. wolfSSL_CTX_free(ctx);
  39747. CloseSocket(sockfd);
  39748. join_thread(serverThread);
  39749. FreeTcpReady(&ready);
  39750. #ifdef WOLFSSL_TIRTOS
  39751. fdOpenSession(Task_self());
  39752. #endif
  39753. res = EXPECT_RESULT();
  39754. #endif
  39755. return res;
  39756. }
  39757. static int test_wolfSSL_BIO_connect(void)
  39758. {
  39759. int res = TEST_SKIPPED;
  39760. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39761. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  39762. EXPECT_DECLS;
  39763. tcp_ready ready;
  39764. func_args server_args;
  39765. THREAD_TYPE serverThread;
  39766. BIO *tcpBio = NULL;
  39767. BIO *sslBio = NULL;
  39768. SSL_CTX* ctx = NULL;
  39769. SSL *ssl = NULL;
  39770. SSL *sslPtr;
  39771. char msg[] = "hello wolfssl!";
  39772. char reply[30];
  39773. char buff[10] = {0};
  39774. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39775. ExpectIntEQ(WOLFSSL_SUCCESS,
  39776. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39777. ExpectIntEQ(WOLFSSL_SUCCESS,
  39778. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39779. ExpectIntEQ(WOLFSSL_SUCCESS,
  39780. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39781. /* Setup server */
  39782. XMEMSET(&server_args, 0, sizeof(func_args));
  39783. StartTCP();
  39784. InitTcpReady(&ready);
  39785. #if defined(USE_WINDOWS_API)
  39786. /* use RNG to get random port if using windows */
  39787. ready.port = GetRandomPort();
  39788. #endif
  39789. server_args.signal = &ready;
  39790. start_thread(test_server_nofail, &server_args, &serverThread);
  39791. wait_tcp_ready(&server_args);
  39792. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39793. /* Start the test proper */
  39794. /* Setup the TCP BIO */
  39795. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  39796. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  39797. /* Setup the SSL object */
  39798. ExpectNotNull(ssl = SSL_new(ctx));
  39799. SSL_set_connect_state(ssl);
  39800. /* Setup the SSL BIO */
  39801. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  39802. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  39803. if (EXPECT_FAIL()) {
  39804. wolfSSL_free(ssl);
  39805. }
  39806. /* Verify that BIO_get_ssl works. */
  39807. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  39808. ExpectPtrEq(ssl, sslPtr);
  39809. /* Link BIO's so that sslBio uses tcpBio for IO */
  39810. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  39811. /* Do TCP connect */
  39812. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39813. /* Do TLS handshake */
  39814. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39815. /* Test writing */
  39816. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39817. /* Expect length of default wolfSSL reply */
  39818. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39819. /* Clean it all up */
  39820. BIO_free_all(sslBio);
  39821. /* Server clean up */
  39822. join_thread(serverThread);
  39823. FreeTcpReady(&ready);
  39824. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  39825. * after. */
  39826. XMEMSET(&server_args, 0, sizeof(func_args));
  39827. StartTCP();
  39828. InitTcpReady(&ready);
  39829. #if defined(USE_WINDOWS_API)
  39830. /* use RNG to get random port if using windows */
  39831. ready.port = GetRandomPort();
  39832. #endif
  39833. server_args.signal = &ready;
  39834. start_thread(test_server_nofail, &server_args, &serverThread);
  39835. wait_tcp_ready(&server_args);
  39836. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39837. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  39838. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  39839. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  39840. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39841. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39842. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39843. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39844. /* Attempt to close the TLS connection gracefully. */
  39845. BIO_ssl_shutdown(sslBio);
  39846. BIO_free_all(sslBio);
  39847. join_thread(serverThread);
  39848. FreeTcpReady(&ready);
  39849. SSL_CTX_free(ctx);
  39850. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39851. wc_ecc_fp_free(); /* free per thread cache */
  39852. #endif
  39853. res = EXPECT_RESULT();
  39854. #endif
  39855. return res;
  39856. }
  39857. static int test_wolfSSL_BIO_tls(void)
  39858. {
  39859. int res = TEST_SKIPPED;
  39860. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  39861. EXPECT_DECLS;
  39862. SSL_CTX* ctx = NULL;
  39863. SSL *ssl = NULL;
  39864. BIO *readBio = NULL;
  39865. BIO *writeBio = NULL;
  39866. int ret;
  39867. int err = 0;
  39868. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39869. ExpectNotNull(ssl = SSL_new(ctx));
  39870. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  39871. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  39872. /* Qt reads data from write-bio,
  39873. * then writes the read data into plain packet.
  39874. * Qt reads data from plain packet,
  39875. * then writes the read data into read-bio.
  39876. */
  39877. SSL_set_bio(ssl, readBio, writeBio);
  39878. do {
  39879. #ifdef WOLFSSL_ASYNC_CRYPT
  39880. if (err == WC_PENDING_E) {
  39881. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39882. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39883. }
  39884. #endif
  39885. ret = SSL_connect(ssl);
  39886. err = SSL_get_error(ssl, 0);
  39887. } while (err == WC_PENDING_E);
  39888. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  39889. /* in this use case, should return WANT READ
  39890. * so that Qt will read the data from plain packet for next state.
  39891. */
  39892. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  39893. SSL_free(ssl);
  39894. SSL_CTX_free(ctx);
  39895. res = EXPECT_RESULT();
  39896. #endif
  39897. return res;
  39898. }
  39899. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39900. defined(HAVE_HTTP_CLIENT)
  39901. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  39902. {
  39903. BIO* clientBio;
  39904. SSL* sslClient;
  39905. SSL_CTX* ctx;
  39906. char connectAddr[20]; /* IP + port */;
  39907. (void)args;
  39908. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  39909. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  39910. AssertIntEQ(BIO_do_connect(clientBio), 1);
  39911. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39912. AssertNotNull(sslClient = SSL_new(ctx));
  39913. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  39914. SSL_set_bio(sslClient, clientBio, clientBio);
  39915. AssertIntEQ(SSL_connect(sslClient), 1);
  39916. SSL_free(sslClient);
  39917. SSL_CTX_free(ctx);
  39918. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39919. wc_ecc_fp_free(); /* free per thread cache */
  39920. #endif
  39921. return 0;
  39922. }
  39923. #endif
  39924. static int test_wolfSSL_BIO_accept(void)
  39925. {
  39926. int res = TEST_SKIPPED;
  39927. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39928. defined(HAVE_HTTP_CLIENT)
  39929. EXPECT_DECLS;
  39930. BIO* serverBindBio = NULL;
  39931. BIO* serverAcceptBio = NULL;
  39932. SSL* sslServer = NULL;
  39933. SSL_CTX* ctx = NULL;
  39934. func_args args;
  39935. THREAD_TYPE thread;
  39936. char port[10]; /* 10 bytes should be enough to store the string
  39937. * representation of the port */
  39938. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  39939. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  39940. /* First BIO_do_accept binds the port */
  39941. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39942. XMEMSET(&args, 0, sizeof(func_args));
  39943. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  39944. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39945. /* Let's plug it into SSL to test */
  39946. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39947. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39948. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39949. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39950. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39951. ExpectNotNull(sslServer = SSL_new(ctx));
  39952. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  39953. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  39954. ExpectIntEQ(SSL_accept(sslServer), 1);
  39955. join_thread(thread);
  39956. BIO_free(serverBindBio);
  39957. SSL_free(sslServer);
  39958. SSL_CTX_free(ctx);
  39959. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39960. wc_ecc_fp_free(); /* free per thread cache */
  39961. #endif
  39962. res = EXPECT_RESULT();
  39963. #endif
  39964. return res;
  39965. }
  39966. static int test_wolfSSL_BIO_write(void)
  39967. {
  39968. int res = TEST_SKIPPED;
  39969. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  39970. EXPECT_DECLS;
  39971. BIO* bio = NULL;
  39972. BIO* bio64 = NULL;
  39973. BIO* bio_mem = NULL;
  39974. BIO* ptr = NULL;
  39975. int sz;
  39976. char msg[] = "conversion test";
  39977. char out[40];
  39978. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  39979. void* bufPtr = NULL;
  39980. BUF_MEM* buf = NULL;
  39981. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39982. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  39983. if (EXPECT_FAIL()) {
  39984. BIO_free(bio64);
  39985. }
  39986. /* now should convert to base64 then write to memory */
  39987. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39988. BIO_flush(bio);
  39989. /* test BIO chain */
  39990. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  39991. ExpectNotNull(buf);
  39992. ExpectIntEQ(buf->length, 25);
  39993. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  39994. ExpectPtrEq(buf->data, bufPtr);
  39995. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  39996. sz = sizeof(out);
  39997. XMEMSET(out, 0, sz);
  39998. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  39999. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40000. /* write then read should return the same message */
  40001. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40002. sz = sizeof(out);
  40003. XMEMSET(out, 0, sz);
  40004. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40005. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40006. /* now try encoding with no line ending */
  40007. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40008. #ifdef HAVE_EX_DATA
  40009. BIO_set_ex_data(bio64, 0, (void*) "data");
  40010. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40011. #endif
  40012. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40013. BIO_flush(bio);
  40014. sz = sizeof(out);
  40015. XMEMSET(out, 0, sz);
  40016. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40017. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40018. BIO_free_all(bio); /* frees bio64 also */
  40019. bio = NULL;
  40020. /* test with more than one bio64 in list */
  40021. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40022. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40023. if (EXPECT_FAIL()) {
  40024. BIO_free(bio64);
  40025. bio64 = NULL;
  40026. }
  40027. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40028. ExpectNotNull(BIO_push(bio64, bio_mem));
  40029. if (EXPECT_FAIL()) {
  40030. BIO_free(bio_mem);
  40031. }
  40032. /* now should convert to base64 when stored and then decode with read */
  40033. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40034. BIO_flush(bio);
  40035. sz = sizeof(out);
  40036. XMEMSET(out, 0, sz);
  40037. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40038. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40039. BIO_clear_flags(bio64, ~0);
  40040. BIO_set_retry_read(bio);
  40041. BIO_free_all(bio); /* frees bio64s also */
  40042. bio = NULL;
  40043. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40044. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40045. BIO_free(bio);
  40046. res = EXPECT_RESULT();
  40047. #endif
  40048. return res;
  40049. }
  40050. static int test_wolfSSL_BIO_printf(void)
  40051. {
  40052. int res = TEST_SKIPPED;
  40053. #if defined(OPENSSL_ALL)
  40054. EXPECT_DECLS;
  40055. BIO* bio = NULL;
  40056. int sz = 7;
  40057. char msg[] = "TLS 1.3 for the world";
  40058. char out[60];
  40059. char expected[] = "TLS 1.3 for the world : sz = 7";
  40060. XMEMSET(out, 0, sizeof(out));
  40061. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40062. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40063. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40064. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40065. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40066. BIO_free(bio);
  40067. res = EXPECT_RESULT();
  40068. #endif
  40069. return res;
  40070. }
  40071. static int test_wolfSSL_BIO_f_md(void)
  40072. {
  40073. int res = TEST_SKIPPED;
  40074. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40075. EXPECT_DECLS;
  40076. BIO* bio = NULL;
  40077. BIO* mem = NULL;
  40078. char msg[] = "message to hash";
  40079. char out[60];
  40080. EVP_MD_CTX* ctx = NULL;
  40081. const unsigned char testKey[] =
  40082. {
  40083. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40084. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40085. 0x0b, 0x0b, 0x0b, 0x0b
  40086. };
  40087. const char testData[] = "Hi There";
  40088. const unsigned char testResult[] =
  40089. {
  40090. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40091. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40092. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40093. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40094. };
  40095. const unsigned char expectedHash[] =
  40096. {
  40097. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40098. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40099. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40100. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40101. };
  40102. const unsigned char emptyHash[] =
  40103. {
  40104. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40105. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40106. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40107. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40108. };
  40109. unsigned char check[sizeof(testResult) + 1];
  40110. size_t checkSz = -1;
  40111. EVP_PKEY* key = NULL;
  40112. XMEMSET(out, 0, sizeof(out));
  40113. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40114. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40115. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40116. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40117. /* should not be able to write/read yet since just digest wrapper and no
  40118. * data is passing through the bio */
  40119. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40120. ExpectIntEQ(BIO_pending(bio), 0);
  40121. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40122. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40123. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40124. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40125. BIO_reset(bio);
  40126. /* append BIO mem to bio in order to read/write */
  40127. ExpectNotNull(bio = BIO_push(bio, mem));
  40128. XMEMSET(out, 0, sizeof(out));
  40129. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40130. ExpectIntEQ(BIO_pending(bio), 16);
  40131. /* this just reads the message and does not hash it (gets calls final) */
  40132. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  40133. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40134. /* create a message digest using BIO */
  40135. XMEMSET(out, 0, sizeof(out));
  40136. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  40137. ExpectIntEQ(BIO_pending(mem), 16);
  40138. ExpectIntEQ(BIO_pending(bio), 16);
  40139. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40140. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  40141. BIO_free(bio);
  40142. bio = NULL;
  40143. BIO_free(mem);
  40144. mem = NULL;
  40145. /* test with HMAC */
  40146. XMEMSET(out, 0, sizeof(out));
  40147. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40148. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40149. BIO_get_md_ctx(bio, &ctx);
  40150. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  40151. (int)sizeof(testKey)));
  40152. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  40153. ExpectNotNull(bio = BIO_push(bio, mem));
  40154. BIO_write(bio, testData, (int)strlen(testData));
  40155. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  40156. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  40157. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  40158. EVP_PKEY_free(key);
  40159. BIO_free(bio);
  40160. BIO_free(mem);
  40161. res = EXPECT_RESULT();
  40162. #endif
  40163. return res;
  40164. }
  40165. static int test_wolfSSL_BIO_up_ref(void)
  40166. {
  40167. int res = TEST_SKIPPED;
  40168. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40169. EXPECT_DECLS;
  40170. BIO* bio = NULL;
  40171. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40172. ExpectIntEQ(BIO_up_ref(NULL), 0);
  40173. ExpectIntEQ(BIO_up_ref(bio), 1);
  40174. BIO_free(bio);
  40175. ExpectIntEQ(BIO_up_ref(bio), 1);
  40176. BIO_free(bio);
  40177. BIO_free(bio);
  40178. res = EXPECT_RESULT();
  40179. #endif
  40180. return res;
  40181. }
  40182. static int test_wolfSSL_BIO_reset(void)
  40183. {
  40184. int res = TEST_SKIPPED;
  40185. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40186. EXPECT_DECLS;
  40187. BIO* bio = NULL;
  40188. byte buf[16];
  40189. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  40190. (word32)XSTRLEN("secure your data")));
  40191. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  40192. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  40193. XMEMSET(buf, 0, 16);
  40194. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  40195. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  40196. /* You cannot write to MEM BIO with read-only mode. */
  40197. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  40198. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  40199. XMEMSET(buf, 0, 16);
  40200. ExpectIntEQ(BIO_reset(bio), 0);
  40201. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  40202. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  40203. BIO_free(bio);
  40204. res = EXPECT_RESULT();
  40205. #endif
  40206. return res;
  40207. }
  40208. #endif /* !NO_BIO */
  40209. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40210. /* test that the callback arg is correct */
  40211. static int certCbArg = 0;
  40212. static int clientCertCb(WOLFSSL* ssl, void* arg)
  40213. {
  40214. if (ssl == NULL || arg != &certCbArg)
  40215. return 0;
  40216. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  40217. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40218. return 0;
  40219. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  40220. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40221. return 0;
  40222. return 1;
  40223. }
  40224. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  40225. {
  40226. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  40227. return TEST_SUCCESS;
  40228. }
  40229. /**
  40230. * This is only done because test_client_nofail has no way to stop
  40231. * certificate and key loading
  40232. */
  40233. static int clientCertClearCb(WOLFSSL* ssl)
  40234. {
  40235. /* Clear the loaded certs to force the callbacks to set them up */
  40236. SSL_certs_clear(ssl);
  40237. return TEST_SUCCESS;
  40238. }
  40239. static int serverCertCb(WOLFSSL* ssl, void* arg)
  40240. {
  40241. if (ssl == NULL || arg != &certCbArg)
  40242. return 0;
  40243. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  40244. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40245. return 0;
  40246. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  40247. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40248. return 0;
  40249. return 1;
  40250. }
  40251. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  40252. {
  40253. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  40254. return TEST_SUCCESS;
  40255. }
  40256. /**
  40257. * This is only done because test_server_nofail has no way to stop
  40258. * certificate and key loading
  40259. */
  40260. static int serverCertClearCb(WOLFSSL* ssl)
  40261. {
  40262. /* Clear the loaded certs to force the callbacks to set them up */
  40263. SSL_certs_clear(ssl);
  40264. return TEST_SUCCESS;
  40265. }
  40266. #endif
  40267. static int test_wolfSSL_cert_cb(void)
  40268. {
  40269. int res = TEST_SKIPPED;
  40270. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40271. EXPECT_DECLS;
  40272. test_ssl_cbf func_cb_client;
  40273. test_ssl_cbf func_cb_server;
  40274. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  40275. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  40276. func_cb_client.ctx_ready = clientCertSetupCb;
  40277. func_cb_client.ssl_ready = clientCertClearCb;
  40278. func_cb_server.ctx_ready = serverCertSetupCb;
  40279. func_cb_server.ssl_ready = serverCertClearCb;
  40280. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40281. &func_cb_server, NULL), TEST_SUCCESS);
  40282. res = EXPECT_RESULT();
  40283. #endif
  40284. return res;
  40285. }
  40286. static int test_wolfSSL_SESSION(void)
  40287. {
  40288. int res = TEST_SKIPPED;
  40289. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40290. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40291. !defined(NO_SESSION_CACHE)
  40292. WOLFSSL* ssl;
  40293. WOLFSSL_CTX* ctx;
  40294. WOLFSSL_SESSION* sess;
  40295. WOLFSSL_SESSION* sess_copy;
  40296. #ifdef OPENSSL_EXTRA
  40297. #ifdef HAVE_EXT_CACHE
  40298. unsigned char* sessDer = NULL;
  40299. unsigned char* ptr = NULL;
  40300. int sz;
  40301. #endif
  40302. const unsigned char context[] = "user app context";
  40303. unsigned int contextSz = (unsigned int)sizeof(context);
  40304. #endif
  40305. int ret, err;
  40306. SOCKET_T sockfd;
  40307. tcp_ready ready;
  40308. func_args server_args;
  40309. THREAD_TYPE serverThread;
  40310. char msg[80];
  40311. const char* sendGET = "GET";
  40312. /* TLS v1.3 requires session tickets */
  40313. /* CHACHA and POLY1305 required for myTicketEncCb */
  40314. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  40315. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  40316. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  40317. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  40318. #else
  40319. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40320. #endif
  40321. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40322. WOLFSSL_FILETYPE_PEM));
  40323. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40324. WOLFSSL_FILETYPE_PEM));
  40325. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40326. WOLFSSL_SUCCESS);
  40327. #ifdef WOLFSSL_ENCRYPTED_KEYS
  40328. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  40329. #endif
  40330. #ifdef HAVE_SESSION_TICKET
  40331. /* Use session tickets, for ticket tests below */
  40332. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  40333. #endif
  40334. XMEMSET(&server_args, 0, sizeof(func_args));
  40335. #ifdef WOLFSSL_TIRTOS
  40336. fdOpenSession(Task_self());
  40337. #endif
  40338. StartTCP();
  40339. InitTcpReady(&ready);
  40340. #if defined(USE_WINDOWS_API)
  40341. /* use RNG to get random port if using windows */
  40342. ready.port = GetRandomPort();
  40343. #endif
  40344. server_args.signal = &ready;
  40345. start_thread(test_server_nofail, &server_args, &serverThread);
  40346. wait_tcp_ready(&server_args);
  40347. /* client connection */
  40348. ssl = wolfSSL_new(ctx);
  40349. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  40350. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40351. #ifdef WOLFSSL_ASYNC_CRYPT
  40352. err = 0; /* Reset error */
  40353. #endif
  40354. do {
  40355. #ifdef WOLFSSL_ASYNC_CRYPT
  40356. if (err == WC_PENDING_E) {
  40357. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40358. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40359. }
  40360. #endif
  40361. ret = wolfSSL_connect(ssl);
  40362. err = wolfSSL_get_error(ssl, 0);
  40363. } while (err == WC_PENDING_E);
  40364. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  40365. #ifdef WOLFSSL_ASYNC_CRYPT
  40366. err = 0; /* Reset error */
  40367. #endif
  40368. do {
  40369. #ifdef WOLFSSL_ASYNC_CRYPT
  40370. if (err == WC_PENDING_E) {
  40371. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40372. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40373. }
  40374. #endif
  40375. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  40376. err = wolfSSL_get_error(ssl, 0);
  40377. } while (err == WC_PENDING_E);
  40378. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  40379. #ifdef WOLFSSL_ASYNC_CRYPT
  40380. err = 0; /* Reset error */
  40381. #endif
  40382. do {
  40383. #ifdef WOLFSSL_ASYNC_CRYPT
  40384. if (err == WC_PENDING_E) {
  40385. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40386. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40387. }
  40388. #endif
  40389. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  40390. err = wolfSSL_get_error(ssl, 0);
  40391. } while (err == WC_PENDING_E);
  40392. AssertIntEQ(ret, 23);
  40393. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  40394. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  40395. #ifdef HAVE_EXT_CACHE
  40396. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  40397. * HAVE_EXT_CACHE we get new objects each time */
  40398. #endif
  40399. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40400. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  40401. sess = wolfSSL_get_session(ssl);
  40402. #ifdef OPENSSL_EXTRA
  40403. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  40404. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  40405. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  40406. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  40407. #ifdef HAVE_SESSION_TICKET
  40408. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  40409. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  40410. SESSION_TICKET_HINT_DEFAULT);
  40411. #else
  40412. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  40413. #endif
  40414. #else
  40415. (void)sess;
  40416. #endif /* OPENSSL_EXTRA */
  40417. /* Retain copy of the session for later testing */
  40418. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  40419. wolfSSL_shutdown(ssl);
  40420. wolfSSL_free(ssl);
  40421. join_thread(serverThread);
  40422. FreeTcpReady(&ready);
  40423. #ifdef WOLFSSL_TIRTOS
  40424. fdOpenSession(Task_self());
  40425. #endif
  40426. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  40427. {
  40428. X509 *x509;
  40429. char buf[30];
  40430. int bufSz;
  40431. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  40432. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  40433. X509_get_subject_name(x509), NID_organizationalUnitName,
  40434. buf, sizeof(buf))), 0);
  40435. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  40436. if (bufSz == 7) {
  40437. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  40438. }
  40439. if (bufSz == 16) {
  40440. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  40441. }
  40442. }
  40443. #endif
  40444. #ifdef HAVE_EXT_CACHE
  40445. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  40446. wolfSSL_SESSION_free(sess_copy);
  40447. sess_copy = NULL;
  40448. #endif
  40449. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  40450. /* get session from DER and update the timeout */
  40451. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  40452. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  40453. wolfSSL_SESSION_free(sess);
  40454. sess = NULL;
  40455. ptr = sessDer;
  40456. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  40457. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  40458. (const unsigned char**)&ptr, sz));
  40459. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  40460. sessDer = NULL;
  40461. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  40462. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40463. #endif
  40464. /* successful set session test */
  40465. AssertNotNull(ssl = wolfSSL_new(ctx));
  40466. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  40467. #ifdef HAVE_SESSION_TICKET
  40468. /* Test set/get session ticket */
  40469. {
  40470. const char* ticket = "This is a session ticket";
  40471. char buf[64] = {0};
  40472. word32 bufSz = (word32)sizeof(buf);
  40473. AssertIntEQ(SSL_SUCCESS,
  40474. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  40475. (word32)XSTRLEN(ticket)));
  40476. AssertIntEQ(SSL_SUCCESS,
  40477. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  40478. AssertStrEQ(ticket, buf);
  40479. }
  40480. #endif
  40481. #ifdef OPENSSL_EXTRA
  40482. /* session timeout case */
  40483. /* make the session to be expired */
  40484. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  40485. XSLEEP_MS(1200);
  40486. /* SSL_set_session should reject specified session but return success
  40487. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  40488. */
  40489. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40490. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  40491. #else
  40492. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  40493. #endif
  40494. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40495. /* fail case with miss match session context IDs (use compatibility API) */
  40496. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  40497. SSL_SUCCESS);
  40498. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40499. wolfSSL_free(ssl);
  40500. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  40501. SSL_FAILURE);
  40502. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  40503. SSL_SUCCESS);
  40504. AssertNotNull(ssl = wolfSSL_new(ctx));
  40505. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40506. #endif /* OPENSSL_EXTRA */
  40507. wolfSSL_free(ssl);
  40508. wolfSSL_SESSION_free(sess);
  40509. wolfSSL_CTX_free(ctx);
  40510. res = TEST_RES_CHECK(1);
  40511. #endif
  40512. return res;
  40513. }
  40514. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40515. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40516. static int clientSessRemCountMalloc = 0;
  40517. static int serverSessRemCountMalloc = 0;
  40518. static int clientSessRemCountFree = 0;
  40519. static int serverSessRemCountFree = 0;
  40520. static WOLFSSL_CTX* serverSessCtx = NULL;
  40521. static WOLFSSL_SESSION* serverSess = NULL;
  40522. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40523. !defined(NO_SESSION_CACHE_REF)
  40524. static WOLFSSL_CTX* clientSessCtx = NULL;
  40525. static WOLFSSL_SESSION* clientSess = NULL;
  40526. #endif
  40527. static int serverSessRemIdx = 3;
  40528. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  40529. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  40530. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  40531. {
  40532. int* side;
  40533. (void)ctx;
  40534. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  40535. if (side != NULL) {
  40536. if (*side == WOLFSSL_CLIENT_END)
  40537. clientSessRemCountFree++;
  40538. else
  40539. serverSessRemCountFree++;
  40540. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  40541. }
  40542. }
  40543. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  40544. {
  40545. EXPECT_DECLS;
  40546. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  40547. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  40548. !defined(NO_SESSION_CACHE_REF)
  40549. /* Allow downgrade, set min version, and disable TLS 1.3.
  40550. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  40551. * reference to the session cache. But with WOLFSSL_TLS13 and without
  40552. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  40553. * session in the cache. In this case we need to downgrade to previous
  40554. * versions to just use the legacy session ID field. */
  40555. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  40556. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  40557. SSL_SUCCESS);
  40558. #endif
  40559. return EXPECT_RESULT();
  40560. }
  40561. static int SessRemSslSetupCb(WOLFSSL* ssl)
  40562. {
  40563. EXPECT_DECLS;
  40564. int* side;
  40565. if (EXPECT_SUCCESS()) {
  40566. if (SSL_is_server(ssl)) {
  40567. side = &sessRemCtx_Server;
  40568. serverSessRemCountMalloc++;
  40569. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  40570. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  40571. SSL_SUCCESS);
  40572. }
  40573. else {
  40574. side = &sessRemCtx_Client;
  40575. clientSessRemCountMalloc++;
  40576. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40577. !defined(NO_SESSION_CACHE_REF)
  40578. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  40579. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  40580. SSL_SUCCESS);
  40581. #endif
  40582. }
  40583. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  40584. serverSessRemIdx, side), SSL_SUCCESS);
  40585. }
  40586. return EXPECT_RESULT();
  40587. }
  40588. #endif
  40589. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  40590. {
  40591. int res = TEST_SKIPPED;
  40592. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40593. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40594. EXPECT_DECLS;
  40595. /* Check that the remove callback gets called for external data in a
  40596. * session object */
  40597. test_ssl_cbf func_cb;
  40598. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  40599. func_cb.ctx_ready = SessRemCtxSetupCb;
  40600. func_cb.on_result = SessRemSslSetupCb;
  40601. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  40602. NULL), TEST_SUCCESS);
  40603. /* Both should have been allocated */
  40604. ExpectIntEQ(clientSessRemCountMalloc, 1);
  40605. ExpectIntEQ(serverSessRemCountMalloc, 1);
  40606. /* This should not be called yet. Session wasn't evicted from cache yet. */
  40607. ExpectIntEQ(clientSessRemCountFree, 0);
  40608. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40609. !defined(NO_SESSION_CACHE_REF)
  40610. /* Force a cache lookup */
  40611. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40612. /* Force a cache update */
  40613. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  40614. /* This should set the timeout to 0 and call the remove callback from within
  40615. * the session cache. */
  40616. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  40617. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40618. ExpectIntEQ(clientSessRemCountFree, 1);
  40619. #endif
  40620. /* Server session is in the cache so ex_data isn't free'd with the SSL
  40621. * object */
  40622. ExpectIntEQ(serverSessRemCountFree, 0);
  40623. /* Force a cache lookup */
  40624. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40625. /* Force a cache update */
  40626. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  40627. /* This should set the timeout to 0 and call the remove callback from within
  40628. * the session cache. */
  40629. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  40630. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40631. ExpectIntEQ(serverSessRemCountFree, 1);
  40632. /* Need to free the references that we kept */
  40633. SSL_CTX_free(serverSessCtx);
  40634. SSL_SESSION_free(serverSess);
  40635. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40636. !defined(NO_SESSION_CACHE_REF)
  40637. SSL_CTX_free(clientSessCtx);
  40638. SSL_SESSION_free(clientSess);
  40639. #endif
  40640. res = EXPECT_RESULT();
  40641. #endif
  40642. return res;
  40643. }
  40644. static int test_wolfSSL_ticket_keys(void)
  40645. {
  40646. int res = TEST_SKIPPED;
  40647. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  40648. !defined(NO_WOLFSSL_SERVER)
  40649. WOLFSSL_CTX* ctx;
  40650. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  40651. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40652. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  40653. WOLFSSL_FAILURE);
  40654. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  40655. WOLFSSL_FAILURE);
  40656. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  40657. WOLFSSL_FAILURE);
  40658. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  40659. WOLFSSL_FAILURE);
  40660. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40661. WOLFSSL_FAILURE);
  40662. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40663. WOLFSSL_FAILURE);
  40664. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40665. WOLFSSL_FAILURE);
  40666. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  40667. WOLFSSL_FAILURE);
  40668. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  40669. WOLFSSL_FAILURE);
  40670. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  40671. WOLFSSL_FAILURE);
  40672. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  40673. WOLFSSL_FAILURE);
  40674. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40675. WOLFSSL_FAILURE);
  40676. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40677. WOLFSSL_FAILURE);
  40678. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40679. WOLFSSL_FAILURE);
  40680. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40681. WOLFSSL_SUCCESS);
  40682. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40683. WOLFSSL_SUCCESS);
  40684. wolfSSL_CTX_free(ctx);
  40685. res = TEST_RES_CHECK(1);
  40686. #endif
  40687. return res;
  40688. }
  40689. #ifndef NO_BIO
  40690. static int test_wolfSSL_d2i_PUBKEY(void)
  40691. {
  40692. int res = TEST_SKIPPED;
  40693. #if defined(OPENSSL_EXTRA)
  40694. BIO* bio;
  40695. EVP_PKEY* pkey;
  40696. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40697. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  40698. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  40699. /* RSA PUBKEY test */
  40700. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  40701. sizeof_client_keypub_der_2048), 0);
  40702. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40703. EVP_PKEY_free(pkey);
  40704. #endif
  40705. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  40706. /* ECC PUBKEY test */
  40707. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  40708. sizeof_ecc_clikeypub_der_256), 0);
  40709. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40710. EVP_PKEY_free(pkey);
  40711. #endif
  40712. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  40713. /* DSA PUBKEY test */
  40714. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  40715. sizeof_dsa_pub_key_der_2048), 0);
  40716. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40717. EVP_PKEY_free(pkey);
  40718. #endif
  40719. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  40720. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  40721. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  40722. (HAVE_FIPS_VERSION > 2))
  40723. /* DH PUBKEY test */
  40724. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  40725. sizeof_dh_pub_key_der_2048), 0);
  40726. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40727. EVP_PKEY_free(pkey);
  40728. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40729. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  40730. BIO_free(bio);
  40731. (void)pkey;
  40732. res = TEST_RES_CHECK(1);
  40733. #endif
  40734. return res;
  40735. }
  40736. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  40737. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  40738. {
  40739. BIO* bio = NULL;
  40740. EVP_PKEY* pkey = NULL;
  40741. #ifndef NO_RSA
  40742. #endif
  40743. WOLFSSL_CTX* ctx;
  40744. #if defined(WOLFSSL_KEY_GEN)
  40745. unsigned char buff[4096];
  40746. unsigned char* bufPtr = buff;
  40747. #endif
  40748. /* test creating new EVP_PKEY with bad arg */
  40749. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  40750. /* test loading RSA key using BIO */
  40751. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40752. {
  40753. XFILE file;
  40754. const char* fname = "./certs/server-key.der";
  40755. size_t sz;
  40756. byte* buf;
  40757. file = XFOPEN(fname, "rb");
  40758. AssertTrue((file != XBADFILE));
  40759. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40760. sz = XFTELL(file);
  40761. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40762. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40763. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  40764. XFCLOSE(file);
  40765. /* Test using BIO new mem and loading DER private key */
  40766. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40767. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40768. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40769. BIO_free(bio);
  40770. bio = NULL;
  40771. EVP_PKEY_free(pkey);
  40772. pkey = NULL;
  40773. }
  40774. #endif
  40775. /* test loading ECC key using BIO */
  40776. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  40777. {
  40778. XFILE file;
  40779. const char* fname = "./certs/ecc-key.der";
  40780. size_t sz;
  40781. byte* buf;
  40782. file = XFOPEN(fname, "rb");
  40783. AssertTrue((file != XBADFILE));
  40784. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40785. sz = XFTELL(file);
  40786. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40787. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40788. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  40789. XFCLOSE(file);
  40790. /* Test using BIO new mem and loading DER private key */
  40791. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40792. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40793. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40794. BIO_free(bio);
  40795. bio = NULL;
  40796. EVP_PKEY_free(pkey);
  40797. pkey = NULL;
  40798. }
  40799. #endif
  40800. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40801. #ifndef NO_WOLFSSL_SERVER
  40802. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  40803. #else
  40804. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  40805. #endif
  40806. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40807. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  40808. {
  40809. RSA* rsa = NULL;
  40810. /* Tests bad parameters */
  40811. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  40812. /* RSA not set yet, expecting to fail*/
  40813. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  40814. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  40815. /* set RSA using bio*/
  40816. AssertIntGT(BIO_write(bio, client_key_der_2048,
  40817. sizeof_client_key_der_2048), 0);
  40818. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  40819. AssertNotNull(rsa);
  40820. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  40821. /*i2d RSAprivate key tests */
  40822. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  40823. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  40824. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40825. sizeof_client_key_der_2048);
  40826. bufPtr -= sizeof_client_key_der_2048;
  40827. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40828. sizeof_client_key_der_2048), 0);
  40829. bufPtr = NULL;
  40830. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40831. sizeof_client_key_der_2048);
  40832. AssertNotNull(bufPtr);
  40833. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40834. sizeof_client_key_der_2048), 0);
  40835. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  40836. RSA_free(rsa);
  40837. rsa = RSA_new();
  40838. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  40839. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  40840. RSA_free(rsa);
  40841. }
  40842. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  40843. SSL_CTX_free(ctx);
  40844. ctx = NULL;
  40845. BIO_free(bio);
  40846. bio = NULL;
  40847. return TEST_RES_CHECK(1);
  40848. }
  40849. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  40850. #endif /* !NO_BIO */
  40851. static int test_wolfSSL_sk_GENERAL_NAME(void)
  40852. {
  40853. int res = TEST_SKIPPED;
  40854. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40855. !defined(NO_RSA)
  40856. X509* x509;
  40857. GENERAL_NAME* gn;
  40858. unsigned char buf[4096];
  40859. const unsigned char* bufPt;
  40860. int bytes, i;
  40861. int j;
  40862. XFILE f;
  40863. STACK_OF(GENERAL_NAME)* sk;
  40864. f = XFOPEN(cliCertDerFileExt, "rb");
  40865. AssertTrue((f != XBADFILE));
  40866. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40867. XFCLOSE(f);
  40868. for (j = 0; j < 2; ++j) {
  40869. bufPt = buf;
  40870. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40871. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40872. NID_subject_alt_name, NULL, NULL));
  40873. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  40874. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  40875. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  40876. switch (gn->type) {
  40877. case GEN_DNS:
  40878. fprintf(stderr, "found type GEN_DNS\n");
  40879. break;
  40880. case GEN_EMAIL:
  40881. fprintf(stderr, "found type GEN_EMAIL\n");
  40882. break;
  40883. case GEN_URI:
  40884. fprintf(stderr, "found type GEN_URI\n");
  40885. break;
  40886. }
  40887. }
  40888. X509_free(x509);
  40889. if (j == 0) {
  40890. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40891. }
  40892. else {
  40893. /*
  40894. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  40895. * it was supposed to. This is a regression test for that bug.
  40896. */
  40897. GENERAL_NAMES_free(sk);
  40898. }
  40899. }
  40900. res = TEST_RES_CHECK(1);
  40901. #endif
  40902. return res;
  40903. }
  40904. static int test_wolfSSL_GENERAL_NAME_print(void)
  40905. {
  40906. int res = TEST_SKIPPED;
  40907. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  40908. X509* x509;
  40909. GENERAL_NAME* gn;
  40910. unsigned char buf[4096];
  40911. const unsigned char* bufPt;
  40912. int bytes;
  40913. XFILE f;
  40914. STACK_OF(GENERAL_NAME)* sk;
  40915. BIO* out;
  40916. unsigned char outbuf[128];
  40917. X509_EXTENSION* ext;
  40918. AUTHORITY_INFO_ACCESS* aia;
  40919. ACCESS_DESCRIPTION* ad;
  40920. const unsigned char v4Addr[] = {192,168,53,1};
  40921. const unsigned char v6Addr[] =
  40922. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  40923. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  40924. const unsigned char email[] =
  40925. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  40926. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  40927. const char* dnsStr = "DNS:example.com";
  40928. const char* uriStr = "URI:http://127.0.0.1:22220";
  40929. const char* v4addStr = "IP Address:192.168.53.1";
  40930. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  40931. const char* emailStr = "email:info@wolfssl.com";
  40932. const char* othrStr = "othername:<unsupported>";
  40933. const char* x400Str = "X400Name:<unsupported>";
  40934. const char* ediStr = "EdiPartyName:<unsupported>";
  40935. /* BIO to output */
  40936. AssertNotNull(out = BIO_new(BIO_s_mem()));
  40937. /* test for NULL param */
  40938. gn = NULL;
  40939. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  40940. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  40941. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  40942. /* test for GEN_DNS */
  40943. f = XFOPEN(cliCertDerFileExt, "rb");
  40944. AssertTrue((f != XBADFILE));
  40945. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40946. XFCLOSE(f);
  40947. bufPt = buf;
  40948. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40949. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40950. NID_subject_alt_name, NULL, NULL));
  40951. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  40952. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40953. XMEMSET(outbuf,0,sizeof(outbuf));
  40954. BIO_read(out, outbuf, sizeof(outbuf));
  40955. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40956. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40957. X509_free(x509);
  40958. /* test for GEN_URI */
  40959. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  40960. AssertTrue((f != XBADFILE));
  40961. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40962. XFCLOSE(f);
  40963. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  40964. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40965. AssertNotNull(aia);
  40966. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  40967. gn = ad->location;
  40968. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40969. XMEMSET(outbuf,0,sizeof(outbuf));
  40970. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40971. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  40972. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40973. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40974. AssertNotNull(aia);
  40975. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  40976. X509_free(x509);
  40977. /* test for GEN_IPADD */
  40978. /* ip v4 address */
  40979. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40980. gn->type = GEN_IPADD;
  40981. gn->d.iPAddress->length = sizeof(v4Addr);
  40982. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  40983. sizeof(v4Addr)), 1);
  40984. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40985. XMEMSET(outbuf,0,sizeof(outbuf));
  40986. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40987. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  40988. GENERAL_NAME_free(gn);
  40989. /* ip v6 address */
  40990. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40991. gn->type = GEN_IPADD;
  40992. gn->d.iPAddress->length = sizeof(v6Addr);
  40993. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  40994. sizeof(v6Addr)), 1);
  40995. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40996. XMEMSET(outbuf,0,sizeof(outbuf));
  40997. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40998. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  40999. GENERAL_NAME_free(gn);
  41000. /* test for GEN_EMAIL */
  41001. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41002. gn->type = GEN_EMAIL;
  41003. gn->d.rfc822Name->length = sizeof(email);
  41004. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  41005. sizeof(email)), 1);
  41006. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  41007. XMEMSET(outbuf,0,sizeof(outbuf));
  41008. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41009. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  41010. GENERAL_NAME_free(gn);
  41011. /* test for GEN_OTHERNAME */
  41012. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41013. gn->type = GEN_OTHERNAME;
  41014. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  41015. XMEMSET(outbuf,0,sizeof(outbuf));
  41016. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41017. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  41018. GENERAL_NAME_free(gn);
  41019. /* test for GEN_X400 */
  41020. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41021. gn->type = GEN_X400;
  41022. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  41023. XMEMSET(outbuf,0,sizeof(outbuf));
  41024. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41025. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  41026. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41027. gn->type = GEN_IA5;
  41028. GENERAL_NAME_free(gn);
  41029. /* test for GEN_EDIPARTY */
  41030. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41031. gn->type = GEN_EDIPARTY;
  41032. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  41033. XMEMSET(outbuf,0,sizeof(outbuf));
  41034. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41035. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  41036. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41037. gn->type = GEN_IA5;
  41038. GENERAL_NAME_free(gn);
  41039. BIO_free(out);
  41040. res = TEST_RES_CHECK(1);
  41041. #endif /* OPENSSL_ALL */
  41042. return res;
  41043. }
  41044. static int test_wolfSSL_sk_DIST_POINT(void)
  41045. {
  41046. int res = TEST_SKIPPED;
  41047. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41048. !defined(NO_RSA)
  41049. X509* x509;
  41050. unsigned char buf[4096];
  41051. const unsigned char* bufPt;
  41052. int bytes, i, j;
  41053. XFILE f;
  41054. DIST_POINT* dp;
  41055. DIST_POINT_NAME* dpn;
  41056. GENERAL_NAME* gn;
  41057. ASN1_IA5STRING* uri;
  41058. STACK_OF(DIST_POINT)* dps;
  41059. STACK_OF(GENERAL_NAME)* gns;
  41060. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  41061. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  41062. AssertTrue((f != XBADFILE));
  41063. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41064. XFCLOSE(f);
  41065. bufPt = buf;
  41066. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41067. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  41068. NID_crl_distribution_points, NULL, NULL));
  41069. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  41070. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  41071. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  41072. AssertNotNull(dpn = dp->distpoint);
  41073. /* this should be type 0, fullname */
  41074. AssertIntEQ(dpn->type, 0);
  41075. gns = dp->distpoint->name.fullname;
  41076. AssertNotNull(gns);
  41077. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  41078. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  41079. gn = sk_GENERAL_NAME_value(gns, j);
  41080. AssertIntEQ(gn->type, GEN_URI);
  41081. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  41082. AssertNotNull(uri->data);
  41083. AssertIntGT(uri->length, 0);
  41084. }
  41085. }
  41086. X509_free(x509);
  41087. CRL_DIST_POINTS_free(dps);
  41088. res = TEST_RES_CHECK(1);
  41089. #endif
  41090. return res;
  41091. }
  41092. static int test_wolfSSL_MD4(void)
  41093. {
  41094. int res = TEST_SKIPPED;
  41095. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  41096. MD4_CTX md4;
  41097. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  41098. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  41099. "789012345678901234567890";
  41100. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  41101. "\xcc\x05\x36";
  41102. int msgSz = (int)XSTRLEN(msg);
  41103. XMEMSET(out, 0, sizeof(out));
  41104. MD4_Init(&md4);
  41105. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  41106. MD4_Final(out, &md4);
  41107. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  41108. res = TEST_RES_CHECK(1);
  41109. #endif
  41110. return res;
  41111. }
  41112. static int test_wolfSSL_verify_mode(void)
  41113. {
  41114. int res = TEST_SKIPPED;
  41115. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  41116. WOLFSSL* ssl;
  41117. WOLFSSL_CTX* ctx;
  41118. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41119. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41120. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41121. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  41122. AssertNotNull(ssl = SSL_new(ctx));
  41123. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41124. SSL_free(ssl);
  41125. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41126. AssertNotNull(ssl = SSL_new(ctx));
  41127. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41128. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41129. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41130. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  41131. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41132. SSL_free(ssl);
  41133. wolfSSL_CTX_set_verify(ctx,
  41134. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41135. AssertNotNull(ssl = SSL_new(ctx));
  41136. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41137. AssertIntEQ(SSL_get_verify_mode(ssl),
  41138. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41139. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  41140. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  41141. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41142. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41143. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41144. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41145. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41146. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41147. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  41148. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  41149. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  41150. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  41151. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  41152. #endif
  41153. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  41154. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41155. SSL_free(ssl);
  41156. SSL_CTX_free(ctx);
  41157. res = TEST_RES_CHECK(1);
  41158. #endif
  41159. return res;
  41160. }
  41161. static int test_wolfSSL_verify_depth(void)
  41162. {
  41163. int res = TEST_SKIPPED;
  41164. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41165. WOLFSSL* ssl;
  41166. WOLFSSL_CTX* ctx;
  41167. long depth;
  41168. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41169. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41170. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41171. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  41172. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  41173. AssertNotNull(ssl = SSL_new(ctx));
  41174. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  41175. SSL_free(ssl);
  41176. SSL_CTX_set_verify_depth(ctx, -1);
  41177. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  41178. SSL_CTX_set_verify_depth(ctx, 2);
  41179. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  41180. AssertNotNull(ssl = SSL_new(ctx));
  41181. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  41182. SSL_free(ssl);
  41183. SSL_CTX_free(ctx);
  41184. res = TEST_RES_CHECK(1);
  41185. #endif
  41186. return res;
  41187. }
  41188. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  41189. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  41190. * buffer of 64 bytes.
  41191. *
  41192. * returns the size of the digest buffer on success and a negative value on
  41193. * failure.
  41194. */
  41195. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  41196. {
  41197. HMAC_CTX ctx1;
  41198. HMAC_CTX ctx2;
  41199. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  41200. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  41201. unsigned char long_key[] =
  41202. "0123456789012345678901234567890123456789"
  41203. "0123456789012345678901234567890123456789"
  41204. "0123456789012345678901234567890123456789"
  41205. "0123456789012345678901234567890123456789";
  41206. unsigned char msg[] = "message to hash";
  41207. unsigned int digestSz = 64;
  41208. int keySz = sizeof(key);
  41209. int long_keySz = sizeof(long_key);
  41210. int msgSz = sizeof(msg);
  41211. unsigned char digest2[64];
  41212. unsigned int digestSz2 = 64;
  41213. HMAC_CTX_init(&ctx1);
  41214. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  41215. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41216. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  41217. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41218. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  41219. HMAC_CTX_cleanup(&ctx1);
  41220. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41221. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  41222. HMAC_CTX_cleanup(&ctx2);
  41223. AssertIntEQ(digestSz, digestSz2);
  41224. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  41225. /* test HMAC_Init with NULL key */
  41226. /* init after copy */
  41227. HMAC_CTX_init(&ctx1);
  41228. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  41229. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41230. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  41231. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  41232. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41233. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41234. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  41235. HMAC_CTX_cleanup(&ctx1);
  41236. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  41237. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41238. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41239. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  41240. HMAC_CTX_cleanup(&ctx2);
  41241. AssertIntEQ(digestSz, digestSz2);
  41242. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  41243. /* long key */
  41244. HMAC_CTX_init(&ctx1);
  41245. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  41246. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41247. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  41248. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  41249. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41250. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41251. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  41252. HMAC_CTX_cleanup(&ctx1);
  41253. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  41254. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41255. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41256. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  41257. HMAC_CTX_cleanup(&ctx2);
  41258. AssertIntEQ(digestSz, digestSz2);
  41259. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  41260. /* init before copy */
  41261. HMAC_CTX_init(&ctx1);
  41262. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  41263. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41264. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  41265. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  41266. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41267. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  41268. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  41269. HMAC_CTX_cleanup(&ctx1);
  41270. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41271. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  41272. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  41273. HMAC_CTX_cleanup(&ctx2);
  41274. AssertIntEQ(digestSz, digestSz2);
  41275. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  41276. return digestSz;
  41277. }
  41278. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  41279. static int test_wolfSSL_HMAC_CTX(void)
  41280. {
  41281. int res = TEST_SKIPPED;
  41282. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  41283. unsigned char digest[64];
  41284. int digestSz;
  41285. #ifndef NO_SHA
  41286. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  41287. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  41288. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  41289. #endif /* !NO_SHA */
  41290. #ifdef WOLFSSL_SHA224
  41291. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  41292. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  41293. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  41294. "\x02\x0E", digest, digestSz), 0);
  41295. #endif /* WOLFSSL_SHA224 */
  41296. #ifndef NO_SHA256
  41297. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  41298. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  41299. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  41300. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  41301. #endif /* !NO_SHA256 */
  41302. #ifdef WOLFSSL_SHA384
  41303. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  41304. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  41305. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  41306. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  41307. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  41308. digest, digestSz), 0);
  41309. #endif /* WOLFSSL_SHA384 */
  41310. #ifdef WOLFSSL_SHA512
  41311. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  41312. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  41313. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  41314. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  41315. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  41316. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  41317. digest, digestSz), 0);
  41318. #endif /* WOLFSSL_SHA512 */
  41319. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  41320. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  41321. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  41322. "\xE4\x98\xDD", digest, digestSz), 0);
  41323. #endif /* !NO_MD5 */
  41324. res = TEST_RES_CHECK(1);
  41325. #endif
  41326. return res;
  41327. }
  41328. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41329. static void sslMsgCb(int w, int version, int type, const void* buf,
  41330. size_t sz, SSL* ssl, void* arg)
  41331. {
  41332. int i;
  41333. unsigned char* pt = (unsigned char*)buf;
  41334. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  41335. (w)?"Writing":"Reading", (int)sz, version, type);
  41336. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  41337. fprintf(stderr, "\n");
  41338. (void)ssl;
  41339. (void)arg;
  41340. }
  41341. #endif /* OPENSSL_EXTRA */
  41342. static int test_wolfSSL_msg_callback(void)
  41343. {
  41344. int res = TEST_SKIPPED;
  41345. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41346. WOLFSSL* ssl;
  41347. WOLFSSL_CTX* ctx;
  41348. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41349. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41350. SSL_FILETYPE_PEM));
  41351. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41352. SSL_FILETYPE_PEM));
  41353. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41354. SSL_SUCCESS);
  41355. AssertNotNull(ssl = SSL_new(ctx));
  41356. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  41357. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  41358. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  41359. SSL_free(ssl);
  41360. SSL_CTX_free(ctx);
  41361. res = TEST_RES_CHECK(1);
  41362. #endif
  41363. return res;
  41364. }
  41365. static int test_wolfSSL_SHA(void)
  41366. {
  41367. int res = TEST_SKIPPED;
  41368. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  41369. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  41370. (!defined(HAVE_FIPS) || \
  41371. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  41372. {
  41373. const unsigned char in[] = "abc";
  41374. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  41375. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  41376. unsigned char out[WC_SHA_DIGEST_SIZE];
  41377. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  41378. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  41379. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  41380. /* SHA interface test */
  41381. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  41382. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  41383. AssertNotNull(SHA(in, 0, out));
  41384. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  41385. AssertNotNull(SHA(NULL, 0, out));
  41386. AssertNotNull(SHA(NULL, 0, NULL));
  41387. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  41388. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  41389. }
  41390. #endif
  41391. #if !defined(NO_SHA256)
  41392. {
  41393. const unsigned char in[] = "abc";
  41394. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  41395. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  41396. "\x15\xAD";
  41397. unsigned char out[WC_SHA256_DIGEST_SIZE];
  41398. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  41399. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  41400. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  41401. #else
  41402. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  41403. #endif
  41404. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  41405. }
  41406. #endif
  41407. #if defined(WOLFSSL_SHA384)
  41408. {
  41409. const unsigned char in[] = "abc";
  41410. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  41411. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  41412. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  41413. "\xc8\x25\xa7";
  41414. unsigned char out[WC_SHA384_DIGEST_SIZE];
  41415. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  41416. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  41417. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  41418. #else
  41419. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  41420. #endif
  41421. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  41422. }
  41423. #endif
  41424. #if defined(WOLFSSL_SHA512)
  41425. {
  41426. const unsigned char in[] = "abc";
  41427. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  41428. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  41429. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  41430. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  41431. "\xa5\x4c\xa4\x9f";
  41432. unsigned char out[WC_SHA512_DIGEST_SIZE];
  41433. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  41434. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  41435. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  41436. #else
  41437. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  41438. #endif
  41439. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  41440. }
  41441. #endif
  41442. res = TEST_RES_CHECK(1);
  41443. #endif
  41444. return res;
  41445. }
  41446. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  41447. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41448. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41449. static void binary_dump(void *ptr, int size)
  41450. {
  41451. #ifdef WOLFSSL_EVP_PRINT
  41452. int i = 0;
  41453. unsigned char *p = (unsigned char *) ptr;
  41454. fprintf(stderr, "{");
  41455. while ((p != NULL) && (i < size)) {
  41456. if ((i % 8) == 0) {
  41457. fprintf(stderr, "\n");
  41458. fprintf(stderr, " ");
  41459. }
  41460. fprintf(stderr, "0x%02x, ", p[i]);
  41461. i++;
  41462. }
  41463. fprintf(stderr, "\n};\n");
  41464. #else
  41465. (void) ptr;
  41466. (void) size;
  41467. #endif
  41468. }
  41469. static int last_val = 0x0f;
  41470. static int check_result(unsigned char *data, int len)
  41471. {
  41472. int i;
  41473. for ( ; len; ) {
  41474. last_val = (last_val + 1) % 16;
  41475. for (i = 0; i < 16; len--, i++, data++)
  41476. if (*data != last_val) {
  41477. return -1;
  41478. }
  41479. }
  41480. return 0;
  41481. }
  41482. static int r_offset;
  41483. static int w_offset;
  41484. static void init_offset(void)
  41485. {
  41486. r_offset = 0;
  41487. w_offset = 0;
  41488. }
  41489. static void get_record(unsigned char *data, unsigned char *buf, int len)
  41490. {
  41491. XMEMCPY(buf, data+r_offset, len);
  41492. r_offset += len;
  41493. }
  41494. static void set_record(unsigned char *data, unsigned char *buf, int len)
  41495. {
  41496. XMEMCPY(data+w_offset, buf, len);
  41497. w_offset += len;
  41498. }
  41499. static void set_plain(unsigned char *plain, int rec)
  41500. {
  41501. int i, j;
  41502. unsigned char *p = plain;
  41503. #define BLOCKSZ 16
  41504. for (i=0; i<(rec/BLOCKSZ); i++) {
  41505. for (j=0; j<BLOCKSZ; j++)
  41506. *p++ = (i % 16);
  41507. }
  41508. }
  41509. #endif
  41510. static int test_wolfSSL_EVP_Cipher_extra(void)
  41511. {
  41512. int res = TEST_SKIPPED;
  41513. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41514. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41515. /* aes128-cbc, keylen=16, ivlen=16 */
  41516. byte aes128_cbc_key[] = {
  41517. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41518. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41519. };
  41520. byte aes128_cbc_iv[] = {
  41521. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  41522. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  41523. };
  41524. /* teset data size table */
  41525. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  41526. int test_drive2[] = {8, 3, 8, 512, 0};
  41527. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  41528. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  41529. int test_drive_len[100];
  41530. int ret = 0;
  41531. EVP_CIPHER_CTX *evp = NULL;
  41532. int ilen = 0;
  41533. int klen = 0;
  41534. int i, j;
  41535. const EVP_CIPHER *type;
  41536. byte *iv;
  41537. byte *key;
  41538. int ivlen;
  41539. int keylen;
  41540. #define RECORDS 16
  41541. #define BUFFSZ 512
  41542. byte plain [BUFFSZ * RECORDS];
  41543. byte cipher[BUFFSZ * RECORDS];
  41544. byte inb[BUFFSZ];
  41545. byte outb[BUFFSZ+16];
  41546. int outl, inl;
  41547. iv = aes128_cbc_iv;
  41548. ivlen = sizeof(aes128_cbc_iv);
  41549. key = aes128_cbc_key;
  41550. keylen = sizeof(aes128_cbc_key);
  41551. type = EVP_aes_128_cbc();
  41552. set_plain(plain, BUFFSZ * RECORDS);
  41553. SSL_library_init();
  41554. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  41555. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41556. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41557. klen = EVP_CIPHER_CTX_key_length(evp);
  41558. if (klen > 0 && keylen != klen) {
  41559. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41560. }
  41561. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41562. if (ilen > 0 && ivlen != ilen) {
  41563. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41564. }
  41565. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41566. for (j = 0; j<RECORDS; j++)
  41567. {
  41568. inl = BUFFSZ;
  41569. get_record(plain, inb, inl);
  41570. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  41571. set_record(cipher, outb, outl);
  41572. }
  41573. for (i = 0; test_drive[i]; i++) {
  41574. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41575. init_offset();
  41576. test_drive_len[i] = 0;
  41577. for (j = 0; test_drive[i][j]; j++)
  41578. {
  41579. inl = test_drive[i][j];
  41580. test_drive_len[i] += inl;
  41581. get_record(plain, inb, inl);
  41582. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  41583. /* output to cipher buffer, so that following Dec test can detect
  41584. if any error */
  41585. set_record(cipher, outb, outl);
  41586. }
  41587. EVP_CipherFinal(evp, outb, &outl);
  41588. if (outl > 0)
  41589. set_record(cipher, outb, outl);
  41590. }
  41591. for (i = 0; test_drive[i]; i++) {
  41592. last_val = 0x0f;
  41593. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  41594. init_offset();
  41595. for (j = 0; test_drive[i][j]; j++) {
  41596. inl = test_drive[i][j];
  41597. get_record(cipher, inb, inl);
  41598. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  41599. binary_dump(outb, outl);
  41600. AssertIntEQ((ret = check_result(outb, outl)), 0);
  41601. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  41602. }
  41603. ret = EVP_CipherFinal(evp, outb, &outl);
  41604. binary_dump(outb, outl);
  41605. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  41606. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  41607. AssertTrue(ret);
  41608. }
  41609. EVP_CIPHER_CTX_free(evp);
  41610. /* Do an extra test to verify correct behavior with empty input. */
  41611. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  41612. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41613. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41614. klen = EVP_CIPHER_CTX_key_length(evp);
  41615. if (klen > 0 && keylen != klen) {
  41616. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41617. }
  41618. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41619. if (ilen > 0 && ivlen != ilen) {
  41620. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41621. }
  41622. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41623. /* outl should be set to 0 after passing NULL, 0 for input args. */
  41624. outl = -1;
  41625. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  41626. AssertIntEQ(outl, 0);
  41627. EVP_CIPHER_CTX_free(evp);
  41628. res = TEST_RES_CHECK(1);
  41629. #endif /* test_EVP_Cipher */
  41630. return res;
  41631. }
  41632. static int test_wolfSSL_PEM_read_DHparams(void)
  41633. {
  41634. int res = TEST_SKIPPED;
  41635. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  41636. !defined(NO_FILESYSTEM)
  41637. DH* dh;
  41638. XFILE fp;
  41639. unsigned char derOut[300];
  41640. unsigned char* derOutBuf = derOut;
  41641. int derOutSz = 0;
  41642. unsigned char derExpected[300];
  41643. int derExpectedSz = 0;
  41644. XMEMSET(derOut, 0, sizeof(derOut));
  41645. XMEMSET(derExpected, 0, sizeof(derExpected));
  41646. /* open DH param file, read into DH struct */
  41647. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  41648. /* bad args */
  41649. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  41650. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  41651. /* good args */
  41652. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41653. XFCLOSE(fp);
  41654. /* read in certs/dh2048.der for comparison against exported params */
  41655. fp = XFOPEN("./certs/dh2048.der", "rb");
  41656. AssertTrue(fp != XBADFILE);
  41657. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  41658. XFCLOSE(fp);
  41659. /* export DH back to DER and compare */
  41660. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  41661. AssertIntEQ(derOutSz, derExpectedSz);
  41662. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  41663. DH_free(dh);
  41664. dh = NULL;
  41665. /* Test parsing with X9.42 header */
  41666. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  41667. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41668. XFCLOSE(fp);
  41669. DH_free(dh);
  41670. res = TEST_RES_CHECK(1);
  41671. #endif
  41672. return res;
  41673. }
  41674. static int test_wolfSSL_AES_ecb_encrypt(void)
  41675. {
  41676. int res = TEST_SKIPPED;
  41677. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  41678. AES_KEY aes;
  41679. const byte msg[] =
  41680. {
  41681. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41682. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  41683. };
  41684. const byte verify[] =
  41685. {
  41686. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  41687. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  41688. };
  41689. const byte key[] =
  41690. {
  41691. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41692. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41693. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41694. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  41695. };
  41696. byte out[AES_BLOCK_SIZE];
  41697. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  41698. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41699. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  41700. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  41701. #ifdef HAVE_AES_DECRYPT
  41702. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  41703. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41704. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  41705. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  41706. #endif
  41707. /* test bad arguments */
  41708. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  41709. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  41710. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  41711. res = TEST_RES_CHECK(1);
  41712. #endif
  41713. return res;
  41714. }
  41715. static int test_wolfSSL_MD5(void)
  41716. {
  41717. int res = TEST_SKIPPED;
  41718. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  41719. byte input1[] = "";
  41720. byte input2[] = "message digest";
  41721. byte hash[WC_MD5_DIGEST_SIZE];
  41722. unsigned char output1[] =
  41723. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  41724. unsigned char output2[] =
  41725. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  41726. WOLFSSL_MD5_CTX md5;
  41727. XMEMSET(&md5, 0, sizeof(md5));
  41728. /* Test cases for illegal parameters */
  41729. AssertIntEQ(MD5_Init(NULL), 0);
  41730. AssertIntEQ(MD5_Init(&md5), 1);
  41731. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  41732. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  41733. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  41734. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  41735. AssertIntEQ(MD5_Final(hash, NULL), 0);
  41736. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  41737. /* Init MD5 CTX */
  41738. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  41739. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  41740. XSTRLEN((const char*)&input1)), 1);
  41741. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  41742. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  41743. /* Init MD5 CTX */
  41744. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  41745. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  41746. (int)XSTRLEN((const char*)input2)), 1);
  41747. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  41748. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  41749. #if !defined(NO_OLD_NAMES) && \
  41750. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  41751. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  41752. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  41753. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  41754. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  41755. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  41756. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  41757. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  41758. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  41759. {
  41760. byte data[] = "Data to be hashed.";
  41761. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  41762. AssertNotNull(MD5(data, sizeof(data), NULL));
  41763. AssertNotNull(MD5(data, sizeof(data), hash));
  41764. AssertNotNull(MD5(NULL, 0, hash));
  41765. AssertNull(MD5(NULL, sizeof(data), hash));
  41766. }
  41767. #endif
  41768. res = TEST_RES_CHECK(1);
  41769. #endif
  41770. return res;
  41771. }
  41772. static int test_wolfSSL_MD5_Transform(void)
  41773. {
  41774. int res = TEST_SKIPPED;
  41775. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  41776. byte input1[] = "";
  41777. byte input2[] = "abc";
  41778. byte local[WC_MD5_BLOCK_SIZE];
  41779. word32 sLen = 0;
  41780. #ifdef BIG_ENDIAN_ORDER
  41781. unsigned char output1[] =
  41782. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  41783. unsigned char output2[] =
  41784. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  41785. #else
  41786. unsigned char output1[] =
  41787. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  41788. unsigned char output2[] =
  41789. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  41790. #endif
  41791. union {
  41792. wc_Md5 native;
  41793. MD5_CTX compat;
  41794. } md5;
  41795. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  41796. XMEMSET(&local, 0, sizeof(local));
  41797. /* sanity check */
  41798. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  41799. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  41800. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  41801. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  41802. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  41803. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  41804. /* Init MD5 CTX */
  41805. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  41806. /* Do Transform*/
  41807. sLen = (word32)XSTRLEN((char*)input1);
  41808. XMEMCPY(local, input1, sLen);
  41809. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  41810. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  41811. WC_MD5_DIGEST_SIZE), 0);
  41812. /* Init MD5 CTX */
  41813. AssertIntEQ(MD5_Init(&md5.compat), 1);
  41814. sLen = (word32)XSTRLEN((char*)input2);
  41815. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  41816. XMEMCPY(local, input2, sLen);
  41817. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  41818. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  41819. WC_MD5_DIGEST_SIZE), 0);
  41820. res = TEST_RES_CHECK(1);
  41821. #endif
  41822. return res;
  41823. }
  41824. static int test_wolfSSL_SHA224(void)
  41825. {
  41826. int res = TEST_SKIPPED;
  41827. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  41828. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  41829. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  41830. unsigned char input[] =
  41831. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  41832. unsigned char output[] =
  41833. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  41834. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  41835. size_t inLen;
  41836. byte hash[WC_SHA224_DIGEST_SIZE];
  41837. inLen = XSTRLEN((char*)input);
  41838. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  41839. AssertNull(SHA224(NULL, inLen, hash));
  41840. AssertNotNull(SHA224(input, 0, hash));
  41841. AssertNotNull(SHA224(input, inLen, NULL));
  41842. AssertNotNull(SHA224(NULL, 0, hash));
  41843. AssertNotNull(SHA224(NULL, 0, NULL));
  41844. AssertNotNull(SHA224(input, inLen, hash));
  41845. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  41846. res = TEST_RES_CHECK(1);
  41847. #endif
  41848. return res;
  41849. }
  41850. static int test_wolfSSL_SHA_Transform(void)
  41851. {
  41852. int res = TEST_SKIPPED;
  41853. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  41854. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  41855. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41856. byte input1[] = "";
  41857. byte input2[] = "abc";
  41858. byte local[WC_SHA_BLOCK_SIZE];
  41859. word32 sLen = 0;
  41860. #ifdef BIG_ENDIAN_ORDER
  41861. unsigned char output1[] =
  41862. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  41863. "\x09\xf7\x3a\x93";
  41864. unsigned char output2[] =
  41865. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  41866. "\x7c\x6e\x08\xb8";
  41867. #else
  41868. unsigned char output1[] =
  41869. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  41870. "\x93\x3a\xf7\x09";
  41871. unsigned char output2[] =
  41872. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  41873. "\xb8\x08\x6e\x7c";
  41874. #endif
  41875. union {
  41876. wc_Sha native;
  41877. SHA_CTX compat;
  41878. } sha;
  41879. union {
  41880. wc_Sha native;
  41881. SHA_CTX compat;
  41882. } sha1;
  41883. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  41884. XMEMSET(&local, 0, sizeof(local));
  41885. /* sanity check */
  41886. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  41887. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  41888. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  41889. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  41890. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  41891. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  41892. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  41893. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  41894. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  41895. /* Init SHA CTX */
  41896. AssertIntEQ(SHA_Init(&sha.compat), 1);
  41897. /* Do Transform*/
  41898. sLen = (word32)XSTRLEN((char*)input1);
  41899. XMEMCPY(local, input1, sLen);
  41900. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  41901. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  41902. WC_SHA_DIGEST_SIZE), 0);
  41903. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  41904. /* Init SHA CTX */
  41905. AssertIntEQ(SHA_Init(&sha.compat), 1);
  41906. sLen = (word32)XSTRLEN((char*)input2);
  41907. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  41908. XMEMCPY(local, input2, sLen);
  41909. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  41910. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  41911. WC_SHA_DIGEST_SIZE), 0);
  41912. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  41913. /* SHA1 */
  41914. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  41915. /* Init SHA CTX */
  41916. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  41917. /* Do Transform*/
  41918. sLen = (word32)XSTRLEN((char*)input1);
  41919. XMEMCPY(local, input1, sLen);
  41920. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  41921. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  41922. WC_SHA_DIGEST_SIZE), 0);
  41923. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  41924. /* Init SHA CTX */
  41925. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  41926. sLen = (word32)XSTRLEN((char*)input2);
  41927. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  41928. XMEMCPY(local, input2, sLen);
  41929. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  41930. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  41931. WC_SHA_DIGEST_SIZE), 0);
  41932. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  41933. res = TEST_RES_CHECK(1);
  41934. #endif
  41935. #endif
  41936. return res;
  41937. }
  41938. static int test_wolfSSL_SHA256_Transform(void)
  41939. {
  41940. int res = TEST_SKIPPED;
  41941. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  41942. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  41943. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  41944. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  41945. !defined(WOLFSSL_KCAPI_HASH)
  41946. byte input1[] = "";
  41947. byte input2[] = "abc";
  41948. byte local[WC_SHA256_BLOCK_SIZE];
  41949. word32 sLen = 0;
  41950. #ifdef BIG_ENDIAN_ORDER
  41951. unsigned char output1[] =
  41952. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  41953. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  41954. unsigned char output2[] =
  41955. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  41956. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  41957. #else
  41958. unsigned char output1[] =
  41959. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  41960. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  41961. unsigned char output2[] =
  41962. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  41963. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  41964. #endif
  41965. union {
  41966. wc_Sha256 native;
  41967. SHA256_CTX compat;
  41968. } sha256;
  41969. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  41970. XMEMSET(&local, 0, sizeof(local));
  41971. /* sanity check */
  41972. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  41973. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  41974. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  41975. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  41976. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  41977. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  41978. /* Init SHA256 CTX */
  41979. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  41980. /* Do Transform*/
  41981. sLen = (word32)XSTRLEN((char*)input1);
  41982. XMEMCPY(local, input1, sLen);
  41983. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  41984. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  41985. WC_SHA256_DIGEST_SIZE), 0);
  41986. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  41987. /* Init SHA256 CTX */
  41988. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  41989. sLen = (word32)XSTRLEN((char*)input2);
  41990. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  41991. XMEMCPY(local, input2, sLen);
  41992. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  41993. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  41994. WC_SHA256_DIGEST_SIZE), 0);
  41995. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  41996. res = TEST_RES_CHECK(1);
  41997. #endif
  41998. #endif
  41999. return res;
  42000. }
  42001. static int test_wolfSSL_SHA256(void)
  42002. {
  42003. int res = TEST_SKIPPED;
  42004. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  42005. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  42006. unsigned char input[] =
  42007. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  42008. unsigned char output[] =
  42009. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  42010. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  42011. "\x06\xC1";
  42012. size_t inLen;
  42013. byte hash[WC_SHA256_DIGEST_SIZE];
  42014. inLen = XSTRLEN((char*)input);
  42015. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  42016. AssertNotNull(SHA256(input, inLen, hash));
  42017. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  42018. res = TEST_RES_CHECK(1);
  42019. #endif
  42020. return res;
  42021. }
  42022. static int test_wolfSSL_SHA512_Transform(void)
  42023. {
  42024. int res = TEST_SKIPPED;
  42025. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  42026. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  42027. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  42028. !defined(WOLFSSL_KCAPI_HASH)
  42029. byte input1[] = "";
  42030. byte input2[] = "abc";
  42031. byte local[WC_SHA512_BLOCK_SIZE];
  42032. word32 sLen = 0;
  42033. #ifdef BIG_ENDIAN_ORDER
  42034. unsigned char output1[] =
  42035. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  42036. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  42037. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  42038. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  42039. unsigned char output2[] =
  42040. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  42041. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  42042. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  42043. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  42044. #else
  42045. unsigned char output1[] =
  42046. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  42047. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  42048. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  42049. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  42050. unsigned char output2[] =
  42051. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  42052. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  42053. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  42054. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  42055. #endif
  42056. union {
  42057. wc_Sha512 native;
  42058. SHA512_CTX compat;
  42059. } sha512;
  42060. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  42061. XMEMSET(&local, 0, sizeof(local));
  42062. /* sanity check */
  42063. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  42064. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  42065. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  42066. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  42067. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  42068. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  42069. /* Init SHA512 CTX */
  42070. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  42071. /* Do Transform*/
  42072. sLen = (word32)XSTRLEN((char*)input1);
  42073. XMEMCPY(local, input1, sLen);
  42074. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  42075. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  42076. WC_SHA512_DIGEST_SIZE), 0);
  42077. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  42078. /* Init SHA512 CTX */
  42079. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  42080. sLen = (word32)XSTRLEN((char*)input2);
  42081. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  42082. XMEMCPY(local, input2, sLen);
  42083. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  42084. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  42085. WC_SHA512_DIGEST_SIZE), 0);
  42086. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  42087. (void)input1;
  42088. res = TEST_RES_CHECK(1);
  42089. #endif
  42090. #endif
  42091. return res;
  42092. }
  42093. static int test_wolfSSL_X509_get_serialNumber(void)
  42094. {
  42095. int res = TEST_SKIPPED;
  42096. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42097. ASN1_INTEGER* a;
  42098. BIGNUM* bn;
  42099. X509* x509;
  42100. char *serialHex;
  42101. byte serial[3];
  42102. int serialSz;
  42103. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42104. SSL_FILETYPE_PEM));
  42105. AssertNotNull(a = X509_get_serialNumber(x509));
  42106. /* check on value of ASN1 Integer */
  42107. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42108. /* test setting serial number and then retrieving it */
  42109. AssertNotNull(a = ASN1_INTEGER_new());
  42110. ASN1_INTEGER_set(a, 3);
  42111. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42112. serialSz = sizeof(serial);
  42113. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42114. WOLFSSL_SUCCESS);
  42115. AssertIntEQ(serialSz, 1);
  42116. AssertIntEQ(serial[0], 3);
  42117. ASN1_INTEGER_free(a);
  42118. /* test setting serial number with 0's in it */
  42119. serial[0] = 0x01;
  42120. serial[1] = 0x00;
  42121. serial[2] = 0x02;
  42122. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42123. a->data[0] = ASN_INTEGER;
  42124. a->data[1] = sizeof(serial);
  42125. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42126. a->length = sizeof(serial) + 2;
  42127. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42128. XMEMSET(serial, 0, sizeof(serial));
  42129. serialSz = sizeof(serial);
  42130. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42131. WOLFSSL_SUCCESS);
  42132. AssertIntEQ(serialSz, 3);
  42133. AssertIntEQ(serial[0], 0x01);
  42134. AssertIntEQ(serial[1], 0x00);
  42135. AssertIntEQ(serial[2], 0x02);
  42136. ASN1_INTEGER_free(a);
  42137. X509_free(x509); /* free's a */
  42138. AssertNotNull(serialHex = BN_bn2hex(bn));
  42139. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42140. AssertStrEQ(serialHex, "01");
  42141. #else
  42142. AssertStrEQ(serialHex, "1");
  42143. #endif
  42144. OPENSSL_free(serialHex);
  42145. AssertIntEQ(BN_get_word(bn), 1);
  42146. BN_free(bn);
  42147. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42148. a = ASN1_INTEGER_new();
  42149. if (a) {
  42150. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42151. DYNAMIC_TYPE_OPENSSL));
  42152. a->isDynamic = 1;
  42153. ASN1_INTEGER_free(a);
  42154. }
  42155. res = TEST_RES_CHECK(1);
  42156. #endif
  42157. return res;
  42158. }
  42159. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42160. {
  42161. int res = TEST_SKIPPED;
  42162. #if defined(OPENSSL_EXTRA)
  42163. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  42164. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  42165. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  42166. res = TEST_RES_CHECK(1);
  42167. #endif
  42168. return res;
  42169. }
  42170. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42171. {
  42172. int res = TEST_SKIPPED;
  42173. #if defined(OPENSSL_EXTRA)
  42174. #define MAX_HEXSTR_BUFSZ 9
  42175. #define NUM_CASES 5
  42176. struct Output {
  42177. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42178. long ret;
  42179. };
  42180. int i;
  42181. int j;
  42182. const char* inputs[NUM_CASES] = {
  42183. "aabcd1357e",
  42184. "01:12:23:34:a5:b6:c7:d8:e9",
  42185. ":01:02",
  42186. "012",
  42187. ":ab:ac:d"
  42188. };
  42189. struct Output expectedOutputs[NUM_CASES] = {
  42190. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42191. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42192. {{0x01, 0x02}, 2},
  42193. {{0x00}, 0},
  42194. {{0x00}, 0}
  42195. };
  42196. long len = 0;
  42197. unsigned char* returnedBuf = NULL;
  42198. for (i = 0; i < NUM_CASES; ++i) {
  42199. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42200. if (returnedBuf == NULL) {
  42201. AssertIntEQ(expectedOutputs[i].ret, 0);
  42202. continue;
  42203. }
  42204. AssertIntEQ(expectedOutputs[i].ret, len);
  42205. for (j = 0; j < len; ++j) {
  42206. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42207. }
  42208. OPENSSL_free(returnedBuf);
  42209. }
  42210. res = TEST_RES_CHECK(1);
  42211. #endif
  42212. return res;
  42213. }
  42214. static int test_wolfSSL_X509_CA_num(void)
  42215. {
  42216. int res = TEST_SKIPPED;
  42217. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42218. defined(HAVE_ECC) && !defined(NO_RSA)
  42219. WOLFSSL_X509_STORE *store;
  42220. WOLFSSL_X509 *x509_1, *x509_2;
  42221. int ca_num = 0;
  42222. store = wolfSSL_X509_STORE_new();
  42223. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  42224. wolfSSL_X509_STORE_add_cert(store, x509_1);
  42225. ca_num = wolfSSL_X509_CA_num(store);
  42226. AssertIntEQ(ca_num, 1);
  42227. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  42228. wolfSSL_X509_STORE_add_cert(store, x509_2);
  42229. ca_num = wolfSSL_X509_CA_num(store);
  42230. AssertIntEQ(ca_num, 2);
  42231. wolfSSL_X509_free(x509_1);
  42232. wolfSSL_X509_free(x509_2);
  42233. wolfSSL_X509_STORE_free(store);
  42234. res = TEST_RES_CHECK(1);
  42235. #endif
  42236. return res;
  42237. }
  42238. static int test_wolfSSL_X509_check_ca(void)
  42239. {
  42240. int res = TEST_SKIPPED;
  42241. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42242. WOLFSSL_X509 *x509;
  42243. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  42244. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42245. wolfSSL_X509_free(x509);
  42246. res = TEST_RES_CHECK(1);
  42247. #endif
  42248. return res;
  42249. }
  42250. static int test_wolfSSL_X509_check_ip_asc(void)
  42251. {
  42252. int res = TEST_SKIPPED;
  42253. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42254. WOLFSSL_X509 *x509;
  42255. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  42256. #if 0
  42257. /* TODO: add cert gen for testing positive case */
  42258. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42259. #endif
  42260. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42261. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42262. wolfSSL_X509_free(x509);
  42263. res = TEST_RES_CHECK(1);
  42264. #endif
  42265. return res;
  42266. }
  42267. static int test_wolfSSL_make_cert(void)
  42268. {
  42269. int res = TEST_SKIPPED;
  42270. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42271. defined(WOLFSSL_CERT_EXT)
  42272. int ret;
  42273. Cert cert;
  42274. CertName name;
  42275. RsaKey key;
  42276. WC_RNG rng;
  42277. byte der[FOURK_BUF];
  42278. word32 idx;
  42279. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42280. #ifdef OPENSSL_EXTRA
  42281. const unsigned char* pt;
  42282. int certSz;
  42283. X509* x509;
  42284. X509_NAME* x509name;
  42285. X509_NAME_ENTRY* entry;
  42286. ASN1_STRING* entryValue;
  42287. #endif
  42288. XMEMSET(&name, 0, sizeof(CertName));
  42289. /* set up cert name */
  42290. XMEMCPY(name.country, "US", sizeof("US"));
  42291. name.countryEnc = CTC_PRINTABLE;
  42292. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42293. name.stateEnc = CTC_UTF8;
  42294. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42295. name.localityEnc = CTC_UTF8;
  42296. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42297. name.surEnc = CTC_UTF8;
  42298. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42299. name.orgEnc = CTC_UTF8;
  42300. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42301. name.unitEnc = CTC_UTF8;
  42302. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42303. name.commonNameEnc = CTC_UTF8;
  42304. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42305. name.serialDevEnc = CTC_PRINTABLE;
  42306. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42307. name.userIdEnc = CTC_PRINTABLE;
  42308. #ifdef WOLFSSL_MULTI_ATTRIB
  42309. #if CTC_MAX_ATTRIB > 2
  42310. {
  42311. NameAttrib* n;
  42312. n = &name.name[0];
  42313. n->id = ASN_DOMAIN_COMPONENT;
  42314. n->type = CTC_UTF8;
  42315. n->sz = sizeof("com");
  42316. XMEMCPY(n->value, "com", sizeof("com"));
  42317. n = &name.name[1];
  42318. n->id = ASN_DOMAIN_COMPONENT;
  42319. n->type = CTC_UTF8;
  42320. n->sz = sizeof("wolfssl");
  42321. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42322. }
  42323. #endif
  42324. #endif /* WOLFSSL_MULTI_ATTRIB */
  42325. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42326. #ifndef HAVE_FIPS
  42327. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42328. #else
  42329. AssertIntEQ(wc_InitRng(&rng), 0);
  42330. #endif
  42331. /* load test RSA key */
  42332. idx = 0;
  42333. #if defined(USE_CERT_BUFFERS_1024)
  42334. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42335. sizeof_server_key_der_1024), 0);
  42336. #elif defined(USE_CERT_BUFFERS_2048)
  42337. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42338. sizeof_server_key_der_2048), 0);
  42339. #else
  42340. /* error case, no RSA key loaded, happens later */
  42341. (void)idx;
  42342. #endif
  42343. XMEMSET(&cert, 0 , sizeof(Cert));
  42344. AssertIntEQ(wc_InitCert(&cert), 0);
  42345. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42346. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42347. cert.serialSz = (int)sizeof(mySerial);
  42348. cert.isCA = 1;
  42349. #ifndef NO_SHA256
  42350. cert.sigType = CTC_SHA256wRSA;
  42351. #else
  42352. cert.sigType = CTC_SHAwRSA;
  42353. #endif
  42354. /* add SKID from the Public Key */
  42355. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42356. /* add AKID from the Public Key */
  42357. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42358. ret = 0;
  42359. do {
  42360. #if defined(WOLFSSL_ASYNC_CRYPT)
  42361. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42362. #endif
  42363. if (ret >= 0) {
  42364. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42365. }
  42366. } while (ret == WC_PENDING_E);
  42367. AssertIntGT(ret, 0);
  42368. #ifdef OPENSSL_EXTRA
  42369. /* der holds a certificate with DC's now check X509 parsing of it */
  42370. certSz = ret;
  42371. pt = der;
  42372. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42373. AssertNotNull(x509name = X509_get_subject_name(x509));
  42374. #ifdef WOLFSSL_MULTI_ATTRIB
  42375. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42376. -1)), 5);
  42377. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42378. idx)), 6);
  42379. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42380. idx)), -1);
  42381. #endif /* WOLFSSL_MULTI_ATTRIB */
  42382. /* compare DN at index 0 */
  42383. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  42384. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42385. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  42386. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  42387. #ifndef WOLFSSL_MULTI_ATTRIB
  42388. /* compare Serial Number */
  42389. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  42390. -1)), 7);
  42391. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42392. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42393. AssertIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  42394. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  42395. #endif
  42396. #ifdef WOLFSSL_MULTI_ATTRIB
  42397. /* get first and second DC and compare result */
  42398. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42399. -1)), 5);
  42400. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42401. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42402. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  42403. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42404. idx)), 6);
  42405. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42406. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42407. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  42408. #endif /* WOLFSSL_MULTI_ATTRIB */
  42409. /* try invalid index locations for regression test and sanity check */
  42410. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  42411. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  42412. X509_free(x509);
  42413. #endif /* OPENSSL_EXTRA */
  42414. wc_FreeRsaKey(&key);
  42415. wc_FreeRng(&rng);
  42416. res = TEST_RES_CHECK(1);
  42417. #endif
  42418. return res;
  42419. }
  42420. static int test_wolfSSL_X509_get_version(void)
  42421. {
  42422. int res = TEST_SKIPPED;
  42423. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42424. WOLFSSL_X509 *x509;
  42425. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  42426. AssertNotNull(x509);
  42427. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  42428. wolfSSL_X509_free(x509);
  42429. res = TEST_RES_CHECK(1);
  42430. #endif
  42431. return res;
  42432. }
  42433. static int test_wolfSSL_DES_ncbc(void)
  42434. {
  42435. int res = TEST_SKIPPED;
  42436. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  42437. const_DES_cblock myDes;
  42438. DES_cblock iv = {1};
  42439. DES_key_schedule key = {0};
  42440. unsigned char msg[] = "hello wolfssl";
  42441. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  42442. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  42443. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  42444. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  42445. /* partial block test */
  42446. DES_set_key(&key, &myDes);
  42447. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  42448. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  42449. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  42450. DES_set_key(&key, &myDes);
  42451. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  42452. *((byte*)&iv) = 1;
  42453. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  42454. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  42455. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  42456. /* full block test */
  42457. DES_set_key(&key, &myDes);
  42458. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  42459. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  42460. *((byte*)&iv) = 1;
  42461. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  42462. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  42463. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  42464. DES_set_key(&key, &myDes);
  42465. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  42466. *((byte*)&iv) = 1;
  42467. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  42468. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  42469. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  42470. res = TEST_RES_CHECK(1);
  42471. #endif
  42472. return res;
  42473. }
  42474. static int test_wolfSSL_AES_cbc_encrypt(void)
  42475. {
  42476. int res = TEST_SKIPPED;
  42477. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  42478. AES_KEY aes;
  42479. AES_KEY* aesN = NULL;
  42480. size_t len = 0;
  42481. size_t lenB = 0;
  42482. int keySz0 = 0;
  42483. int keySzN = -1;
  42484. byte out[AES_BLOCK_SIZE] = {0};
  42485. byte* outN = NULL;
  42486. /* Test vectors retrieved from:
  42487. * <begin URL>
  42488. * https://csrc.nist.gov/
  42489. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  42490. * documents/aes/KAT_AES.zip
  42491. * </end URL>
  42492. */
  42493. const byte* pt128N = NULL;
  42494. byte* key128N = NULL;
  42495. byte* iv128N = NULL;
  42496. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  42497. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42498. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  42499. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  42500. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  42501. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42502. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  42503. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  42504. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  42505. len = sizeof(pt128);
  42506. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  42507. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  42508. AssertIntNE(XMEMCMP(b, g, h), i)
  42509. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  42510. /* Stressing wolfSSL_AES_cbc_encrypt() */
  42511. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  42512. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  42513. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  42514. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  42515. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  42516. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  42517. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  42518. /* Stressing wolfSSL_AES_set_encrypt_key */
  42519. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  42520. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  42521. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  42522. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  42523. /* Stressing wolfSSL_AES_set_decrypt_key */
  42524. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  42525. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  42526. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  42527. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  42528. #ifdef WOLFSSL_AES_128
  42529. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  42530. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42531. RESET_IV(iv128tmp, iv128);
  42532. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  42533. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  42534. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  42535. wc_AesFree((Aes*)&aes);
  42536. #ifdef HAVE_AES_DECRYPT
  42537. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  42538. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42539. RESET_IV(iv128tmp, iv128);
  42540. len = sizeof(ct128);
  42541. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  42542. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  42543. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  42544. wc_AesFree((Aes*)&aes);
  42545. #endif
  42546. #endif /* WOLFSSL_AES_128 */
  42547. #ifdef WOLFSSL_AES_192
  42548. {
  42549. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  42550. * Appendix F.2.3 */
  42551. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  42552. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  42553. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  42554. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  42555. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  42556. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  42557. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  42558. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  42559. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  42560. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  42561. len = sizeof(pt192);
  42562. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  42563. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42564. RESET_IV(iv192tmp, iv192);
  42565. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  42566. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  42567. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  42568. wc_AesFree((Aes*)&aes);
  42569. #ifdef HAVE_AES_DECRYPT
  42570. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  42571. len = sizeof(ct192);
  42572. RESET_IV(iv192tmp, iv192);
  42573. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42574. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  42575. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  42576. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  42577. wc_AesFree((Aes*)&aes);
  42578. #endif
  42579. }
  42580. #endif /* WOLFSSL_AES_192 */
  42581. #ifdef WOLFSSL_AES_256
  42582. {
  42583. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  42584. * Appendix F.2.5 */
  42585. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  42586. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  42587. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  42588. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  42589. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  42590. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  42591. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  42592. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  42593. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  42594. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  42595. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  42596. len = sizeof(pt256);
  42597. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  42598. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42599. RESET_IV(iv256tmp, iv256);
  42600. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42601. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  42602. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  42603. wc_AesFree((Aes*)&aes);
  42604. #ifdef HAVE_AES_DECRYPT
  42605. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  42606. len = sizeof(ct256);
  42607. RESET_IV(iv256tmp, iv256);
  42608. XMEMSET(out, 0, AES_BLOCK_SIZE);
  42609. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42610. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  42611. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  42612. wc_AesFree((Aes*)&aes);
  42613. #endif
  42614. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  42615. {
  42616. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  42617. byte wrapPlain[sizeof(key256)] = { 0 };
  42618. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  42619. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  42620. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42621. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  42622. 15), WOLFSSL_FAILURE);
  42623. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  42624. sizeof(key256)), sizeof(wrapCipher));
  42625. wc_AesFree((Aes*)&aes);
  42626. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  42627. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42628. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  42629. 23), WOLFSSL_FAILURE);
  42630. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  42631. sizeof(wrapCipher)), sizeof(wrapPlain));
  42632. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  42633. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  42634. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  42635. wc_AesFree((Aes*)&aes);
  42636. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  42637. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42638. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  42639. sizeof(key256)), sizeof(wrapCipher));
  42640. wc_AesFree((Aes*)&aes);
  42641. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  42642. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  42643. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  42644. sizeof(wrapCipher)), sizeof(wrapPlain));
  42645. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  42646. wc_AesFree((Aes*)&aes);
  42647. }
  42648. #endif /* HAVE_AES_KEYWRAP */
  42649. }
  42650. #endif /* WOLFSSL_AES_256 */
  42651. res = TEST_RES_CHECK(1);
  42652. #endif
  42653. return res;
  42654. }
  42655. static int test_wolfSSL_CRYPTO_cts128(void)
  42656. {
  42657. int res = TEST_SKIPPED;
  42658. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  42659. && defined(HAVE_CTS)
  42660. byte tmp[64]; /* Largest vector size */
  42661. /* Test vectors taken form RFC3962 Appendix B */
  42662. const testVector vects[] = {
  42663. {
  42664. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42665. "\x20",
  42666. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  42667. "\x97",
  42668. 17, 17
  42669. },
  42670. {
  42671. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42672. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  42673. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  42674. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  42675. 31, 31
  42676. },
  42677. {
  42678. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42679. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  42680. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  42681. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  42682. 32, 32
  42683. },
  42684. {
  42685. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42686. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  42687. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  42688. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  42689. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  42690. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  42691. 47, 47
  42692. },
  42693. {
  42694. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42695. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  42696. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  42697. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  42698. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  42699. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  42700. 48, 48
  42701. },
  42702. {
  42703. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  42704. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  42705. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  42706. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  42707. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  42708. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  42709. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  42710. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  42711. 64, 64
  42712. }
  42713. };
  42714. byte keyBytes[AES_128_KEY_SIZE] = {
  42715. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  42716. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  42717. };
  42718. size_t i;
  42719. XMEMSET(tmp, 0, sizeof(tmp));
  42720. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  42721. AES_KEY encKey;
  42722. AES_KEY decKey;
  42723. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  42724. XMEMSET(iv, 0, sizeof(iv));
  42725. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  42726. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  42727. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  42728. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  42729. vects[i].outLen);
  42730. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  42731. XMEMSET(iv, 0, sizeof(iv));
  42732. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  42733. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  42734. vects[i].inLen);
  42735. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  42736. }
  42737. res = TEST_RES_CHECK(1);
  42738. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  42739. return res;
  42740. }
  42741. #if defined(OPENSSL_ALL)
  42742. static int test_wolfSSL_sk_CIPHER_description(void)
  42743. {
  42744. int res = TEST_SKIPPED;
  42745. #if !defined(NO_RSA)
  42746. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42747. int i,j,k;
  42748. int numCiphers = 0;
  42749. const SSL_METHOD *method = NULL;
  42750. const SSL_CIPHER *cipher = NULL;
  42751. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42752. SSL_CTX *ctx = NULL;
  42753. SSL *ssl = NULL;
  42754. char buf[256];
  42755. char test_str[9] = "0000000";
  42756. const char badStr[] = "unknown";
  42757. const char certPath[] = "./certs/client-cert.pem";
  42758. XMEMSET(buf, 0, sizeof(buf));
  42759. AssertNotNull(method = TLSv1_2_client_method());
  42760. AssertNotNull(ctx = SSL_CTX_new(method));
  42761. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42762. SSL_CTX_set_verify_depth(ctx, 4);
  42763. SSL_CTX_set_options(ctx, flags);
  42764. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42765. WOLFSSL_SUCCESS);
  42766. AssertNotNull(ssl = SSL_new(ctx));
  42767. /* SSL_get_ciphers returns a stack of all configured ciphers
  42768. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  42769. */
  42770. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42771. /* loop through the amount of supportedCiphers */
  42772. numCiphers = sk_num(supportedCiphers);
  42773. for (i = 0; i < numCiphers; ++i) {
  42774. /* sk_value increments "sk->data.cipher->cipherOffset".
  42775. * wolfSSL_sk_CIPHER_description sets the description for
  42776. * the cipher based on the provided offset.
  42777. */
  42778. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  42779. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  42780. }
  42781. /* Search cipher description string for "unknown" descriptor */
  42782. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  42783. k = 0;
  42784. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  42785. test_str[k] = badStr[k];
  42786. j++;
  42787. k++;
  42788. }
  42789. }
  42790. /* Fail if test_str == badStr == "unknown" */
  42791. AssertStrNE(test_str,badStr);
  42792. }
  42793. SSL_free(ssl);
  42794. SSL_CTX_free(ctx);
  42795. res = TEST_RES_CHECK(1);
  42796. #endif
  42797. return res;
  42798. }
  42799. static int test_wolfSSL_get_ciphers_compat(void)
  42800. {
  42801. int res = TEST_SKIPPED;
  42802. #if !defined(NO_RSA)
  42803. const SSL_METHOD *method = NULL;
  42804. const char certPath[] = "./certs/client-cert.pem";
  42805. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42806. SSL_CTX *ctx = NULL;
  42807. WOLFSSL *ssl = NULL;
  42808. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42809. method = SSLv23_client_method();
  42810. AssertNotNull(method);
  42811. ctx = SSL_CTX_new(method);
  42812. AssertNotNull(ctx);
  42813. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42814. SSL_CTX_set_verify_depth(ctx, 4);
  42815. SSL_CTX_set_options(ctx, flags);
  42816. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42817. WOLFSSL_SUCCESS);
  42818. AssertNotNull(ssl = SSL_new(ctx));
  42819. /* Test Bad NULL input */
  42820. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  42821. /* Test for Good input */
  42822. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42823. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  42824. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  42825. SSL_free(ssl);
  42826. SSL_CTX_free(ctx);
  42827. res = TEST_RES_CHECK(1);
  42828. #endif
  42829. return res;
  42830. }
  42831. static int test_wolfSSL_X509_PUBKEY_get(void)
  42832. {
  42833. WOLFSSL_X509_PUBKEY pubkey;
  42834. WOLFSSL_X509_PUBKEY* key;
  42835. WOLFSSL_EVP_PKEY evpkey ;
  42836. WOLFSSL_EVP_PKEY* evpPkey;
  42837. WOLFSSL_EVP_PKEY* retEvpPkey;
  42838. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  42839. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  42840. key = &pubkey;
  42841. evpPkey = &evpkey;
  42842. evpPkey->type = WOLFSSL_SUCCESS;
  42843. key->pkey = evpPkey;
  42844. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42845. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  42846. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  42847. key->pkey = NULL;
  42848. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42849. return TEST_RES_CHECK(retEvpPkey == NULL);
  42850. }
  42851. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  42852. {
  42853. int res = TEST_SKIPPED;
  42854. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42855. DSA *dsa = NULL;
  42856. DSA *setDsa = NULL;
  42857. EVP_PKEY *pkey = NULL;
  42858. EVP_PKEY *set1Pkey = NULL;
  42859. SHA_CTX sha;
  42860. byte signature[DSA_SIG_SIZE];
  42861. byte hash[WC_SHA_DIGEST_SIZE];
  42862. word32 bytes;
  42863. int answer;
  42864. #ifdef USE_CERT_BUFFERS_1024
  42865. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  42866. int dsaKeySz = sizeof_dsa_key_der_1024;
  42867. byte tmp[ONEK_BUF];
  42868. XMEMSET(tmp, 0, sizeof(tmp));
  42869. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42870. bytes = dsaKeySz;
  42871. #elif defined(USE_CERT_BUFFERS_2048)
  42872. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  42873. int dsaKeySz = sizeof_dsa_key_der_2048;
  42874. byte tmp[TWOK_BUF];
  42875. XMEMSET(tmp, 0, sizeof(tmp));
  42876. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42877. bytes = dsaKeySz;
  42878. #else
  42879. byte tmp[TWOK_BUF];
  42880. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  42881. int dsaKeySz;
  42882. XMEMSET(tmp, 0, sizeof(tmp));
  42883. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  42884. if (fp == XBADFILE) {
  42885. return WOLFSSL_BAD_FILE;
  42886. }
  42887. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  42888. XFCLOSE(fp);
  42889. #endif /* END USE_CERT_BUFFERS_1024 */
  42890. /* Create hash to later Sign and Verify */
  42891. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  42892. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  42893. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  42894. /* Initialize pkey with der format dsa key */
  42895. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  42896. (long)dsaKeySz));
  42897. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  42898. /* Should Fail: NULL argument */
  42899. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  42900. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  42901. /* Should Pass: Initialized pkey argument */
  42902. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  42903. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  42904. #ifdef USE_CERT_BUFFERS_1024
  42905. AssertIntEQ(DSA_bits(dsa), 1024);
  42906. #else
  42907. AssertIntEQ(DSA_bits(dsa), 2048);
  42908. #endif
  42909. /* Sign */
  42910. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  42911. /* Verify. */
  42912. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  42913. WOLFSSL_SUCCESS);
  42914. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  42915. /* Should Fail: set1Pkey not initialized */
  42916. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42917. /* Initialize set1Pkey */
  42918. set1Pkey = EVP_PKEY_new();
  42919. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  42920. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  42921. WOLFSSL_SUCCESS);
  42922. /* Should Pass: set dsa into set1Pkey */
  42923. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42924. DSA_free(dsa);
  42925. DSA_free(setDsa);
  42926. EVP_PKEY_free(pkey);
  42927. EVP_PKEY_free(set1Pkey);
  42928. res = TEST_RES_CHECK(1);
  42929. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42930. return res;
  42931. } /* END test_EVP_PKEY_set1_get1_DSA */
  42932. static int test_wolfSSL_DSA_SIG(void)
  42933. {
  42934. int res = TEST_SKIPPED;
  42935. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  42936. !defined(HAVE_FIPS)
  42937. DSA *dsa = NULL;
  42938. DSA *dsa2 = NULL;
  42939. DSA_SIG *sig = NULL;
  42940. const BIGNUM *p = NULL;
  42941. const BIGNUM *q = NULL;
  42942. const BIGNUM *g = NULL;
  42943. const BIGNUM *pub = NULL;
  42944. const BIGNUM *priv = NULL;
  42945. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  42946. AssertNotNull(dsa = DSA_generate_parameters(2048,
  42947. NULL, 0, NULL, NULL, NULL, NULL));
  42948. DSA_free(dsa);
  42949. AssertNotNull(dsa = DSA_new());
  42950. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  42951. NULL, 0, NULL, NULL, NULL), 1);
  42952. AssertIntEQ(DSA_generate_key(dsa), 1);
  42953. DSA_get0_pqg(dsa, &p, &q, &g);
  42954. DSA_get0_key(dsa, &pub, &priv);
  42955. AssertNotNull(p = BN_dup(p));
  42956. AssertNotNull(q = BN_dup(q));
  42957. AssertNotNull(g = BN_dup(g));
  42958. AssertNotNull(pub = BN_dup(pub));
  42959. AssertNotNull(priv = BN_dup(priv));
  42960. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  42961. AssertNotNull(dsa2 = DSA_new());
  42962. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  42963. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  42964. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  42965. DSA_free(dsa);
  42966. DSA_free(dsa2);
  42967. DSA_SIG_free(sig);
  42968. res = TEST_RES_CHECK(1);
  42969. #endif
  42970. return res;
  42971. }
  42972. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  42973. {
  42974. int res = TEST_SKIPPED;
  42975. #ifdef HAVE_ECC
  42976. WOLFSSL_EC_KEY *ecKey = NULL;
  42977. WOLFSSL_EC_KEY *ecGet1 = NULL;
  42978. EVP_PKEY *pkey = NULL;
  42979. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42980. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42981. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  42982. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42983. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42984. /* Should fail since ecKey is empty */
  42985. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42986. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42987. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42988. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  42989. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  42990. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  42991. wolfSSL_EC_KEY_free(ecKey);
  42992. wolfSSL_EC_KEY_free(ecGet1);
  42993. EVP_PKEY_free(pkey);
  42994. res = TEST_RES_CHECK(1);
  42995. #endif /* HAVE_ECC */
  42996. return res;
  42997. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  42998. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  42999. {
  43000. int res = TEST_SKIPPED;
  43001. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43002. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43003. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43004. DH *dh = NULL;
  43005. DH *setDh = NULL;
  43006. EVP_PKEY *pkey = NULL;
  43007. FILE* f = NULL;
  43008. unsigned char buf[4096];
  43009. const unsigned char* pt = buf;
  43010. const char* dh2048 = "./certs/dh2048.der";
  43011. long len = 0;
  43012. int code = -1;
  43013. XMEMSET(buf, 0, sizeof(buf));
  43014. f = XFOPEN(dh2048, "rb");
  43015. AssertTrue(f != XBADFILE);
  43016. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  43017. XFCLOSE(f);
  43018. /* Load dh2048.der into DH with internal format */
  43019. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43020. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43021. AssertIntEQ(code, 0);
  43022. code = -1;
  43023. pkey = wolfSSL_EVP_PKEY_new();
  43024. /* Set DH into PKEY */
  43025. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43026. /* Get DH from PKEY */
  43027. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43028. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43029. AssertIntEQ(code, 0);
  43030. EVP_PKEY_free(pkey);
  43031. DH_free(setDh);
  43032. DH_free(dh);
  43033. res = TEST_RES_CHECK(1);
  43034. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43035. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43036. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43037. return res;
  43038. } /* END test_EVP_PKEY_set1_get1_DH */
  43039. static int test_wolfSSL_CTX_ctrl(void)
  43040. {
  43041. int res = TEST_SKIPPED;
  43042. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43043. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43044. char caFile[] = "./certs/client-ca.pem";
  43045. char clientFile[] = "./certs/client-cert.pem";
  43046. SSL_CTX* ctx;
  43047. X509* x509 = NULL;
  43048. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43049. byte buf[6000];
  43050. char file[] = "./certs/dsaparams.pem";
  43051. XFILE f;
  43052. int bytes;
  43053. BIO* bio;
  43054. DSA* dsa;
  43055. DH* dh;
  43056. #endif
  43057. #ifdef HAVE_ECC
  43058. WOLFSSL_EC_KEY* ecKey;
  43059. #endif
  43060. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43061. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  43062. AssertNotNull(x509);
  43063. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43064. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  43065. AssertNotNull(x509);
  43066. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43067. /* Initialize DH */
  43068. f = XFOPEN(file, "rb");
  43069. AssertTrue((f != XBADFILE));
  43070. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  43071. XFCLOSE(f);
  43072. bio = BIO_new_mem_buf((void*)buf, bytes);
  43073. AssertNotNull(bio);
  43074. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  43075. AssertNotNull(dsa);
  43076. dh = wolfSSL_DSA_dup_DH(dsa);
  43077. AssertNotNull(dh);
  43078. #endif
  43079. #ifdef HAVE_ECC
  43080. /* Initialize WOLFSSL_EC_KEY */
  43081. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  43082. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  43083. #endif
  43084. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  43085. /* additional test of getting EVP_PKEY key size from X509
  43086. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43087. * allowed with user RSA */
  43088. {
  43089. EVP_PKEY* pkey;
  43090. #if defined(HAVE_ECC)
  43091. X509* ecX509;
  43092. #endif /* HAVE_ECC */
  43093. AssertNotNull(pkey = X509_get_pubkey(x509));
  43094. /* current RSA key is 2048 bit (256 bytes) */
  43095. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  43096. EVP_PKEY_free(pkey);
  43097. #if defined(HAVE_ECC)
  43098. #if defined(USE_CERT_BUFFERS_256)
  43099. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43100. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43101. SSL_FILETYPE_ASN1));
  43102. #else
  43103. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43104. cliEccCertFile, SSL_FILETYPE_PEM));
  43105. #endif
  43106. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  43107. /* current ECC key is 256 bit (32 bytes) */
  43108. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  43109. X509_free(ecX509);
  43110. EVP_PKEY_free(pkey);
  43111. #endif /* HAVE_ECC */
  43112. }
  43113. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  43114. /* Tests should fail with passed in NULL pointer */
  43115. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  43116. SSL_FAILURE);
  43117. #if !defined(NO_DH) && !defined(NO_DSA)
  43118. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  43119. SSL_FAILURE);
  43120. #endif
  43121. #ifdef HAVE_ECC
  43122. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  43123. SSL_FAILURE);
  43124. #endif
  43125. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43126. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43127. */
  43128. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  43129. SSL_SUCCESS);
  43130. /* Test with SSL_CTRL_OPTIONS
  43131. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43132. */
  43133. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  43134. == SSL_OP_NO_TLSv1);
  43135. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43136. /* Test with SSL_CTRL_SET_TMP_DH
  43137. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43138. */
  43139. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43140. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  43141. SSL_SUCCESS);
  43142. #endif
  43143. /* Test with SSL_CTRL_SET_TMP_ECDH
  43144. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43145. */
  43146. #ifdef HAVE_ECC
  43147. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  43148. SSL_SUCCESS);
  43149. #endif
  43150. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43151. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  43152. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43153. #endif
  43154. /* Test for min/max proto */
  43155. #ifndef WOLFSSL_NO_TLS12
  43156. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43157. 0, NULL), SSL_SUCCESS);
  43158. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43159. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43160. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43161. #endif
  43162. #ifdef WOLFSSL_TLS13
  43163. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43164. 0, NULL), SSL_SUCCESS);
  43165. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43166. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43167. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43168. #ifndef WOLFSSL_NO_TLS12
  43169. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43170. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43171. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43172. #endif
  43173. #endif
  43174. /* Cleanup and Pass */
  43175. #if !defined(NO_DH) && !defined(NO_DSA)
  43176. #ifndef NO_BIO
  43177. BIO_free(bio);
  43178. DSA_free(dsa);
  43179. DH_free(dh);
  43180. #endif
  43181. #endif
  43182. #ifdef HAVE_ECC
  43183. wolfSSL_EC_KEY_free(ecKey);
  43184. #endif
  43185. SSL_CTX_free(ctx);
  43186. res = TEST_RES_CHECK(1);
  43187. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43188. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43189. return res;
  43190. }
  43191. static int test_wolfSSL_EVP_PKEY_assign(void)
  43192. {
  43193. int res = TEST_SKIPPED;
  43194. int type;
  43195. WOLFSSL_EVP_PKEY* pkey;
  43196. #ifndef NO_RSA
  43197. WOLFSSL_RSA* rsa;
  43198. #endif
  43199. #ifndef NO_DSA
  43200. WOLFSSL_DSA* dsa;
  43201. #endif
  43202. #ifdef HAVE_ECC
  43203. WOLFSSL_EC_KEY* ecKey;
  43204. #endif
  43205. (void)pkey;
  43206. #ifndef NO_RSA
  43207. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43208. type = EVP_PKEY_RSA;
  43209. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43210. AssertNotNull(rsa = wolfSSL_RSA_new());
  43211. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  43212. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  43213. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  43214. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  43215. wolfSSL_EVP_PKEY_free(pkey);
  43216. res = TEST_RES_CHECK(1);
  43217. }
  43218. #endif /* NO_RSA */
  43219. #ifndef NO_DSA
  43220. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43221. type = EVP_PKEY_DSA;
  43222. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43223. AssertNotNull(dsa = wolfSSL_DSA_new());
  43224. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  43225. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  43226. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  43227. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  43228. wolfSSL_EVP_PKEY_free(pkey);
  43229. res = TEST_RES_CHECK(1);
  43230. }
  43231. #endif /* NO_DSA */
  43232. #ifdef HAVE_ECC
  43233. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43234. type = EVP_PKEY_EC;
  43235. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43236. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  43237. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  43238. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  43239. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  43240. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  43241. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43242. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  43243. wolfSSL_EVP_PKEY_free(pkey);
  43244. res = TEST_RES_CHECK(1);
  43245. }
  43246. #endif /* HAVE_ECC */
  43247. (void)type;
  43248. return res;
  43249. }
  43250. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43251. {
  43252. WOLFSSL_EVP_PKEY* pkey;
  43253. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43254. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43255. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43256. EVP_PKEY_free(pkey);
  43257. return TEST_RES_CHECK(1);
  43258. }
  43259. static int test_wolfSSL_EVP_PKEY_id(void)
  43260. {
  43261. WOLFSSL_EVP_PKEY* pkey;
  43262. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43263. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43264. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43265. EVP_PKEY_free(pkey);
  43266. return TEST_RES_CHECK(1);
  43267. }
  43268. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43269. {
  43270. int res = TEST_SKIPPED;
  43271. #if defined(OPENSSL_ALL) && \
  43272. !defined(NO_ECC_SECP) && \
  43273. /* This last bit is taken from ecc.c. It is the condition that
  43274. * defines ECC256 */ \
  43275. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43276. ECC_MIN_KEY_SZ <= 256)
  43277. EVP_PKEY_CTX* ctx;
  43278. EVP_PKEY* pkey = NULL;
  43279. /* Test error conditions. */
  43280. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43281. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43282. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43283. #ifndef NO_RSA
  43284. EVP_PKEY_CTX_free(ctx);
  43285. /* Parameter generation for RSA not supported yet. */
  43286. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43287. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43288. #endif
  43289. #ifdef HAVE_ECC
  43290. EVP_PKEY_CTX_free(ctx);
  43291. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43292. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43293. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43294. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43295. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43296. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43297. WOLFSSL_SUCCESS);
  43298. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43299. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43300. #endif
  43301. EVP_PKEY_CTX_free(ctx);
  43302. EVP_PKEY_free(pkey);
  43303. res = TEST_RES_CHECK(1);
  43304. #endif
  43305. return res;
  43306. }
  43307. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43308. {
  43309. WOLFSSL_EVP_PKEY* pkey = NULL;
  43310. EVP_PKEY_CTX* ctx = NULL;
  43311. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43312. WOLFSSL_EVP_PKEY* params = NULL;
  43313. DH* dh = NULL;
  43314. const BIGNUM* pubkey = NULL;
  43315. const BIGNUM* privkey = NULL;
  43316. ASN1_INTEGER* asn1int = NULL;
  43317. unsigned int length = 0;
  43318. byte* derBuffer = NULL;
  43319. #endif
  43320. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43321. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43322. /* Bad cases */
  43323. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43324. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43325. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43326. /* Good case */
  43327. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43328. EVP_PKEY_CTX_free(ctx);
  43329. EVP_PKEY_free(pkey);
  43330. pkey = NULL;
  43331. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43332. /* Test DH keygen */
  43333. {
  43334. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  43335. AssertNotNull(dh = DH_get_2048_256());
  43336. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43337. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43338. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43339. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43340. DH_free(dh);
  43341. EVP_PKEY_CTX_free(ctx);
  43342. EVP_PKEY_free(params);
  43343. /* try exporting generated key to DER, to verify */
  43344. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43345. DH_get0_key(dh, &pubkey, &privkey);
  43346. AssertNotNull(pubkey);
  43347. AssertNotNull(privkey);
  43348. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43349. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43350. ASN1_INTEGER_free(asn1int);
  43351. DH_free(dh);
  43352. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43353. EVP_PKEY_free(pkey);
  43354. }
  43355. #endif
  43356. return TEST_RES_CHECK(1);
  43357. }
  43358. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43359. {
  43360. WOLFSSL_EVP_PKEY* pkey;
  43361. EVP_PKEY_CTX *ctx;
  43362. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43363. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43364. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43365. EVP_PKEY_CTX_free(ctx);
  43366. EVP_PKEY_free(pkey);
  43367. return TEST_RES_CHECK(1);
  43368. }
  43369. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43370. {
  43371. int res = TEST_SKIPPED;
  43372. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43373. WOLFSSL_EVP_PKEY* pkey;
  43374. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43375. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43376. EVP_PKEY_free(pkey);
  43377. res = TEST_RES_CHECK(1);
  43378. #endif
  43379. return res;
  43380. }
  43381. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43382. {
  43383. int res = TEST_SKIPPED;
  43384. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43385. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43386. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43387. !defined(NO_FILESYSTEM)
  43388. WOLFSSL_EVP_PKEY* params = NULL;
  43389. WOLFSSL_EVP_PKEY* copy = NULL;
  43390. DH* dh = NULL;
  43391. BIGNUM* p1;
  43392. BIGNUM* g1;
  43393. BIGNUM* q1;
  43394. BIGNUM* p2;
  43395. BIGNUM* g2;
  43396. BIGNUM* q2;
  43397. /* create DH with DH_get_2048_256 params */
  43398. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  43399. AssertNotNull(dh = DH_get_2048_256());
  43400. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43401. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43402. (const BIGNUM**)&q1,
  43403. (const BIGNUM**)&g1);
  43404. DH_free(dh);
  43405. /* create DH with random generated DH params */
  43406. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  43407. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43408. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43409. DH_free(dh);
  43410. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43411. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  43412. AssertNotNull(dh->p);
  43413. AssertNotNull(dh->g);
  43414. AssertNotNull(dh->q);
  43415. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43416. (const BIGNUM**)&q2,
  43417. (const BIGNUM**)&g2);
  43418. AssertIntEQ(BN_cmp(p1, p2), 0);
  43419. AssertIntEQ(BN_cmp(q1, q2), 0);
  43420. AssertIntEQ(BN_cmp(g1, g2), 0);
  43421. DH_free(dh);
  43422. EVP_PKEY_free(copy);
  43423. EVP_PKEY_free(params);
  43424. res = TEST_RES_CHECK(1);
  43425. #endif
  43426. return res;
  43427. }
  43428. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43429. {
  43430. WOLFSSL_EVP_PKEY* pkey;
  43431. EVP_PKEY_CTX *ctx;
  43432. int bits = 2048;
  43433. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43434. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43435. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43436. WOLFSSL_SUCCESS);
  43437. EVP_PKEY_CTX_free(ctx);
  43438. EVP_PKEY_free(pkey);
  43439. return TEST_RES_CHECK(1);
  43440. }
  43441. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43442. {
  43443. /* This is large enough to be used for all key sizes */
  43444. byte key[AES_256_KEY_SIZE] = {0};
  43445. byte iv[AES_BLOCK_SIZE] = {0};
  43446. int i, enumlen;
  43447. EVP_CIPHER_CTX *ctx;
  43448. const EVP_CIPHER *init;
  43449. int enumArray[] = {
  43450. #ifdef HAVE_AES_CBC
  43451. NID_aes_128_cbc,
  43452. #endif
  43453. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43454. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43455. #ifdef HAVE_AESGCM
  43456. NID_aes_128_gcm,
  43457. #endif
  43458. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43459. #ifdef WOLFSSL_AES_COUNTER
  43460. NID_aes_128_ctr,
  43461. #endif
  43462. #ifndef NO_DES3
  43463. NID_des_cbc,
  43464. NID_des_ede3_cbc,
  43465. #endif
  43466. };
  43467. int iv_lengths[] = {
  43468. #ifdef HAVE_AES_CBC
  43469. AES_BLOCK_SIZE,
  43470. #endif
  43471. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43472. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43473. #ifdef HAVE_AESGCM
  43474. GCM_NONCE_MID_SZ,
  43475. #endif
  43476. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43477. #ifdef WOLFSSL_AES_COUNTER
  43478. AES_BLOCK_SIZE,
  43479. #endif
  43480. #ifndef NO_DES3
  43481. DES_BLOCK_SIZE,
  43482. DES_BLOCK_SIZE,
  43483. #endif
  43484. };
  43485. enumlen = (sizeof(enumArray)/sizeof(int));
  43486. for (i = 0; i < enumlen; i++) {
  43487. ctx = EVP_CIPHER_CTX_new();
  43488. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  43489. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43490. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43491. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43492. EVP_CIPHER_CTX_free(ctx);
  43493. }
  43494. return TEST_RES_CHECK(1);
  43495. }
  43496. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43497. {
  43498. int res = TEST_SKIPPED;
  43499. #if !defined(NO_DES3)
  43500. byte key[AES_256_KEY_SIZE] = {0};
  43501. byte iv[AES_BLOCK_SIZE] = {0};
  43502. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43503. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43504. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43505. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43506. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  43507. EVP_CIPHER_CTX_free(ctx);
  43508. res = TEST_RES_CHECK(1);
  43509. #endif
  43510. return res;
  43511. }
  43512. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  43513. {
  43514. int res = TEST_SKIPPED;
  43515. #if !defined(NO_DES3)
  43516. byte key[AES_256_KEY_SIZE] = {0};
  43517. byte iv[AES_BLOCK_SIZE] = {0};
  43518. int keylen;
  43519. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43520. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43521. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43522. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43523. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43524. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  43525. WOLFSSL_SUCCESS);
  43526. EVP_CIPHER_CTX_free(ctx);
  43527. res = TEST_RES_CHECK(1);
  43528. #endif
  43529. return res;
  43530. }
  43531. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43532. {
  43533. int res = TEST_SKIPPED;
  43534. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43535. byte key[DES3_KEY_SIZE] = {0};
  43536. byte iv[DES_BLOCK_SIZE] = {0};
  43537. int ivLen, keyLen;
  43538. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43539. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43540. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43541. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43542. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43543. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43544. /* Bad cases */
  43545. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  43546. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  43547. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43548. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43549. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  43550. /* Good case */
  43551. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43552. EVP_CIPHER_CTX_free(ctx);
  43553. res = TEST_RES_CHECK(1);
  43554. #endif
  43555. return res;
  43556. }
  43557. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43558. {
  43559. WOLFSSL_ENGINE* e = NULL;
  43560. int id = 0;
  43561. EVP_PKEY_CTX *ctx;
  43562. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43563. EVP_PKEY_CTX_free(ctx);
  43564. return TEST_RES_CHECK(1);
  43565. }
  43566. static int test_wolfSSL_EVP_rc4(void)
  43567. {
  43568. int res = TEST_SKIPPED;
  43569. #if !defined(NO_RC4)
  43570. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  43571. #endif
  43572. return res;
  43573. }
  43574. static int test_wolfSSL_EVP_enc_null(void)
  43575. {
  43576. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  43577. }
  43578. static int test_wolfSSL_EVP_rc2_cbc(void)
  43579. {
  43580. int res = TEST_SKIPPED;
  43581. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43582. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  43583. #endif
  43584. return res;
  43585. }
  43586. static int test_wolfSSL_EVP_mdc2(void)
  43587. {
  43588. int res = TEST_SKIPPED;
  43589. #if !defined(NO_WOLFSSL_STUB)
  43590. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  43591. #endif
  43592. return res;
  43593. }
  43594. static int test_wolfSSL_EVP_md4(void)
  43595. {
  43596. int res = TEST_SKIPPED;
  43597. #if !defined(NO_MD4)
  43598. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  43599. #endif
  43600. return res;
  43601. }
  43602. static int test_wolfSSL_EVP_aes_256_gcm(void)
  43603. {
  43604. int res = TEST_SKIPPED;
  43605. #ifdef HAVE_AESGCM
  43606. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  43607. #endif
  43608. return res;
  43609. }
  43610. static int test_wolfSSL_EVP_aes_192_gcm(void)
  43611. {
  43612. int res = TEST_SKIPPED;
  43613. #ifdef HAVE_AESGCM
  43614. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  43615. #endif
  43616. return res;
  43617. }
  43618. static int test_wolfSSL_EVP_aes_256_ccm(void)
  43619. {
  43620. int res = TEST_SKIPPED;
  43621. #ifdef HAVE_AESCCM
  43622. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_ccm() != NULL);
  43623. #endif
  43624. return res;
  43625. }
  43626. static int test_wolfSSL_EVP_aes_192_ccm(void)
  43627. {
  43628. int res = TEST_SKIPPED;
  43629. #ifdef HAVE_AESCCM
  43630. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_ccm() != NULL);
  43631. #endif
  43632. return res;
  43633. }
  43634. static int test_wolfSSL_EVP_aes_128_ccm(void)
  43635. {
  43636. int res = TEST_SKIPPED;
  43637. #ifdef HAVE_AESCCM
  43638. res = TEST_RES_CHECK(wolfSSL_EVP_aes_128_ccm() != NULL);
  43639. #endif
  43640. return res;
  43641. }
  43642. static int test_wolfSSL_EVP_ripemd160(void)
  43643. {
  43644. int res = TEST_SKIPPED;
  43645. #if !defined(NO_WOLFSSL_STUB)
  43646. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  43647. #endif
  43648. return res;
  43649. }
  43650. static int test_wolfSSL_EVP_get_digestbynid(void)
  43651. {
  43652. #ifndef NO_MD5
  43653. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  43654. #endif
  43655. #ifndef NO_SHA
  43656. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  43657. #endif
  43658. #ifndef NO_SHA256
  43659. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  43660. #endif
  43661. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  43662. return TEST_RES_CHECK(1);
  43663. }
  43664. static int test_wolfSSL_EVP_MD_nid(void)
  43665. {
  43666. #ifndef NO_MD5
  43667. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  43668. #endif
  43669. #ifndef NO_SHA
  43670. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  43671. #endif
  43672. #ifndef NO_SHA256
  43673. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  43674. #endif
  43675. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  43676. return TEST_RES_CHECK(1);
  43677. }
  43678. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  43679. {
  43680. int res = TEST_SKIPPED;
  43681. #if defined(HAVE_ECC)
  43682. WOLFSSL_EVP_PKEY* pkey;
  43683. AssertNotNull(pkey = EVP_PKEY_new());
  43684. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  43685. EVP_PKEY_free(pkey);
  43686. res = TEST_RES_CHECK(1);
  43687. #endif
  43688. return res;
  43689. }
  43690. static int test_wolfSSL_EVP_X_STATE(void)
  43691. {
  43692. int res = TEST_SKIPPED;
  43693. #if !defined(NO_DES3) && !defined(NO_RC4)
  43694. byte key[DES3_KEY_SIZE] = {0};
  43695. byte iv[DES_IV_SIZE] = {0};
  43696. EVP_CIPHER_CTX *ctx;
  43697. const EVP_CIPHER *init;
  43698. /* Bad test cases */
  43699. ctx = EVP_CIPHER_CTX_new();
  43700. init = EVP_des_ede3_cbc();
  43701. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43702. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43703. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  43704. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  43705. EVP_CIPHER_CTX_free(ctx);
  43706. /* Good test case */
  43707. ctx = EVP_CIPHER_CTX_new();
  43708. init = wolfSSL_EVP_rc4();
  43709. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43710. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43711. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  43712. EVP_CIPHER_CTX_free(ctx);
  43713. res = TEST_RES_CHECK(1);
  43714. #endif
  43715. return res;
  43716. }
  43717. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  43718. {
  43719. int res = TEST_SKIPPED;
  43720. #if !defined(NO_DES3) && !defined(NO_RC4)
  43721. byte key[DES3_KEY_SIZE] = {0};
  43722. byte iv[DES_IV_SIZE] = {0};
  43723. EVP_CIPHER_CTX *ctx;
  43724. const EVP_CIPHER *init;
  43725. /* Bad test cases */
  43726. ctx = EVP_CIPHER_CTX_new();
  43727. init = EVP_des_ede3_cbc();
  43728. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43729. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43730. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  43731. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  43732. EVP_CIPHER_CTX_free(ctx);
  43733. /* Good test case */
  43734. ctx = EVP_CIPHER_CTX_new();
  43735. init = wolfSSL_EVP_rc4();
  43736. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43737. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43738. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  43739. EVP_CIPHER_CTX_free(ctx);
  43740. res = TEST_RES_CHECK(1);
  43741. #endif
  43742. return res;
  43743. }
  43744. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  43745. {
  43746. int res = TEST_SKIPPED;
  43747. #ifdef HAVE_AES_CBC
  43748. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43749. #ifdef WOLFSSL_AES_128
  43750. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  43751. #endif
  43752. #ifdef WOLFSSL_AES_192
  43753. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  43754. #endif
  43755. #ifdef WOLFSSL_AES_256
  43756. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  43757. #endif
  43758. res = TEST_RES_CHECK(1);
  43759. }
  43760. #endif
  43761. #ifdef HAVE_AESGCM
  43762. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43763. #ifdef WOLFSSL_AES_128
  43764. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  43765. #endif
  43766. #ifdef WOLFSSL_AES_192
  43767. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  43768. #endif
  43769. #ifdef WOLFSSL_AES_256
  43770. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  43771. #endif
  43772. res = TEST_RES_CHECK(1);
  43773. }
  43774. #endif
  43775. #ifdef HAVE_AESCCM
  43776. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43777. #ifdef WOLFSSL_AES_128
  43778. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  43779. #endif
  43780. #ifdef WOLFSSL_AES_192
  43781. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  43782. #endif
  43783. #ifdef WOLFSSL_AES_256
  43784. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  43785. #endif
  43786. res = TEST_RES_CHECK(1);
  43787. }
  43788. #endif
  43789. #ifdef WOLFSSL_AES_COUNTER
  43790. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43791. #ifdef WOLFSSL_AES_128
  43792. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  43793. #endif
  43794. #ifdef WOLFSSL_AES_192
  43795. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  43796. #endif
  43797. #ifdef WOLFSSL_AES_256
  43798. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  43799. #endif
  43800. res = TEST_RES_CHECK(1);
  43801. }
  43802. #endif
  43803. #ifdef HAVE_AES_ECB
  43804. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43805. #ifdef WOLFSSL_AES_128
  43806. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  43807. #endif
  43808. #ifdef WOLFSSL_AES_192
  43809. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  43810. #endif
  43811. #ifdef WOLFSSL_AES_256
  43812. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  43813. #endif
  43814. res = TEST_RES_CHECK(1);
  43815. }
  43816. #endif
  43817. #ifdef WOLFSSL_AES_OFB
  43818. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43819. #ifdef WOLFSSL_AES_128
  43820. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  43821. #endif
  43822. #ifdef WOLFSSL_AES_192
  43823. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  43824. #endif
  43825. #ifdef WOLFSSL_AES_256
  43826. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  43827. #endif
  43828. res = TEST_RES_CHECK(1);
  43829. }
  43830. #endif
  43831. #ifndef NO_RC4
  43832. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43833. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  43834. res = TEST_RES_CHECK(1);
  43835. }
  43836. #endif
  43837. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43838. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  43839. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  43840. res = TEST_RES_CHECK(1);
  43841. }
  43842. #endif
  43843. return res;
  43844. }
  43845. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  43846. {
  43847. int i, enumlen;
  43848. int enumArray[] = {
  43849. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43850. #ifdef WOLFSSL_AES_128
  43851. NID_aes_128_cbc,
  43852. #endif
  43853. #ifdef WOLFSSL_AES_192
  43854. NID_aes_192_cbc,
  43855. #endif
  43856. #ifdef WOLFSSL_AES_256
  43857. NID_aes_256_cbc,
  43858. #endif
  43859. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43860. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43861. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43862. #ifdef HAVE_AESGCM
  43863. #ifdef WOLFSSL_AES_128
  43864. NID_aes_128_gcm,
  43865. #endif
  43866. #ifdef WOLFSSL_AES_192
  43867. NID_aes_192_gcm,
  43868. #endif
  43869. #ifdef WOLFSSL_AES_256
  43870. NID_aes_256_gcm,
  43871. #endif
  43872. #endif /* HAVE_AESGCM */
  43873. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43874. #ifdef WOLFSSL_AES_COUNTER
  43875. #ifdef WOLFSSL_AES_128
  43876. NID_aes_128_ctr,
  43877. #endif
  43878. #ifdef WOLFSSL_AES_192
  43879. NID_aes_192_ctr,
  43880. #endif
  43881. #ifdef WOLFSSL_AES_256
  43882. NID_aes_256_ctr,
  43883. #endif
  43884. #endif
  43885. #ifndef NO_DES3
  43886. NID_des_cbc,
  43887. NID_des_ede3_cbc,
  43888. #endif
  43889. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43890. NID_chacha20_poly1305,
  43891. #endif
  43892. };
  43893. int iv_lengths[] = {
  43894. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43895. #ifdef WOLFSSL_AES_128
  43896. AES_BLOCK_SIZE,
  43897. #endif
  43898. #ifdef WOLFSSL_AES_192
  43899. AES_BLOCK_SIZE,
  43900. #endif
  43901. #ifdef WOLFSSL_AES_256
  43902. AES_BLOCK_SIZE,
  43903. #endif
  43904. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43905. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43906. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43907. #ifdef HAVE_AESGCM
  43908. #ifdef WOLFSSL_AES_128
  43909. GCM_NONCE_MID_SZ,
  43910. #endif
  43911. #ifdef WOLFSSL_AES_192
  43912. GCM_NONCE_MID_SZ,
  43913. #endif
  43914. #ifdef WOLFSSL_AES_256
  43915. GCM_NONCE_MID_SZ,
  43916. #endif
  43917. #endif /* HAVE_AESGCM */
  43918. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43919. #ifdef WOLFSSL_AES_COUNTER
  43920. #ifdef WOLFSSL_AES_128
  43921. AES_BLOCK_SIZE,
  43922. #endif
  43923. #ifdef WOLFSSL_AES_192
  43924. AES_BLOCK_SIZE,
  43925. #endif
  43926. #ifdef WOLFSSL_AES_256
  43927. AES_BLOCK_SIZE,
  43928. #endif
  43929. #endif
  43930. #ifndef NO_DES3
  43931. DES_BLOCK_SIZE,
  43932. DES_BLOCK_SIZE,
  43933. #endif
  43934. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43935. CHACHA20_POLY1305_AEAD_IV_SIZE,
  43936. #endif
  43937. };
  43938. enumlen = (sizeof(enumArray)/sizeof(int));
  43939. for (i = 0; i < enumlen; i++) {
  43940. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  43941. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  43942. }
  43943. return TEST_RES_CHECK(1);
  43944. }
  43945. static int test_wolfSSL_EVP_SignInit_ex(void)
  43946. {
  43947. WOLFSSL_EVP_MD_CTX mdCtx;
  43948. WOLFSSL_ENGINE* e = 0;
  43949. const EVP_MD* md;
  43950. md = "SHA256";
  43951. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43952. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  43953. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43954. return TEST_RES_CHECK(1);
  43955. }
  43956. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  43957. {
  43958. int res = TEST_SKIPPED;
  43959. #if !defined(NO_SHA256)
  43960. WOLFSSL_EVP_MD_CTX mdCtx;
  43961. unsigned int s = 0;
  43962. unsigned char md[WC_SHA256_DIGEST_SIZE];
  43963. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  43964. /* Bad Case */
  43965. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43966. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43967. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  43968. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43969. #else
  43970. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43971. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  43972. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43973. #endif
  43974. /* Good Case */
  43975. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43976. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  43977. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  43978. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43979. res = TEST_RES_CHECK(1);
  43980. #endif
  43981. return res;
  43982. }
  43983. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43984. {
  43985. int res = TEST_SKIPPED;
  43986. #if !defined(NO_DH) && \
  43987. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43988. FILE* f = NULL;
  43989. unsigned char buf[4096];
  43990. const unsigned char* pt = buf;
  43991. const char* params1 = "./certs/dh2048.der";
  43992. long len = 0;
  43993. WOLFSSL_DH* dh = NULL;
  43994. WOLFSSL_EVP_PKEY* pkey;
  43995. XMEMSET(buf, 0, sizeof(buf));
  43996. f = XFOPEN(params1, "rb");
  43997. AssertTrue(f != XBADFILE);
  43998. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  43999. XFCLOSE(f);
  44000. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  44001. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  44002. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44003. /* Bad cases */
  44004. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  44005. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  44006. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  44007. /* Good case */
  44008. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  44009. EVP_PKEY_free(pkey);
  44010. res = TEST_RES_CHECK(1);
  44011. #endif
  44012. return res;
  44013. }
  44014. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44015. {
  44016. int res = TEST_SKIPPED;
  44017. #if defined(OPENSSL_EXTRA)
  44018. EVP_PKEY* pkey;
  44019. EVP_PKEY_CTX* ctx;
  44020. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44021. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44022. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44023. /* void */
  44024. EVP_PKEY_CTX_free(ctx);
  44025. AssertTrue(1);
  44026. #else
  44027. /* int */
  44028. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44029. #endif
  44030. EVP_PKEY_free(pkey);
  44031. res = TEST_RES_CHECK(1);
  44032. #endif
  44033. return res;
  44034. }
  44035. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44036. {
  44037. int res = TEST_SKIPPED;
  44038. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44039. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44040. DH *dh = NULL;
  44041. DH *setDh = NULL;
  44042. EVP_PKEY *pkey = NULL;
  44043. EVP_PKEY_CTX* ctx = NULL;
  44044. FILE* f = NULL;
  44045. unsigned char buf[512];
  44046. const unsigned char* pt = buf;
  44047. const char* dh2048 = "./certs/dh2048.der";
  44048. long len = 0;
  44049. int code = -1;
  44050. XMEMSET(buf, 0, sizeof(buf));
  44051. f = XFOPEN(dh2048, "rb");
  44052. AssertTrue(f != XBADFILE);
  44053. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  44054. XFCLOSE(f);
  44055. /* Load dh2048.der into DH with internal format */
  44056. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44057. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44058. AssertIntEQ(code, 0);
  44059. code = -1;
  44060. pkey = wolfSSL_EVP_PKEY_new();
  44061. /* Set DH into PKEY */
  44062. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44063. /* create ctx from pkey */
  44064. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44065. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44066. /* */
  44067. /* TO DO invlaid case */
  44068. /* */
  44069. EVP_PKEY_CTX_free(ctx);
  44070. EVP_PKEY_free(pkey);
  44071. DH_free(setDh);
  44072. DH_free(dh);
  44073. res = TEST_RES_CHECK(1);
  44074. #endif
  44075. #endif
  44076. return res;
  44077. }
  44078. static int test_wolfSSL_EVP_BytesToKey(void)
  44079. {
  44080. int res = TEST_SKIPPED;
  44081. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44082. byte key[AES_BLOCK_SIZE] = {0};
  44083. byte iv[AES_BLOCK_SIZE] = {0};
  44084. int sz = 5;
  44085. int count = 0;
  44086. const EVP_MD* md = "SHA256";
  44087. const EVP_CIPHER *type;
  44088. const unsigned char *salt = (unsigned char *)"salt1234";
  44089. const byte data[] = {
  44090. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44091. 0x72,0x6c,0x64
  44092. };
  44093. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44094. /* Bad cases */
  44095. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44096. 0);
  44097. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44098. 16);
  44099. md = "2";
  44100. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44101. WOLFSSL_FAILURE);
  44102. /* Good case */
  44103. md = "SHA256";
  44104. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44105. 16);
  44106. res = TEST_RES_CHECK(1);
  44107. #endif
  44108. return res;
  44109. }
  44110. static int test_evp_cipher_aes_gcm(void)
  44111. {
  44112. int res = TEST_SKIPPED;
  44113. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44114. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44115. (HAVE_FIPS_VERSION >= 2)))
  44116. /*
  44117. * This test checks data at various points in the encrypt/decrypt process
  44118. * against known values produced using the same test with OpenSSL. This
  44119. * interop testing is critical for verifying the correctness of our
  44120. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44121. * a flow supported by OpenSSL that uses the control command
  44122. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44123. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44124. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44125. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44126. * happened to "cancel out" if both sides of the connection had the bug.
  44127. */
  44128. enum {
  44129. NUM_ENCRYPTIONS = 3,
  44130. AAD_SIZE = 4
  44131. };
  44132. byte plainText1[] = {
  44133. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44134. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44135. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44136. };
  44137. byte plainText2[] = {
  44138. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44139. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44140. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44141. };
  44142. byte plainText3[] = {
  44143. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44144. 0x5b, 0x57, 0x10
  44145. };
  44146. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44147. plainText1,
  44148. plainText2,
  44149. plainText3
  44150. };
  44151. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44152. sizeof(plainText1),
  44153. sizeof(plainText2),
  44154. sizeof(plainText3)
  44155. };
  44156. byte aad1[AAD_SIZE] = {
  44157. 0x00, 0x00, 0x00, 0x01
  44158. };
  44159. byte aad2[AAD_SIZE] = {
  44160. 0x00, 0x00, 0x00, 0x10
  44161. };
  44162. byte aad3[AAD_SIZE] = {
  44163. 0x00, 0x00, 0x01, 0x00
  44164. };
  44165. byte* aads[NUM_ENCRYPTIONS] = {
  44166. aad1,
  44167. aad2,
  44168. aad3
  44169. };
  44170. const byte iv[GCM_NONCE_MID_SZ] = {
  44171. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44172. };
  44173. byte currentIv[GCM_NONCE_MID_SZ];
  44174. const byte key[] = {
  44175. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44176. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44177. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44178. };
  44179. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44180. {
  44181. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44182. 0xEF
  44183. },
  44184. {
  44185. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44186. 0xF0
  44187. },
  44188. {
  44189. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44190. 0xF1
  44191. }
  44192. };
  44193. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44194. {
  44195. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44196. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44197. },
  44198. {
  44199. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44200. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44201. },
  44202. {
  44203. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44204. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44205. }
  44206. };
  44207. const byte expCipherText1[] = {
  44208. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44209. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44210. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44211. };
  44212. const byte expCipherText2[] = {
  44213. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44214. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44215. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44216. };
  44217. const byte expCipherText3[] = {
  44218. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44219. 0x80, 0x5E, 0x6B,
  44220. };
  44221. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44222. expCipherText1,
  44223. expCipherText2,
  44224. expCipherText3
  44225. };
  44226. byte* cipherText;
  44227. byte* calcPlainText;
  44228. byte tag[AES_BLOCK_SIZE];
  44229. EVP_CIPHER_CTX* encCtx = NULL;
  44230. EVP_CIPHER_CTX* decCtx = NULL;
  44231. int i, j, outl;
  44232. /****************************************************/
  44233. for (i = 0; i < 3; ++i) {
  44234. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  44235. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  44236. /* First iteration, set key before IV. */
  44237. if (i == 0) {
  44238. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44239. SSL_SUCCESS);
  44240. /*
  44241. * The call to EVP_CipherInit below (with NULL key) should clear the
  44242. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44243. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44244. * behavior.
  44245. */
  44246. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44247. (void*)iv), SSL_SUCCESS);
  44248. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44249. SSL_SUCCESS);
  44250. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44251. currentIv), SSL_FAILURE);
  44252. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44253. SSL_SUCCESS);
  44254. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44255. SSL_SUCCESS);
  44256. }
  44257. /* Second iteration, IV before key. */
  44258. else {
  44259. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44260. SSL_SUCCESS);
  44261. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44262. SSL_SUCCESS);
  44263. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44264. SSL_SUCCESS);
  44265. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44266. SSL_SUCCESS);
  44267. }
  44268. /*
  44269. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44270. * been issued first.
  44271. */
  44272. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44273. currentIv), SSL_FAILURE);
  44274. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44275. (void*)iv), SSL_SUCCESS);
  44276. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44277. (void*)iv), SSL_SUCCESS);
  44278. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44279. /*************** Encrypt ***************/
  44280. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44281. currentIv), SSL_SUCCESS);
  44282. /* Check current IV against expected. */
  44283. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44284. /* Add AAD. */
  44285. if (i == 2) {
  44286. /* Test streaming API. */
  44287. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44288. AAD_SIZE), SSL_SUCCESS);
  44289. }
  44290. else {
  44291. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44292. AAD_SIZE);
  44293. }
  44294. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44295. DYNAMIC_TYPE_TMP_BUFFER));
  44296. /* Encrypt plaintext. */
  44297. if (i == 2) {
  44298. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44299. plainTexts[j], plainTextSzs[j]),
  44300. SSL_SUCCESS);
  44301. }
  44302. else {
  44303. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44304. plainTextSzs[j]), plainTextSzs[j]);
  44305. }
  44306. if (i == 2) {
  44307. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44308. SSL_SUCCESS);
  44309. }
  44310. else {
  44311. /*
  44312. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44313. * akin to calling EVP_CipherFinal.
  44314. */
  44315. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44316. }
  44317. /* Check ciphertext against expected. */
  44318. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44319. 0);
  44320. /* Get and check tag against expected. */
  44321. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44322. sizeof(tag), tag), SSL_SUCCESS);
  44323. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44324. /*************** Decrypt ***************/
  44325. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44326. currentIv), SSL_SUCCESS);
  44327. /* Check current IV against expected. */
  44328. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44329. /* Add AAD. */
  44330. if (i == 2) {
  44331. /* Test streaming API. */
  44332. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44333. AAD_SIZE), SSL_SUCCESS);
  44334. }
  44335. else {
  44336. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44337. AAD_SIZE);
  44338. }
  44339. /* Set expected tag. */
  44340. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44341. sizeof(tag), tag), SSL_SUCCESS);
  44342. /* Decrypt ciphertext. */
  44343. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44344. DYNAMIC_TYPE_TMP_BUFFER));
  44345. if (i == 2) {
  44346. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44347. cipherText, plainTextSzs[j]),
  44348. SSL_SUCCESS);
  44349. }
  44350. else {
  44351. /* This first EVP_Cipher call will check the tag, too. */
  44352. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44353. plainTextSzs[j]), plainTextSzs[j]);
  44354. }
  44355. if (i == 2) {
  44356. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44357. SSL_SUCCESS);
  44358. }
  44359. else {
  44360. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44361. }
  44362. /* Check plaintext against expected. */
  44363. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44364. 0);
  44365. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44366. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44367. }
  44368. EVP_CIPHER_CTX_free(encCtx);
  44369. EVP_CIPHER_CTX_free(decCtx);
  44370. }
  44371. res = TEST_RES_CHECK(1);
  44372. #endif
  44373. return res;
  44374. }
  44375. static int test_wolfSSL_OBJ_ln(void)
  44376. {
  44377. const int nid_set[] = {
  44378. NID_commonName,
  44379. NID_serialNumber,
  44380. NID_countryName,
  44381. NID_localityName,
  44382. NID_stateOrProvinceName,
  44383. NID_organizationName,
  44384. NID_organizationalUnitName,
  44385. NID_domainComponent,
  44386. NID_businessCategory,
  44387. NID_jurisdictionCountryName,
  44388. NID_jurisdictionStateOrProvinceName,
  44389. NID_emailAddress
  44390. };
  44391. const char* ln_set[] = {
  44392. "commonName",
  44393. "serialNumber",
  44394. "countryName",
  44395. "localityName",
  44396. "stateOrProvinceName",
  44397. "organizationName",
  44398. "organizationalUnitName",
  44399. "domainComponent",
  44400. "businessCategory",
  44401. "jurisdictionCountryName",
  44402. "jurisdictionStateOrProvinceName",
  44403. "emailAddress",
  44404. };
  44405. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44406. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44407. #ifdef HAVE_ECC
  44408. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44409. {
  44410. EC_builtin_curve r[27];
  44411. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44412. nCurves = EC_get_builtin_curves(r,nCurves);
  44413. for (i = 0; i < nCurves; i++) {
  44414. /* skip ECC_CURVE_INVALID */
  44415. if (r[i].nid != ECC_CURVE_INVALID) {
  44416. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44417. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44418. }
  44419. }
  44420. }
  44421. #endif
  44422. #endif
  44423. for (i = 0; i < maxIdx; i++) {
  44424. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44425. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44426. }
  44427. return TEST_RES_CHECK(1);
  44428. }
  44429. static int test_wolfSSL_OBJ_sn(void)
  44430. {
  44431. int i = 0, maxIdx = 7;
  44432. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44433. NID_stateOrProvinceName,NID_organizationName,
  44434. NID_organizationalUnitName,NID_emailAddress};
  44435. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44436. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44437. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44438. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44439. WOLFSSL_EMAIL_ADDR};
  44440. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44441. for (i = 0; i < maxIdx; i++) {
  44442. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44443. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44444. }
  44445. return TEST_RES_CHECK(1);
  44446. }
  44447. #if !defined(NO_BIO)
  44448. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44449. {
  44450. return lh_strhash(s[3]);
  44451. }
  44452. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44453. {
  44454. return XSTRCMP(a[3], b[3]);
  44455. }
  44456. #endif
  44457. static int test_wolfSSL_TXT_DB(void)
  44458. {
  44459. int res = TEST_SKIPPED;
  44460. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44461. BIO *bio;
  44462. TXT_DB *db = NULL;
  44463. const int columns = 6;
  44464. const char *fields[6] = {
  44465. "V",
  44466. "320926161116Z",
  44467. "",
  44468. "12BD",
  44469. "unknown",
  44470. "/CN=rsa doe",
  44471. };
  44472. char** fields_copy;
  44473. /* Test read */
  44474. AssertNotNull(bio = BIO_new(BIO_s_file()));
  44475. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44476. AssertNotNull(db = TXT_DB_read(bio, columns));
  44477. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44478. DYNAMIC_TYPE_OPENSSL));
  44479. XMEMCPY(fields_copy, fields, sizeof(fields));
  44480. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44481. BIO_free(bio);
  44482. /* Test write */
  44483. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  44484. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  44485. BIO_free(bio);
  44486. /* Test index */
  44487. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44488. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44489. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44490. fields[3] = "12DA";
  44491. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44492. fields[3] = "FFFF";
  44493. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44494. fields[3] = "";
  44495. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44496. TXT_DB_free(db);
  44497. res = TEST_RES_CHECK(1);
  44498. #endif
  44499. return res;
  44500. }
  44501. static int test_wolfSSL_NCONF(void)
  44502. {
  44503. int res = TEST_SKIPPED;
  44504. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44505. const char* confFile = "./tests/NCONF_test.cnf";
  44506. CONF* conf = NULL;
  44507. long eline = 0;
  44508. long num = 0;
  44509. AssertNotNull(conf = NCONF_new(NULL));
  44510. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44511. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44512. AssertIntEQ(num, 1234);
  44513. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44514. AssertIntEQ(num, 4321);
  44515. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44516. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44517. "./test-dir/file1");
  44518. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44519. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44520. NCONF_free(conf);
  44521. res = TEST_RES_CHECK(1);
  44522. #endif
  44523. return res;
  44524. }
  44525. #endif /* OPENSSL_ALL */
  44526. static int test_wolfSSL_X509V3_EXT_get(void) {
  44527. int res = TEST_SKIPPED;
  44528. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44529. FILE* f;
  44530. int numOfExt =0;
  44531. int extNid = 0;
  44532. int i = 0;
  44533. WOLFSSL_X509* x509;
  44534. WOLFSSL_X509_EXTENSION* ext;
  44535. const WOLFSSL_v3_ext_method* method;
  44536. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  44537. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44538. fclose(f);
  44539. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44540. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44541. for (i = 0; i < numOfExt; i++) {
  44542. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44543. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  44544. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44545. AssertIntEQ(method->ext_nid, extNid);
  44546. }
  44547. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44548. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44549. wolfSSL_X509_free(x509);
  44550. res = TEST_RES_CHECK(1);
  44551. #endif
  44552. return res;
  44553. }
  44554. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44555. {
  44556. int res = TEST_SKIPPED;
  44557. #ifdef OPENSSL_ALL
  44558. const char *ext_names[] = {
  44559. "subjectKeyIdentifier",
  44560. "authorityKeyIdentifier",
  44561. "subjectAltName",
  44562. "keyUsage",
  44563. };
  44564. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44565. int ext_nids[] = {
  44566. NID_subject_key_identifier,
  44567. NID_authority_key_identifier,
  44568. NID_subject_alt_name,
  44569. NID_key_usage,
  44570. };
  44571. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44572. const char *ext_values[] = {
  44573. "hash",
  44574. "hash",
  44575. "DNS:example.com, IP:127.0.0.1",
  44576. "digitalSignature,keyEncipherment,dataEncipherment",
  44577. };
  44578. size_t i;
  44579. X509_EXTENSION* ext;
  44580. X509* x509 = X509_new();
  44581. for (i = 0; i < ext_names_count; i++) {
  44582. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  44583. AssertNotNull(ext);
  44584. X509_EXTENSION_free(ext);
  44585. }
  44586. for (i = 0; i < ext_nids_count; i++) {
  44587. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  44588. AssertNotNull(ext);
  44589. X509_EXTENSION_free(ext);
  44590. }
  44591. /* Test adding extension to X509 */
  44592. for (i = 0; i < ext_nids_count; i++) {
  44593. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  44594. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  44595. X509_EXTENSION_free(ext);
  44596. }
  44597. X509_free(x509);
  44598. res = TEST_RES_CHECK(1);
  44599. #endif
  44600. return res;
  44601. }
  44602. static int test_wolfSSL_X509V3_EXT(void) {
  44603. int res = TEST_SKIPPED;
  44604. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44605. FILE* f;
  44606. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  44607. char* str;
  44608. unsigned char* data;
  44609. const WOLFSSL_v3_ext_method* method;
  44610. WOLFSSL_X509* x509;
  44611. WOLFSSL_X509_EXTENSION* ext;
  44612. WOLFSSL_X509_EXTENSION* ext2;
  44613. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  44614. WOLFSSL_ASN1_STRING* asn1str;
  44615. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  44616. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  44617. WOLFSSL_BASIC_CONSTRAINTS* bc;
  44618. WOLFSSL_ACCESS_DESCRIPTION* ad;
  44619. WOLFSSL_GENERAL_NAME* gn;
  44620. /* Check NULL argument */
  44621. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  44622. /* Using OCSP cert with X509V3 extensions */
  44623. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  44624. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44625. fclose(f);
  44626. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44627. /* Basic Constraints */
  44628. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44629. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44630. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  44631. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  44632. AssertIntEQ(bc->ca, 1);
  44633. AssertNull(bc->pathlen);
  44634. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  44635. i++;
  44636. /* Subject Key Identifier */
  44637. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44638. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44639. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  44640. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44641. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  44642. asn1str));
  44643. X509_EXTENSION_free(ext2);
  44644. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44645. AssertNotNull(method->i2s);
  44646. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  44647. wolfSSL_ASN1_STRING_free(asn1str);
  44648. actual = strcmp(str,
  44649. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44650. AssertIntEQ(actual, 0);
  44651. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44652. i++;
  44653. /* Authority Key Identifier */
  44654. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44655. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44656. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  44657. AssertNotNull(aKeyId =
  44658. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  44659. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44660. AssertNotNull(asn1str = aKeyId->keyid);
  44661. AssertNotNull(str =
  44662. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  44663. actual = strcmp(str,
  44664. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44665. AssertIntEQ(actual, 0);
  44666. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44667. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  44668. i++;
  44669. /* Key Usage */
  44670. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44671. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44672. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  44673. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44674. #if defined(WOLFSSL_QT)
  44675. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  44676. #else
  44677. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  44678. #endif
  44679. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  44680. #ifdef BIG_ENDIAN_ORDER
  44681. actual = data[1];
  44682. #else
  44683. actual = data[0];
  44684. #endif
  44685. AssertIntEQ(actual, expected);
  44686. wolfSSL_ASN1_STRING_free(asn1str);
  44687. #if 1
  44688. i++;
  44689. /* Authority Info Access */
  44690. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44691. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44692. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  44693. AssertNotNull(aia =
  44694. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  44695. #if defined(WOLFSSL_QT)
  44696. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44697. #else
  44698. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44699. #endif
  44700. /* URI entry is an ACCESS_DESCRIPTION type */
  44701. #if defined(WOLFSSL_QT)
  44702. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  44703. #else
  44704. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  44705. #endif
  44706. AssertNotNull(adObj = ad->method);
  44707. /* Make sure nid is OCSP */
  44708. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  44709. /* GENERAL_NAME stores URI as an ASN1_STRING */
  44710. AssertNotNull(gn = ad->location);
  44711. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  44712. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  44713. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  44714. #if defined(WOLFSSL_QT)
  44715. str = (char*)ASN1_STRING_get0_data(asn1str);
  44716. #else
  44717. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  44718. #endif
  44719. actual = strcmp(str, "http://127.0.0.1:22220");
  44720. AssertIntEQ(actual, 0);
  44721. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  44722. #else
  44723. (void) aia; (void) ad; (void) adObj; (void) gn;
  44724. #endif
  44725. wolfSSL_X509_free(x509);
  44726. res = TEST_RES_CHECK(1);
  44727. #endif
  44728. return res;
  44729. }
  44730. static int test_wolfSSL_X509_get_extension_flags(void)
  44731. {
  44732. int res = TEST_SKIPPED;
  44733. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44734. XFILE f;
  44735. X509* x509;
  44736. unsigned int extFlags;
  44737. unsigned int keyUsageFlags;
  44738. unsigned int extKeyUsageFlags;
  44739. /* client-int-cert.pem has the following extension flags. */
  44740. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  44741. /* and the following key usage flags. */
  44742. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44743. | KU_NON_REPUDIATION
  44744. | KU_KEY_ENCIPHERMENT;
  44745. /* and the following extended key usage flags. */
  44746. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  44747. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  44748. AssertTrue(f != XBADFILE);
  44749. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44750. XFCLOSE(f);
  44751. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  44752. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44753. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44754. X509_free(x509);
  44755. /* client-cert-ext.pem has the following extension flags. */
  44756. extFlags = EXFLAG_KUSAGE;
  44757. /* and the following key usage flags. */
  44758. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44759. | KU_KEY_CERT_SIGN
  44760. | KU_CRL_SIGN;
  44761. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  44762. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44763. XFCLOSE(f);
  44764. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  44765. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44766. X509_free(x509);
  44767. res = TEST_RES_CHECK(1);
  44768. #endif /* OPENSSL_ALL */
  44769. return res;
  44770. }
  44771. static int test_wolfSSL_X509_get_ext(void)
  44772. {
  44773. int res = TEST_SKIPPED;
  44774. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44775. int ret = 0;
  44776. FILE* f;
  44777. WOLFSSL_X509* x509;
  44778. WOLFSSL_X509_EXTENSION* foundExtension;
  44779. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  44780. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44781. fclose(f);
  44782. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  44783. /* wolfSSL_X509_get_ext() valid input */
  44784. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  44785. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  44786. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  44787. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  44788. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  44789. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  44790. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  44791. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  44792. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  44793. wolfSSL_X509_free(x509);
  44794. res = TEST_RES_CHECK(1);
  44795. #endif
  44796. return res;
  44797. }
  44798. static int test_wolfSSL_X509_get_ext_by_NID(void)
  44799. {
  44800. int res = TEST_SKIPPED;
  44801. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44802. int rc;
  44803. FILE* f;
  44804. WOLFSSL_X509* x509;
  44805. ASN1_OBJECT* obj = NULL;
  44806. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  44807. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44808. fclose(f);
  44809. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  44810. AssertIntGE(rc, 0);
  44811. /* Start search from last location (should fail) */
  44812. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  44813. AssertIntGE(rc, -1);
  44814. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  44815. AssertIntGE(rc, -1);
  44816. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  44817. AssertIntEQ(rc, -1);
  44818. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  44819. AssertIntEQ(rc, -1);
  44820. /* NID_ext_key_usage, check also its nid and oid */
  44821. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  44822. AssertIntGT(rc, -1);
  44823. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  44824. AssertIntEQ(obj->nid, NID_ext_key_usage);
  44825. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  44826. wolfSSL_X509_free(x509);
  44827. res = TEST_RES_CHECK(1);
  44828. #endif
  44829. return res;
  44830. }
  44831. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  44832. {
  44833. int res = TEST_SKIPPED;
  44834. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44835. int rc;
  44836. XFILE f;
  44837. WOLFSSL_X509* x509;
  44838. WOLFSSL_X509_EXTENSION* ext;
  44839. WOLFSSL_ASN1_STRING* sanString;
  44840. byte* sanDer;
  44841. const byte expectedDer[] = {
  44842. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  44843. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  44844. f = XFOPEN("./certs/server-cert.pem", "rb");
  44845. AssertTrue(f != XBADFILE);
  44846. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44847. fclose(f);
  44848. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  44849. AssertIntNE(rc, -1);
  44850. AssertNotNull(ext = X509_get_ext(x509, rc));
  44851. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  44852. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  44853. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  44854. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  44855. X509_free(x509);
  44856. res = TEST_RES_CHECK(1);
  44857. #endif
  44858. return res;
  44859. }
  44860. static int test_wolfSSL_X509_EXTENSION_new(void)
  44861. {
  44862. int res = TEST_SKIPPED;
  44863. #if defined (OPENSSL_ALL)
  44864. WOLFSSL_X509_EXTENSION* ext;
  44865. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  44866. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  44867. ext->obj->nid = WOLFSSL_SUCCESS;
  44868. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  44869. wolfSSL_X509_EXTENSION_free(ext);
  44870. res = TEST_RES_CHECK(1);
  44871. #endif
  44872. return res;
  44873. }
  44874. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  44875. {
  44876. int res = TEST_SKIPPED;
  44877. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44878. WOLFSSL_X509* x509;
  44879. WOLFSSL_X509_EXTENSION* ext;
  44880. WOLFSSL_ASN1_OBJECT* o;
  44881. FILE* file;
  44882. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  44883. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44884. fclose(file);
  44885. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  44886. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44887. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  44888. AssertIntEQ(o->nid, 128);
  44889. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  44890. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  44891. wolfSSL_X509_free(x509);
  44892. res = TEST_RES_CHECK(1);
  44893. #endif
  44894. return res;
  44895. }
  44896. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  44897. {
  44898. int res = TEST_SKIPPED;
  44899. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44900. WOLFSSL_X509* x509;
  44901. WOLFSSL_X509_EXTENSION* ext;
  44902. WOLFSSL_ASN1_STRING* str;
  44903. FILE* file;
  44904. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  44905. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44906. fclose(file);
  44907. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44908. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  44909. wolfSSL_X509_free(x509);
  44910. res = TEST_RES_CHECK(1);
  44911. #endif
  44912. return res;
  44913. }
  44914. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  44915. {
  44916. int res = TEST_SKIPPED;
  44917. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44918. WOLFSSL_X509* x509;
  44919. WOLFSSL_X509_EXTENSION* ext;
  44920. FILE* file;
  44921. int crit;
  44922. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  44923. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44924. fclose(file);
  44925. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44926. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  44927. AssertIntEQ(crit, 0);
  44928. wolfSSL_X509_free(x509);
  44929. res = TEST_RES_CHECK(1);
  44930. #endif
  44931. return res;
  44932. }
  44933. static int test_wolfSSL_X509V3_EXT_print(void)
  44934. {
  44935. int res = TEST_SKIPPED;
  44936. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  44937. !defined(NO_RSA)
  44938. {
  44939. FILE* f;
  44940. WOLFSSL_X509* x509;
  44941. X509_EXTENSION * ext = NULL;
  44942. int loc;
  44943. BIO *bio = NULL;
  44944. AssertNotNull(f = fopen(svrCertFile, "rb"));
  44945. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44946. fclose(f);
  44947. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  44948. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  44949. AssertIntGT(loc, -1);
  44950. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44951. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44952. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  44953. AssertIntGT(loc, -1);
  44954. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44955. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44956. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  44957. AssertIntGT(loc, -1);
  44958. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44959. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44960. wolfSSL_BIO_free(bio);
  44961. wolfSSL_X509_free(x509);
  44962. }
  44963. {
  44964. X509 *x509;
  44965. BIO *bio;
  44966. X509_EXTENSION *ext;
  44967. unsigned int i;
  44968. unsigned int idx;
  44969. /* Some NIDs to test with */
  44970. int nids[] = {
  44971. /* NID_key_usage, currently X509_get_ext returns this as a bit
  44972. * string, which messes up X509V3_EXT_print */
  44973. /* NID_ext_key_usage, */
  44974. NID_subject_alt_name,
  44975. };
  44976. int* n;
  44977. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  44978. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  44979. WOLFSSL_FILETYPE_PEM));
  44980. fprintf(stderr, "\nPrinting extension values:\n");
  44981. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  44982. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  44983. * update the index */
  44984. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  44985. AssertNotNull(ext = X509_get_ext(x509, idx));
  44986. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  44987. fprintf(stderr, "\n");
  44988. }
  44989. BIO_free(bio);
  44990. X509_free(x509);
  44991. }
  44992. res = TEST_RES_CHECK(1);
  44993. #endif
  44994. return res;
  44995. }
  44996. static int test_wolfSSL_X509_cmp(void)
  44997. {
  44998. int res = TEST_SKIPPED;
  44999. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45000. FILE* file1;
  45001. FILE* file2;
  45002. WOLFSSL_X509* cert1;
  45003. WOLFSSL_X509* cert2;
  45004. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  45005. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  45006. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45007. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45008. fclose(file1);
  45009. fclose(file2);
  45010. /* wolfSSL_X509_cmp() testing matching certs */
  45011. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45012. /* wolfSSL_X509_cmp() testing mismatched certs */
  45013. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45014. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45015. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45016. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45017. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45018. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45019. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45020. wolfSSL_X509_free(cert1);
  45021. wolfSSL_X509_free(cert2);
  45022. res = TEST_RES_CHECK(1);
  45023. #endif
  45024. return res;
  45025. }
  45026. static int test_wolfSSL_PKEY_up_ref(void)
  45027. {
  45028. int res = TEST_SKIPPED;
  45029. #if defined(OPENSSL_ALL)
  45030. EVP_PKEY* pkey;
  45031. pkey = EVP_PKEY_new();
  45032. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45033. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45034. EVP_PKEY_free(pkey);
  45035. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45036. EVP_PKEY_free(pkey);
  45037. EVP_PKEY_free(pkey);
  45038. res = TEST_RES_CHECK(1);
  45039. #endif
  45040. return res;
  45041. }
  45042. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45043. {
  45044. int res = TEST_SKIPPED;
  45045. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45046. EVP_PKEY* pkey;
  45047. const unsigned char* p;
  45048. unsigned char *der = NULL, *tmp = NULL;
  45049. int derLen;
  45050. p = client_keypub_der_2048;
  45051. /* Check that key can be successfully decoded. */
  45052. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45053. sizeof_client_keypub_der_2048));
  45054. /* Check that key can be successfully encoded. */
  45055. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45056. /* Ensure that the encoded version matches the original. */
  45057. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  45058. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45059. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45060. * is advanced. */
  45061. tmp = der;
  45062. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45063. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  45064. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45065. AssertTrue(der + derLen == tmp);
  45066. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45067. EVP_PKEY_free(pkey);
  45068. res = TEST_RES_CHECK(1);
  45069. #endif
  45070. return res;
  45071. }
  45072. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45073. {
  45074. int res = TEST_SKIPPED;
  45075. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45076. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45077. EVP_PKEY* pkey;
  45078. const unsigned char* p;
  45079. unsigned char *der = NULL, *tmp = NULL;
  45080. int derLen;
  45081. unsigned char pub_buf[65];
  45082. const int pub_len = 65;
  45083. BN_CTX * ctx;
  45084. EC_GROUP * curve;
  45085. EC_KEY * ephemeral_key;
  45086. const EC_POINT * h;
  45087. /* Generate an x963 key pair and get public part into pub_buf */
  45088. AssertNotNull(ctx = BN_CTX_new());
  45089. AssertNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45090. AssertNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45091. NID_X9_62_prime256v1));
  45092. AssertIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45093. AssertNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45094. AssertIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45095. POINT_CONVERSION_UNCOMPRESSED,
  45096. pub_buf, pub_len, ctx));
  45097. /* Prepare the EVP_PKEY */
  45098. AssertNotNull(pkey = EVP_PKEY_new());
  45099. p = pub_buf;
  45100. /* Check that key can be successfully decoded. */
  45101. AssertNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45102. pub_len));
  45103. /* Check that key can be successfully encoded. */
  45104. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45105. /* Ensure that the encoded version matches the original. */
  45106. AssertIntEQ(derLen, pub_len);
  45107. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45108. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45109. * is advanced. */
  45110. tmp = der;
  45111. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45112. AssertIntEQ(derLen, pub_len);
  45113. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45114. AssertTrue(der + derLen == tmp);
  45115. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45116. EVP_PKEY_free(pkey);
  45117. EC_KEY_free(ephemeral_key);
  45118. EC_GROUP_free(curve);
  45119. res = TEST_RES_CHECK(1);
  45120. #endif
  45121. return res;
  45122. }
  45123. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45124. {
  45125. int res = TEST_SKIPPED;
  45126. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45127. DSA* dsa;
  45128. char file[] = "./certs/dsaparams.der";
  45129. XFILE f;
  45130. int derInLen;
  45131. byte* derIn;
  45132. int derOutLen;
  45133. byte* derOut = NULL;
  45134. f = XFOPEN(file, "rb");
  45135. AssertTrue(f != XBADFILE);
  45136. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  45137. derInLen = (int)XFTELL(f);
  45138. AssertTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  45139. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  45140. DYNAMIC_TYPE_TMP_BUFFER));
  45141. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  45142. XFCLOSE(f);
  45143. /* Check that params can be successfully decoded. */
  45144. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  45145. /* Check that params can be successfully encoded. */
  45146. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45147. /* Ensure that the encoded version matches the original. */
  45148. AssertIntEQ(derInLen, derOutLen);
  45149. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45150. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45151. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45152. DSA_free(dsa);
  45153. res = TEST_RES_CHECK(1);
  45154. #endif
  45155. return res;
  45156. }
  45157. static int test_wolfSSL_i2d_PrivateKey(void)
  45158. {
  45159. int res = TEST_SKIPPED;
  45160. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  45161. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45162. {
  45163. EVP_PKEY* pkey;
  45164. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  45165. unsigned char buf[FOURK_BUF];
  45166. unsigned char* pt = NULL;
  45167. int bufSz;
  45168. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45169. (long)sizeof_server_key_der_2048));
  45170. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45171. pt = buf;
  45172. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45173. AssertIntNE((pt - buf), 0);
  45174. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45175. EVP_PKEY_free(pkey);
  45176. }
  45177. #endif
  45178. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45179. {
  45180. EVP_PKEY* pkey;
  45181. const unsigned char* client_key =
  45182. (const unsigned char*)ecc_clikey_der_256;
  45183. unsigned char buf[FOURK_BUF];
  45184. unsigned char* pt = NULL;
  45185. int bufSz;
  45186. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45187. sizeof_ecc_clikey_der_256)));
  45188. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45189. pt = buf;
  45190. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45191. AssertIntNE((pt - buf), 0);
  45192. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45193. EVP_PKEY_free(pkey);
  45194. }
  45195. #endif
  45196. res = TEST_RES_CHECK(1);
  45197. #endif
  45198. return res;
  45199. }
  45200. static int test_wolfSSL_OCSP_id_get0_info(void)
  45201. {
  45202. int res = TEST_SKIPPED;
  45203. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  45204. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45205. X509* cert;
  45206. X509* issuer;
  45207. OCSP_CERTID* id;
  45208. OCSP_CERTID* id2;
  45209. ASN1_STRING* name = NULL;
  45210. ASN1_OBJECT* pmd = NULL;
  45211. ASN1_STRING* keyHash = NULL;
  45212. ASN1_INTEGER* serial = NULL;
  45213. ASN1_INTEGER* x509Int;
  45214. AssertNotNull(cert =
  45215. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  45216. AssertNotNull(issuer =
  45217. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  45218. id = OCSP_cert_to_id(NULL, cert, issuer);
  45219. AssertNotNull(id);
  45220. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  45221. AssertNotNull(id2);
  45222. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45223. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45224. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45225. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45226. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45227. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45228. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45229. AssertNotNull(serial);
  45230. /* compare serial number to one in cert, should be equal */
  45231. x509Int = X509_get_serialNumber(cert);
  45232. AssertNotNull(x509Int);
  45233. AssertIntEQ(x509Int->length, serial->length);
  45234. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45235. /* test OCSP_id_cmp */
  45236. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45237. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  45238. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  45239. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  45240. id->issuerHash[0] = ~id->issuerHash[0];
  45241. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  45242. OCSP_CERTID_free(id);
  45243. OCSP_CERTID_free(id2);
  45244. X509_free(cert); /* free's x509Int */
  45245. X509_free(issuer);
  45246. res = TEST_RES_CHECK(1);
  45247. #endif
  45248. return res;
  45249. }
  45250. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45251. {
  45252. int res = TEST_SKIPPED;
  45253. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45254. WOLFSSL_OCSP_CERTID certId;
  45255. byte* targetBuffer;
  45256. byte* beginTargetBuffer;
  45257. /* OCSP CertID bytes taken from PCAP */
  45258. byte rawCertId[] = {
  45259. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45260. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45261. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45262. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45263. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45264. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45265. 0xcf, 0xbc, 0x91
  45266. };
  45267. int ret, i;
  45268. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45269. certId.rawCertId = rawCertId;
  45270. certId.rawCertIdSize = sizeof(rawCertId);
  45271. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45272. beginTargetBuffer = targetBuffer;
  45273. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  45274. /* If target buffer is not null, function increments targetBuffer to point
  45275. just past the end of the encoded data. */
  45276. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  45277. /* Function returns the size of the encoded data. */
  45278. AssertIntEQ(ret, sizeof(rawCertId));
  45279. for (i = 0; i < ret; ++i)
  45280. {
  45281. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  45282. }
  45283. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45284. targetBuffer = NULL;
  45285. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  45286. /* If target buffer is null, function allocates memory for a buffer and
  45287. copies the encoded data into it. targetBuffer then points to the start of
  45288. this newly allocate buffer. */
  45289. AssertIntEQ(ret, sizeof(rawCertId));
  45290. for (i = 0; i < ret; ++i)
  45291. {
  45292. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  45293. }
  45294. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45295. res = TEST_RES_CHECK(1);
  45296. #endif
  45297. return res;
  45298. }
  45299. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45300. {
  45301. int res = TEST_SKIPPED;
  45302. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45303. WOLFSSL_OCSP_CERTID* certId;
  45304. WOLFSSL_OCSP_CERTID* certIdBad;
  45305. const unsigned char* rawCertIdPtr;
  45306. const unsigned char rawCertId[] = {
  45307. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45308. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45309. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45310. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45311. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45312. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45313. 0xcf, 0xbc, 0x91
  45314. };
  45315. rawCertIdPtr = &rawCertId[0];
  45316. /* If the cert ID is NULL the function should allocate it and copy the
  45317. * data to it. */
  45318. certId = NULL;
  45319. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  45320. AssertNotNull(certId);
  45321. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45322. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45323. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45324. /* If the cert ID is not NULL the function will just copy the data to it. */
  45325. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45326. DYNAMIC_TYPE_TMP_BUFFER);
  45327. AssertNotNull(certId);
  45328. XMEMSET(certId, 0, sizeof(*certId));
  45329. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45330. rawCertIdPtr = &rawCertId[0];
  45331. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  45332. AssertNotNull(certId);
  45333. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45334. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45335. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45336. /* The below tests should fail when passed bad parameters. NULL should
  45337. * always be returned. */
  45338. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  45339. AssertNull(certIdBad);
  45340. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  45341. AssertNull(certIdBad);
  45342. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  45343. AssertNull(certIdBad);
  45344. res = TEST_RES_CHECK(1);
  45345. #endif
  45346. return res;
  45347. }
  45348. static int test_wolfSSL_OCSP_id_cmp(void)
  45349. {
  45350. int res = TEST_SKIPPED;
  45351. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45352. OCSP_CERTID id1;
  45353. OCSP_CERTID id2;
  45354. XMEMSET(&id1, 0, sizeof(id1));
  45355. XMEMSET(&id2, 0, sizeof(id2));
  45356. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45357. res = TEST_RES_CHECK(1);
  45358. #endif
  45359. return res;
  45360. }
  45361. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45362. {
  45363. int res = TEST_SKIPPED;
  45364. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45365. WOLFSSL_OCSP_SINGLERESP single;
  45366. const WOLFSSL_OCSP_CERTID* certId;
  45367. XMEMSET(&single, 0, sizeof(single));
  45368. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45369. AssertPtrEq(&single, certId);
  45370. res = TEST_RES_CHECK(1);
  45371. #endif
  45372. return res;
  45373. }
  45374. static int test_wolfSSL_OCSP_single_get0_status(void)
  45375. {
  45376. int res = TEST_SKIPPED;
  45377. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45378. WOLFSSL_OCSP_SINGLERESP single;
  45379. CertStatus certStatus;
  45380. WOLFSSL_ASN1_TIME* thisDate;
  45381. WOLFSSL_ASN1_TIME* nextDate;
  45382. int ret, i;
  45383. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45384. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45385. /* Fill the date fields with some dummy data. */
  45386. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45387. certStatus.thisDateParsed.data[i] = i;
  45388. certStatus.nextDateParsed.data[i] = i;
  45389. }
  45390. certStatus.status = CERT_GOOD;
  45391. single.status = &certStatus;
  45392. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45393. &nextDate);
  45394. AssertIntEQ(ret, CERT_GOOD);
  45395. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  45396. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  45397. res = TEST_RES_CHECK(1);
  45398. #endif
  45399. return res;
  45400. }
  45401. static int test_wolfSSL_OCSP_resp_count(void)
  45402. {
  45403. int res = TEST_SKIPPED;
  45404. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45405. WOLFSSL_OCSP_BASICRESP basicResp;
  45406. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45407. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45408. int count;
  45409. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45410. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45411. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45412. count = wolfSSL_OCSP_resp_count(&basicResp);
  45413. AssertIntEQ(count, 0);
  45414. basicResp.single = &singleRespOne;
  45415. count = wolfSSL_OCSP_resp_count(&basicResp);
  45416. AssertIntEQ(count, 1);
  45417. singleRespOne.next = &singleRespTwo;
  45418. count = wolfSSL_OCSP_resp_count(&basicResp);
  45419. AssertIntEQ(count, 2);
  45420. res = TEST_RES_CHECK(1);
  45421. #endif
  45422. return res;
  45423. }
  45424. static int test_wolfSSL_OCSP_resp_get0(void)
  45425. {
  45426. int res = TEST_SKIPPED;
  45427. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45428. WOLFSSL_OCSP_BASICRESP basicResp;
  45429. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45430. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45431. WOLFSSL_OCSP_SINGLERESP* ret;
  45432. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45433. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45434. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45435. basicResp.single = &singleRespOne;
  45436. singleRespOne.next = &singleRespTwo;
  45437. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  45438. AssertPtrEq(ret, &singleRespOne);
  45439. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  45440. AssertPtrEq(ret, &singleRespTwo);
  45441. res = TEST_RES_CHECK(1);
  45442. #endif
  45443. return res;
  45444. }
  45445. static int test_wolfSSL_EVP_PKEY_derive(void)
  45446. {
  45447. int res = TEST_SKIPPED;
  45448. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45449. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45450. EVP_PKEY_CTX *ctx;
  45451. unsigned char *skey;
  45452. size_t skeylen;
  45453. EVP_PKEY *pkey, *peerkey;
  45454. const unsigned char* key;
  45455. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45456. /* DH */
  45457. key = dh_key_der_2048;
  45458. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45459. sizeof_dh_key_der_2048)));
  45460. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45461. key = dh_key_der_2048;
  45462. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45463. sizeof_dh_key_der_2048)));
  45464. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45465. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45466. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45467. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45468. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45469. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  45470. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45471. EVP_PKEY_CTX_free(ctx);
  45472. EVP_PKEY_free(peerkey);
  45473. EVP_PKEY_free(pkey);
  45474. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45475. #endif
  45476. #ifdef HAVE_ECC
  45477. /* ECDH */
  45478. key = ecc_clikey_der_256;
  45479. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45480. sizeof_ecc_clikey_der_256)));
  45481. key = ecc_clikeypub_der_256;
  45482. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45483. sizeof_ecc_clikeypub_der_256)));
  45484. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45485. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45486. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45487. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45488. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  45489. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45490. EVP_PKEY_CTX_free(ctx);
  45491. EVP_PKEY_free(peerkey);
  45492. EVP_PKEY_free(pkey);
  45493. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45494. #endif /* HAVE_ECC */
  45495. res = TEST_RES_CHECK(1);
  45496. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45497. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45498. return res;
  45499. }
  45500. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45501. {
  45502. int res = TEST_SKIPPED;
  45503. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45504. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45505. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45506. int ret;
  45507. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45508. int pwdlen = sizeof(pwd);
  45509. const byte salt[] = {'N','a','C','l'};
  45510. int saltlen = sizeof(salt);
  45511. byte key[80];
  45512. word64 numOvr32 = (word64)INT32_MAX + 1;
  45513. /* expected derived key for N:16, r:1, p:1 */
  45514. const byte expectedKey[] = {
  45515. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45516. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45517. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45518. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45519. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45520. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45521. 0xE7, 0xE9, 0xC0, 0x9A};
  45522. /* N r p mx key keylen */
  45523. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45524. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  45525. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45526. AssertIntEQ(ret, 0); /* N must be power of 2 */
  45527. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45528. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  45529. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45530. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  45531. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45532. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  45533. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  45534. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  45535. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  45536. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  45537. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  45538. AssertIntEQ(ret, 1); /* should succeed */
  45539. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  45540. key, 64);
  45541. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  45542. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  45543. key, 64);
  45544. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  45545. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  45546. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  45547. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  45548. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  45549. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  45550. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  45551. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  45552. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  45553. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  45554. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  45555. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  45556. AssertIntEQ(ret, 1);
  45557. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  45558. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  45559. res = TEST_RES_CHECK(1);
  45560. #endif /* !NO_PWDBASED && !NO_SHA256 */
  45561. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  45562. return res;
  45563. }
  45564. static int test_no_op_functions(void)
  45565. {
  45566. int res = TEST_SKIPPED;
  45567. #if defined(OPENSSL_EXTRA)
  45568. /* this makes sure wolfSSL can compile and run these no-op functions */
  45569. SSL_load_error_strings();
  45570. ENGINE_load_builtin_engines();
  45571. OpenSSL_add_all_ciphers();
  45572. AssertIntEQ(CRYPTO_malloc_init(), 0);
  45573. res = TEST_RES_CHECK(1);
  45574. #endif
  45575. return res;
  45576. }
  45577. static int test_wolfSSL_CRYPTO_memcmp(void)
  45578. {
  45579. int res = TEST_SKIPPED;
  45580. #ifdef OPENSSL_EXTRA
  45581. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45582. "implementation of TLS/SSL for embedded devices to the cloud.";
  45583. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45584. "implementation of TLS/SSL for embedded devices to the cloud.";
  45585. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45586. "implementation of TLS/SSL for embedded devices to the cloud!";
  45587. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  45588. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  45589. res = TEST_RES_CHECK(1);
  45590. #endif
  45591. return res;
  45592. }
  45593. /*----------------------------------------------------------------------------*
  45594. | wolfCrypt ASN
  45595. *----------------------------------------------------------------------------*/
  45596. static int test_wc_CreateEncryptedPKCS8Key(void)
  45597. {
  45598. int res = TEST_SKIPPED;
  45599. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  45600. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  45601. WC_RNG rng;
  45602. byte* encKey = NULL;
  45603. word32 encKeySz = 0;
  45604. word32 decKeySz = 0;
  45605. const char password[] = "Lorem ipsum dolor sit amet";
  45606. word32 passwordSz = (word32)XSTRLEN(password);
  45607. word32 tradIdx = 0;
  45608. AssertIntEQ(wc_InitRng(&rng), 0);
  45609. /* Call with NULL for out buffer to get necessary length. */
  45610. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45611. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  45612. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45613. LENGTH_ONLY_E);
  45614. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  45615. DYNAMIC_TYPE_TMP_BUFFER));
  45616. /* Call with the allocated out buffer. */
  45617. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45618. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  45619. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45620. 0);
  45621. /* Decrypt the encrypted PKCS8 key we just made. */
  45622. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  45623. passwordSz)), 0);
  45624. /* encKey now holds the decrypted key (decrypted in place). */
  45625. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  45626. /* Check that the decrypted key matches the key prior to encryption. */
  45627. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  45628. sizeof_server_key_der_2048), 0);
  45629. if (encKey != NULL)
  45630. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45631. wc_FreeRng(&rng);
  45632. res = TEST_RES_CHECK(1);
  45633. #endif
  45634. return res;
  45635. }
  45636. static int test_wc_GetPkcs8TraditionalOffset(void)
  45637. {
  45638. int res = TEST_SKIPPED;
  45639. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  45640. int length, derSz;
  45641. word32 inOutIdx;
  45642. const char* path = "./certs/server-keyPkcs8.der";
  45643. XFILE file;
  45644. byte der[2048];
  45645. file = XFOPEN(path, "rb");
  45646. AssertTrue(file != XBADFILE);
  45647. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  45648. XFCLOSE(file);
  45649. /* valid case */
  45650. inOutIdx = 0;
  45651. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  45652. AssertIntGT(length, 0);
  45653. /* inOutIdx > sz */
  45654. inOutIdx = 4000;
  45655. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  45656. AssertIntEQ(length, BAD_FUNC_ARG);
  45657. /* null input */
  45658. inOutIdx = 0;
  45659. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  45660. AssertIntEQ(length, BAD_FUNC_ARG);
  45661. /* invalid input, fill buffer with 1's */
  45662. XMEMSET(der, 1, sizeof(der));
  45663. inOutIdx = 0;
  45664. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  45665. AssertIntEQ(length, ASN_PARSE_E);
  45666. res = TEST_RES_CHECK(1);
  45667. #endif /* NO_ASN */
  45668. return res;
  45669. }
  45670. static int test_wc_SetSubjectRaw(void)
  45671. {
  45672. int res = TEST_SKIPPED;
  45673. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45674. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45675. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45676. WOLFSSL_X509* x509;
  45677. int peerCertSz;
  45678. const byte* peerCertBuf;
  45679. Cert forgedCert;
  45680. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  45681. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45682. AssertIntEQ(0, wc_InitCert(&forgedCert));
  45683. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  45684. wolfSSL_FreeX509(x509);
  45685. res = TEST_RES_CHECK(1);
  45686. #endif
  45687. return res;
  45688. }
  45689. static int test_wc_GetSubjectRaw(void)
  45690. {
  45691. int res = TEST_SKIPPED;
  45692. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45693. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  45694. Cert cert;
  45695. byte *subjectRaw;
  45696. AssertIntEQ(0, wc_InitCert(&cert));
  45697. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  45698. res = TEST_RES_CHECK(1);
  45699. #endif
  45700. return res;
  45701. }
  45702. static int test_wc_SetIssuerRaw(void)
  45703. {
  45704. int res = TEST_SKIPPED;
  45705. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45706. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45707. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45708. WOLFSSL_X509* x509;
  45709. int peerCertSz;
  45710. const byte* peerCertBuf;
  45711. Cert forgedCert;
  45712. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  45713. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45714. AssertIntEQ(0, wc_InitCert(&forgedCert));
  45715. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  45716. wolfSSL_FreeX509(x509);
  45717. res = TEST_RES_CHECK(1);
  45718. #endif
  45719. return res;
  45720. }
  45721. static int test_wc_SetIssueBuffer(void)
  45722. {
  45723. int res = TEST_SKIPPED;
  45724. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45725. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45726. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45727. WOLFSSL_X509* x509;
  45728. int peerCertSz;
  45729. const byte* peerCertBuf;
  45730. Cert forgedCert;
  45731. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  45732. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45733. AssertIntEQ(0, wc_InitCert(&forgedCert));
  45734. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  45735. wolfSSL_FreeX509(x509);
  45736. res = TEST_RES_CHECK(1);
  45737. #endif
  45738. return res;
  45739. }
  45740. /*
  45741. * Testing wc_SetSubjectKeyId
  45742. */
  45743. static int test_wc_SetSubjectKeyId(void)
  45744. {
  45745. int res = TEST_SKIPPED;
  45746. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45747. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45748. Cert cert;
  45749. const char* file = "certs/ecc-client-keyPub.pem";
  45750. AssertIntEQ(0, wc_InitCert(&cert));
  45751. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  45752. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  45753. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  45754. res = TEST_RES_CHECK(1);
  45755. #endif
  45756. return res;
  45757. } /* END test_wc_SetSubjectKeyId */
  45758. /*
  45759. * Testing wc_SetSubject
  45760. */
  45761. static int test_wc_SetSubject(void)
  45762. {
  45763. int res = TEST_SKIPPED;
  45764. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45765. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45766. Cert cert;
  45767. const char* file = "./certs/ca-ecc-cert.pem";
  45768. AssertIntEQ(0, wc_InitCert(&cert));
  45769. AssertIntEQ(0, wc_SetSubject(&cert, file));
  45770. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  45771. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  45772. res = TEST_RES_CHECK(1);
  45773. #endif
  45774. return res;
  45775. } /* END test_wc_SetSubject */
  45776. static int test_CheckCertSignature(void)
  45777. {
  45778. int res = TEST_SKIPPED;
  45779. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  45780. WOLFSSL_CERT_MANAGER* cm = NULL;
  45781. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45782. FILE* fp;
  45783. byte cert[4096];
  45784. int certSz;
  45785. #endif
  45786. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  45787. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45788. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  45789. #ifndef NO_RSA
  45790. #ifdef USE_CERT_BUFFERS_1024
  45791. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  45792. sizeof_server_cert_der_1024, NULL, cm));
  45793. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45794. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  45795. WOLFSSL_FILETYPE_ASN1));
  45796. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  45797. sizeof_server_cert_der_1024, NULL, cm));
  45798. #elif defined(USE_CERT_BUFFERS_2048)
  45799. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  45800. sizeof_server_cert_der_2048, NULL, cm));
  45801. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45802. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  45803. WOLFSSL_FILETYPE_ASN1));
  45804. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  45805. sizeof_server_cert_der_2048, NULL, cm));
  45806. #endif
  45807. #endif
  45808. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45809. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  45810. sizeof_serv_ecc_der_256, NULL, cm));
  45811. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45812. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  45813. WOLFSSL_FILETYPE_ASN1));
  45814. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  45815. NULL, cm));
  45816. #endif
  45817. #if !defined(NO_FILESYSTEM)
  45818. wolfSSL_CertManagerFree(cm);
  45819. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45820. #ifndef NO_RSA
  45821. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  45822. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45823. XFCLOSE(fp);
  45824. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45825. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45826. "./certs/ca-cert.pem", NULL));
  45827. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45828. #endif
  45829. #ifdef HAVE_ECC
  45830. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  45831. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45832. XFCLOSE(fp);
  45833. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45834. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45835. "./certs/ca-ecc-cert.pem", NULL));
  45836. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45837. #endif
  45838. #endif
  45839. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45840. (void)fp;
  45841. (void)cert;
  45842. (void)certSz;
  45843. #endif
  45844. wolfSSL_CertManagerFree(cm);
  45845. res = TEST_RES_CHECK(1);
  45846. #endif
  45847. return res;
  45848. }
  45849. static int test_wc_ParseCert(void)
  45850. {
  45851. int res = TEST_SKIPPED;
  45852. #if !defined(NO_CERTS) && !defined(NO_RSA)
  45853. DecodedCert decodedCert;
  45854. const byte* rawCert = client_cert_der_2048;
  45855. const int rawCertSize = sizeof_client_cert_der_2048;
  45856. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  45857. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45858. #ifndef IGNORE_NAME_CONSTRAINTS
  45859. /* check that the subjects emailAddress was not put in the alt name list */
  45860. AssertNotNull(decodedCert.subjectEmail);
  45861. AssertNull(decodedCert.altEmailNames);
  45862. #endif
  45863. wc_FreeDecodedCert(&decodedCert);
  45864. res = TEST_RES_CHECK(1);
  45865. #endif
  45866. return res;
  45867. }
  45868. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  45869. * the API safely errors out on badly-formed ASN input.
  45870. * NOTE: Test not compatible with released FIPS implementations!
  45871. */
  45872. static int test_wc_ParseCert_Error(void)
  45873. {
  45874. int res = TEST_SKIPPED;
  45875. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  45876. (!defined(HAVE_FIPS) || \
  45877. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  45878. DecodedCert decodedCert;
  45879. int i;
  45880. /* Certificate data */
  45881. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  45882. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45883. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45884. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  45885. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  45886. /* Test data */
  45887. const struct testStruct {
  45888. const byte* c;
  45889. const int cSz;
  45890. const int expRet;
  45891. } t[] = {
  45892. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  45893. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  45894. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  45895. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  45896. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  45897. };
  45898. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  45899. for (i = 0; i < tSz; i++) {
  45900. WOLFSSL_MSG_EX("i == %d", i);
  45901. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  45902. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  45903. wc_FreeDecodedCert(&decodedCert);
  45904. }
  45905. res = TEST_RES_CHECK(1);
  45906. #endif
  45907. return res;
  45908. }
  45909. static int test_MakeCertWithPathLen(void)
  45910. {
  45911. int res = TEST_SKIPPED;
  45912. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  45913. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45914. const byte expectedPathLen = 7;
  45915. Cert cert;
  45916. DecodedCert decodedCert;
  45917. byte der[FOURK_BUF];
  45918. int derSize = 0;
  45919. WC_RNG rng;
  45920. ecc_key key;
  45921. AssertIntEQ(wc_InitRng(&rng), 0);
  45922. AssertIntEQ(wc_ecc_init(&key), 0);
  45923. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45924. AssertIntEQ(wc_InitCert(&cert), 0);
  45925. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45926. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45927. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45928. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45929. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45930. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  45931. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  45932. cert.selfSigned = 1;
  45933. cert.isCA = 1;
  45934. cert.pathLen = expectedPathLen;
  45935. cert.pathLenSet = 1;
  45936. cert.sigType = CTC_SHA256wECDSA;
  45937. #ifdef WOLFSSL_CERT_EXT
  45938. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  45939. #endif
  45940. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45941. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  45942. &key, &rng);
  45943. AssertIntGE(derSize, 0);
  45944. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45945. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45946. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  45947. wc_FreeDecodedCert(&decodedCert);
  45948. AssertIntEQ(wc_ecc_free(&key), 0);
  45949. AssertIntEQ(wc_FreeRng(&rng), 0);
  45950. res = TEST_RES_CHECK(1);
  45951. #endif
  45952. return res;
  45953. }
  45954. /*----------------------------------------------------------------------------*
  45955. | wolfCrypt ECC
  45956. *----------------------------------------------------------------------------*/
  45957. static int test_wc_ecc_get_curve_size_from_name(void)
  45958. {
  45959. int res = TEST_SKIPPED;
  45960. #ifdef HAVE_ECC
  45961. int ret;
  45962. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45963. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  45964. AssertIntEQ(ret, 32);
  45965. #endif
  45966. /* invalid case */
  45967. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  45968. AssertIntEQ(ret, -1);
  45969. /* NULL input */
  45970. ret = wc_ecc_get_curve_size_from_name(NULL);
  45971. AssertIntEQ(ret, BAD_FUNC_ARG);
  45972. res = TEST_RES_CHECK(1);
  45973. #endif /* HAVE_ECC */
  45974. return res;
  45975. }
  45976. static int test_wc_ecc_get_curve_id_from_name(void)
  45977. {
  45978. int res = TEST_SKIPPED;
  45979. #ifdef HAVE_ECC
  45980. int id;
  45981. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45982. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  45983. AssertIntEQ(id, ECC_SECP256R1);
  45984. #endif
  45985. /* invalid case */
  45986. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  45987. AssertIntEQ(id, -1);
  45988. /* NULL input */
  45989. id = wc_ecc_get_curve_id_from_name(NULL);
  45990. AssertIntEQ(id, BAD_FUNC_ARG);
  45991. res = TEST_RES_CHECK(1);
  45992. #endif /* HAVE_ECC */
  45993. return res;
  45994. }
  45995. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  45996. !defined(HAVE_SELFTEST) && \
  45997. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  45998. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  45999. {
  46000. int id;
  46001. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46002. int curve_id;
  46003. ecc_key* key;
  46004. const ecc_set_type* params;
  46005. int ret;
  46006. #endif
  46007. WOLFSSL_EC_KEY *ecKey = NULL;
  46008. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46009. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  46010. AssertIntEQ(id, ECC_SECP256R1);
  46011. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  46012. AssertNotNull(ecKey);
  46013. ret = EC_KEY_generate_key(ecKey);
  46014. if (ret == 0) {
  46015. /* normal test */
  46016. key = (ecc_key*)ecKey->internal;
  46017. params = key->dp;
  46018. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  46019. AssertIntEQ(curve_id, id);
  46020. }
  46021. #endif
  46022. /* invalid case, NULL input*/
  46023. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  46024. AssertIntEQ(id, BAD_FUNC_ARG);
  46025. wolfSSL_EC_KEY_free(ecKey);
  46026. return TEST_RES_CHECK(1);
  46027. }
  46028. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46029. static int test_wc_ecc_get_curve_id_from_params(void)
  46030. {
  46031. int res = TEST_SKIPPED;
  46032. #ifdef HAVE_ECC
  46033. int id;
  46034. const byte prime[] =
  46035. {
  46036. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46038. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46039. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46040. };
  46041. const byte primeInvalid[] =
  46042. {
  46043. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46045. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46046. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46047. };
  46048. const byte Af[] =
  46049. {
  46050. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46051. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46052. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46053. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46054. };
  46055. const byte Bf[] =
  46056. {
  46057. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46058. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46059. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46060. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46061. };
  46062. const byte order[] =
  46063. {
  46064. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46065. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46066. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46067. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46068. };
  46069. const byte Gx[] =
  46070. {
  46071. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46072. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46073. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46074. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46075. };
  46076. const byte Gy[] =
  46077. {
  46078. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46079. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46080. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46081. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46082. };
  46083. int cofactor = 1;
  46084. int fieldSize = 256;
  46085. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46086. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  46087. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46088. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  46089. AssertIntEQ(id, ECC_SECP256R1);
  46090. #endif
  46091. /* invalid case, fieldSize = 0 */
  46092. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46093. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46094. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  46095. AssertIntEQ(id, ECC_CURVE_INVALID);
  46096. /* invalid case, NULL prime */
  46097. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46098. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46099. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  46100. AssertIntEQ(id, BAD_FUNC_ARG);
  46101. /* invalid case, invalid prime */
  46102. id = wc_ecc_get_curve_id_from_params(fieldSize,
  46103. primeInvalid, sizeof(primeInvalid),
  46104. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46105. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  46106. AssertIntEQ(id, ECC_CURVE_INVALID);
  46107. res = TEST_RES_CHECK(1);
  46108. #endif
  46109. return res;
  46110. }
  46111. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46112. {
  46113. int res = TEST_SKIPPED;
  46114. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46115. !defined(HAVE_FAST_RSA)
  46116. WOLFSSL_RSA* rsa = NULL;
  46117. WOLFSSL_EVP_PKEY* pkey = NULL;
  46118. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46119. const char* in = "What is easy to do is easy not to do.";
  46120. size_t inlen = XSTRLEN(in);
  46121. size_t outEncLen = 0;
  46122. byte* outEnc = NULL;
  46123. byte* outDec = NULL;
  46124. size_t outDecLen = 0;
  46125. size_t rsaKeySz = 2048/8; /* Bytes */
  46126. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46127. byte* inTmp = NULL;
  46128. byte* outEncTmp = NULL;
  46129. byte* outDecTmp = NULL;
  46130. #endif
  46131. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46132. XMEMSET(outEnc, 0, rsaKeySz);
  46133. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46134. XMEMSET(outDec, 0, rsaKeySz);
  46135. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46136. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46137. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46138. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46139. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46140. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46141. WOLFSSL_SUCCESS);
  46142. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46143. since ctx uses it.*/
  46144. AssertIntEQ(pkey->ref.count, 2);
  46145. EVP_PKEY_free(pkey);
  46146. AssertIntEQ(pkey->ref.count, 1);
  46147. /* Encrypt data */
  46148. /* Check that we can get the required output buffer length by passing in a
  46149. * NULL output buffer. */
  46150. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46151. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46152. AssertIntEQ(rsaKeySz, outEncLen);
  46153. /* Now do the actual encryption. */
  46154. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46155. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46156. /* Decrypt data */
  46157. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46158. /* Check that we can get the required output buffer length by passing in a
  46159. * NULL output buffer. */
  46160. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46161. WOLFSSL_SUCCESS);
  46162. AssertIntEQ(rsaKeySz, outDecLen);
  46163. /* Now do the actual decryption. */
  46164. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46165. WOLFSSL_SUCCESS);
  46166. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46167. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46168. /* The input length must be the same size as the RSA key.*/
  46169. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46170. XMEMSET(inTmp, 9, rsaKeySz);
  46171. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46172. XMEMSET(outEncTmp, 0, rsaKeySz);
  46173. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46174. XMEMSET(outDecTmp, 0, rsaKeySz);
  46175. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46176. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46177. WOLFSSL_SUCCESS);
  46178. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46179. WOLFSSL_SUCCESS);
  46180. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46181. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  46182. WOLFSSL_SUCCESS);
  46183. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46184. #endif
  46185. EVP_PKEY_CTX_free(ctx);
  46186. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46187. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46188. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46189. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46190. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46191. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46192. #endif
  46193. res = TEST_RES_CHECK(1);
  46194. #endif
  46195. return res;
  46196. }
  46197. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  46198. {
  46199. int res = TEST_SKIPPED;
  46200. #if defined(OPENSSL_EXTRA)
  46201. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46202. WOLFSSL_DSA* dsa = NULL;
  46203. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46204. WOLFSSL_EVP_PKEY* pkey = NULL;
  46205. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46206. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46207. const char* in = "What is easy to do is easy not to do.";
  46208. size_t inlen = XSTRLEN(in);
  46209. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46210. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46211. SHA256_CTX c;
  46212. byte* sig = NULL;
  46213. byte* sigVerify = NULL;
  46214. size_t siglen;
  46215. size_t siglenOnlyLen;
  46216. size_t keySz = 2048/8; /* Bytes */
  46217. int i;
  46218. int encs[3] = {0};
  46219. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46220. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  46221. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46222. encs[0] = EVP_PKEY_RSA;
  46223. #endif
  46224. #endif
  46225. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46226. encs[1] = EVP_PKEY_DSA;
  46227. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46228. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46229. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46230. encs[2] = EVP_PKEY_EC;
  46231. #endif
  46232. #endif
  46233. AssertNotNull(sig =
  46234. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46235. AssertNotNull(sigVerify =
  46236. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46237. for (i = 0; i < 3; i++) {
  46238. if (encs[i] == 0)
  46239. continue;
  46240. siglen = keySz;
  46241. XMEMSET(sig, 0, keySz);
  46242. XMEMSET(sigVerify, 0, keySz);
  46243. /* Generate hash */
  46244. SHA256_Init(&c);
  46245. SHA256_Update(&c, in, inlen);
  46246. SHA256_Final(hash, &c);
  46247. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46248. /* workaround for small stack cache case */
  46249. wc_Sha256Free((wc_Sha256*)&c);
  46250. #endif
  46251. /* Generate key */
  46252. AssertNotNull(pkey = EVP_PKEY_new());
  46253. switch (encs[i]) {
  46254. case EVP_PKEY_RSA:
  46255. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46256. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  46257. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46258. {
  46259. WOLFSSL_RSA* rsa = NULL;
  46260. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46261. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46262. }
  46263. #endif
  46264. #endif
  46265. break;
  46266. case EVP_PKEY_DSA:
  46267. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46268. AssertNotNull(dsa = DSA_new());
  46269. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46270. NULL, 0, NULL, NULL, NULL), 1);
  46271. AssertIntEQ(DSA_generate_key(dsa), 1);
  46272. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46273. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46274. break;
  46275. case EVP_PKEY_EC:
  46276. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46277. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46278. {
  46279. WOLFSSL_EC_KEY* ecKey = NULL;
  46280. AssertNotNull(ecKey = EC_KEY_new());
  46281. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  46282. AssertIntEQ(
  46283. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46284. }
  46285. #endif
  46286. #endif
  46287. break;
  46288. }
  46289. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46290. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46291. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46292. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  46293. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46294. if (encs[i] == EVP_PKEY_RSA)
  46295. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46296. WOLFSSL_SUCCESS);
  46297. #endif
  46298. #endif
  46299. /* Check returning only length */
  46300. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46301. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46302. AssertIntGT(siglenOnlyLen, 0);
  46303. /* Sign data */
  46304. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46305. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46306. AssertIntGE(siglenOnlyLen, siglen);
  46307. /* Verify signature */
  46308. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46309. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46310. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46311. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  46312. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46313. if (encs[i] == EVP_PKEY_RSA)
  46314. AssertIntEQ(
  46315. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46316. WOLFSSL_SUCCESS);
  46317. #endif
  46318. #endif
  46319. AssertIntEQ(EVP_PKEY_verify(
  46320. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46321. WOLFSSL_SUCCESS);
  46322. AssertIntEQ(EVP_PKEY_verify(
  46323. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46324. WOLFSSL_FAILURE);
  46325. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46326. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  46327. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46328. if (encs[i] == EVP_PKEY_RSA) {
  46329. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46330. /* Try RSA sign/verify with no padding. */
  46331. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46332. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46333. WOLFSSL_SUCCESS);
  46334. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46335. siglen), WOLFSSL_SUCCESS);
  46336. AssertIntGE(siglenOnlyLen, siglen);
  46337. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46338. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46339. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46340. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46341. siglen), WOLFSSL_SUCCESS);
  46342. #endif
  46343. /* Wrong padding schemes. */
  46344. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46345. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46346. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46347. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46348. siglen), WOLFSSL_SUCCESS);
  46349. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46350. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46351. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46352. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46353. siglen), WOLFSSL_SUCCESS);
  46354. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46355. WOLFSSL_SUCCESS);
  46356. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46357. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46358. }
  46359. #endif
  46360. #endif
  46361. /* error cases */
  46362. siglen = keySz; /* Reset because sig size may vary slightly */
  46363. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46364. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46365. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46366. WOLFSSL_SUCCESS);
  46367. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46368. WOLFSSL_SUCCESS);
  46369. EVP_PKEY_free(pkey);
  46370. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46371. DSA_free(dsa);
  46372. dsa = NULL;
  46373. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46374. EVP_PKEY_CTX_free(ctx_verify);
  46375. EVP_PKEY_CTX_free(ctx);
  46376. }
  46377. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46378. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46379. res = TEST_RES_CHECK(1);
  46380. #endif /* OPENSSL_EXTRA */
  46381. return res;
  46382. }
  46383. static int test_EVP_PKEY_rsa(void)
  46384. {
  46385. int res = TEST_SKIPPED;
  46386. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46387. WOLFSSL_RSA* rsa;
  46388. WOLFSSL_EVP_PKEY* pkey;
  46389. AssertNotNull(rsa = wolfSSL_RSA_new());
  46390. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46391. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46392. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46393. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46394. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46395. wolfSSL_EVP_PKEY_free(pkey);
  46396. res = TEST_RES_CHECK(1);
  46397. #endif
  46398. return res;
  46399. }
  46400. static int test_EVP_PKEY_ec(void)
  46401. {
  46402. int res = TEST_SKIPPED;
  46403. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46404. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46405. WOLFSSL_EC_KEY* ecKey;
  46406. WOLFSSL_EVP_PKEY* pkey;
  46407. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  46408. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46409. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46410. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46411. /* Should fail since ecKey is empty */
  46412. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46413. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46414. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46415. wolfSSL_EVP_PKEY_free(pkey);
  46416. res = TEST_RES_CHECK(1);
  46417. #endif
  46418. #endif
  46419. return res;
  46420. }
  46421. static int test_EVP_PKEY_cmp(void)
  46422. {
  46423. int res = TEST_SKIPPED;
  46424. #if defined(OPENSSL_EXTRA)
  46425. EVP_PKEY *a, *b;
  46426. const unsigned char *in;
  46427. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46428. in = client_key_der_2048;
  46429. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46430. &in, (long)sizeof_client_key_der_2048));
  46431. in = client_key_der_2048;
  46432. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46433. &in, (long)sizeof_client_key_der_2048));
  46434. /* Test success case RSA */
  46435. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46436. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  46437. #else
  46438. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  46439. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46440. EVP_PKEY_free(b);
  46441. EVP_PKEY_free(a);
  46442. #endif
  46443. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46444. in = ecc_clikey_der_256;
  46445. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46446. &in, (long)sizeof_ecc_clikey_der_256));
  46447. in = ecc_clikey_der_256;
  46448. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46449. &in, (long)sizeof_ecc_clikey_der_256));
  46450. /* Test success case ECC */
  46451. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46452. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  46453. #else
  46454. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  46455. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46456. EVP_PKEY_free(b);
  46457. EVP_PKEY_free(a);
  46458. #endif
  46459. /* Test failure cases */
  46460. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  46461. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46462. in = client_key_der_2048;
  46463. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46464. &in, (long)sizeof_client_key_der_2048));
  46465. in = ecc_clikey_der_256;
  46466. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46467. &in, (long)sizeof_ecc_clikey_der_256));
  46468. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46469. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  46470. #else
  46471. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  46472. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46473. EVP_PKEY_free(b);
  46474. EVP_PKEY_free(a);
  46475. #endif
  46476. /* invalid or empty failure cases */
  46477. a = EVP_PKEY_new();
  46478. b = EVP_PKEY_new();
  46479. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46480. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  46481. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  46482. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  46483. #ifdef NO_RSA
  46484. /* Type check will fail since RSA is the default EVP key type */
  46485. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  46486. #else
  46487. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  46488. #endif
  46489. #else
  46490. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  46491. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  46492. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  46493. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  46494. #endif
  46495. EVP_PKEY_free(b);
  46496. EVP_PKEY_free(a);
  46497. (void)in;
  46498. res = TEST_RES_CHECK(1);
  46499. #endif
  46500. return res;
  46501. }
  46502. static int test_ERR_load_crypto_strings(void)
  46503. {
  46504. int res = TEST_SKIPPED;
  46505. #if defined(OPENSSL_ALL)
  46506. ERR_load_crypto_strings();
  46507. res = TEST_RES_CHECK(1);
  46508. #endif
  46509. return res;
  46510. }
  46511. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46512. static void free_x509(X509* x)
  46513. {
  46514. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  46515. }
  46516. #endif
  46517. static int test_sk_X509(void)
  46518. {
  46519. int res = TEST_SKIPPED;
  46520. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46521. {
  46522. STACK_OF(X509)* s;
  46523. AssertNotNull(s = sk_X509_new_null());
  46524. AssertIntEQ(sk_X509_num(s), 0);
  46525. sk_X509_pop_free(s, NULL);
  46526. AssertNotNull(s = sk_X509_new_null());
  46527. AssertIntEQ(sk_X509_num(s), 0);
  46528. sk_X509_pop_free(s, NULL);
  46529. AssertNotNull(s = sk_X509_new_null());
  46530. sk_X509_push(s, (X509*)1);
  46531. AssertIntEQ(sk_X509_num(s), 1);
  46532. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  46533. sk_X509_push(s, (X509*)2);
  46534. AssertIntEQ(sk_X509_num(s), 2);
  46535. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  46536. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  46537. sk_X509_push(s, (X509*)2);
  46538. sk_X509_pop_free(s, free_x509);
  46539. }
  46540. {
  46541. /* Push a list of 10 X509s onto stack, then verify that
  46542. * value(), push(), shift(), and pop() behave as expected. */
  46543. STACK_OF(X509)* s;
  46544. X509* xList[10];
  46545. int i = 0;
  46546. const int len = (sizeof(xList) / sizeof(xList[0]));
  46547. for (i = 0; i < len; ++i)
  46548. AssertNotNull(xList[i] = X509_new());
  46549. /* test push, pop, and free */
  46550. AssertNotNull(s = sk_X509_new_null());
  46551. for (i = 0; i < len; ++i) {
  46552. sk_X509_push(s, xList[i]);
  46553. AssertIntEQ(sk_X509_num(s), i + 1);
  46554. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46555. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46556. }
  46557. /* pop returns and removes last pushed on stack, which is index 0
  46558. * in sk_x509_value */
  46559. for (i = 0; i < len; ++i) {
  46560. X509 * x = sk_X509_value(s, 0);
  46561. X509 * y = sk_X509_pop(s);
  46562. X509 * z = xList[len - 1 - i];
  46563. AssertIntEQ((x == y), 1);
  46564. AssertIntEQ((x == z), 1);
  46565. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  46566. }
  46567. sk_free(s);
  46568. /* test push, shift, and free */
  46569. AssertNotNull(s = sk_X509_new_null());
  46570. for (i = 0; i < len; ++i) {
  46571. sk_X509_push(s, xList[i]);
  46572. AssertIntEQ(sk_X509_num(s), i + 1);
  46573. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46574. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46575. }
  46576. /* shift returns and removes first pushed on stack, which is index i
  46577. * in sk_x509_value() */
  46578. for (i = 0; i < len; ++i) {
  46579. X509 * x = sk_X509_value(s, len - 1 - i);
  46580. X509 * y = sk_X509_shift(s);
  46581. X509 * z = xList[i];
  46582. AssertIntEQ((x == y), 1);
  46583. AssertIntEQ((x == z), 1);
  46584. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  46585. }
  46586. sk_free(s);
  46587. for (i = 0; i < len; ++i)
  46588. X509_free(xList[i]);
  46589. }
  46590. res = TEST_RES_CHECK(1);
  46591. #endif
  46592. return res;
  46593. }
  46594. static int test_sk_X509_CRL(void)
  46595. {
  46596. int res = TEST_SKIPPED;
  46597. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  46598. X509_CRL* crl;
  46599. XFILE fp;
  46600. STACK_OF(X509_CRL)* s;
  46601. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  46602. AssertTrue((fp != XBADFILE));
  46603. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  46604. XFCLOSE(fp);
  46605. AssertNotNull(s = sk_X509_CRL_new());
  46606. AssertIntEQ(sk_X509_CRL_num(s), 0);
  46607. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  46608. AssertIntEQ(sk_X509_CRL_num(s), 1);
  46609. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  46610. sk_X509_CRL_free(s);
  46611. res = TEST_RES_CHECK(1);
  46612. #endif
  46613. return res;
  46614. }
  46615. static int test_X509_get_signature_nid(void)
  46616. {
  46617. int res = TEST_SKIPPED;
  46618. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46619. X509* x509;
  46620. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  46621. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  46622. SSL_FILETYPE_PEM));
  46623. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  46624. X509_free(x509);
  46625. res = TEST_RES_CHECK(1);
  46626. #endif
  46627. return res;
  46628. }
  46629. static int test_X509_REQ(void)
  46630. {
  46631. int res = TEST_SKIPPED;
  46632. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  46633. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  46634. X509_NAME* name;
  46635. #ifndef NO_RSA
  46636. X509_NAME* subject;
  46637. #endif
  46638. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46639. X509_REQ* req;
  46640. EVP_PKEY* priv;
  46641. EVP_PKEY* pub;
  46642. unsigned char* der = NULL;
  46643. int len;
  46644. #endif
  46645. #ifndef NO_RSA
  46646. EVP_MD_CTX *mctx = NULL;
  46647. EVP_PKEY_CTX *pkctx = NULL;
  46648. #ifdef USE_CERT_BUFFERS_1024
  46649. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  46650. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  46651. #elif defined(USE_CERT_BUFFERS_2048)
  46652. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  46653. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  46654. #endif
  46655. #endif
  46656. #ifdef HAVE_ECC
  46657. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  46658. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  46659. #endif
  46660. AssertNotNull(name = X509_NAME_new());
  46661. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  46662. (byte*)"wolfssl.com", 11, 0, 1),
  46663. WOLFSSL_SUCCESS);
  46664. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  46665. (byte*)"support@wolfssl.com", 19, -1,
  46666. 1), WOLFSSL_SUCCESS);
  46667. #ifndef NO_RSA
  46668. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  46669. (long)sizeof_client_key_der_2048));
  46670. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  46671. (long)sizeof_client_keypub_der_2048));
  46672. AssertNotNull(req = X509_REQ_new());
  46673. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  46674. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  46675. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46676. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  46677. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  46678. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46679. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  46680. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46681. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  46682. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46683. len = i2d_X509_REQ(req, &der);
  46684. DEBUG_WRITE_DER(der, len, "req.der");
  46685. #ifdef USE_CERT_BUFFERS_1024
  46686. AssertIntEQ(len, 381);
  46687. #else
  46688. AssertIntEQ(len, 643);
  46689. #endif
  46690. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46691. der = NULL;
  46692. mctx = EVP_MD_CTX_new();
  46693. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  46694. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  46695. EVP_MD_CTX_free(mctx);
  46696. X509_REQ_free(NULL);
  46697. X509_REQ_free(req);
  46698. /* Test getting the subject from a newly created X509_REQ */
  46699. AssertNotNull(req = X509_REQ_new());
  46700. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  46701. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  46702. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  46703. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  46704. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  46705. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  46706. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  46707. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  46708. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  46709. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  46710. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  46711. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  46712. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  46713. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46714. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46715. len = i2d_X509_REQ(req, &der);
  46716. DEBUG_WRITE_DER(der, len, "req2.der");
  46717. #ifdef USE_CERT_BUFFERS_1024
  46718. AssertIntEQ(len, 435);
  46719. #else
  46720. AssertIntEQ(len, 696);
  46721. #endif
  46722. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46723. der = NULL;
  46724. EVP_PKEY_free(pub);
  46725. EVP_PKEY_free(priv);
  46726. X509_REQ_free(req);
  46727. #endif
  46728. #ifdef HAVE_ECC
  46729. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  46730. sizeof_ecc_clikey_der_256));
  46731. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  46732. sizeof_ecc_clikeypub_der_256));
  46733. AssertNotNull(req = X509_REQ_new());
  46734. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46735. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46736. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46737. /* Signature is random and may be shorter or longer. */
  46738. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  46739. AssertIntLE(len, 253);
  46740. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46741. X509_REQ_free(req);
  46742. EVP_PKEY_free(pub);
  46743. EVP_PKEY_free(priv);
  46744. #ifdef FP_ECC
  46745. wc_ecc_fp_free();
  46746. #endif
  46747. #endif /* HAVE_ECC */
  46748. X509_NAME_free(name);
  46749. res = TEST_RES_CHECK(1);
  46750. #endif
  46751. return res;
  46752. }
  46753. static int test_wolfssl_PKCS7(void)
  46754. {
  46755. int res = TEST_SKIPPED;
  46756. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46757. !defined(NO_RSA)
  46758. PKCS7* pkcs7;
  46759. byte data[FOURK_BUF];
  46760. word32 len = sizeof(data);
  46761. const byte* p = data;
  46762. byte content[] = "Test data to encode.";
  46763. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46764. BIO* bio;
  46765. byte key[sizeof(client_key_der_2048)];
  46766. word32 keySz = (word32)sizeof(key);
  46767. byte* out = NULL;
  46768. #endif
  46769. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  46770. (word32)sizeof(content),
  46771. 0, 0, 0, RSA_TYPE)), 0);
  46772. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  46773. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  46774. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46775. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  46776. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  46777. PKCS7_free(pkcs7);
  46778. /* fail case, without PKCS7_NOVERIFY */
  46779. p = data;
  46780. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46781. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46782. 0), WOLFSSL_FAILURE);
  46783. PKCS7_free(pkcs7);
  46784. /* success case, with PKCS7_NOVERIFY */
  46785. p = data;
  46786. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46787. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46788. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  46789. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46790. /* test i2d */
  46791. XMEMCPY(key, client_key_der_2048, keySz);
  46792. pkcs7->privateKey = key;
  46793. pkcs7->privateKeySz = (word32)sizeof(key);
  46794. pkcs7->encryptOID = RSAk;
  46795. #ifdef NO_SHA
  46796. pkcs7->hashOID = SHA256h;
  46797. #else
  46798. pkcs7->hashOID = SHAh;
  46799. #endif
  46800. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  46801. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  46802. #ifndef NO_ASN_TIME
  46803. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  46804. #else
  46805. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  46806. #endif
  46807. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46808. BIO_free(bio);
  46809. #endif
  46810. PKCS7_free(NULL);
  46811. PKCS7_free(pkcs7);
  46812. res = TEST_RES_CHECK(1);
  46813. #endif
  46814. return res;
  46815. }
  46816. static int test_wolfSSL_PKCS7_sign(void)
  46817. {
  46818. int res = TEST_SKIPPED;
  46819. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46820. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46821. PKCS7* p7 = NULL;
  46822. PKCS7* p7Ver = NULL;
  46823. byte* out = NULL;
  46824. byte* tmpPtr = NULL;
  46825. int outLen = 0;
  46826. int flags = 0;
  46827. byte data[] = "Test data to encode.";
  46828. const char* cert = "./certs/server-cert.pem";
  46829. const char* key = "./certs/server-key.pem";
  46830. const char* ca = "./certs/ca-cert.pem";
  46831. WOLFSSL_BIO* certBio = NULL;
  46832. WOLFSSL_BIO* keyBio = NULL;
  46833. WOLFSSL_BIO* caBio = NULL;
  46834. WOLFSSL_BIO* inBio = NULL;
  46835. X509* signCert = NULL;
  46836. EVP_PKEY* signKey = NULL;
  46837. X509* caCert = NULL;
  46838. X509_STORE* store = NULL;
  46839. /* read signer cert/key into BIO */
  46840. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  46841. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  46842. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46843. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46844. /* read CA cert into store (for verify) */
  46845. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  46846. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46847. AssertNotNull(store = X509_STORE_new());
  46848. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46849. /* data to be signed into BIO */
  46850. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46851. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46852. /* PKCS7_sign, bad args: signer NULL */
  46853. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  46854. /* PKCS7_sign, bad args: signer key NULL */
  46855. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  46856. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  46857. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  46858. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  46859. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  46860. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  46861. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  46862. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  46863. {
  46864. flags = PKCS7_BINARY;
  46865. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46866. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46867. /* verify with d2i_PKCS7 */
  46868. tmpPtr = out;
  46869. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46870. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46871. PKCS7_free(p7Ver);
  46872. /* verify with wc_PKCS7_VerifySignedData */
  46873. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46874. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46875. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46876. /* compare the signer found to expected signer */
  46877. AssertIntNE(p7Ver->verifyCertSz, 0);
  46878. tmpPtr = NULL;
  46879. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  46880. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  46881. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  46882. tmpPtr = NULL;
  46883. wc_PKCS7_Free(p7Ver);
  46884. AssertNotNull(out);
  46885. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46886. out = NULL;
  46887. PKCS7_free(p7);
  46888. }
  46889. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  46890. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  46891. {
  46892. /* re-populate input BIO, may have been consumed */
  46893. BIO_free(inBio);
  46894. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46895. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46896. flags = PKCS7_BINARY | PKCS7_STREAM;
  46897. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46898. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46899. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46900. /* PKCS7_final, bad args: PKCS7 null */
  46901. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  46902. /* PKCS7_final, bad args: PKCS7 null */
  46903. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  46904. tmpPtr = out;
  46905. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46906. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46907. PKCS7_free(p7Ver);
  46908. AssertNotNull(out);
  46909. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46910. out = NULL;
  46911. PKCS7_free(p7);
  46912. }
  46913. /* TEST SUCCESS: Detached, not streaming, not MIME */
  46914. {
  46915. /* re-populate input BIO, may have been consumed */
  46916. BIO_free(inBio);
  46917. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46918. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46919. flags = PKCS7_BINARY | PKCS7_DETACHED;
  46920. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46921. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46922. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46923. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46924. p7Ver->content = data;
  46925. p7Ver->contentSz = sizeof(data);
  46926. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46927. wc_PKCS7_Free(p7Ver);
  46928. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  46929. * yet support detached content */
  46930. tmpPtr = out;
  46931. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46932. PKCS7_free(p7Ver);
  46933. AssertNotNull(out);
  46934. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46935. out = NULL;
  46936. PKCS7_free(p7);
  46937. }
  46938. /* TEST SUCCESS: Detached, streaming, not MIME */
  46939. {
  46940. /* re-populate input BIO, may have been consumed */
  46941. BIO_free(inBio);
  46942. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46943. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46944. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  46945. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46946. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46947. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46948. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46949. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46950. p7Ver->content = data;
  46951. p7Ver->contentSz = sizeof(data);
  46952. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46953. wc_PKCS7_Free(p7Ver);
  46954. AssertNotNull(out);
  46955. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46956. PKCS7_free(p7);
  46957. }
  46958. X509_STORE_free(store);
  46959. X509_free(caCert);
  46960. X509_free(signCert);
  46961. EVP_PKEY_free(signKey);
  46962. BIO_free(inBio);
  46963. BIO_free(keyBio);
  46964. BIO_free(certBio);
  46965. BIO_free(caBio);
  46966. res = TEST_RES_CHECK(1);
  46967. #endif
  46968. return res;
  46969. }
  46970. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  46971. {
  46972. int res = TEST_SKIPPED;
  46973. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  46974. PKCS7_SIGNED* pkcs7;
  46975. pkcs7 = PKCS7_SIGNED_new();
  46976. AssertNotNull(pkcs7);
  46977. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  46978. PKCS7_SIGNED_free(pkcs7);
  46979. res = TEST_RES_CHECK(1);
  46980. #endif
  46981. return res;
  46982. }
  46983. #ifndef NO_BIO
  46984. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  46985. {
  46986. int res = TEST_SKIPPED;
  46987. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  46988. PKCS7* pkcs7 = NULL;
  46989. BIO* bio = NULL;
  46990. const byte* cert_buf = NULL;
  46991. int ret = 0;
  46992. WC_RNG rng;
  46993. const byte data[] = { /* Hello World */
  46994. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46995. 0x72,0x6c,0x64
  46996. };
  46997. #ifndef NO_RSA
  46998. #if defined(USE_CERT_BUFFERS_2048)
  46999. byte key[sizeof(client_key_der_2048)];
  47000. byte cert[sizeof(client_cert_der_2048)];
  47001. word32 keySz = (word32)sizeof(key);
  47002. word32 certSz = (word32)sizeof(cert);
  47003. XMEMSET(key, 0, keySz);
  47004. XMEMSET(cert, 0, certSz);
  47005. XMEMCPY(key, client_key_der_2048, keySz);
  47006. XMEMCPY(cert, client_cert_der_2048, certSz);
  47007. #elif defined(USE_CERT_BUFFERS_1024)
  47008. byte key[sizeof_client_key_der_1024];
  47009. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47010. word32 keySz = (word32)sizeof(key);
  47011. word32 certSz = (word32)sizeof(cert);
  47012. XMEMSET(key, 0, keySz);
  47013. XMEMSET(cert, 0, certSz);
  47014. XMEMCPY(key, client_key_der_1024, keySz);
  47015. XMEMCPY(cert, client_cert_der_1024, certSz);
  47016. #else
  47017. unsigned char cert[ONEK_BUF];
  47018. unsigned char key[ONEK_BUF];
  47019. XFILE fp;
  47020. int certSz;
  47021. int keySz;
  47022. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  47023. AssertTrue((fp != XBADFILE));
  47024. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  47025. XFCLOSE(fp);
  47026. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  47027. AssertTrue(fp != XBADFILE);
  47028. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  47029. XFCLOSE(fp);
  47030. #endif
  47031. #elif defined(HAVE_ECC)
  47032. #if defined(USE_CERT_BUFFERS_256)
  47033. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47034. unsigned char key[sizeof(ecc_clikey_der_256)];
  47035. int certSz = (int)sizeof(cert);
  47036. int keySz = (int)sizeof(key);
  47037. XMEMSET(cert, 0, certSz);
  47038. XMEMSET(key, 0, keySz);
  47039. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47040. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47041. #else
  47042. unsigned char cert[ONEK_BUF];
  47043. unsigned char key[ONEK_BUF];
  47044. XFILE fp;
  47045. int certSz, keySz;
  47046. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  47047. AssertTrue(fp != XBADFILE);
  47048. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  47049. XFCLOSE(fp);
  47050. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  47051. AssertTrue(fp != XBADFILE);
  47052. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  47053. XFCLOSE(fp);
  47054. #endif
  47055. #else
  47056. #error PKCS7 requires ECC or RSA
  47057. #endif
  47058. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47059. /* initialize with DER encoded cert */
  47060. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47061. /* init rng */
  47062. AssertIntEQ(wc_InitRng(&rng), 0);
  47063. pkcs7->rng = &rng;
  47064. pkcs7->content = (byte*)data; /* not used for ex */
  47065. pkcs7->contentSz = (word32)sizeof(data);
  47066. pkcs7->contentOID = SIGNED_DATA;
  47067. pkcs7->privateKey = key;
  47068. pkcs7->privateKeySz = (word32)sizeof(key);
  47069. pkcs7->encryptOID = RSAk;
  47070. #ifdef NO_SHA
  47071. pkcs7->hashOID = SHA256h;
  47072. #else
  47073. pkcs7->hashOID = SHAh;
  47074. #endif
  47075. pkcs7->signedAttribs = NULL;
  47076. pkcs7->signedAttribsSz = 0;
  47077. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47078. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47079. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47080. /* Read PKCS#7 PEM from BIO */
  47081. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47082. AssertIntGE(ret, 0);
  47083. BIO_free(bio);
  47084. wc_PKCS7_Free(pkcs7);
  47085. wc_FreeRng(&rng);
  47086. res = TEST_RES_CHECK(1);
  47087. #endif
  47088. return res;
  47089. }
  47090. #ifdef HAVE_SMIME
  47091. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47092. {
  47093. int res = TEST_SKIPPED;
  47094. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47095. !defined(NO_RSA)
  47096. PKCS7* pkcs7 = NULL;
  47097. BIO* bio = NULL;
  47098. BIO* bcont = NULL;
  47099. BIO* out = NULL;
  47100. const byte* outBuf = NULL;
  47101. int outBufLen = 0;
  47102. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47103. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  47104. /* smime-test.p7s */
  47105. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47106. AssertNotNull(bio);
  47107. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47108. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47109. AssertNotNull(pkcs7);
  47110. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47111. PKCS7_NOVERIFY), SSL_SUCCESS);
  47112. XFCLOSE(smimeTestFile);
  47113. if (bcont) BIO_free(bcont);
  47114. wolfSSL_PKCS7_free(pkcs7);
  47115. /* smime-test-multipart.p7s */
  47116. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47117. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47118. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47119. AssertNotNull(pkcs7);
  47120. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47121. PKCS7_NOVERIFY), SSL_SUCCESS);
  47122. XFCLOSE(smimeTestFile);
  47123. if (bcont) BIO_free(bcont);
  47124. wolfSSL_PKCS7_free(pkcs7);
  47125. /* smime-test-multipart-badsig.p7s */
  47126. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47127. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47128. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47129. AssertNull(pkcs7);
  47130. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47131. PKCS7_NOVERIFY), SSL_FAILURE);
  47132. XFCLOSE(smimeTestFile);
  47133. if (bcont) BIO_free(bcont);
  47134. wolfSSL_PKCS7_free(pkcs7);
  47135. /* smime-test-canon.p7s */
  47136. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47137. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47138. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47139. AssertNotNull(pkcs7);
  47140. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47141. PKCS7_NOVERIFY), SSL_SUCCESS);
  47142. XFCLOSE(smimeTestFile);
  47143. if (bcont) BIO_free(bcont);
  47144. wolfSSL_PKCS7_free(pkcs7);
  47145. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47146. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47147. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47148. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47149. AssertNotNull(pkcs7);
  47150. out = wolfSSL_BIO_new(BIO_s_mem());
  47151. AssertNotNull(out);
  47152. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47153. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47154. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47155. /* Content-Type should not show up at beginning of output buffer */
  47156. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  47157. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47158. BIO_free(out);
  47159. BIO_free(bio);
  47160. if (bcont) BIO_free(bcont);
  47161. wolfSSL_PKCS7_free(pkcs7);
  47162. res = TEST_RES_CHECK(1);
  47163. #endif
  47164. return res;
  47165. }
  47166. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47167. {
  47168. int res = TEST_SKIPPED;
  47169. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47170. PKCS7* p7 = NULL;
  47171. PKCS7* p7Ver = NULL;
  47172. int flags = 0;
  47173. byte data[] = "Test data to encode.";
  47174. const char* cert = "./certs/server-cert.pem";
  47175. const char* key = "./certs/server-key.pem";
  47176. const char* ca = "./certs/ca-cert.pem";
  47177. WOLFSSL_BIO* certBio = NULL;
  47178. WOLFSSL_BIO* keyBio = NULL;
  47179. WOLFSSL_BIO* caBio = NULL;
  47180. WOLFSSL_BIO* inBio = NULL;
  47181. WOLFSSL_BIO* outBio = NULL;
  47182. WOLFSSL_BIO* content = NULL;
  47183. X509* signCert = NULL;
  47184. EVP_PKEY* signKey = NULL;
  47185. X509* caCert = NULL;
  47186. X509_STORE* store = NULL;
  47187. /* read signer cert/key into BIO */
  47188. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  47189. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  47190. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47191. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47192. /* read CA cert into store (for verify) */
  47193. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  47194. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47195. AssertNotNull(store = X509_STORE_new());
  47196. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47197. /* generate and verify SMIME: not detached */
  47198. {
  47199. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  47200. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47201. flags = PKCS7_STREAM;
  47202. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47203. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  47204. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47205. /* bad arg: out NULL */
  47206. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47207. /* bad arg: pkcs7 NULL */
  47208. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47209. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47210. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47211. BIO_free(content);
  47212. BIO_free(inBio);
  47213. BIO_free(outBio);
  47214. PKCS7_free(p7Ver);
  47215. PKCS7_free(p7);
  47216. }
  47217. /* generate and verify SMIME: not detached, add Content-Type */
  47218. {
  47219. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  47220. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47221. flags = PKCS7_STREAM | PKCS7_TEXT;
  47222. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47223. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  47224. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47225. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47226. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47227. BIO_free(content);
  47228. BIO_free(inBio);
  47229. BIO_free(outBio);
  47230. PKCS7_free(p7Ver);
  47231. PKCS7_free(p7);
  47232. }
  47233. /* generate and verify SMIME: detached */
  47234. {
  47235. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  47236. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47237. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47238. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47239. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  47240. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47241. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47242. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47243. BIO_free(content);
  47244. BIO_free(inBio);
  47245. BIO_free(outBio);
  47246. PKCS7_free(p7Ver);
  47247. PKCS7_free(p7);
  47248. }
  47249. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47250. {
  47251. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  47252. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47253. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47254. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47255. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  47256. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47257. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47258. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47259. BIO_free(content);
  47260. BIO_free(inBio);
  47261. BIO_free(outBio);
  47262. PKCS7_free(p7Ver);
  47263. PKCS7_free(p7);
  47264. }
  47265. X509_STORE_free(store);
  47266. X509_free(caCert);
  47267. X509_free(signCert);
  47268. EVP_PKEY_free(signKey);
  47269. BIO_free(keyBio);
  47270. BIO_free(certBio);
  47271. BIO_free(caBio);
  47272. res = TEST_RES_CHECK(1);
  47273. #endif
  47274. return res;
  47275. }
  47276. #endif /* HAVE_SMIME */
  47277. #endif /* !NO_BIO */
  47278. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47279. * returns 0) */
  47280. static int test_X509_STORE_No_SSL_CTX(void)
  47281. {
  47282. int res = TEST_SKIPPED;
  47283. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  47284. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  47285. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47286. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47287. !defined(NO_RSA)
  47288. X509_STORE * store;
  47289. X509_STORE_CTX * storeCtx;
  47290. X509_CRL * crl;
  47291. X509 * ca;
  47292. X509 * cert;
  47293. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47294. const char srvCert[] = "./certs/server-cert.pem";
  47295. const char caCert[] = "./certs/ca-cert.pem";
  47296. const char caDir[] = "./certs/crl/hash_pem";
  47297. XFILE fp;
  47298. X509_LOOKUP * lookup;
  47299. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  47300. /* Set up store with CA */
  47301. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47302. SSL_FILETYPE_PEM)));
  47303. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47304. /* Add CRL lookup directory to store
  47305. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  47306. * of crl.pem */
  47307. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  47308. X509_LOOKUP_hash_dir())));
  47309. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  47310. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  47311. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47312. SSL_SUCCESS);
  47313. /* Add CRL to store NOT containing the verified certificate, which
  47314. * forces use of the CRL lookup directory */
  47315. fp = XFOPEN(cliCrlPem, "rb");
  47316. AssertTrue((fp != XBADFILE));
  47317. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47318. NULL, NULL));
  47319. XFCLOSE(fp);
  47320. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47321. /* Create verification context outside of an SSL session */
  47322. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  47323. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47324. SSL_FILETYPE_PEM)));
  47325. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47326. /* Perform verification, which should NOT indicate CRL missing due to the
  47327. * store CM's X509 store pointer being NULL */
  47328. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47329. X509_CRL_free(crl);
  47330. X509_STORE_free(store);
  47331. X509_STORE_CTX_free(storeCtx);
  47332. X509_free(cert);
  47333. X509_free(ca);
  47334. res = TEST_RES_CHECK(1);
  47335. #endif
  47336. return res;
  47337. }
  47338. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  47339. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  47340. static int test_X509_LOOKUP_add_dir(void)
  47341. {
  47342. int res = TEST_SKIPPED;
  47343. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  47344. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  47345. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47346. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47347. !defined(NO_RSA)
  47348. X509_STORE * store;
  47349. X509_STORE_CTX * storeCtx;
  47350. X509_CRL * crl;
  47351. X509 * ca;
  47352. X509 * cert;
  47353. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47354. const char srvCert[] = "./certs/server-cert.pem";
  47355. const char caCert[] = "./certs/ca-cert.pem";
  47356. const char caDir[] = "./certs/crl/hash_der";
  47357. XFILE fp;
  47358. X509_LOOKUP * lookup;
  47359. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  47360. /* Set up store with CA */
  47361. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47362. SSL_FILETYPE_PEM)));
  47363. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47364. /* Add CRL lookup directory to store.
  47365. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  47366. * of crl.der */
  47367. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  47368. X509_LOOKUP_hash_dir())));
  47369. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  47370. SSL_SUCCESS);
  47371. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47372. SSL_SUCCESS);
  47373. /* Add CRL to store NOT containing the verified certificate, which
  47374. * forces use of the CRL lookup directory */
  47375. fp = XFOPEN(cliCrlPem, "rb");
  47376. AssertTrue((fp != XBADFILE));
  47377. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47378. NULL, NULL));
  47379. XFCLOSE(fp);
  47380. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47381. /* Create verification context outside of an SSL session */
  47382. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  47383. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47384. SSL_FILETYPE_PEM)));
  47385. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47386. /* Perform verification, which should NOT return CRL missing */
  47387. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47388. X509_CRL_free(crl);
  47389. X509_STORE_free(store);
  47390. X509_STORE_CTX_free(storeCtx);
  47391. X509_free(cert);
  47392. X509_free(ca);
  47393. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  47394. * We should get CRL_MISSING at the end, because the lookup
  47395. * dir has only ASN1 CRLs. */
  47396. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  47397. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47398. SSL_FILETYPE_PEM)));
  47399. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47400. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  47401. X509_LOOKUP_hash_dir())));
  47402. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  47403. SSL_SUCCESS);
  47404. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47405. SSL_SUCCESS);
  47406. fp = XFOPEN(cliCrlPem, "rb");
  47407. AssertTrue((fp != XBADFILE));
  47408. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47409. NULL, NULL));
  47410. XFCLOSE(fp);
  47411. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47412. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  47413. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47414. SSL_FILETYPE_PEM)));
  47415. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47416. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  47417. * in dir containing only ASN1/DER. */
  47418. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  47419. X509_CRL_free(crl);
  47420. X509_STORE_free(store);
  47421. X509_STORE_CTX_free(storeCtx);
  47422. X509_free(cert);
  47423. X509_free(ca);
  47424. res = TEST_RES_CHECK(1);
  47425. #endif
  47426. return res;
  47427. }
  47428. /*----------------------------------------------------------------------------*
  47429. | Certificate Failure Checks
  47430. *----------------------------------------------------------------------------*/
  47431. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  47432. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  47433. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  47434. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  47435. int type)
  47436. {
  47437. int ret;
  47438. WOLFSSL_CERT_MANAGER* cm = NULL;
  47439. switch (type) {
  47440. case TESTING_RSA:
  47441. #ifdef NO_RSA
  47442. fprintf(stderr, "RSA disabled, skipping test\n");
  47443. return ASN_SIG_CONFIRM_E;
  47444. #else
  47445. break;
  47446. #endif
  47447. case TESTING_ECC:
  47448. #ifndef HAVE_ECC
  47449. fprintf(stderr, "ECC disabled, skipping test\n");
  47450. return ASN_SIG_CONFIRM_E;
  47451. #else
  47452. break;
  47453. #endif
  47454. default:
  47455. fprintf(stderr, "Bad function argument\n");
  47456. return BAD_FUNC_ARG;
  47457. }
  47458. cm = wolfSSL_CertManagerNew();
  47459. if (cm == NULL) {
  47460. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  47461. return -1;
  47462. }
  47463. #ifndef NO_FILESYSTEM
  47464. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  47465. if (ret != WOLFSSL_SUCCESS) {
  47466. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  47467. wolfSSL_CertManagerFree(cm);
  47468. return ret;
  47469. }
  47470. #else
  47471. (void)ca;
  47472. #endif
  47473. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  47474. /* Let AssertIntEQ handle return code */
  47475. wolfSSL_CertManagerFree(cm);
  47476. return ret;
  47477. }
  47478. #if !defined(NO_FILESYSTEM)
  47479. static int test_RsaSigFailure_cm(void)
  47480. {
  47481. int ret = 0;
  47482. const char* ca_cert = "./certs/ca-cert.pem";
  47483. const char* server_cert = "./certs/server-cert.der";
  47484. byte* cert_buf = NULL;
  47485. size_t cert_sz = 0;
  47486. ret = load_file(server_cert, &cert_buf, &cert_sz);
  47487. if (ret == 0) {
  47488. /* corrupt DER - invert last byte, which is signature */
  47489. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47490. /* test bad cert */
  47491. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  47492. }
  47493. if (cert_buf)
  47494. free(cert_buf);
  47495. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47496. if (ret == WOLFSSL_FATAL_ERROR) {
  47497. ret = 0;
  47498. }
  47499. #else
  47500. if (ret == ASN_SIG_CONFIRM_E) {
  47501. ret = 0;
  47502. }
  47503. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  47504. return TEST_RES_CHECK(ret == 0);
  47505. }
  47506. static int test_EccSigFailure_cm(void)
  47507. {
  47508. int ret = 0;
  47509. /* self-signed ECC cert, so use server cert as CA */
  47510. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  47511. const char* server_cert = "./certs/server-ecc.der";
  47512. byte* cert_buf = NULL;
  47513. size_t cert_sz = 0;
  47514. ret = load_file(server_cert, &cert_buf, &cert_sz);
  47515. if (ret == 0) {
  47516. /* corrupt DER - invert last byte, which is signature */
  47517. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47518. /* test bad cert */
  47519. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  47520. }
  47521. if (cert_buf)
  47522. free(cert_buf);
  47523. #ifdef FP_ECC
  47524. wc_ecc_fp_free();
  47525. #endif
  47526. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47527. if (ret == WOLFSSL_FATAL_ERROR) {
  47528. ret = 0;
  47529. }
  47530. #else
  47531. if (ret == ASN_SIG_CONFIRM_E) {
  47532. ret = 0;
  47533. }
  47534. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  47535. return TEST_RES_CHECK(ret == 0);
  47536. }
  47537. #endif /* !NO_FILESYSTEM */
  47538. #endif /* NO_CERTS */
  47539. #ifdef WOLFSSL_TLS13
  47540. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  47541. #ifdef WC_SHA384_DIGEST_SIZE
  47542. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  47543. #else
  47544. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  47545. #endif
  47546. #endif
  47547. #ifdef WOLFSSL_EARLY_DATA
  47548. static const char earlyData[] = "Early Data";
  47549. static char earlyDataBuffer[1];
  47550. #endif
  47551. static int test_tls13_apis(void)
  47552. {
  47553. int ret = 0;
  47554. #ifndef WOLFSSL_NO_TLS12
  47555. #ifndef NO_WOLFSSL_CLIENT
  47556. WOLFSSL_CTX* clientTls12Ctx;
  47557. WOLFSSL* clientTls12Ssl;
  47558. #endif
  47559. #ifndef NO_WOLFSSL_SERVER
  47560. WOLFSSL_CTX* serverTls12Ctx;
  47561. WOLFSSL* serverTls12Ssl;
  47562. #endif
  47563. #endif
  47564. #ifndef NO_WOLFSSL_CLIENT
  47565. WOLFSSL_CTX* clientCtx;
  47566. WOLFSSL* clientSsl;
  47567. #endif
  47568. #ifndef NO_WOLFSSL_SERVER
  47569. WOLFSSL_CTX* serverCtx;
  47570. WOLFSSL* serverSsl;
  47571. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47572. const char* ourCert = svrCertFile;
  47573. const char* ourKey = svrKeyFile;
  47574. #endif
  47575. #endif
  47576. int required;
  47577. #ifdef WOLFSSL_EARLY_DATA
  47578. int outSz;
  47579. #endif
  47580. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  47581. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  47582. #ifdef HAVE_PQC
  47583. WOLFSSL_KYBER_LEVEL1
  47584. #else
  47585. WOLFSSL_ECC_SECP256R1
  47586. #endif
  47587. };
  47588. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  47589. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  47590. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  47591. int numGroups = 2;
  47592. #endif
  47593. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47594. char groupList[] =
  47595. #ifndef NO_ECC_SECP
  47596. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  47597. "P-521:"
  47598. #endif
  47599. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  47600. "P-384:"
  47601. #endif
  47602. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  47603. "P-256"
  47604. #ifdef HAVE_PQC
  47605. ":P256_KYBER_LEVEL1"
  47606. #endif
  47607. #endif
  47608. #endif /* !defined(NO_ECC_SECP) */
  47609. #ifdef HAVE_PQC
  47610. ":KYBER_LEVEL1"
  47611. #endif
  47612. "";
  47613. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47614. (void)ret;
  47615. #ifndef WOLFSSL_NO_TLS12
  47616. #ifndef NO_WOLFSSL_CLIENT
  47617. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  47618. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  47619. #endif
  47620. #ifndef NO_WOLFSSL_SERVER
  47621. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  47622. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47623. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  47624. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  47625. #endif
  47626. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  47627. #endif
  47628. #endif
  47629. #ifndef NO_WOLFSSL_CLIENT
  47630. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  47631. clientSsl = wolfSSL_new(clientCtx);
  47632. #endif
  47633. #ifndef NO_WOLFSSL_SERVER
  47634. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  47635. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47636. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  47637. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  47638. #endif
  47639. serverSsl = wolfSSL_new(serverCtx);
  47640. AssertNotNull(serverSsl);
  47641. #endif
  47642. #ifdef WOLFSSL_SEND_HRR_COOKIE
  47643. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  47644. #ifndef NO_WOLFSSL_CLIENT
  47645. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  47646. #endif
  47647. #ifndef NO_WOLFSSL_SERVER
  47648. #ifndef WOLFSSL_NO_TLS12
  47649. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  47650. #endif
  47651. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  47652. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  47653. WOLFSSL_SUCCESS);
  47654. #endif
  47655. #endif
  47656. #ifdef HAVE_SUPPORTED_CURVES
  47657. #ifdef HAVE_ECC
  47658. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  47659. #ifndef NO_WOLFSSL_SERVER
  47660. do {
  47661. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  47662. #ifdef WOLFSSL_ASYNC_CRYPT
  47663. if (ret == WC_PENDING_E)
  47664. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  47665. #endif
  47666. } while (ret == WC_PENDING_E);
  47667. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47668. #endif
  47669. #ifndef NO_WOLFSSL_CLIENT
  47670. #ifndef WOLFSSL_NO_TLS12
  47671. do {
  47672. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  47673. #ifdef WOLFSSL_ASYNC_CRYPT
  47674. if (ret == WC_PENDING_E)
  47675. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  47676. #endif
  47677. } while (ret == WC_PENDING_E);
  47678. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47679. #endif
  47680. do {
  47681. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  47682. #ifdef WOLFSSL_ASYNC_CRYPT
  47683. if (ret == WC_PENDING_E)
  47684. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  47685. #endif
  47686. } while (ret == WC_PENDING_E);
  47687. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47688. #endif
  47689. #elif defined(HAVE_CURVE25519)
  47690. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  47691. #ifndef NO_WOLFSSL_SERVER
  47692. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  47693. WOLFSSL_SUCCESS);
  47694. #endif
  47695. #ifndef NO_WOLFSSL_CLIENT
  47696. #ifndef WOLFSSL_NO_TLS12
  47697. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  47698. WOLFSSL_SUCCESS);
  47699. #endif
  47700. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  47701. WOLFSSL_SUCCESS);
  47702. #endif
  47703. #elif defined(HAVE_CURVE448)
  47704. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  47705. #ifndef NO_WOLFSSL_SERVER
  47706. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  47707. WOLFSSL_SUCCESS);
  47708. #endif
  47709. #ifndef NO_WOLFSSL_CLIENT
  47710. #ifndef WOLFSSL_NO_TLS12
  47711. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  47712. WOLFSSL_SUCCESS);
  47713. #endif
  47714. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  47715. WOLFSSL_SUCCESS);
  47716. #endif
  47717. #else
  47718. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  47719. #ifndef NO_WOLFSSL_CLIENT
  47720. #ifndef WOLFSSL_NO_TLS12
  47721. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  47722. NOT_COMPILED_IN);
  47723. #endif
  47724. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  47725. NOT_COMPILED_IN);
  47726. #endif
  47727. #endif
  47728. #if defined(HAVE_PQC)
  47729. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  47730. #ifndef NO_WOLFSSL_SERVER
  47731. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  47732. WOLFSSL_SUCCESS);
  47733. #endif
  47734. #ifndef NO_WOLFSSL_CLIENT
  47735. #ifndef WOLFSSL_NO_TLS12
  47736. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  47737. BAD_FUNC_ARG);
  47738. #endif
  47739. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  47740. WOLFSSL_SUCCESS);
  47741. #endif
  47742. #endif
  47743. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  47744. #ifndef NO_WOLFSSL_SERVER
  47745. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  47746. #endif
  47747. #ifndef NO_WOLFSSL_CLIENT
  47748. #ifndef WOLFSSL_NO_TLS12
  47749. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  47750. #endif
  47751. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  47752. #endif
  47753. #endif /* HAVE_SUPPORTED_CURVES */
  47754. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47755. #ifndef NO_WOLFSSL_CLIENT
  47756. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  47757. #endif
  47758. #ifndef NO_WOLFSSL_SERVER
  47759. #ifndef WOLFSSL_NO_TLS12
  47760. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  47761. #endif
  47762. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  47763. #endif
  47764. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47765. #ifndef NO_WOLFSSL_CLIENT
  47766. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  47767. #endif
  47768. #ifndef NO_WOLFSSL_SERVER
  47769. #ifndef WOLFSSL_NO_TLS12
  47770. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  47771. #endif
  47772. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  47773. #endif
  47774. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47775. #ifndef NO_WOLFSSL_CLIENT
  47776. #ifndef WOLFSSL_NO_TLS12
  47777. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  47778. #endif
  47779. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  47780. #endif
  47781. #ifndef NO_WOLFSSL_SERVER
  47782. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  47783. #endif
  47784. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47785. #ifndef NO_WOLFSSL_CLIENT
  47786. #ifndef WOLFSSL_NO_TLS12
  47787. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  47788. #endif
  47789. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  47790. #endif
  47791. #ifndef NO_WOLFSSL_SERVER
  47792. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  47793. #endif
  47794. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  47795. #ifndef NO_WOLFSSL_CLIENT
  47796. #ifndef WOLFSSL_NO_TLS12
  47797. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  47798. #endif
  47799. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  47800. #endif
  47801. #ifndef NO_WOLFSSL_SERVER
  47802. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  47803. #endif
  47804. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  47805. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  47806. #ifndef NO_WOLFSSL_CLIENT
  47807. #ifndef WOLFSSL_NO_TLS12
  47808. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  47809. BAD_FUNC_ARG);
  47810. #endif
  47811. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  47812. #endif
  47813. #ifndef NO_WOLFSSL_SERVER
  47814. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  47815. #endif
  47816. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  47817. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47818. #ifndef NO_WOLFSSL_SERVER
  47819. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  47820. #endif
  47821. #ifndef NO_WOLFSSL_CLIENT
  47822. #ifndef WOLFSSL_NO_TLS12
  47823. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  47824. BAD_FUNC_ARG);
  47825. #endif
  47826. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  47827. #endif
  47828. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47829. #ifndef NO_WOLFSSL_SERVER
  47830. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  47831. #endif
  47832. #ifndef NO_WOLFSSL_CLIENT
  47833. #ifndef WOLFSSL_NO_TLS12
  47834. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  47835. BAD_FUNC_ARG);
  47836. #endif
  47837. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  47838. #endif
  47839. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  47840. #ifndef NO_WOLFSSL_CLIENT
  47841. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  47842. #endif
  47843. #ifndef NO_WOLFSSL_SERVER
  47844. #ifndef WOLFSSL_NO_TLS12
  47845. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  47846. BAD_FUNC_ARG);
  47847. #endif
  47848. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  47849. #endif
  47850. #endif
  47851. #ifdef HAVE_ECC
  47852. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  47853. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  47854. #ifndef NO_WOLFSSL_SERVER
  47855. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  47856. #endif
  47857. #ifndef NO_WOLFSSL_CLIENT
  47858. #ifndef WOLFSSL_NO_TLS12
  47859. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  47860. #endif
  47861. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  47862. #endif
  47863. #endif
  47864. #ifdef HAVE_SUPPORTED_CURVES
  47865. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47866. #ifndef NO_WOLFSSL_CLIENT
  47867. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  47868. #endif
  47869. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47870. #ifndef NO_WOLFSSL_CLIENT
  47871. #ifndef WOLFSSL_NO_TLS12
  47872. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  47873. BAD_FUNC_ARG);
  47874. #endif
  47875. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  47876. WOLFSSL_MAX_GROUP_COUNT + 1),
  47877. BAD_FUNC_ARG);
  47878. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  47879. WOLFSSL_SUCCESS);
  47880. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  47881. BAD_FUNC_ARG);
  47882. #endif
  47883. #ifndef NO_WOLFSSL_SERVER
  47884. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  47885. WOLFSSL_SUCCESS);
  47886. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  47887. BAD_FUNC_ARG);
  47888. #endif
  47889. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47890. #ifndef NO_WOLFSSL_CLIENT
  47891. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  47892. #endif
  47893. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47894. #ifndef NO_WOLFSSL_CLIENT
  47895. #ifndef WOLFSSL_NO_TLS12
  47896. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  47897. BAD_FUNC_ARG);
  47898. #endif
  47899. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  47900. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47901. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  47902. WOLFSSL_SUCCESS);
  47903. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  47904. BAD_FUNC_ARG);
  47905. #endif
  47906. #ifndef NO_WOLFSSL_SERVER
  47907. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  47908. WOLFSSL_SUCCESS);
  47909. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  47910. BAD_FUNC_ARG);
  47911. #endif
  47912. #ifdef OPENSSL_EXTRA
  47913. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47914. #ifndef NO_WOLFSSL_CLIENT
  47915. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  47916. #endif
  47917. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  47918. #ifndef NO_WOLFSSL_CLIENT
  47919. #ifndef WOLFSSL_NO_TLS12
  47920. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  47921. WOLFSSL_FAILURE);
  47922. #endif
  47923. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  47924. WOLFSSL_SUCCESS);
  47925. #endif
  47926. #ifndef NO_WOLFSSL_SERVER
  47927. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  47928. WOLFSSL_SUCCESS);
  47929. #endif
  47930. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47931. #ifndef NO_WOLFSSL_CLIENT
  47932. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  47933. #endif
  47934. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  47935. #ifndef NO_WOLFSSL_CLIENT
  47936. #ifndef WOLFSSL_NO_TLS12
  47937. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  47938. WOLFSSL_FAILURE);
  47939. #endif
  47940. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  47941. WOLFSSL_SUCCESS);
  47942. #endif
  47943. #ifndef NO_WOLFSSL_SERVER
  47944. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  47945. WOLFSSL_SUCCESS);
  47946. #endif
  47947. #endif /* OPENSSL_EXTRA */
  47948. #endif /* HAVE_SUPPORTED_CURVES */
  47949. #endif /* HAVE_ECC */
  47950. #ifdef WOLFSSL_EARLY_DATA
  47951. #ifndef OPENSSL_EXTRA
  47952. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47953. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47954. #else
  47955. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47956. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47957. #endif
  47958. #ifndef NO_WOLFSSL_CLIENT
  47959. #ifndef OPENSSL_EXTRA
  47960. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47961. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47962. #else
  47963. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47964. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47965. #endif
  47966. #endif
  47967. #ifndef NO_WOLFSSL_SERVER
  47968. #ifndef WOLFSSL_NO_TLS12
  47969. #ifndef OPENSSL_EXTRA
  47970. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47971. BAD_FUNC_ARG);
  47972. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47973. #else
  47974. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47975. BAD_FUNC_ARG);
  47976. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47977. #endif
  47978. #endif
  47979. #ifndef OPENSSL_EXTRA
  47980. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47981. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), WOLFSSL_SUCCESS);
  47982. #else
  47983. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  47984. #endif
  47985. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  47986. #else
  47987. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  47988. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  47989. #endif
  47990. #endif
  47991. #ifndef OPENSSL_EXTRA
  47992. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47993. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47994. #else
  47995. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47996. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47997. #endif
  47998. #ifndef NO_WOLFSSL_CLIENT
  47999. #ifndef OPENSSL_EXTRA
  48000. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48001. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48002. #else
  48003. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48004. #endif
  48005. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48006. #else
  48007. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48008. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48009. #endif
  48010. #endif
  48011. #ifndef NO_WOLFSSL_SERVER
  48012. #ifndef WOLFSSL_NO_TLS12
  48013. #ifndef OPENSSL_EXTRA
  48014. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48015. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48016. #else
  48017. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48018. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48019. #endif
  48020. #endif
  48021. #ifndef OPENSSL_EXTRA
  48022. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48023. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48024. #else
  48025. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48026. #endif
  48027. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48028. #else
  48029. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48030. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48031. #endif
  48032. #endif
  48033. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48034. &outSz), BAD_FUNC_ARG);
  48035. #ifndef NO_WOLFSSL_CLIENT
  48036. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48037. &outSz), BAD_FUNC_ARG);
  48038. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48039. BAD_FUNC_ARG);
  48040. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48041. sizeof(earlyData), NULL),
  48042. BAD_FUNC_ARG);
  48043. #endif
  48044. #ifndef NO_WOLFSSL_SERVER
  48045. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48046. sizeof(earlyData), &outSz),
  48047. SIDE_ERROR);
  48048. #endif
  48049. #ifndef NO_WOLFSSL_CLIENT
  48050. #ifndef WOLFSSL_NO_TLS12
  48051. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48052. sizeof(earlyData), &outSz),
  48053. BAD_FUNC_ARG);
  48054. #endif
  48055. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48056. sizeof(earlyData), &outSz),
  48057. WOLFSSL_FATAL_ERROR);
  48058. #endif
  48059. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48060. sizeof(earlyDataBuffer), &outSz),
  48061. BAD_FUNC_ARG);
  48062. #ifndef NO_WOLFSSL_SERVER
  48063. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48064. sizeof(earlyDataBuffer), &outSz),
  48065. BAD_FUNC_ARG);
  48066. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  48067. BAD_FUNC_ARG);
  48068. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48069. sizeof(earlyDataBuffer), NULL),
  48070. BAD_FUNC_ARG);
  48071. #endif
  48072. #ifndef NO_WOLFSSL_CLIENT
  48073. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48074. sizeof(earlyDataBuffer), &outSz),
  48075. SIDE_ERROR);
  48076. #endif
  48077. #ifndef NO_WOLFSSL_SERVER
  48078. #ifndef WOLFSSL_NO_TLS12
  48079. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48080. sizeof(earlyDataBuffer), &outSz),
  48081. BAD_FUNC_ARG);
  48082. #endif
  48083. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48084. sizeof(earlyDataBuffer), &outSz),
  48085. WOLFSSL_FATAL_ERROR);
  48086. #endif
  48087. #endif
  48088. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48089. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  48090. #endif
  48091. #ifndef NO_WOLFSSL_SERVER
  48092. wolfSSL_free(serverSsl);
  48093. wolfSSL_CTX_free(serverCtx);
  48094. #endif
  48095. #ifndef NO_WOLFSSL_CLIENT
  48096. wolfSSL_free(clientSsl);
  48097. wolfSSL_CTX_free(clientCtx);
  48098. #endif
  48099. #ifndef WOLFSSL_NO_TLS12
  48100. #ifndef NO_WOLFSSL_SERVER
  48101. wolfSSL_free(serverTls12Ssl);
  48102. wolfSSL_CTX_free(serverTls12Ctx);
  48103. #endif
  48104. #ifndef NO_WOLFSSL_CLIENT
  48105. wolfSSL_free(clientTls12Ssl);
  48106. wolfSSL_CTX_free(clientTls12Ctx);
  48107. #endif
  48108. #endif
  48109. return TEST_RES_CHECK(1);
  48110. }
  48111. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48112. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48113. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48114. /* Called when writing. */
  48115. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48116. {
  48117. (void)ssl;
  48118. (void)buf;
  48119. (void)sz;
  48120. (void)ctx;
  48121. /* Force error return from wolfSSL_accept_TLSv13(). */
  48122. return WANT_WRITE;
  48123. }
  48124. /* Called when reading. */
  48125. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48126. {
  48127. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48128. int len = (int)msg->length;
  48129. (void)ssl;
  48130. (void)sz;
  48131. /* Pass back as much of message as will fit in buffer. */
  48132. if (len > sz)
  48133. len = sz;
  48134. XMEMCPY(buf, msg->buffer, len);
  48135. /* Move over returned data. */
  48136. msg->buffer += len;
  48137. msg->length -= len;
  48138. /* Amount actually copied. */
  48139. return len;
  48140. }
  48141. #endif
  48142. static int test_tls13_cipher_suites(void)
  48143. {
  48144. int res = TEST_SKIPPED;
  48145. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48146. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48147. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48148. WOLFSSL_CTX* ctx;
  48149. WOLFSSL *ssl;
  48150. int i;
  48151. byte clientHello[] = {
  48152. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48153. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48154. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48155. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48156. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48157. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48158. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48159. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48162. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48163. 0x13, 0x01,
  48164. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48165. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48166. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48167. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48168. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48169. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48170. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48171. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48172. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48173. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48174. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48175. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48176. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48177. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48178. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48179. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48180. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48181. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48182. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48183. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48184. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48185. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48186. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48187. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48188. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48189. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48190. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48191. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48192. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48193. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48194. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48195. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48196. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48197. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48198. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48199. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48200. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48201. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48202. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48203. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48204. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48205. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48206. };
  48207. WOLFSSL_BUFFER_INFO msg;
  48208. /* Offset into ClientHello message data of first cipher suite. */
  48209. const int csOff = 78;
  48210. /* Server cipher list. */
  48211. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48212. /* Suite list with duplicates. */
  48213. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48214. "TLS13-AES128-GCM-SHA256:"
  48215. "TLS13-AES256-GCM-SHA384:"
  48216. "TLS13-AES256-GCM-SHA384:"
  48217. "TLS13-AES128-GCM-SHA256";
  48218. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48219. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48220. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48221. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48222. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48223. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48224. #endif
  48225. /* Set up wolfSSL context. */
  48226. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48227. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48228. WOLFSSL_FILETYPE_PEM));
  48229. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48230. WOLFSSL_FILETYPE_PEM));
  48231. /* Read from 'msg'. */
  48232. wolfSSL_SetIORecv(ctx, CsRecv);
  48233. /* No where to send to - dummy sender. */
  48234. wolfSSL_SetIOSend(ctx, CsSend);
  48235. /* Test cipher suite list with many copies of a cipher suite. */
  48236. AssertNotNull(ssl = wolfSSL_new(ctx));
  48237. msg.buffer = clientHello;
  48238. msg.length = (unsigned int)sizeof(clientHello);
  48239. wolfSSL_SetIOReadCtx(ssl, &msg);
  48240. /* Force server to have as many occurrences of same cipher suite as
  48241. * possible. */
  48242. {
  48243. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48244. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48245. for (i = 0; i < suites->suiteSz; i += 2) {
  48246. suites->suites[i + 0] = TLS13_BYTE;
  48247. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48248. }
  48249. }
  48250. /* Test multiple occurrences of same cipher suite. */
  48251. wolfSSL_accept_TLSv13(ssl);
  48252. wolfSSL_free(ssl);
  48253. /* Set client order opposite to server order:
  48254. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48255. clientHello[csOff + 0] = TLS13_BYTE;
  48256. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48257. clientHello[csOff + 2] = TLS13_BYTE;
  48258. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48259. /* Test server order negotiation. */
  48260. AssertNotNull(ssl = wolfSSL_new(ctx));
  48261. msg.buffer = clientHello;
  48262. msg.length = (unsigned int)sizeof(clientHello);
  48263. wolfSSL_SetIOReadCtx(ssl, &msg);
  48264. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48265. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48266. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48267. wolfSSL_accept_TLSv13(ssl);
  48268. /* Check refined order - server order. */
  48269. AssertIntEQ(ssl->suites->suiteSz, 4);
  48270. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48271. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48272. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48273. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48274. wolfSSL_free(ssl);
  48275. /* Test client order negotiation. */
  48276. AssertNotNull(ssl = wolfSSL_new(ctx));
  48277. msg.buffer = clientHello;
  48278. msg.length = (unsigned int)sizeof(clientHello);
  48279. wolfSSL_SetIOReadCtx(ssl, &msg);
  48280. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48281. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48282. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  48283. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  48284. wolfSSL_accept_TLSv13(ssl);
  48285. /* Check refined order - client order. */
  48286. AssertIntEQ(ssl->suites->suiteSz, 4);
  48287. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48288. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48289. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48290. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48291. wolfSSL_free(ssl);
  48292. /* Check duplicate detection is working. */
  48293. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  48294. AssertIntEQ(ctx->suites->suiteSz, 4);
  48295. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48296. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48297. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48298. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48299. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48300. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  48301. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  48302. AssertIntEQ(ctx->suites->suiteSz, 4);
  48303. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48304. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48305. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48306. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48307. #endif
  48308. wolfSSL_CTX_free(ctx);
  48309. res = TEST_RES_CHECK(1);
  48310. #endif
  48311. return res;
  48312. }
  48313. #endif
  48314. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  48315. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48316. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48317. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48318. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  48319. const unsigned char* priv, unsigned int privSz,
  48320. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  48321. unsigned char* out, unsigned int* outlen,
  48322. void* ctx)
  48323. {
  48324. int result;
  48325. /* Test fail when context associated with WOLFSSL is NULL */
  48326. if (ctx == NULL) {
  48327. return -1;
  48328. }
  48329. (void)ssl;
  48330. /* return 0 on success */
  48331. PRIVATE_KEY_UNLOCK();
  48332. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  48333. PRIVATE_KEY_LOCK();
  48334. return result;
  48335. }
  48336. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  48337. EXPECT_DECLS;
  48338. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  48339. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  48340. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  48341. WOLFSSL_SUCCESS);
  48342. #endif
  48343. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  48344. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  48345. WOLFSSL_SUCCESS);
  48346. #endif
  48347. return EXPECT_RESULT();
  48348. }
  48349. static int test_dh_ssl_setup(WOLFSSL* ssl)
  48350. {
  48351. EXPECT_DECLS;
  48352. static int dh_test_ctx = 1;
  48353. int ret;
  48354. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  48355. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  48356. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48357. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48358. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48359. }
  48360. return EXPECT_RESULT();
  48361. }
  48362. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  48363. {
  48364. EXPECT_DECLS;
  48365. int ret;
  48366. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  48367. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  48368. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48369. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48370. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48371. }
  48372. return EXPECT_RESULT();
  48373. }
  48374. #endif
  48375. static int test_DhCallbacks(void)
  48376. {
  48377. int res = TEST_SKIPPED;
  48378. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48379. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48380. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48381. EXPECT_DECLS;
  48382. WOLFSSL_CTX *ctx = NULL;
  48383. WOLFSSL *ssl = NULL;
  48384. int test;
  48385. test_ssl_cbf func_cb_client;
  48386. test_ssl_cbf func_cb_server;
  48387. /* Test that DH callback APIs work. */
  48388. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48389. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  48390. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  48391. /* load client ca cert */
  48392. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  48393. WOLFSSL_SUCCESS);
  48394. /* test with NULL arguments */
  48395. wolfSSL_SetDhAgreeCtx(NULL, &test);
  48396. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  48397. /* test success case */
  48398. test = 1;
  48399. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48400. wolfSSL_SetDhAgreeCtx(ssl, &test);
  48401. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  48402. wolfSSL_free(ssl);
  48403. wolfSSL_CTX_free(ctx);
  48404. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48405. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48406. /* set callbacks to use DH functions */
  48407. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48408. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  48409. func_cb_client.method = wolfTLSv1_2_client_method;
  48410. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48411. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  48412. func_cb_server.method = wolfTLSv1_2_server_method;
  48413. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48414. &func_cb_server, NULL), TEST_SUCCESS);
  48415. /* Test fail */
  48416. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48417. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48418. /* set callbacks to use DH functions */
  48419. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48420. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  48421. func_cb_client.method = wolfTLSv1_2_client_method;
  48422. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48423. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  48424. func_cb_server.method = wolfTLSv1_2_server_method;
  48425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48426. &func_cb_server, NULL), TEST_FAIL);
  48427. res = EXPECT_RESULT();
  48428. #endif
  48429. return res;
  48430. }
  48431. #endif /* HAVE_PK_CALLBACKS */
  48432. #ifdef HAVE_HASHDRBG
  48433. #ifdef TEST_RESEED_INTERVAL
  48434. static int test_wc_RNG_GenerateBlock_Reseed(void)
  48435. {
  48436. int i, ret;
  48437. WC_RNG rng;
  48438. byte key[32];
  48439. ret = wc_InitRng(&rng);
  48440. if (ret == 0) {
  48441. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  48442. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  48443. if (ret != 0) {
  48444. break;
  48445. }
  48446. }
  48447. }
  48448. wc_FreeRng(&rng);
  48449. return TEST_RES_CHECK(ret == 0);
  48450. }
  48451. #endif /* TEST_RESEED_INTERVAL */
  48452. static int test_wc_RNG_GenerateBlock(void)
  48453. {
  48454. int i, ret;
  48455. WC_RNG rng;
  48456. byte key[32];
  48457. ret = wc_InitRng(&rng);
  48458. if (ret == 0) {
  48459. for (i = 0; i < 10; i++) {
  48460. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  48461. if (ret != 0) {
  48462. break;
  48463. }
  48464. }
  48465. }
  48466. wc_FreeRng(&rng);
  48467. (void)rng; /* for WC_NO_RNG case */
  48468. (void)key;
  48469. return TEST_RES_CHECK(ret == 0);
  48470. }
  48471. #endif
  48472. /*
  48473. * Testing get_rand_digit
  48474. */
  48475. static int test_get_rand_digit(void)
  48476. {
  48477. int res = TEST_SKIPPED;
  48478. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  48479. int ret = 0;
  48480. WC_RNG rng;
  48481. mp_digit d;
  48482. ret = wc_InitRng(&rng);
  48483. if (ret == 0) {
  48484. ret = get_rand_digit(&rng, &d);
  48485. }
  48486. if (ret == 0) {
  48487. ret = get_rand_digit(NULL, NULL);
  48488. if (ret == BAD_FUNC_ARG) {
  48489. ret = 0;
  48490. }
  48491. }
  48492. if (ret == 0) {
  48493. ret = get_rand_digit(NULL, &d);
  48494. if (ret == BAD_FUNC_ARG) {
  48495. ret = 0;
  48496. }
  48497. }
  48498. if (ret == 0) {
  48499. ret = get_rand_digit(&rng, NULL);
  48500. if (ret == BAD_FUNC_ARG) {
  48501. ret = 0;
  48502. }
  48503. }
  48504. if (ret == 0) {
  48505. ret = wc_FreeRng(&rng);
  48506. }
  48507. res = TEST_RES_CHECK(ret == 0);
  48508. #endif
  48509. return res;
  48510. }/* End test_get_rand_digit*/
  48511. /*
  48512. * Testing get_digit_count
  48513. */
  48514. static int test_get_digit_count(void)
  48515. {
  48516. int res = TEST_SKIPPED;
  48517. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  48518. int ret = 0;
  48519. mp_int a;
  48520. if (mp_init(&a) != MP_OKAY) {
  48521. ret = -1;
  48522. }
  48523. if (ret == 0) {
  48524. ret = get_digit_count(NULL);
  48525. }
  48526. if (ret == 0) {
  48527. ret = get_digit_count(&a);
  48528. }
  48529. mp_clear(&a);
  48530. res = TEST_RES_CHECK(ret == 0);
  48531. #endif
  48532. return res;
  48533. }/* End test_get_digit_count*/
  48534. /*
  48535. * Testing mp_cond_copy
  48536. */
  48537. static int test_mp_cond_copy(void)
  48538. {
  48539. int res = TEST_SKIPPED;
  48540. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  48541. defined(WOLFSSL_PUBLIC_MP)
  48542. int ret = 0;
  48543. mp_int a;
  48544. mp_int b;
  48545. int copy = 0;
  48546. if (mp_init(&a) != MP_OKAY) {
  48547. ret = -1;
  48548. }
  48549. if (ret == 0) {
  48550. if (mp_init(&b) != MP_OKAY) {
  48551. ret = -1;
  48552. }
  48553. }
  48554. if (ret == 0) {
  48555. ret = mp_cond_copy(NULL, copy, NULL);
  48556. if (ret == BAD_FUNC_ARG) {
  48557. ret = 0;
  48558. }
  48559. }
  48560. if (ret == 0) {
  48561. ret = mp_cond_copy(NULL, copy, &b);
  48562. if (ret == BAD_FUNC_ARG) {
  48563. ret = 0;
  48564. }
  48565. }
  48566. if (ret == 0) {
  48567. ret = mp_cond_copy(&a, copy, NULL);
  48568. if (ret == BAD_FUNC_ARG) {
  48569. ret = 0;
  48570. }
  48571. }
  48572. if (ret == 0) {
  48573. ret = mp_cond_copy(&a, copy, &b);
  48574. }
  48575. mp_clear(&a);
  48576. mp_clear(&b);
  48577. res = TEST_RES_CHECK(ret == 0);
  48578. #endif
  48579. return res;
  48580. }/* End test_mp_cond_copy*/
  48581. /*
  48582. * Testing mp_rand
  48583. */
  48584. static int test_mp_rand(void)
  48585. {
  48586. int res = TEST_SKIPPED;
  48587. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  48588. int ret = 0;
  48589. mp_int a;
  48590. int digits = 1;
  48591. WC_RNG rng;
  48592. if (mp_init(&a) != MP_OKAY) {
  48593. ret = -1;
  48594. }
  48595. if (ret == 0) {
  48596. ret = wc_InitRng(&rng);
  48597. }
  48598. if (ret == 0) {
  48599. ret = mp_rand(&a, digits, NULL);
  48600. if (ret == MISSING_RNG_E) {
  48601. ret = 0;
  48602. }
  48603. }
  48604. if (ret == 0) {
  48605. ret = mp_rand(NULL, digits, &rng);
  48606. if (ret == BAD_FUNC_ARG) {
  48607. ret = 0;
  48608. }
  48609. }
  48610. if (ret == 0) {
  48611. ret = mp_rand(&a, 0, &rng);
  48612. if (ret == BAD_FUNC_ARG) {
  48613. ret = 0;
  48614. }
  48615. }
  48616. if (ret == 0) {
  48617. ret = mp_rand(&a, digits, &rng);
  48618. }
  48619. mp_clear(&a);
  48620. wc_FreeRng(&rng);
  48621. res = TEST_RES_CHECK(ret == 0);
  48622. #endif
  48623. return res;
  48624. }/* End test_mp_rand*/
  48625. /*
  48626. * Testing get_digit
  48627. */
  48628. static int test_get_digit(void)
  48629. {
  48630. int res = TEST_SKIPPED;
  48631. #if defined(WOLFSSL_PUBLIC_MP)
  48632. int ret = 0;
  48633. mp_int a;
  48634. int n = 0;
  48635. if (mp_init(&a) != MP_OKAY) {
  48636. ret = -1;
  48637. }
  48638. if (ret == 0) {
  48639. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  48640. ret = -1;
  48641. }
  48642. }
  48643. if (ret == 0) {
  48644. if (get_digit(NULL, n) == 0) { /* Should hit this */
  48645. ret = 0;
  48646. }
  48647. }
  48648. if (ret == 0) {
  48649. if (get_digit(&a, n) != 0) { /* Should not hit this */
  48650. ret = -1;
  48651. }
  48652. }
  48653. if (ret == 0) {
  48654. if (get_digit(&a, n) == 0) { /* Should hit this */
  48655. ret = 0;
  48656. }
  48657. }
  48658. mp_clear(&a);
  48659. res = TEST_RES_CHECK(ret == 0);
  48660. #endif
  48661. return res;
  48662. }/* End test_get_digit*/
  48663. /*
  48664. * Testing wc_export_int
  48665. */
  48666. static int test_wc_export_int(void)
  48667. {
  48668. int res = TEST_SKIPPED;
  48669. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  48670. defined(WOLFSSL_PUBLIC_MP)
  48671. int ret = 0;
  48672. mp_int mp;
  48673. byte buf[32];
  48674. word32 keySz = (word32)sizeof(buf);
  48675. word32 len = (word32)sizeof(buf);
  48676. if (mp_init(&mp) != MP_OKAY) {
  48677. ret = -1;
  48678. }
  48679. if (ret == 0) {
  48680. ret = mp_set(&mp, 1234);
  48681. }
  48682. if (ret == 0) {
  48683. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  48684. if (ret == BAD_FUNC_ARG) {
  48685. ret = 0;
  48686. }
  48687. }
  48688. if (ret == 0) {
  48689. len = sizeof(buf)-1;
  48690. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  48691. if (ret == BUFFER_E) {
  48692. ret = 0;
  48693. }
  48694. }
  48695. if (ret == 0) {
  48696. len = sizeof(buf);
  48697. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  48698. }
  48699. if (ret == 0) {
  48700. len = 4; /* test input too small */
  48701. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  48702. if (ret == BUFFER_E) {
  48703. ret = 0;
  48704. }
  48705. }
  48706. if (ret == 0) {
  48707. len = sizeof(buf);
  48708. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  48709. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  48710. if (ret == 0 && len != 5) {
  48711. ret = BAD_FUNC_ARG;
  48712. }
  48713. }
  48714. mp_clear(&mp);
  48715. res = TEST_RES_CHECK(ret == 0);
  48716. #endif
  48717. return res;
  48718. }/* End test_wc_export_int*/
  48719. static int test_wc_InitRngNonce(void)
  48720. {
  48721. int res = TEST_SKIPPED;
  48722. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48723. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  48724. int ret;
  48725. WC_RNG rng;
  48726. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48727. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48728. word32 nonceSz = sizeof(nonce);
  48729. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  48730. wc_FreeRng(&rng);
  48731. res = TEST_RES_CHECK(ret == 0);
  48732. #endif
  48733. return res;
  48734. }/* End test_wc_InitRngNonce*/
  48735. /*
  48736. * Testing wc_InitRngNonce_ex
  48737. */
  48738. static int test_wc_InitRngNonce_ex(void)
  48739. {
  48740. int res = TEST_SKIPPED;
  48741. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48742. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  48743. int ret;
  48744. WC_RNG rng;
  48745. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48746. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48747. word32 nonceSz = sizeof(nonce);
  48748. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  48749. wc_FreeRng(&rng);
  48750. res = TEST_RES_CHECK(ret == 0);
  48751. #endif
  48752. return res;
  48753. }/*End test_wc_InitRngNonce_ex*/
  48754. static int test_wolfSSL_X509_CRL(void)
  48755. {
  48756. int res = TEST_SKIPPED;
  48757. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  48758. X509_CRL *crl;
  48759. char pem[][100] = {
  48760. "./certs/crl/crl.pem",
  48761. "./certs/crl/crl2.pem",
  48762. "./certs/crl/caEccCrl.pem",
  48763. "./certs/crl/eccCliCRL.pem",
  48764. "./certs/crl/eccSrvCRL.pem",
  48765. ""
  48766. };
  48767. #ifndef NO_BIO
  48768. BIO *bio;
  48769. #endif
  48770. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48771. char der[][100] = {
  48772. "./certs/crl/crl.der",
  48773. "./certs/crl/crl2.der",
  48774. ""};
  48775. #endif
  48776. XFILE fp;
  48777. int i;
  48778. for (i = 0; pem[i][0] != '\0'; i++)
  48779. {
  48780. fp = XFOPEN(pem[i], "rb");
  48781. AssertTrue((fp != XBADFILE));
  48782. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  48783. AssertNotNull(crl);
  48784. X509_CRL_free(crl);
  48785. XFCLOSE(fp);
  48786. fp = XFOPEN(pem[i], "rb");
  48787. AssertTrue((fp != XBADFILE));
  48788. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  48789. AssertNotNull(crl);
  48790. X509_CRL_free(crl);
  48791. XFCLOSE(fp);
  48792. }
  48793. #ifndef NO_BIO
  48794. for (i = 0; pem[i][0] != '\0'; i++)
  48795. {
  48796. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  48797. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  48798. X509_CRL_free(crl);
  48799. BIO_free(bio);
  48800. }
  48801. #endif
  48802. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48803. for (i = 0; der[i][0] != '\0'; i++) {
  48804. fp = XFOPEN(der[i], "rb");
  48805. AssertTrue((fp != XBADFILE));
  48806. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  48807. AssertNotNull(crl);
  48808. X509_CRL_free(crl);
  48809. XFCLOSE(fp);
  48810. fp = XFOPEN(der[i], "rb");
  48811. AssertTrue((fp != XBADFILE));
  48812. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  48813. AssertNotNull(crl);
  48814. X509_CRL_free(crl);
  48815. XFCLOSE(fp);
  48816. }
  48817. #endif
  48818. res = TEST_RES_CHECK(1);
  48819. #endif
  48820. return res;
  48821. }
  48822. static int test_wolfSSL_X509_load_crl_file(void)
  48823. {
  48824. int res = TEST_SKIPPED;
  48825. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48826. !defined(NO_RSA) && !defined(NO_BIO)
  48827. EXPECT_DECLS;
  48828. int i;
  48829. char pem[][100] = {
  48830. "./certs/crl/crl.pem",
  48831. "./certs/crl/crl2.pem",
  48832. "./certs/crl/caEccCrl.pem",
  48833. "./certs/crl/eccCliCRL.pem",
  48834. "./certs/crl/eccSrvCRL.pem",
  48835. ""
  48836. };
  48837. char der[][100] = {
  48838. "./certs/crl/crl.der",
  48839. "./certs/crl/crl2.der",
  48840. ""
  48841. };
  48842. WOLFSSL_X509_STORE* store = NULL;
  48843. WOLFSSL_X509_LOOKUP* lookup = NULL;
  48844. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48845. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48846. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48847. X509_FILETYPE_PEM), 1);
  48848. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48849. X509_FILETYPE_PEM), 1);
  48850. if (store) {
  48851. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48852. WOLFSSL_FILETYPE_PEM), 1);
  48853. /* since store hasn't yet known the revoked cert*/
  48854. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48855. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48856. }
  48857. for (i = 0; pem[i][0] != '\0'; i++) {
  48858. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  48859. 1);
  48860. }
  48861. if (store) {
  48862. /* since store knows crl list */
  48863. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48864. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48865. CRL_CERT_REVOKED);
  48866. }
  48867. /* once feeing store */
  48868. X509_STORE_free(store);
  48869. store = NULL;
  48870. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48871. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48872. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48873. X509_FILETYPE_PEM), 1);
  48874. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48875. X509_FILETYPE_PEM), 1);
  48876. if (store) {
  48877. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48878. WOLFSSL_FILETYPE_PEM), 1);
  48879. /* since store hasn't yet known the revoked cert*/
  48880. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48881. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48882. }
  48883. for (i = 0; der[i][0] != '\0'; i++) {
  48884. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  48885. 1);
  48886. }
  48887. if (store) {
  48888. /* since store knows crl list */
  48889. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48890. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48891. CRL_CERT_REVOKED);
  48892. }
  48893. /* test for incorrect parameter */
  48894. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  48895. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  48896. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  48897. X509_STORE_free(store);
  48898. store = NULL;
  48899. res = EXPECT_RESULT();
  48900. #endif
  48901. return res;
  48902. }
  48903. static int test_wolfSSL_d2i_X509_REQ(void)
  48904. {
  48905. int res = TEST_SKIPPED;
  48906. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  48907. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  48908. !defined(WOLFSSL_SP_MATH)
  48909. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  48910. * generated by libest
  48911. * ./certs/csr.attr.der contains sample attributes
  48912. * ./certs/csr.ext.der contains sample extensions */
  48913. const char* csrFile = "./certs/csr.signed.der";
  48914. const char* csrPopFile = "./certs/csr.attr.der";
  48915. const char* csrExtFile = "./certs/csr.ext.der";
  48916. /* ./certs/csr.dsa.pem is generated using
  48917. * openssl req -newkey dsa:certs/dsaparams.pem \
  48918. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  48919. * -outform PEM
  48920. * with the passphrase "wolfSSL"
  48921. */
  48922. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48923. const char* csrDsaFile = "./certs/csr.dsa.pem";
  48924. XFILE f;
  48925. #endif
  48926. BIO* bio = NULL;
  48927. X509* req = NULL;
  48928. EVP_PKEY *pub_key = NULL;
  48929. {
  48930. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  48931. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48932. /*
  48933. * Extract the public key from the CSR
  48934. */
  48935. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48936. /*
  48937. * Verify the signature in the CSR
  48938. */
  48939. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48940. X509_free(req);
  48941. BIO_free(bio);
  48942. EVP_PKEY_free(pub_key);
  48943. }
  48944. {
  48945. #ifdef OPENSSL_ALL
  48946. X509_ATTRIBUTE* attr;
  48947. ASN1_TYPE *at;
  48948. #endif
  48949. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48950. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48951. /*
  48952. * Extract the public key from the CSR
  48953. */
  48954. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48955. /*
  48956. * Verify the signature in the CSR
  48957. */
  48958. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48959. #ifdef OPENSSL_ALL
  48960. /*
  48961. * Obtain the challenge password from the CSR
  48962. */
  48963. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  48964. 1);
  48965. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  48966. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48967. AssertNotNull(at->value.asn1_string);
  48968. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  48969. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48970. #endif
  48971. X509_free(req);
  48972. BIO_free(bio);
  48973. EVP_PKEY_free(pub_key);
  48974. }
  48975. {
  48976. #ifdef OPENSSL_ALL
  48977. X509_ATTRIBUTE* attr;
  48978. ASN1_TYPE *at;
  48979. STACK_OF(X509_EXTENSION) *exts = NULL;
  48980. #endif
  48981. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48982. /* This CSR contains an Extension Request attribute so
  48983. * we test extension parsing in a CSR attribute here. */
  48984. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48985. /*
  48986. * Extract the public key from the CSR
  48987. */
  48988. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48989. /*
  48990. * Verify the signature in the CSR
  48991. */
  48992. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48993. #ifdef OPENSSL_ALL
  48994. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  48995. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48996. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48997. /*
  48998. * Obtain the challenge password from the CSR
  48999. */
  49000. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  49001. 0);
  49002. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  49003. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49004. AssertNotNull(at->value.asn1_string);
  49005. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49006. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49007. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49008. #endif
  49009. X509_free(req);
  49010. BIO_free(bio);
  49011. EVP_PKEY_free(pub_key);
  49012. }
  49013. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49014. {
  49015. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49016. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49017. /*
  49018. * Extract the public key from the CSR
  49019. */
  49020. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  49021. /*
  49022. * Verify the signature in the CSR
  49023. */
  49024. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  49025. X509_free(req);
  49026. BIO_free(bio);
  49027. /* Run the same test, but with a file pointer instead of a BIO.
  49028. * (PEM_read_X509_REQ)*/
  49029. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49030. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49031. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  49032. X509_free(req);
  49033. EVP_PKEY_free(pub_key);
  49034. }
  49035. res = TEST_RES_CHECK(1);
  49036. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49037. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49038. return res;
  49039. }
  49040. static int test_wolfSSL_PEM_read_X509(void)
  49041. {
  49042. int res = TEST_SKIPPED;
  49043. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49044. !defined(NO_RSA)
  49045. X509 *x509 = NULL;
  49046. XFILE fp;
  49047. fp = XFOPEN(svrCertFile, "rb");
  49048. AssertTrue((fp != XBADFILE));
  49049. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49050. X509_free(x509);
  49051. XFCLOSE(fp);
  49052. res = TEST_RES_CHECK(1);
  49053. #endif
  49054. return res;
  49055. }
  49056. static int test_wolfSSL_PEM_read(void)
  49057. {
  49058. int res = TEST_SKIPPED;
  49059. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49060. const char* filename = "./certs/server-keyEnc.pem";
  49061. XFILE fp;
  49062. char* name = NULL;
  49063. char* header = NULL;
  49064. byte* data = NULL;
  49065. long len;
  49066. EVP_CIPHER_INFO cipher;
  49067. WOLFSSL_BIO* bio;
  49068. byte* fileData;
  49069. size_t fileDataSz;
  49070. byte* out;
  49071. fp = XFOPEN(filename, "rb");
  49072. AssertTrue((fp != XBADFILE));
  49073. /* Fail cases. */
  49074. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49075. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49076. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49077. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49078. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49079. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49080. AssertIntGT(XSTRLEN(header), 0);
  49081. AssertIntGT(len, 0);
  49082. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49083. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  49084. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49085. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49086. DYNAMIC_TYPE_TMP_BUFFER));
  49087. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49088. XFCLOSE(fp);
  49089. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49090. /* Fail cases. */
  49091. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49092. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49093. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49094. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49095. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49096. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49097. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49098. /* Fail cases. */
  49099. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49100. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49101. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49102. #ifndef NO_DES3
  49103. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49104. #endif
  49105. /* Fail cases. */
  49106. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49107. (void*)"yassl123"), WOLFSSL_FAILURE);
  49108. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49109. (void*)"yassl123"), WOLFSSL_FAILURE);
  49110. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49111. (void*)"yassl123"), WOLFSSL_FAILURE);
  49112. #if !defined(NO_DES3) && !defined(NO_MD5)
  49113. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49114. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49115. #endif
  49116. BIO_free(bio);
  49117. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49118. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49119. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49120. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49121. name = NULL;
  49122. header = NULL;
  49123. data = NULL;
  49124. fp = XFOPEN(svrKeyFile, "rb");
  49125. AssertTrue((fp != XBADFILE));
  49126. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49127. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49128. AssertIntEQ(XSTRLEN(header), 0);
  49129. AssertIntGT(len, 0);
  49130. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49131. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  49132. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49133. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49134. DYNAMIC_TYPE_TMP_BUFFER));
  49135. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49136. XFCLOSE(fp);
  49137. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49138. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49139. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49140. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49141. BIO_free(bio);
  49142. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49143. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49144. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49145. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49146. res = TEST_RES_CHECK(1);
  49147. #endif
  49148. return res;
  49149. }
  49150. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49151. {
  49152. int res = TEST_SKIPPED;
  49153. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49154. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49155. const byte iv[12] = { 0 };
  49156. const byte key[16] = { 0 };
  49157. const byte cleartext[16] = { 0 };
  49158. const byte aad[] = {
  49159. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49161. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49162. 0x4f
  49163. };
  49164. byte out1Part[16];
  49165. byte outTag1Part[16];
  49166. byte out2Part[16];
  49167. byte outTag2Part[16];
  49168. byte decryptBuf[16];
  49169. int len;
  49170. int tlen;
  49171. EVP_CIPHER_CTX* ctx = NULL;
  49172. /* ENCRYPT */
  49173. /* Send AAD and data in 1 part */
  49174. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  49175. tlen = 0;
  49176. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49177. 1);
  49178. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49179. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49180. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49181. sizeof(cleartext)), 1);
  49182. tlen += len;
  49183. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49184. tlen += len;
  49185. AssertIntEQ(tlen, sizeof(cleartext));
  49186. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49187. outTag1Part), 1);
  49188. EVP_CIPHER_CTX_free(ctx);
  49189. /* DECRYPT */
  49190. /* Send AAD and data in 1 part */
  49191. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  49192. tlen = 0;
  49193. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49194. 1);
  49195. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49196. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49197. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49198. sizeof(cleartext)), 1);
  49199. tlen += len;
  49200. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49201. outTag1Part), 1);
  49202. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49203. tlen += len;
  49204. AssertIntEQ(tlen, sizeof(cleartext));
  49205. EVP_CIPHER_CTX_free(ctx);
  49206. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49207. /* ENCRYPT */
  49208. /* Send AAD and data in 2 parts */
  49209. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  49210. tlen = 0;
  49211. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49212. 1);
  49213. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49214. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49215. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49216. 1);
  49217. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49218. tlen += len;
  49219. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49220. sizeof(cleartext) - 1), 1);
  49221. tlen += len;
  49222. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49223. tlen += len;
  49224. AssertIntEQ(tlen, sizeof(cleartext));
  49225. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49226. outTag2Part), 1);
  49227. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49228. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49229. EVP_CIPHER_CTX_free(ctx);
  49230. /* DECRYPT */
  49231. /* Send AAD and data in 2 parts */
  49232. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  49233. tlen = 0;
  49234. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49235. 1);
  49236. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49237. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49238. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49239. 1);
  49240. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49241. tlen += len;
  49242. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49243. sizeof(cleartext) - 1), 1);
  49244. tlen += len;
  49245. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49246. outTag1Part), 1);
  49247. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49248. tlen += len;
  49249. AssertIntEQ(tlen, sizeof(cleartext));
  49250. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49251. /* Test AAD re-use */
  49252. EVP_CIPHER_CTX_free(ctx);
  49253. res = TEST_RES_CHECK(1);
  49254. #endif
  49255. return res;
  49256. }
  49257. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49258. {
  49259. int res = TEST_SKIPPED;
  49260. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49261. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49262. /* Zero length plain text */
  49263. byte key[] = {
  49264. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49265. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49266. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49267. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49268. }; /* align */
  49269. byte iv[] = {
  49270. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49271. }; /* align */
  49272. byte plaintxt[1];
  49273. int ivSz = 12;
  49274. int plaintxtSz = 0;
  49275. unsigned char tag[16];
  49276. unsigned char tag_kat[] =
  49277. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49278. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  49279. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49280. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49281. int ciphertxtSz = 0;
  49282. int decryptedtxtSz = 0;
  49283. int len = 0;
  49284. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49285. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49286. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49287. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49288. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49289. plaintxtSz));
  49290. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49291. ciphertxtSz += len;
  49292. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49293. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49294. AssertIntEQ(0, ciphertxtSz);
  49295. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49296. EVP_CIPHER_CTX_init(de);
  49297. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  49298. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49299. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49300. decryptedtxtSz = len;
  49301. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49302. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49303. decryptedtxtSz += len;
  49304. AssertIntEQ(0, decryptedtxtSz);
  49305. EVP_CIPHER_CTX_free(en);
  49306. EVP_CIPHER_CTX_free(de);
  49307. res = TEST_RES_CHECK(1);
  49308. #endif
  49309. return res;
  49310. }
  49311. static int test_wolfssl_EVP_aes_gcm(void)
  49312. {
  49313. int res = TEST_SKIPPED;
  49314. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49315. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49316. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49317. byte *key = (byte*)"01234567890123456789012345678901";
  49318. /* A 128 bit IV */
  49319. byte *iv = (byte*)"0123456789012345";
  49320. int ivSz = AES_BLOCK_SIZE;
  49321. /* Message to be encrypted */
  49322. byte *plaintxt = (byte*)"for things to change you have to change";
  49323. /* Additional non-confidential data */
  49324. byte *aad = (byte*)"Don't spend major time on minor things.";
  49325. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49326. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49327. int aadSz = (int)XSTRLEN((char*)aad);
  49328. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49329. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49330. int ciphertxtSz = 0;
  49331. int decryptedtxtSz = 0;
  49332. int len = 0;
  49333. int i = 0;
  49334. EVP_CIPHER_CTX en[2];
  49335. EVP_CIPHER_CTX de[2];
  49336. for (i = 0; i < 2; i++) {
  49337. EVP_CIPHER_CTX_init(&en[i]);
  49338. if (i == 0) {
  49339. /* Default uses 96-bits IV length */
  49340. #ifdef WOLFSSL_AES_128
  49341. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  49342. #elif defined(WOLFSSL_AES_192)
  49343. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  49344. #elif defined(WOLFSSL_AES_256)
  49345. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  49346. #endif
  49347. }
  49348. else {
  49349. #ifdef WOLFSSL_AES_128
  49350. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  49351. #elif defined(WOLFSSL_AES_192)
  49352. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  49353. #elif defined(WOLFSSL_AES_256)
  49354. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  49355. #endif
  49356. /* non-default must to set the IV length first */
  49357. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49358. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49359. }
  49360. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49361. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  49362. ciphertxtSz = len;
  49363. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49364. ciphertxtSz += len;
  49365. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  49366. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49367. EVP_CIPHER_CTX_init(&de[i]);
  49368. if (i == 0) {
  49369. /* Default uses 96-bits IV length */
  49370. #ifdef WOLFSSL_AES_128
  49371. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  49372. #elif defined(WOLFSSL_AES_192)
  49373. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  49374. #elif defined(WOLFSSL_AES_256)
  49375. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  49376. #endif
  49377. }
  49378. else {
  49379. #ifdef WOLFSSL_AES_128
  49380. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  49381. #elif defined(WOLFSSL_AES_192)
  49382. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  49383. #elif defined(WOLFSSL_AES_256)
  49384. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  49385. #endif
  49386. /* non-default must to set the IV length first */
  49387. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49388. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49389. }
  49390. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49391. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49392. decryptedtxtSz = len;
  49393. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  49394. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49395. decryptedtxtSz += len;
  49396. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  49397. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49398. /* modify tag*/
  49399. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49400. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49401. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  49402. /* fail due to wrong tag */
  49403. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49404. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49405. AssertIntEQ(0, len);
  49406. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49407. }
  49408. res = TEST_RES_CHECK(1);
  49409. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  49410. return res;
  49411. }
  49412. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  49413. {
  49414. int res = TEST_SKIPPED;
  49415. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49416. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49417. /* Zero length plain text */
  49418. byte key[] = {
  49419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49421. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49422. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49423. }; /* align */
  49424. byte iv[] = {
  49425. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49426. }; /* align */
  49427. byte plaintxt[1];
  49428. int ivSz = 12;
  49429. int plaintxtSz = 0;
  49430. unsigned char tag[16];
  49431. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49432. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49433. int ciphertxtSz = 0;
  49434. int decryptedtxtSz = 0;
  49435. int len = 0;
  49436. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49437. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49438. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  49439. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49440. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49441. plaintxtSz));
  49442. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49443. ciphertxtSz += len;
  49444. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  49445. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49446. AssertIntEQ(0, ciphertxtSz);
  49447. EVP_CIPHER_CTX_init(de);
  49448. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  49449. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49450. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49451. decryptedtxtSz = len;
  49452. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  49453. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49454. decryptedtxtSz += len;
  49455. AssertIntEQ(0, decryptedtxtSz);
  49456. EVP_CIPHER_CTX_free(en);
  49457. EVP_CIPHER_CTX_free(de);
  49458. res = TEST_RES_CHECK(1);
  49459. #endif
  49460. return res;
  49461. }
  49462. static int test_wolfssl_EVP_aes_ccm(void)
  49463. {
  49464. int res = TEST_SKIPPED;
  49465. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49466. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49467. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49468. byte *key = (byte*)"01234567890123456789012345678901";
  49469. /* A 128 bit IV */
  49470. byte *iv = (byte*)"0123456789012";
  49471. int ivSz = (int)XSTRLEN((char*)iv);
  49472. /* Message to be encrypted */
  49473. byte *plaintxt = (byte*)"for things to change you have to change";
  49474. /* Additional non-confidential data */
  49475. byte *aad = (byte*)"Don't spend major time on minor things.";
  49476. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49477. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49478. int aadSz = (int)XSTRLEN((char*)aad);
  49479. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49480. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49481. int ciphertxtSz = 0;
  49482. int decryptedtxtSz = 0;
  49483. int len = 0;
  49484. int i = 0;
  49485. EVP_CIPHER_CTX en[2];
  49486. EVP_CIPHER_CTX de[2];
  49487. for (i = 0; i < 2; i++) {
  49488. EVP_CIPHER_CTX_init(&en[i]);
  49489. if (i == 0) {
  49490. /* Default uses 96-bits IV length */
  49491. #ifdef WOLFSSL_AES_128
  49492. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49493. EVP_aes_128_ccm(), NULL, key, iv));
  49494. #elif defined(WOLFSSL_AES_192)
  49495. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49496. EVP_aes_192_ccm(), NULL, key, iv));
  49497. #elif defined(WOLFSSL_AES_256)
  49498. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49499. EVP_aes_256_ccm(), NULL, key, iv));
  49500. #endif
  49501. }
  49502. else {
  49503. #ifdef WOLFSSL_AES_128
  49504. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49505. EVP_aes_128_ccm(), NULL, NULL, NULL));
  49506. #elif defined(WOLFSSL_AES_192)
  49507. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49508. EVP_aes_192_ccm(), NULL, NULL, NULL));
  49509. #elif defined(WOLFSSL_AES_256)
  49510. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49511. EVP_aes_256_ccm(), NULL, NULL, NULL));
  49512. #endif
  49513. /* non-default must to set the IV length first */
  49514. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  49515. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49516. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  49517. NULL, NULL, key, iv));
  49518. }
  49519. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49520. AssertIntEQ(1, EVP_EncryptUpdate(&en[i],
  49521. ciphertxt, &len, plaintxt, plaintxtSz));
  49522. ciphertxtSz = len;
  49523. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49524. ciphertxtSz += len;
  49525. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  49526. EVP_CTRL_CCM_GET_TAG, AES_BLOCK_SIZE, tag));
  49527. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49528. EVP_CIPHER_CTX_init(&de[i]);
  49529. if (i == 0) {
  49530. /* Default uses 96-bits IV length */
  49531. #ifdef WOLFSSL_AES_128
  49532. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49533. EVP_aes_128_ccm(), NULL, key, iv));
  49534. #elif defined(WOLFSSL_AES_192)
  49535. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49536. EVP_aes_192_ccm(), NULL, key, iv));
  49537. #elif defined(WOLFSSL_AES_256)
  49538. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49539. EVP_aes_256_ccm(), NULL, key, iv));
  49540. #endif
  49541. }
  49542. else {
  49543. #ifdef WOLFSSL_AES_128
  49544. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49545. EVP_aes_128_ccm(), NULL, NULL, NULL));
  49546. #elif defined(WOLFSSL_AES_192)
  49547. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49548. EVP_aes_192_ccm(), NULL, NULL, NULL));
  49549. #elif defined(WOLFSSL_AES_256)
  49550. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  49551. EVP_aes_256_ccm(), NULL, NULL, NULL));
  49552. #endif
  49553. /* non-default must to set the IV length first */
  49554. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  49555. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49556. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49557. }
  49558. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49559. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  49560. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49561. decryptedtxtSz = len;
  49562. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  49563. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  49564. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i],
  49565. decryptedtxt, &len));
  49566. decryptedtxtSz += len;
  49567. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  49568. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49569. /* modify tag*/
  49570. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49571. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49572. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  49573. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  49574. /* fail due to wrong tag */
  49575. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  49576. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49577. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49578. AssertIntEQ(0, len);
  49579. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49580. }
  49581. res = TEST_RES_CHECK(1);
  49582. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  49583. return res;
  49584. }
  49585. static int test_wolfssl_EVP_chacha20_poly1305(void)
  49586. {
  49587. int res = TEST_SKIPPED;
  49588. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  49589. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  49590. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  49591. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49592. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  49593. byte cipherText[sizeof(plainText)];
  49594. byte decryptedText[sizeof(plainText)];
  49595. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  49596. EVP_CIPHER_CTX* ctx;
  49597. int outSz;
  49598. /* Encrypt. */
  49599. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49600. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49601. NULL), WOLFSSL_SUCCESS);
  49602. /* Invalid IV length. */
  49603. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49604. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  49605. /* Valid IV length. */
  49606. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49607. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49608. /* Invalid tag length. */
  49609. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49610. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  49611. /* Valid tag length. */
  49612. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49613. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  49614. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49615. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49616. WOLFSSL_SUCCESS);
  49617. AssertIntEQ(outSz, sizeof(aad));
  49618. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49619. sizeof(plainText)), WOLFSSL_SUCCESS);
  49620. AssertIntEQ(outSz, sizeof(plainText));
  49621. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49622. AssertIntEQ(outSz, 0);
  49623. /* Invalid tag length. */
  49624. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49625. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  49626. /* Valid tag length. */
  49627. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49628. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49629. EVP_CIPHER_CTX_free(ctx);
  49630. /* Decrypt. */
  49631. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49632. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49633. NULL), WOLFSSL_SUCCESS);
  49634. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49635. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49636. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49637. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49638. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49639. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49640. WOLFSSL_SUCCESS);
  49641. AssertIntEQ(outSz, sizeof(aad));
  49642. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49643. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49644. AssertIntEQ(outSz, sizeof(cipherText));
  49645. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49646. WOLFSSL_SUCCESS);
  49647. AssertIntEQ(outSz, 0);
  49648. EVP_CIPHER_CTX_free(ctx);
  49649. /* Test partial Inits. CipherInit() allow setting of key and iv
  49650. * in separate calls. */
  49651. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49652. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  49653. key, NULL, 1), WOLFSSL_SUCCESS);
  49654. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49655. WOLFSSL_SUCCESS);
  49656. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  49657. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  49658. AssertIntEQ(outSz, sizeof(aad));
  49659. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49660. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49661. AssertIntEQ(outSz, sizeof(cipherText));
  49662. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49663. WOLFSSL_SUCCESS);
  49664. AssertIntEQ(outSz, 0);
  49665. EVP_CIPHER_CTX_free(ctx);
  49666. res = TEST_RES_CHECK(1);
  49667. #endif
  49668. return res;
  49669. }
  49670. static int test_wolfssl_EVP_chacha20(void)
  49671. {
  49672. int res = TEST_SKIPPED;
  49673. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  49674. byte key[CHACHA_MAX_KEY_SZ];
  49675. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  49676. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49677. byte cipherText[sizeof(plainText)];
  49678. byte decryptedText[sizeof(plainText)];
  49679. EVP_CIPHER_CTX* ctx;
  49680. int outSz;
  49681. /* Encrypt. */
  49682. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49683. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49684. NULL), WOLFSSL_SUCCESS);
  49685. /* Any tag length must fail - not an AEAD cipher. */
  49686. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49687. 16, NULL), WOLFSSL_FAILURE);
  49688. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49689. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49690. sizeof(plainText)), WOLFSSL_SUCCESS);
  49691. AssertIntEQ(outSz, sizeof(plainText));
  49692. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49693. AssertIntEQ(outSz, 0);
  49694. EVP_CIPHER_CTX_free(ctx);
  49695. /* Decrypt. */
  49696. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49697. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49698. NULL), WOLFSSL_SUCCESS);
  49699. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49700. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49701. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49702. AssertIntEQ(outSz, sizeof(cipherText));
  49703. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49704. WOLFSSL_SUCCESS);
  49705. AssertIntEQ(outSz, 0);
  49706. EVP_CIPHER_CTX_free(ctx);
  49707. /* Test partial Inits. CipherInit() allow setting of key and iv
  49708. * in separate calls. */
  49709. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  49710. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  49711. key, NULL, 1), WOLFSSL_SUCCESS);
  49712. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49713. WOLFSSL_SUCCESS);
  49714. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49715. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49716. AssertIntEQ(outSz, sizeof(cipherText));
  49717. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49718. WOLFSSL_SUCCESS);
  49719. AssertIntEQ(outSz, 0);
  49720. EVP_CIPHER_CTX_free(ctx);
  49721. res = TEST_RES_CHECK(1);
  49722. #endif
  49723. return res;
  49724. }
  49725. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  49726. {
  49727. int res = TEST_SKIPPED;
  49728. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  49729. EVP_PKEY_CTX* ctx;
  49730. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  49731. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  49732. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  49733. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  49734. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  49735. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  49736. byte outKey[34];
  49737. size_t outKeySz = sizeof(outKey);
  49738. /* These expected outputs were gathered by running the same test below using
  49739. * OpenSSL. */
  49740. const byte extractAndExpand[] = {
  49741. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  49742. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  49743. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  49744. };
  49745. const byte extractOnly[] = {
  49746. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  49747. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  49748. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  49749. };
  49750. const byte expandOnly[] = {
  49751. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  49752. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  49753. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  49754. };
  49755. const byte extractAndExpandAddInfo[] = {
  49756. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  49757. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  49758. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  49759. };
  49760. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  49761. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  49762. /* NULL ctx. */
  49763. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  49764. /* NULL md. */
  49765. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  49766. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  49767. /* NULL ctx. */
  49768. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  49769. WOLFSSL_FAILURE);
  49770. /* NULL salt is ok. */
  49771. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  49772. WOLFSSL_SUCCESS);
  49773. /* Salt length <= 0. */
  49774. /* Length 0 salt is ok. */
  49775. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  49776. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  49777. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  49778. WOLFSSL_SUCCESS);
  49779. /* NULL ctx. */
  49780. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  49781. WOLFSSL_FAILURE);
  49782. /* NULL key. */
  49783. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  49784. WOLFSSL_FAILURE);
  49785. /* Key length <= 0 */
  49786. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  49787. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  49788. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  49789. WOLFSSL_SUCCESS);
  49790. /* NULL ctx. */
  49791. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  49792. WOLFSSL_FAILURE);
  49793. /* NULL info is ok. */
  49794. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  49795. WOLFSSL_SUCCESS);
  49796. /* Info length <= 0 */
  49797. /* Length 0 info is ok. */
  49798. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  49799. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  49800. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  49801. WOLFSSL_SUCCESS);
  49802. /* NULL ctx. */
  49803. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49804. WOLFSSL_FAILURE);
  49805. /* Extract and expand (default). */
  49806. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49807. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  49808. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  49809. /* Extract only. */
  49810. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49811. WOLFSSL_SUCCESS);
  49812. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49813. AssertIntEQ(outKeySz, sizeof(extractOnly));
  49814. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  49815. outKeySz = sizeof(outKey);
  49816. /* Expand only. */
  49817. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  49818. WOLFSSL_SUCCESS);
  49819. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49820. AssertIntEQ(outKeySz, sizeof(expandOnly));
  49821. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  49822. outKeySz = sizeof(outKey);
  49823. /* Extract and expand with appended additional info. */
  49824. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  49825. WOLFSSL_SUCCESS);
  49826. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  49827. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  49828. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49829. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  49830. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  49831. EVP_PKEY_CTX_free(ctx);
  49832. res = TEST_RES_CHECK(1);
  49833. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  49834. return res;
  49835. }
  49836. #ifndef NO_BIO
  49837. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  49838. {
  49839. int res = TEST_SKIPPED;
  49840. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49841. BIO* bio;
  49842. X509_INFO* info;
  49843. STACK_OF(X509_INFO)* sk;
  49844. char* subject;
  49845. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49846. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49847. AssertNotNull(bio = BIO_new(BIO_s_file()));
  49848. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  49849. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  49850. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  49851. /* using dereference to maintain testing for Apache port*/
  49852. AssertNotNull(info = sk_X509_INFO_pop(sk));
  49853. AssertNotNull(subject =
  49854. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  49855. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  49856. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49857. X509_INFO_free(info);
  49858. AssertNotNull(info = sk_X509_INFO_pop(sk));
  49859. AssertNotNull(subject =
  49860. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  49861. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  49862. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49863. X509_INFO_free(info);
  49864. AssertNull(info = sk_X509_INFO_pop(sk));
  49865. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  49866. BIO_free(bio);
  49867. res = TEST_RES_CHECK(1);
  49868. #endif
  49869. return res;
  49870. }
  49871. #endif /* !NO_BIO */
  49872. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  49873. {
  49874. int res = TEST_SKIPPED;
  49875. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49876. EXPECT_DECLS;
  49877. X509 *x509 = NULL;
  49878. X509_NAME* name = NULL;
  49879. int idx = 0;
  49880. X509_NAME_ENTRY *ne = NULL;
  49881. ASN1_OBJECT *object = NULL;
  49882. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  49883. WOLFSSL_FILETYPE_PEM));
  49884. ExpectNotNull(name = X509_get_subject_name(x509));
  49885. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  49886. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  49887. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  49888. X509_free(x509);
  49889. res = EXPECT_RESULT();
  49890. #endif
  49891. return res;
  49892. }
  49893. static int test_wolfSSL_X509_STORE_get1_certs(void)
  49894. {
  49895. int res = TEST_SKIPPED;
  49896. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  49897. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49898. EXPECT_DECLS;
  49899. X509_STORE_CTX *storeCtx = NULL;
  49900. X509_STORE *store = NULL;
  49901. X509 *caX509 = NULL;
  49902. X509 *svrX509 = NULL;
  49903. X509_NAME *subject = NULL;
  49904. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  49905. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  49906. SSL_FILETYPE_PEM));
  49907. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49908. SSL_FILETYPE_PEM)));
  49909. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  49910. ExpectNotNull(store = X509_STORE_new());
  49911. ExpectNotNull(subject = X509_get_subject_name(caX509));
  49912. /* Errors */
  49913. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  49914. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  49915. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  49916. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  49917. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  49918. SSL_SUCCESS);
  49919. /* Should find the cert */
  49920. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49921. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  49922. sk_X509_pop_free(certs, NULL);
  49923. certs = NULL;
  49924. /* Should not find the cert */
  49925. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  49926. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49927. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  49928. sk_X509_pop_free(certs, NULL);
  49929. certs = NULL;
  49930. X509_STORE_free(store);
  49931. X509_STORE_CTX_free(storeCtx);
  49932. X509_free(svrX509);
  49933. X509_free(caX509);
  49934. res = EXPECT_RESULT();
  49935. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  49936. return res;
  49937. }
  49938. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  49939. * have default (hidden) visibility, and in the absence of visibility, it's
  49940. * benign to mask out the library implementation.
  49941. */
  49942. #define WOLFSSL_MISC_INCLUDED
  49943. #include <wolfcrypt/src/misc.c>
  49944. static int test_ForceZero(void)
  49945. {
  49946. unsigned char data[32];
  49947. unsigned int i, j, len;
  49948. /* Test case with 0 length */
  49949. ForceZero(data, 0);
  49950. /* Test ForceZero */
  49951. for (i = 0; i < sizeof(data); i++) {
  49952. for (len = 1; len < sizeof(data) - i; len++) {
  49953. for (j = 0; j < sizeof(data); j++)
  49954. data[j] = j + 1;
  49955. ForceZero(data + i, len);
  49956. for (j = 0; j < sizeof(data); j++) {
  49957. if (j < i || j >= i + len) {
  49958. if (data[j] == 0x00)
  49959. return -10200;
  49960. }
  49961. else if (data[j] != 0x00)
  49962. return -10201;
  49963. }
  49964. }
  49965. }
  49966. return TEST_RES_CHECK(1);
  49967. }
  49968. #ifndef NO_BIO
  49969. static int test_wolfSSL_X509_print(void)
  49970. {
  49971. int res = TEST_SKIPPED;
  49972. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49973. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49974. X509 *x509;
  49975. BIO *bio;
  49976. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49977. const X509_ALGOR *cert_sig_alg;
  49978. #endif
  49979. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  49980. AssertNotNull(x509);
  49981. /* print to memory */
  49982. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  49983. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49984. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49985. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49986. /* Will print IP address subject alt name. */
  49987. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49988. #elif defined(NO_ASN_TIME)
  49989. /* Will print IP address subject alt name but not Validity. */
  49990. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49991. #else
  49992. /* Will print IP address subject alt name. */
  49993. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49994. #endif
  49995. #elif defined(NO_ASN_TIME)
  49996. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49997. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49998. #else
  49999. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  50000. #endif
  50001. BIO_free(bio);
  50002. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50003. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50004. /* Print signature */
  50005. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  50006. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  50007. #endif
  50008. /* print to stderr */
  50009. #if !defined(NO_WOLFSSL_DIR)
  50010. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50011. #endif
  50012. /* print again */
  50013. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  50014. X509_free(x509);
  50015. BIO_free(bio);
  50016. res = TEST_RES_CHECK(1);
  50017. #endif
  50018. return res;
  50019. }
  50020. static int test_wolfSSL_X509_CRL_print(void)
  50021. {
  50022. int res = TEST_SKIPPED;
  50023. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  50024. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  50025. X509_CRL* crl;
  50026. BIO *bio;
  50027. XFILE fp;
  50028. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  50029. AssertTrue((fp != XBADFILE));
  50030. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50031. NULL, NULL));
  50032. XFCLOSE(fp);
  50033. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  50034. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  50035. X509_CRL_free(crl);
  50036. BIO_free(bio);
  50037. res = TEST_RES_CHECK(1);
  50038. #endif
  50039. return res;
  50040. }
  50041. static int test_wolfSSL_BIO_get_len(void)
  50042. {
  50043. int res = TEST_SKIPPED;
  50044. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50045. BIO *bio = NULL;
  50046. const char txt[] = "Some example text to push to the BIO.";
  50047. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  50048. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50049. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  50050. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  50051. BIO_free(bio);
  50052. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50053. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  50054. BIO_free(bio);
  50055. res = TEST_RES_CHECK(1);
  50056. #endif
  50057. return res;
  50058. }
  50059. #endif /* !NO_BIO */
  50060. static int test_wolfSSL_RSA(void)
  50061. {
  50062. int res = TEST_SKIPPED;
  50063. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  50064. defined(WOLFSSL_KEY_GEN)
  50065. RSA* rsa;
  50066. const BIGNUM *n;
  50067. const BIGNUM *e;
  50068. const BIGNUM *d;
  50069. const BIGNUM *p;
  50070. const BIGNUM *q;
  50071. const BIGNUM *dmp1;
  50072. const BIGNUM *dmq1;
  50073. const BIGNUM *iqmp;
  50074. AssertNotNull(rsa = RSA_new());
  50075. AssertIntEQ(RSA_size(NULL), 0);
  50076. AssertIntEQ(RSA_size(rsa), 0);
  50077. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  50078. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  50079. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  50080. #ifdef WOLFSSL_RSA_KEY_CHECK
  50081. AssertIntEQ(RSA_check_key(rsa), 0);
  50082. #endif
  50083. RSA_free(rsa);
  50084. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50085. AssertIntEQ(RSA_size(rsa), 256);
  50086. #ifdef WOLFSSL_RSA_KEY_CHECK
  50087. AssertIntEQ(RSA_check_key(NULL), 0);
  50088. AssertIntEQ(RSA_check_key(rsa), 1);
  50089. #endif
  50090. /* sanity check */
  50091. AssertIntEQ(RSA_bits(NULL), 0);
  50092. /* key */
  50093. AssertIntEQ(RSA_bits(rsa), 2048);
  50094. RSA_get0_key(rsa, &n, &e, &d);
  50095. AssertPtrEq(rsa->n, n);
  50096. AssertPtrEq(rsa->e, e);
  50097. AssertPtrEq(rsa->d, d);
  50098. AssertNotNull(n = BN_new());
  50099. AssertNotNull(e = BN_new());
  50100. AssertNotNull(d = BN_new());
  50101. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  50102. AssertPtrEq(rsa->n, n);
  50103. AssertPtrEq(rsa->e, e);
  50104. AssertPtrEq(rsa->d, d);
  50105. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  50106. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  50107. /* crt_params */
  50108. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  50109. AssertPtrEq(rsa->dmp1, dmp1);
  50110. AssertPtrEq(rsa->dmq1, dmq1);
  50111. AssertPtrEq(rsa->iqmp, iqmp);
  50112. AssertNotNull(dmp1 = BN_new());
  50113. AssertNotNull(dmq1 = BN_new());
  50114. AssertNotNull(iqmp = BN_new());
  50115. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50116. (BIGNUM*)iqmp), 1);
  50117. AssertPtrEq(rsa->dmp1, dmp1);
  50118. AssertPtrEq(rsa->dmq1, dmq1);
  50119. AssertPtrEq(rsa->iqmp, iqmp);
  50120. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  50121. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50122. (BIGNUM*)iqmp), 0);
  50123. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  50124. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  50125. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  50126. AssertNull(dmp1);
  50127. AssertNull(dmq1);
  50128. AssertNull(iqmp);
  50129. /* factors */
  50130. RSA_get0_factors(rsa, NULL, NULL);
  50131. RSA_get0_factors(rsa, &p, &q);
  50132. AssertPtrEq(rsa->p, p);
  50133. AssertPtrEq(rsa->q, q);
  50134. AssertNotNull(p = BN_new());
  50135. AssertNotNull(q = BN_new());
  50136. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  50137. AssertPtrEq(rsa->p, p);
  50138. AssertPtrEq(rsa->q, q);
  50139. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  50140. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  50141. RSA_get0_factors(NULL, NULL, NULL);
  50142. RSA_get0_factors(NULL, &p, &q);
  50143. AssertNull(p);
  50144. AssertNull(q);
  50145. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  50146. AssertIntEQ(RSA_bits(rsa), 21);
  50147. RSA_free(rsa);
  50148. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  50149. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  50150. AssertIntEQ(RSA_size(rsa), 384);
  50151. AssertIntEQ(RSA_bits(rsa), 3072);
  50152. RSA_free(rsa);
  50153. #endif
  50154. /* remove for now with odd key size until adjusting rsa key size check with
  50155. wc_MakeRsaKey()
  50156. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  50157. RSA_free(rsa);
  50158. */
  50159. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  50160. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  50161. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  50162. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  50163. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  50164. {
  50165. byte buff[FOURK_BUF];
  50166. byte der[FOURK_BUF];
  50167. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  50168. XFILE f;
  50169. int bytes;
  50170. /* test loading encrypted RSA private pem w/o password */
  50171. f = XFOPEN(PrivKeyPemFile, "rb");
  50172. AssertTrue((f != XBADFILE));
  50173. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  50174. XFCLOSE(f);
  50175. XMEMSET(der, 0, sizeof(der));
  50176. /* test that error value is returned with no password */
  50177. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  50178. }
  50179. #endif
  50180. res = TEST_RES_CHECK(1);
  50181. #endif
  50182. return res;
  50183. }
  50184. static int test_wolfSSL_RSA_DER(void)
  50185. {
  50186. int res = TEST_SKIPPED;
  50187. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  50188. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  50189. RSA *rsa;
  50190. int i;
  50191. const unsigned char *buff = NULL;
  50192. unsigned char *newBuff = NULL;
  50193. struct tbl_s
  50194. {
  50195. const unsigned char *der;
  50196. int sz;
  50197. } tbl[] = {
  50198. #ifdef USE_CERT_BUFFERS_1024
  50199. {client_key_der_1024, sizeof_client_key_der_1024},
  50200. {server_key_der_1024, sizeof_server_key_der_1024},
  50201. #endif
  50202. #ifdef USE_CERT_BUFFERS_2048
  50203. {client_key_der_2048, sizeof_client_key_der_2048},
  50204. {server_key_der_2048, sizeof_server_key_der_2048},
  50205. #endif
  50206. {NULL, 0}
  50207. };
  50208. /* Public Key DER */
  50209. struct tbl_s pub[] = {
  50210. #ifdef USE_CERT_BUFFERS_1024
  50211. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  50212. #endif
  50213. #ifdef USE_CERT_BUFFERS_2048
  50214. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  50215. #endif
  50216. {NULL, 0}
  50217. };
  50218. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  50219. buff = pub[0].der;
  50220. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  50221. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  50222. buff = tbl[0].der;
  50223. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  50224. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  50225. rsa = RSA_new();
  50226. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  50227. RSA_free(rsa);
  50228. for (i = 0; tbl[i].der != NULL; i++)
  50229. {
  50230. /* Passing in pointer results in pointer moving. */
  50231. buff = tbl[i].der;
  50232. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  50233. AssertNotNull(rsa);
  50234. RSA_free(rsa);
  50235. }
  50236. for (i = 0; tbl[i].der != NULL; i++)
  50237. {
  50238. /* Passing in pointer results in pointer moving. */
  50239. buff = tbl[i].der;
  50240. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  50241. AssertNotNull(rsa);
  50242. RSA_free(rsa);
  50243. }
  50244. for (i = 0; pub[i].der != NULL; i++)
  50245. {
  50246. buff = pub[i].der;
  50247. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  50248. AssertNotNull(rsa);
  50249. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  50250. newBuff = NULL;
  50251. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  50252. AssertNotNull(newBuff);
  50253. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  50254. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50255. RSA_free(rsa);
  50256. }
  50257. res = TEST_RES_CHECK(1);
  50258. #endif
  50259. return res;
  50260. }
  50261. static int test_wolfSSL_RSA_print(void)
  50262. {
  50263. int res = TEST_SKIPPED;
  50264. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50265. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  50266. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  50267. BIO *bio;
  50268. WOLFSSL_RSA* rsa = NULL;
  50269. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50270. AssertNotNull(rsa = RSA_new());
  50271. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  50272. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  50273. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  50274. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  50275. /* Some very large number of indent spaces. */
  50276. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  50277. /* RSA is empty. */
  50278. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  50279. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  50280. RSA_free(rsa);
  50281. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50282. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  50283. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  50284. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  50285. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  50286. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  50287. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  50288. BIO_free(bio);
  50289. RSA_free(rsa);
  50290. res = TEST_RES_CHECK(1);
  50291. #endif
  50292. return res;
  50293. }
  50294. #ifndef NO_RSA
  50295. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  50296. {
  50297. int res = TEST_SKIPPED;
  50298. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50299. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50300. RSA *rsa;
  50301. const unsigned char *derBuf = client_key_der_2048;
  50302. unsigned char em[256] = {0}; /* len = 2048/8 */
  50303. /* Random data simulating a hash */
  50304. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  50305. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50306. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50307. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50308. };
  50309. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50310. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  50311. RSA_PSS_SALTLEN_DIGEST), 0);
  50312. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  50313. RSA_PSS_SALTLEN_DIGEST), 0);
  50314. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  50315. RSA_PSS_SALTLEN_DIGEST), 0);
  50316. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  50317. RSA_PSS_SALTLEN_DIGEST), 0);
  50318. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  50319. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  50320. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50321. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  50322. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50323. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  50324. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50325. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  50326. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50327. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50328. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50329. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  50330. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50331. RSA_PSS_SALTLEN_DIGEST), 1);
  50332. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50333. RSA_PSS_SALTLEN_DIGEST), 1);
  50334. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50335. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50336. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50337. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50338. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50339. RSA_PSS_SALTLEN_MAX), 1);
  50340. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50341. RSA_PSS_SALTLEN_MAX), 1);
  50342. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  50343. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  50344. RSA_free(rsa);
  50345. res = TEST_RES_CHECK(1);
  50346. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50347. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50348. return res;
  50349. }
  50350. #endif
  50351. static int test_wolfSSL_RSA_sign_sha3(void)
  50352. {
  50353. int res = TEST_SKIPPED;
  50354. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  50355. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50356. RSA *rsa;
  50357. const unsigned char *derBuf = client_key_der_2048;
  50358. unsigned char sigRet[256] = {0};
  50359. unsigned int sigLen = sizeof(sigRet);
  50360. /* Random data simulating a hash */
  50361. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  50362. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50363. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50364. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50365. };
  50366. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50367. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  50368. &sigLen, rsa), 1);
  50369. RSA_free(rsa);
  50370. res = TEST_RES_CHECK(1);
  50371. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50372. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  50373. return res;
  50374. }
  50375. static int test_wolfSSL_RSA_get0_key(void)
  50376. {
  50377. int res = TEST_SKIPPED;
  50378. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  50379. RSA *rsa = NULL;
  50380. const BIGNUM* n = NULL;
  50381. const BIGNUM* e = NULL;
  50382. const BIGNUM* d = NULL;
  50383. const unsigned char* der;
  50384. int derSz;
  50385. #ifdef USE_CERT_BUFFERS_1024
  50386. der = client_key_der_1024;
  50387. derSz = sizeof_client_key_der_1024;
  50388. #elif defined(USE_CERT_BUFFERS_2048)
  50389. der = client_key_der_2048;
  50390. derSz = sizeof_client_key_der_2048;
  50391. #else
  50392. der = NULL;
  50393. derSz = 0;
  50394. #endif
  50395. if (der != NULL) {
  50396. RSA_get0_key(NULL, NULL, NULL, NULL);
  50397. RSA_get0_key(rsa, NULL, NULL, NULL);
  50398. RSA_get0_key(NULL, &n, &e, &d);
  50399. AssertNull(n);
  50400. AssertNull(e);
  50401. AssertNull(d);
  50402. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  50403. AssertNotNull(rsa);
  50404. RSA_get0_key(rsa, NULL, NULL, NULL);
  50405. RSA_get0_key(rsa, &n, NULL, NULL);
  50406. AssertNotNull(n);
  50407. RSA_get0_key(rsa, NULL, &e, NULL);
  50408. AssertNotNull(e);
  50409. RSA_get0_key(rsa, NULL, NULL, &d);
  50410. AssertNotNull(d);
  50411. RSA_get0_key(rsa, &n, &e, &d);
  50412. AssertNotNull(n);
  50413. AssertNotNull(e);
  50414. AssertNotNull(d);
  50415. RSA_free(rsa);
  50416. }
  50417. res = TEST_RES_CHECK(1);
  50418. #endif
  50419. return res;
  50420. }
  50421. static int test_wolfSSL_RSA_meth(void)
  50422. {
  50423. int res = TEST_SKIPPED;
  50424. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50425. RSA *rsa;
  50426. RSA_METHOD *rsa_meth;
  50427. #ifdef WOLFSSL_KEY_GEN
  50428. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50429. RSA_free(rsa);
  50430. #else
  50431. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50432. #endif
  50433. AssertNotNull(RSA_get_default_method());
  50434. wolfSSL_RSA_meth_free(NULL);
  50435. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  50436. AssertNotNull(rsa_meth =
  50437. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  50438. #ifndef NO_WOLFSSL_STUB
  50439. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  50440. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  50441. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  50442. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  50443. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  50444. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  50445. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  50446. #endif
  50447. AssertIntEQ(RSA_flags(NULL), 0);
  50448. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50449. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50450. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  50451. AssertNotNull(rsa = RSA_new());
  50452. /* No method set. */
  50453. AssertIntEQ(RSA_flags(rsa), 0);
  50454. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50455. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50456. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50457. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  50458. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  50459. AssertNull(RSA_get_method(NULL));
  50460. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  50461. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  50462. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50463. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50464. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  50465. RSA_METHOD_FLAG_NO_CHECK);
  50466. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50467. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50468. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  50469. /* rsa_meth is freed here */
  50470. RSA_free(rsa);
  50471. res = TEST_RES_CHECK(1);
  50472. #endif
  50473. return res;
  50474. }
  50475. static int test_wolfSSL_RSA_verify(void)
  50476. {
  50477. int res = TEST_SKIPPED;
  50478. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  50479. !defined(NO_FILESYSTEM)
  50480. #ifndef NO_BIO
  50481. XFILE fp;
  50482. RSA *pKey, *pubKey;
  50483. X509 *cert;
  50484. const char *text = "Hello wolfSSL !";
  50485. unsigned char hash[SHA256_DIGEST_LENGTH];
  50486. unsigned char signature[2048/8];
  50487. unsigned int signatureLength;
  50488. byte *buf;
  50489. BIO *bio;
  50490. SHA256_CTX c;
  50491. EVP_PKEY *evpPkey, *evpPubkey;
  50492. size_t sz;
  50493. /* generate hash */
  50494. SHA256_Init(&c);
  50495. SHA256_Update(&c, text, strlen(text));
  50496. SHA256_Final(hash, &c);
  50497. #ifdef WOLFSSL_SMALL_STACK_CACHE
  50498. /* workaround for small stack cache case */
  50499. wc_Sha256Free((wc_Sha256*)&c);
  50500. #endif
  50501. /* read privete key file */
  50502. fp = XFOPEN(svrKeyFile, "rb");
  50503. AssertTrue((fp != XBADFILE));
  50504. AssertIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  50505. sz = XFTELL(fp);
  50506. AssertIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  50507. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  50508. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  50509. XFCLOSE(fp);
  50510. /* read private key and sign hash data */
  50511. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  50512. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  50513. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  50514. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  50515. signature, &signatureLength, pKey), SSL_SUCCESS);
  50516. /* read public key and verify signed data */
  50517. fp = XFOPEN(svrCertFile,"rb");
  50518. AssertTrue((fp != XBADFILE));
  50519. cert = PEM_read_X509(fp, 0, 0, 0 );
  50520. XFCLOSE(fp);
  50521. evpPubkey = X509_get_pubkey(cert);
  50522. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  50523. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50524. signatureLength, pubKey), SSL_SUCCESS);
  50525. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  50526. signatureLength, NULL), SSL_FAILURE);
  50527. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  50528. signatureLength, pubKey), SSL_FAILURE);
  50529. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  50530. signatureLength, pubKey), SSL_FAILURE);
  50531. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50532. signatureLength, NULL), SSL_FAILURE);
  50533. RSA_free(pKey);
  50534. EVP_PKEY_free(evpPkey);
  50535. RSA_free(pubKey);
  50536. EVP_PKEY_free(evpPubkey);
  50537. X509_free(cert);
  50538. BIO_free(bio);
  50539. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  50540. res = TEST_RES_CHECK(1);
  50541. #endif
  50542. #endif
  50543. return res;
  50544. }
  50545. static int test_wolfSSL_RSA_sign(void)
  50546. {
  50547. int res = TEST_SKIPPED;
  50548. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50549. RSA *rsa;
  50550. unsigned char hash[SHA256_DIGEST_LENGTH];
  50551. #ifdef USE_CERT_BUFFERS_1024
  50552. const unsigned char* privDer = client_key_der_1024;
  50553. size_t privDerSz = sizeof_client_key_der_1024;
  50554. const unsigned char* pubDer = client_keypub_der_1024;
  50555. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50556. unsigned char signature[1024/8];
  50557. #else
  50558. const unsigned char* privDer = client_key_der_2048;
  50559. size_t privDerSz = sizeof_client_key_der_2048;
  50560. const unsigned char* pubDer = client_keypub_der_2048;
  50561. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50562. unsigned char signature[2048/8];
  50563. #endif
  50564. unsigned int signatureLen;
  50565. const unsigned char* der;
  50566. XMEMSET(hash, 0, sizeof(hash));
  50567. der = privDer;
  50568. rsa = NULL;
  50569. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50570. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  50571. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  50572. &signatureLen, rsa), 0);
  50573. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  50574. &signatureLen, rsa), 0);
  50575. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  50576. &signatureLen, rsa), 0);
  50577. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50578. NULL, rsa), 0);
  50579. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50580. &signatureLen, NULL), 0);
  50581. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50582. &signatureLen, rsa), 1);
  50583. RSA_free(rsa);
  50584. der = pubDer;
  50585. rsa = NULL;
  50586. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50587. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50588. signatureLen, rsa), 1);
  50589. RSA_free(rsa);
  50590. res = TEST_RES_CHECK(1);
  50591. #endif
  50592. return res;
  50593. }
  50594. static int test_wolfSSL_RSA_sign_ex(void)
  50595. {
  50596. int res = TEST_SKIPPED;
  50597. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50598. RSA *rsa;
  50599. unsigned char hash[SHA256_DIGEST_LENGTH];
  50600. #ifdef USE_CERT_BUFFERS_1024
  50601. const unsigned char* privDer = client_key_der_1024;
  50602. size_t privDerSz = sizeof_client_key_der_1024;
  50603. const unsigned char* pubDer = client_keypub_der_1024;
  50604. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50605. unsigned char signature[1024/8];
  50606. #else
  50607. const unsigned char* privDer = client_key_der_2048;
  50608. size_t privDerSz = sizeof_client_key_der_2048;
  50609. const unsigned char* pubDer = client_keypub_der_2048;
  50610. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50611. unsigned char signature[2048/8];
  50612. #endif
  50613. unsigned int signatureLen;
  50614. const unsigned char* der;
  50615. unsigned char encodedHash[51];
  50616. unsigned int encodedHashLen;
  50617. const unsigned char expEncHash[] = {
  50618. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  50619. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  50620. 0x00, 0x04, 0x20,
  50621. /* Hash data */
  50622. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50623. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50624. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50625. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50626. };
  50627. XMEMSET(hash, 0, sizeof(hash));
  50628. AssertNotNull(rsa = wolfSSL_RSA_new());
  50629. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50630. &signatureLen, rsa, 1), 0);
  50631. wolfSSL_RSA_free(rsa);
  50632. der = privDer;
  50633. rsa = NULL;
  50634. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50635. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  50636. -1), 0);
  50637. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  50638. signature, &signatureLen, rsa, 1), 0);
  50639. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50640. &signatureLen, rsa, 1), 0);
  50641. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50642. &signatureLen, rsa, 1), 0);
  50643. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50644. NULL, rsa, 1), 0);
  50645. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50646. &signatureLen, NULL, 1), 0);
  50647. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50648. &signatureLen, rsa, -1), 0);
  50649. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50650. &signatureLen, rsa, 0), 0);
  50651. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50652. &signatureLen, rsa, 0), 0);
  50653. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50654. NULL, rsa, 0), 0);
  50655. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50656. &signatureLen, rsa, 1), 1);
  50657. /* Test returning encoded hash. */
  50658. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  50659. &encodedHashLen, rsa, 0), 1);
  50660. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  50661. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  50662. RSA_free(rsa);
  50663. der = pubDer;
  50664. rsa = NULL;
  50665. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50666. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50667. signatureLen, rsa), 1);
  50668. RSA_free(rsa);
  50669. res = TEST_RES_CHECK(1);
  50670. #endif
  50671. return res;
  50672. }
  50673. static int test_wolfSSL_RSA_public_decrypt(void)
  50674. {
  50675. int res = TEST_SKIPPED;
  50676. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50677. RSA *rsa;
  50678. unsigned char msg[SHA256_DIGEST_LENGTH];
  50679. #ifdef USE_CERT_BUFFERS_1024
  50680. const unsigned char* pubDer = client_keypub_der_1024;
  50681. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50682. unsigned char decMsg[1024/8];
  50683. const unsigned char encMsg[] = {
  50684. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50685. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50686. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50687. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50688. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50689. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50690. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50691. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50692. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50693. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50694. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50695. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50696. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50697. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50698. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50699. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50700. };
  50701. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50702. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50703. defined(WC_RSA_NO_PADDING)
  50704. const unsigned char encMsgNoPad[] = {
  50705. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50706. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50707. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50708. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50709. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50710. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50711. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50712. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50713. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50714. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50715. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50716. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50717. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50718. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50719. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50720. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50721. };
  50722. #endif
  50723. #else
  50724. const unsigned char* pubDer = client_keypub_der_2048;
  50725. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50726. unsigned char decMsg[2048/8];
  50727. const unsigned char encMsg[] = {
  50728. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50729. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50730. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50731. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50732. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50733. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50734. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50735. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50736. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50737. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50738. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50739. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50740. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50741. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50742. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50743. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50744. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50745. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50746. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50747. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50748. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50749. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50750. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50751. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50752. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50753. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50754. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50755. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50756. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50757. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50758. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50759. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50760. };
  50761. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50762. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50763. defined(WC_RSA_NO_PADDING)
  50764. const unsigned char encMsgNoPad[] = {
  50765. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50766. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50767. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50768. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50769. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50770. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50771. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50772. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50773. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50774. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50775. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50776. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50777. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50778. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50779. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50780. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50781. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50782. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50783. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50784. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50785. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50786. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50787. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50788. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50789. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50790. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50791. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50792. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50793. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50794. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50795. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50796. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50797. };
  50798. #endif
  50799. #endif
  50800. const unsigned char* der;
  50801. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50802. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50803. defined(WC_RSA_NO_PADDING)
  50804. int i;
  50805. #endif
  50806. XMEMSET(msg, 0, sizeof(msg));
  50807. der = pubDer;
  50808. rsa = NULL;
  50809. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50810. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  50811. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  50812. RSA_PKCS1_PADDING), -1);
  50813. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  50814. RSA_PKCS1_PADDING), -1);
  50815. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50816. RSA_PKCS1_PADDING), -1);
  50817. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  50818. RSA_PKCS1_PADDING), -1);
  50819. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50820. RSA_PKCS1_PSS_PADDING), -1);
  50821. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50822. RSA_PKCS1_PADDING), 32);
  50823. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  50824. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50825. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50826. defined(WC_RSA_NO_PADDING)
  50827. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  50828. rsa, RSA_NO_PADDING), sizeof(decMsg));
  50829. /* Zeros before actual data. */
  50830. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  50831. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50832. }
  50833. /* Check actual data. */
  50834. XMEMSET(msg, 0x01, sizeof(msg));
  50835. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50836. #endif
  50837. RSA_free(rsa);
  50838. res = TEST_RES_CHECK(1);
  50839. #endif
  50840. return res;
  50841. }
  50842. static int test_wolfSSL_RSA_private_encrypt(void)
  50843. {
  50844. int res = TEST_SKIPPED;
  50845. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50846. RSA *rsa;
  50847. unsigned char msg[SHA256_DIGEST_LENGTH];
  50848. #ifdef USE_CERT_BUFFERS_1024
  50849. const unsigned char* privDer = client_key_der_1024;
  50850. size_t privDerSz = sizeof_client_key_der_1024;
  50851. unsigned char encMsg[1024/8];
  50852. const unsigned char expEncMsg[] = {
  50853. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50854. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50855. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50856. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50857. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50858. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50859. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50860. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50861. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50862. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50863. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50864. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50865. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50866. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50867. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50868. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50869. };
  50870. #ifdef WC_RSA_NO_PADDING
  50871. const unsigned char expEncMsgNoPad[] = {
  50872. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50873. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50874. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50875. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50876. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50877. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50878. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50879. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50880. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50881. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50882. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50883. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50884. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50885. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50886. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50887. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50888. };
  50889. #endif
  50890. #else
  50891. const unsigned char* privDer = client_key_der_2048;
  50892. size_t privDerSz = sizeof_client_key_der_2048;
  50893. unsigned char encMsg[2048/8];
  50894. const unsigned char expEncMsg[] = {
  50895. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50896. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50897. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50898. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50899. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50900. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50901. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50902. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50903. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50904. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50905. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50906. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50907. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50908. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50909. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50910. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50911. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50912. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50913. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50914. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50915. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50916. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50917. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50918. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50919. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50920. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50921. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50922. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50923. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50924. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50925. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50926. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50927. };
  50928. #ifdef WC_RSA_NO_PADDING
  50929. const unsigned char expEncMsgNoPad[] = {
  50930. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50931. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50932. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50933. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50934. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50935. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50936. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50937. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50938. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50939. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50940. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50941. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50942. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50943. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50944. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50945. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50946. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50947. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50948. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50949. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50950. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50951. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50952. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50953. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50954. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50955. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50956. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50957. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50958. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50959. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50960. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50961. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50962. };
  50963. #endif
  50964. #endif
  50965. const unsigned char* der;
  50966. XMEMSET(msg, 0x00, sizeof(msg));
  50967. der = privDer;
  50968. rsa = NULL;
  50969. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50970. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50971. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50972. -1);
  50973. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50974. RSA_PKCS1_PADDING), -1);
  50975. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50976. RSA_PKCS1_PADDING), -1);
  50977. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50978. RSA_PKCS1_PADDING), -1);
  50979. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50980. RSA_PKCS1_PSS_PADDING), -1);
  50981. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50982. RSA_PKCS1_PADDING), sizeof(encMsg));
  50983. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50984. #ifdef WC_RSA_NO_PADDING
  50985. /* Non-zero message. */
  50986. XMEMSET(msg, 0x01, sizeof(msg));
  50987. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50988. RSA_NO_PADDING), sizeof(encMsg));
  50989. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50990. #endif
  50991. RSA_free(rsa);
  50992. res = TEST_RES_CHECK(1);
  50993. #endif
  50994. return res;
  50995. }
  50996. static int test_wolfSSL_RSA_public_encrypt(void)
  50997. {
  50998. int res = TEST_SKIPPED;
  50999. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  51000. RSA* rsa;
  51001. const unsigned char msg[2048/8] = { 0 };
  51002. unsigned char encMsg[2048/8];
  51003. AssertNotNull(rsa = RSA_new());
  51004. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  51005. RSA_PKCS1_PADDING), -1);
  51006. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51007. RSA_PKCS1_PADDING), -1);
  51008. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  51009. RSA_PKCS1_PADDING), -1);
  51010. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  51011. RSA_PKCS1_PADDING), -1);
  51012. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51013. RSA_PKCS1_PSS_PADDING), -1);
  51014. /* Empty RSA key. */
  51015. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51016. RSA_PKCS1_PADDING), -1);
  51017. RSA_free(rsa);
  51018. res = TEST_RES_CHECK(1);
  51019. #endif
  51020. return res;
  51021. }
  51022. static int test_wolfSSL_RSA_private_decrypt(void)
  51023. {
  51024. int res = TEST_SKIPPED;
  51025. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  51026. RSA* rsa;
  51027. unsigned char msg[2048/8];
  51028. const unsigned char encMsg[2048/8] = { 0 };
  51029. AssertNotNull(rsa = RSA_new());
  51030. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  51031. RSA_PKCS1_PADDING), -1);
  51032. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  51033. RSA_PKCS1_PADDING), -1);
  51034. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51035. RSA_PKCS1_PADDING), -1);
  51036. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  51037. RSA_PKCS1_PADDING), -1);
  51038. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51039. RSA_PKCS1_PSS_PADDING), -1);
  51040. /* Empty RSA key. */
  51041. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51042. RSA_PKCS1_PADDING), -1);
  51043. RSA_free(rsa);
  51044. res = TEST_RES_CHECK(1);
  51045. #endif
  51046. return res;
  51047. }
  51048. static int test_wolfSSL_RSA_GenAdd(void)
  51049. {
  51050. int res = TEST_SKIPPED;
  51051. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51052. RSA *rsa;
  51053. #ifdef USE_CERT_BUFFERS_1024
  51054. const unsigned char* privDer = client_key_der_1024;
  51055. size_t privDerSz = sizeof_client_key_der_1024;
  51056. const unsigned char* pubDer = client_keypub_der_1024;
  51057. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51058. #else
  51059. const unsigned char* privDer = client_key_der_2048;
  51060. size_t privDerSz = sizeof_client_key_der_2048;
  51061. const unsigned char* pubDer = client_keypub_der_2048;
  51062. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51063. #endif
  51064. const unsigned char* der;
  51065. der = privDer;
  51066. rsa = NULL;
  51067. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51068. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  51069. #ifndef RSA_LOW_MEM
  51070. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  51071. #else
  51072. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  51073. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51074. #endif
  51075. RSA_free(rsa);
  51076. der = pubDer;
  51077. rsa = NULL;
  51078. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51079. /* Need private values. */
  51080. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51081. RSA_free(rsa);
  51082. res = TEST_RES_CHECK(1);
  51083. #endif
  51084. return res;
  51085. }
  51086. static int test_wolfSSL_RSA_blinding_on(void)
  51087. {
  51088. int res = TEST_SKIPPED;
  51089. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  51090. RSA *rsa;
  51091. WOLFSSL_BN_CTX *bnCtx;
  51092. #ifdef USE_CERT_BUFFERS_1024
  51093. const unsigned char* privDer = client_key_der_1024;
  51094. size_t privDerSz = sizeof_client_key_der_1024;
  51095. #else
  51096. const unsigned char* privDer = client_key_der_2048;
  51097. size_t privDerSz = sizeof_client_key_der_2048;
  51098. #endif
  51099. const unsigned char* der;
  51100. der = privDer;
  51101. rsa = NULL;
  51102. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51103. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  51104. /* Does nothing so all parameters are valid. */
  51105. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  51106. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  51107. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  51108. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  51109. wolfSSL_BN_CTX_free(bnCtx);
  51110. RSA_free(rsa);
  51111. res = TEST_RES_CHECK(1);
  51112. #endif
  51113. return res;
  51114. }
  51115. static int test_wolfSSL_RSA_ex_data(void)
  51116. {
  51117. int res = TEST_SKIPPED;
  51118. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51119. RSA* rsa;
  51120. unsigned char data[1];
  51121. rsa = RSA_new();
  51122. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  51123. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51124. #ifdef MAX_EX_DATA
  51125. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  51126. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  51127. #endif
  51128. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  51129. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  51130. #ifdef HAVE_EX_DATA
  51131. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  51132. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  51133. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  51134. #else
  51135. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  51136. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  51137. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51138. #endif
  51139. RSA_free(rsa);
  51140. res = TEST_RES_CHECK(1);
  51141. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51142. return res;
  51143. }
  51144. static int test_wolfSSL_RSA_LoadDer(void)
  51145. {
  51146. int res = TEST_SKIPPED;
  51147. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  51148. defined(OPENSSL_EXTRA_X509_SMALL))
  51149. RSA *rsa;
  51150. #ifdef USE_CERT_BUFFERS_1024
  51151. const unsigned char* privDer = client_key_der_1024;
  51152. size_t privDerSz = sizeof_client_key_der_1024;
  51153. #else
  51154. const unsigned char* privDer = client_key_der_2048;
  51155. size_t privDerSz = sizeof_client_key_der_2048;
  51156. #endif
  51157. AssertNotNull(rsa = RSA_new());
  51158. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  51159. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  51160. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  51161. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  51162. RSA_free(rsa);
  51163. res = TEST_RES_CHECK(1);
  51164. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51165. return res;
  51166. }
  51167. /* Local API. */
  51168. static int test_wolfSSL_RSA_To_Der(void)
  51169. {
  51170. int res = TEST_SKIPPED;
  51171. #ifdef WOLFSSL_TEST_STATIC_BUILD
  51172. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  51173. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51174. RSA* rsa;
  51175. #ifdef USE_CERT_BUFFERS_1024
  51176. const unsigned char* privDer = client_key_der_1024;
  51177. size_t privDerSz = sizeof_client_key_der_1024;
  51178. const unsigned char* pubDer = client_keypub_der_1024;
  51179. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51180. unsigned char out[sizeof(client_key_der_1024)];
  51181. #else
  51182. const unsigned char* privDer = client_key_der_2048;
  51183. size_t privDerSz = sizeof_client_key_der_2048;
  51184. const unsigned char* pubDer = client_keypub_der_2048;
  51185. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51186. unsigned char out[sizeof(client_key_der_2048)];
  51187. #endif
  51188. const unsigned char* der;
  51189. unsigned char* outDer = NULL;
  51190. der = privDer;
  51191. rsa = NULL;
  51192. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51193. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51194. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  51195. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  51196. outDer = out;
  51197. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51198. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  51199. outDer = NULL;
  51200. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51201. AssertNotNull(outDer);
  51202. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  51203. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  51204. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  51205. outDer = out;
  51206. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  51207. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  51208. RSA_free(rsa);
  51209. AssertNotNull(rsa = RSA_new());
  51210. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51211. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  51212. RSA_free(rsa);
  51213. der = pubDer;
  51214. rsa = NULL;
  51215. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51216. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51217. RSA_free(rsa);
  51218. res = TEST_RES_CHECK(1);
  51219. #endif
  51220. #endif
  51221. return res;
  51222. }
  51223. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51224. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  51225. {
  51226. int res = TEST_SKIPPED;
  51227. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  51228. XFILE file;
  51229. const char* fname = "./certs/server-keyPub.pem";
  51230. RSA *rsa;
  51231. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  51232. file = XFOPEN(fname, "rb");
  51233. AssertTrue((file != XBADFILE));
  51234. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  51235. AssertIntEQ(RSA_size(rsa), 256);
  51236. RSA_free(rsa);
  51237. XFCLOSE(file);
  51238. res = TEST_RES_CHECK(1);
  51239. #endif
  51240. return res;
  51241. }
  51242. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51243. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  51244. {
  51245. int res = TEST_SKIPPED;
  51246. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51247. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  51248. RSA* rsa = NULL;
  51249. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  51250. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  51251. /* Valid but stub so returns 0. */
  51252. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  51253. res = TEST_RES_CHECK(1);
  51254. #endif
  51255. return res;
  51256. }
  51257. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  51258. {
  51259. int res = TEST_SKIPPED;
  51260. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51261. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  51262. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  51263. RSA* rsa;
  51264. #ifdef USE_CERT_BUFFERS_1024
  51265. const unsigned char* privDer = client_key_der_1024;
  51266. size_t privDerSz = sizeof_client_key_der_1024;
  51267. #else
  51268. const unsigned char* privDer = client_key_der_2048;
  51269. size_t privDerSz = sizeof_client_key_der_2048;
  51270. #endif
  51271. const unsigned char* der;
  51272. #ifndef NO_AES
  51273. unsigned char passwd[] = "password";
  51274. #endif
  51275. AssertNotNull(rsa = RSA_new());
  51276. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51277. NULL, NULL), 0);
  51278. RSA_free(rsa);
  51279. der = privDer;
  51280. rsa = NULL;
  51281. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51282. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  51283. NULL, NULL), 0);
  51284. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  51285. NULL, NULL), 0);
  51286. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51287. NULL, NULL), 1);
  51288. #ifndef NO_AES
  51289. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51290. NULL, 0, NULL, NULL), 1);
  51291. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51292. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  51293. #endif
  51294. RSA_free(rsa);
  51295. res = TEST_RES_CHECK(1);
  51296. #endif
  51297. return res;
  51298. }
  51299. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  51300. {
  51301. int res = TEST_SKIPPED;
  51302. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51303. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  51304. defined(WOLFSSL_DER_TO_PEM))
  51305. RSA* rsa;
  51306. #ifdef USE_CERT_BUFFERS_1024
  51307. const unsigned char* privDer = client_key_der_1024;
  51308. size_t privDerSz = sizeof_client_key_der_1024;
  51309. #else
  51310. const unsigned char* privDer = client_key_der_2048;
  51311. size_t privDerSz = sizeof_client_key_der_2048;
  51312. #endif
  51313. const unsigned char* der;
  51314. #ifndef NO_AES
  51315. unsigned char passwd[] = "password";
  51316. #endif
  51317. unsigned char* pem;
  51318. int plen;
  51319. AssertNotNull(rsa = RSA_new());
  51320. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51321. &plen), 0);
  51322. RSA_free(rsa);
  51323. der = privDer;
  51324. rsa = NULL;
  51325. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51326. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  51327. &plen), 0);
  51328. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  51329. &plen), 0);
  51330. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51331. NULL), 0);
  51332. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51333. &plen), 1);
  51334. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51335. #ifndef NO_AES
  51336. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51337. NULL, 0, &pem, &plen), 1);
  51338. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51339. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51340. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  51341. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51342. #endif
  51343. RSA_free(rsa);
  51344. res = TEST_RES_CHECK(1);
  51345. #endif
  51346. return res;
  51347. }
  51348. static int test_wolfSSL_DH(void)
  51349. {
  51350. int res = TEST_SKIPPED;
  51351. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51352. DH *dh = NULL;
  51353. BIGNUM* p;
  51354. BIGNUM* q;
  51355. BIGNUM* g;
  51356. BIGNUM* pub;
  51357. BIGNUM* priv;
  51358. #if defined(OPENSSL_ALL)
  51359. #if !defined(HAVE_FIPS) || \
  51360. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  51361. FILE* f = NULL;
  51362. unsigned char buf[268];
  51363. const unsigned char* pt = buf;
  51364. long len = 0;
  51365. dh = NULL;
  51366. XMEMSET(buf, 0, sizeof(buf));
  51367. /* Test 2048 bit parameters */
  51368. f = XFOPEN("./certs/dh2048.der", "rb");
  51369. AssertTrue(f != XBADFILE);
  51370. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51371. XFCLOSE(f);
  51372. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  51373. AssertNotNull(dh->p);
  51374. AssertNotNull(dh->g);
  51375. AssertTrue(pt == buf);
  51376. AssertIntEQ(DH_generate_key(dh), 1);
  51377. AssertIntEQ(DH_generate_key(dh), 1);
  51378. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  51379. AssertNotNull(pub = BN_new());
  51380. AssertIntEQ(BN_set_word(pub, 1), 1);
  51381. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  51382. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  51383. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  51384. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  51385. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  51386. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  51387. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  51388. BN_free(pub);
  51389. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51390. (const BIGNUM**)&q,
  51391. (const BIGNUM**)&g);
  51392. AssertPtrEq(p, dh->p);
  51393. AssertPtrEq(q, dh->q);
  51394. AssertPtrEq(g, dh->g);
  51395. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51396. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51397. AssertPtrEq(pub, dh->pub_key);
  51398. AssertPtrEq(priv, dh->priv_key);
  51399. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  51400. AssertPtrEq(pub, dh->pub_key);
  51401. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  51402. AssertPtrEq(priv, dh->priv_key);
  51403. AssertNotNull(pub = BN_new());
  51404. AssertNotNull(priv = BN_new());
  51405. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  51406. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  51407. AssertNotNull(pub = BN_new());
  51408. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  51409. AssertNotNull(priv = BN_new());
  51410. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  51411. AssertPtrEq(pub, dh->pub_key);
  51412. AssertPtrEq(priv, dh->priv_key);
  51413. DH_free(dh);
  51414. AssertNotNull(dh = DH_new());
  51415. AssertNotNull(p = BN_new());
  51416. AssertIntEQ(BN_set_word(p, 1), 1);
  51417. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  51418. AssertNotNull(pub = BN_new());
  51419. AssertNotNull(priv = BN_new());
  51420. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  51421. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  51422. BN_free(p);
  51423. DH_free(dh);
  51424. #ifdef WOLFSSL_KEY_GEN
  51425. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  51426. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  51427. DH_free(dh);
  51428. #endif
  51429. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  51430. #endif /* OPENSSL_ALL */
  51431. (void)dh;
  51432. (void)p;
  51433. (void)q;
  51434. (void)g;
  51435. (void)pub;
  51436. (void)priv;
  51437. dh = wolfSSL_DH_new();
  51438. AssertNotNull(dh);
  51439. /* invalid parameters test */
  51440. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  51441. (const BIGNUM**)&q,
  51442. (const BIGNUM**)&g);
  51443. DH_get0_pqg(dh, NULL,
  51444. (const BIGNUM**)&q,
  51445. (const BIGNUM**)&g);
  51446. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  51447. DH_get0_pqg(dh, NULL, NULL, NULL);
  51448. AssertTrue(1);
  51449. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51450. (const BIGNUM**)&q,
  51451. (const BIGNUM**)&g);
  51452. AssertPtrEq(p, NULL);
  51453. AssertPtrEq(q, NULL);
  51454. AssertPtrEq(g, NULL);
  51455. DH_free(dh);
  51456. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  51457. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  51458. #if defined(OPENSSL_ALL) || \
  51459. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51460. dh = wolfSSL_DH_new();
  51461. AssertNotNull(dh);
  51462. p = wolfSSL_BN_new();
  51463. AssertNotNull(p);
  51464. AssertIntEQ(BN_set_word(p, 11), 1);
  51465. g = wolfSSL_BN_new();
  51466. AssertNotNull(g);
  51467. AssertIntEQ(BN_set_word(g, 2), 1);
  51468. q = wolfSSL_BN_new();
  51469. AssertNotNull(q);
  51470. AssertIntEQ(BN_set_word(q, 5), 1);
  51471. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  51472. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  51473. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  51474. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  51475. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  51476. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  51477. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  51478. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  51479. /* Don't need q. */
  51480. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51481. /* Setting again will free the p and g. */
  51482. wolfSSL_BN_free(q);
  51483. DH_free(dh);
  51484. dh = wolfSSL_DH_new();
  51485. AssertNotNull(dh);
  51486. p = wolfSSL_BN_new();
  51487. AssertNotNull(p);
  51488. AssertIntEQ(BN_set_word(p, 11), 1);
  51489. g = wolfSSL_BN_new();
  51490. AssertNotNull(g);
  51491. AssertIntEQ(BN_set_word(g, 2), 1);
  51492. q = wolfSSL_BN_new();
  51493. AssertNotNull(q);
  51494. AssertIntEQ(BN_set_word(q, 5), 1);
  51495. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  51496. /* p, q and g are now owned by dh - don't free. */
  51497. p = wolfSSL_BN_new();
  51498. AssertNotNull(p);
  51499. AssertIntEQ(BN_set_word(p, 11), 1);
  51500. g = wolfSSL_BN_new();
  51501. AssertNotNull(g);
  51502. AssertIntEQ(BN_set_word(g, 2), 1);
  51503. q = wolfSSL_BN_new();
  51504. AssertNotNull(q);
  51505. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  51506. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  51507. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  51508. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  51509. /* p, q and g are now owned by dh - don't free. */
  51510. DH_free(dh);
  51511. AssertIntEQ(DH_generate_key(NULL), 0);
  51512. AssertNotNull(dh = DH_new());
  51513. AssertIntEQ(DH_generate_key(dh), 0);
  51514. p = wolfSSL_BN_new();
  51515. AssertNotNull(p);
  51516. AssertIntEQ(BN_set_word(p, 0), 1);
  51517. g = wolfSSL_BN_new();
  51518. AssertNotNull(g);
  51519. AssertIntEQ(BN_set_word(g, 2), 1);
  51520. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51521. AssertIntEQ(DH_generate_key(dh), 0);
  51522. DH_free(dh);
  51523. #endif
  51524. #endif
  51525. /* Test DH_up_ref() */
  51526. dh = wolfSSL_DH_new();
  51527. AssertNotNull(dh);
  51528. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  51529. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  51530. DH_free(dh); /* decrease ref count */
  51531. DH_free(dh); /* free WOLFSSL_DH */
  51532. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  51533. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  51534. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  51535. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  51536. #ifdef HAVE_FFDHE_2048
  51537. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51538. DH_free(dh);
  51539. #endif
  51540. #ifdef HAVE_FFDHE_3072
  51541. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  51542. DH_free(dh);
  51543. #endif
  51544. #ifdef HAVE_FFDHE_4096
  51545. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  51546. DH_free(dh);
  51547. #endif
  51548. #else
  51549. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51550. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  51551. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  51552. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  51553. res = TEST_RES_CHECK(1);
  51554. #endif /* OPENSSL_EXTRA && !NO_DH */
  51555. return res;
  51556. }
  51557. static int test_wolfSSL_DH_dup(void)
  51558. {
  51559. int res = TEST_SKIPPED;
  51560. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  51561. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  51562. defined(OPENSSL_EXTRA)
  51563. DH *dh;
  51564. DH *dhDup;
  51565. WOLFSSL_BIGNUM* p;
  51566. WOLFSSL_BIGNUM* g;
  51567. AssertNotNull(p = wolfSSL_BN_new());
  51568. AssertNotNull(g = wolfSSL_BN_new());
  51569. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  51570. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  51571. dhDup = wolfSSL_DH_dup(NULL);
  51572. AssertNull(dhDup);
  51573. dh = wolfSSL_DH_new();
  51574. AssertNotNull(dh);
  51575. dhDup = wolfSSL_DH_dup(dh);
  51576. AssertNull(dhDup);
  51577. #if defined(OPENSSL_ALL) || \
  51578. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51579. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51580. dhDup = wolfSSL_DH_dup(dh);
  51581. AssertNotNull(dhDup);
  51582. wolfSSL_DH_free(dhDup);
  51583. #else
  51584. wolfSSL_BN_free(p);
  51585. wolfSSL_BN_free(g);
  51586. #endif
  51587. wolfSSL_DH_free(dh);
  51588. res = TEST_RES_CHECK(1);
  51589. #endif
  51590. #endif
  51591. return res;
  51592. }
  51593. static int test_wolfSSL_DH_check(void)
  51594. {
  51595. int res = TEST_SKIPPED;
  51596. #ifdef OPENSSL_ALL
  51597. #ifndef NO_DH
  51598. #ifndef NO_BIO
  51599. #ifndef NO_DSA
  51600. byte buf[6000];
  51601. char file[] = "./certs/dsaparams.pem";
  51602. XFILE f;
  51603. int bytes;
  51604. BIO* bio;
  51605. DSA* dsa;
  51606. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51607. static const byte dh2048[] = {
  51608. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51609. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51610. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51611. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51612. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51613. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51614. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51615. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51616. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51617. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51618. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51619. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51620. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51621. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51622. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51623. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51624. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51625. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51626. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51627. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51628. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51629. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51630. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51631. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51632. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51633. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51634. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51635. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51636. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51637. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51638. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51639. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51640. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51641. 0x93, 0x02, 0x01, 0x02
  51642. };
  51643. const byte* params;
  51644. #endif
  51645. DH* dh = NULL;
  51646. WOLFSSL_BIGNUM* p;
  51647. WOLFSSL_BIGNUM* g;
  51648. WOLFSSL_BIGNUM* pTmp = NULL;
  51649. WOLFSSL_BIGNUM* gTmp = NULL;
  51650. int codes = -1;
  51651. #ifndef NO_DSA
  51652. /* Initialize DH */
  51653. f = XFOPEN(file, "rb");
  51654. AssertTrue((f != XBADFILE));
  51655. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  51656. XFCLOSE(f);
  51657. bio = BIO_new_mem_buf((void*)buf, bytes);
  51658. AssertNotNull(bio);
  51659. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  51660. AssertNotNull(dsa);
  51661. dh = wolfSSL_DSA_dup_DH(dsa);
  51662. AssertNotNull(dh);
  51663. BIO_free(bio);
  51664. DSA_free(dsa);
  51665. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51666. params = dh2048;
  51667. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  51668. AssertNotNull(dh);
  51669. #else
  51670. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  51671. AssertNotNull(dh);
  51672. #endif
  51673. /* Test assumed to be valid dh.
  51674. * Should return WOLFSSL_SUCCESS
  51675. * codes should be 0
  51676. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  51677. */
  51678. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51679. AssertIntEQ(codes, 0);
  51680. /* Test NULL dh: expected BAD_FUNC_ARG */
  51681. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  51682. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  51683. pTmp = dh->p;
  51684. dh->p = NULL;
  51685. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51686. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51687. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51688. /* set dh->p back to normal so it wont fail on next tests */
  51689. dh->p = pTmp;
  51690. pTmp = NULL;
  51691. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  51692. gTmp = dh->g;
  51693. dh->g = NULL;
  51694. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51695. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51696. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  51697. dh->g = gTmp;
  51698. gTmp = NULL;
  51699. /* Cleanup */
  51700. DH_free(dh);
  51701. dh = DH_new();
  51702. AssertNotNull(dh);
  51703. /* Check empty DH. */
  51704. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51705. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51706. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  51707. /* Check non-prime valued p. */
  51708. AssertNotNull(p = BN_new());
  51709. AssertIntEQ(BN_set_word(p, 4), 1);
  51710. AssertNotNull(g = BN_new());
  51711. AssertIntEQ(BN_set_word(g, 2), 1);
  51712. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  51713. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51714. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51715. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51716. DH_free(dh);
  51717. res = TEST_RES_CHECK(1);
  51718. #endif
  51719. #endif /* !NO_DH && !NO_DSA */
  51720. #endif
  51721. return res;
  51722. }
  51723. static int test_wolfSSL_DH_prime(void)
  51724. {
  51725. int res = TEST_SKIPPED;
  51726. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51727. WOLFSSL_BIGNUM* bn;
  51728. #if WOLFSSL_MAX_BN_BITS >= 768
  51729. WOLFSSL_BIGNUM* bn2;
  51730. #endif
  51731. bn = wolfSSL_DH_768_prime(NULL);
  51732. #if WOLFSSL_MAX_BN_BITS >= 768
  51733. AssertNotNull(bn);
  51734. bn2 = wolfSSL_DH_768_prime(bn);
  51735. AssertNotNull(bn2);
  51736. AssertTrue(bn == bn2);
  51737. wolfSSL_BN_free(bn);
  51738. #else
  51739. AssertNull(bn);
  51740. #endif
  51741. bn = wolfSSL_DH_1024_prime(NULL);
  51742. #if WOLFSSL_MAX_BN_BITS >= 1024
  51743. AssertNotNull(bn);
  51744. wolfSSL_BN_free(bn);
  51745. #else
  51746. AssertNull(bn);
  51747. #endif
  51748. bn = wolfSSL_DH_2048_prime(NULL);
  51749. #if WOLFSSL_MAX_BN_BITS >= 2048
  51750. AssertNotNull(bn);
  51751. wolfSSL_BN_free(bn);
  51752. #else
  51753. AssertNull(bn);
  51754. #endif
  51755. bn = wolfSSL_DH_3072_prime(NULL);
  51756. #if WOLFSSL_MAX_BN_BITS >= 3072
  51757. AssertNotNull(bn);
  51758. wolfSSL_BN_free(bn);
  51759. #else
  51760. AssertNull(bn);
  51761. #endif
  51762. bn = wolfSSL_DH_4096_prime(NULL);
  51763. #if WOLFSSL_MAX_BN_BITS >= 4096
  51764. AssertNotNull(bn);
  51765. wolfSSL_BN_free(bn);
  51766. #else
  51767. AssertNull(bn);
  51768. #endif
  51769. bn = wolfSSL_DH_6144_prime(NULL);
  51770. #if WOLFSSL_MAX_BN_BITS >= 6144
  51771. AssertNotNull(bn);
  51772. wolfSSL_BN_free(bn);
  51773. #else
  51774. AssertNull(bn);
  51775. #endif
  51776. bn = wolfSSL_DH_8192_prime(NULL);
  51777. #if WOLFSSL_MAX_BN_BITS >= 8192
  51778. AssertNotNull(bn);
  51779. wolfSSL_BN_free(bn);
  51780. #else
  51781. AssertNull(bn);
  51782. #endif
  51783. res = TEST_RES_CHECK(1);
  51784. #endif
  51785. return res;
  51786. }
  51787. static int test_wolfSSL_DH_1536_prime(void)
  51788. {
  51789. int res = TEST_SKIPPED;
  51790. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51791. BIGNUM* bn;
  51792. unsigned char bits[200];
  51793. int sz = 192; /* known binary size */
  51794. const byte expected[] = {
  51795. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51796. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  51797. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  51798. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  51799. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  51800. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  51801. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  51802. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  51803. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  51804. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  51805. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  51806. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  51807. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  51808. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  51809. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  51810. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  51811. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  51812. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  51813. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  51814. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  51815. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  51816. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  51817. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  51818. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51819. };
  51820. bn = get_rfc3526_prime_1536(NULL);
  51821. AssertNotNull(bn);
  51822. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  51823. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  51824. BN_free(bn);
  51825. res = TEST_RES_CHECK(1);
  51826. #endif
  51827. return res;
  51828. }
  51829. static int test_wolfSSL_DH_get_2048_256(void)
  51830. {
  51831. int res = TEST_SKIPPED;
  51832. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51833. WOLFSSL_DH* dh;
  51834. const WOLFSSL_BIGNUM* pBn;
  51835. const WOLFSSL_BIGNUM* gBn;
  51836. const WOLFSSL_BIGNUM* qBn;
  51837. const byte pExpected[] = {
  51838. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  51839. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  51840. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  51841. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  51842. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  51843. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  51844. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  51845. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  51846. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  51847. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  51848. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  51849. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  51850. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  51851. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  51852. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  51853. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  51854. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  51855. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  51856. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  51857. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  51858. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  51859. 0x1E, 0x1A, 0x15, 0x97
  51860. };
  51861. const byte gExpected[] = {
  51862. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  51863. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  51864. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  51865. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  51866. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  51867. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  51868. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  51869. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  51870. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  51871. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  51872. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  51873. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  51874. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  51875. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  51876. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  51877. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  51878. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  51879. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  51880. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  51881. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  51882. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  51883. 0x6C, 0xC4, 0x16, 0x59
  51884. };
  51885. const byte qExpected[] = {
  51886. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  51887. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  51888. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  51889. };
  51890. int pSz;
  51891. int qSz;
  51892. int gSz;
  51893. byte* pReturned;
  51894. byte* qReturned;
  51895. byte* gReturned;
  51896. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  51897. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  51898. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  51899. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  51900. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  51901. AssertIntEQ(pSz, sizeof(pExpected));
  51902. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  51903. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  51904. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  51905. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  51906. AssertIntEQ(qSz, sizeof(qExpected));
  51907. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  51908. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  51909. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  51910. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  51911. AssertIntEQ(gSz, sizeof(gExpected));
  51912. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  51913. wolfSSL_DH_free(dh);
  51914. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51915. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51916. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51917. res = TEST_RES_CHECK(1);
  51918. #endif
  51919. return res;
  51920. }
  51921. static int test_wolfSSL_PEM_write_DHparams(void)
  51922. {
  51923. int res = TEST_SKIPPED;
  51924. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  51925. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  51926. DH* dh;
  51927. BIO* bio;
  51928. XFILE fp;
  51929. byte pem[2048];
  51930. int pemSz;
  51931. const char expected[] =
  51932. "-----BEGIN DH PARAMETERS-----\n"
  51933. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  51934. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  51935. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  51936. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  51937. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  51938. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  51939. "-----END DH PARAMETERS-----\n";
  51940. const char badPem[] =
  51941. "-----BEGIN DH PARAMETERS-----\n"
  51942. "-----END DH PARAMETERS-----\n";
  51943. const char emptySeqPem[] =
  51944. "-----BEGIN DH PARAMETERS-----\n"
  51945. "MAA=\n"
  51946. "-----END DH PARAMETERS-----\n";
  51947. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  51948. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51949. XFCLOSE(fp);
  51950. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  51951. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51952. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51953. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51954. (int)sizeof(badPem));
  51955. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51956. BIO_free(bio);
  51957. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51958. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51959. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51960. (int)sizeof(emptySeqPem));
  51961. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51962. BIO_free(bio);
  51963. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51964. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51965. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51966. BIO_free(bio);
  51967. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51968. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51969. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51970. DH_free(dh);
  51971. dh = wolfSSL_DH_new();
  51972. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51973. XFCLOSE(fp);
  51974. wolfSSL_DH_free(dh);
  51975. /* check results */
  51976. XMEMSET(pem, 0, sizeof(pem));
  51977. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  51978. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51979. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51980. XFCLOSE(fp);
  51981. res = TEST_RES_CHECK(1);
  51982. #endif
  51983. return res;
  51984. }
  51985. static int test_wolfSSL_d2i_DHparams(void)
  51986. {
  51987. int res = TEST_SKIPPED;
  51988. #ifdef OPENSSL_ALL
  51989. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51990. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51991. FILE* f = NULL;
  51992. unsigned char buf[4096];
  51993. const unsigned char* pt = buf;
  51994. #ifdef HAVE_FFDHE_2048
  51995. const char* params1 = "./certs/dh2048.der";
  51996. #endif
  51997. #ifdef HAVE_FFDHE_3072
  51998. const char* params2 = "./certs/dh3072.der";
  51999. #endif
  52000. long len = 0;
  52001. WOLFSSL_DH* dh = NULL;
  52002. XMEMSET(buf, 0, sizeof(buf));
  52003. /* Test 2048 bit parameters */
  52004. #ifdef HAVE_FFDHE_2048
  52005. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52006. f = XFOPEN(params1, "rb");
  52007. AssertTrue(f != XBADFILE);
  52008. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  52009. XFCLOSE(f);
  52010. /* Valid case */
  52011. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52012. AssertNotNull(dh->p);
  52013. AssertNotNull(dh->g);
  52014. AssertTrue(pt == buf);
  52015. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  52016. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  52017. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  52018. /* Invalid cases */
  52019. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52020. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52021. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  52022. DH_free(dh);
  52023. *buf = 0;
  52024. pt = buf;
  52025. res = TEST_RES_CHECK(1);
  52026. }
  52027. #endif /* HAVE_FFDHE_2048 */
  52028. /* Test 3072 bit parameters */
  52029. #ifdef HAVE_FFDHE_3072
  52030. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52031. f = XFOPEN(params2, "rb");
  52032. AssertTrue(f != XBADFILE);
  52033. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  52034. XFCLOSE(f);
  52035. /* Valid case */
  52036. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  52037. AssertNotNull(dh->p);
  52038. AssertNotNull(dh->g);
  52039. AssertTrue(pt != buf);
  52040. AssertIntEQ(DH_generate_key(dh), 1);
  52041. /* Invalid cases */
  52042. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52043. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52044. DH_free(dh);
  52045. res = TEST_RES_CHECK(1);
  52046. }
  52047. #endif /* HAVE_FFDHE_3072 */
  52048. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52049. #endif /* !NO_DH */
  52050. #endif
  52051. return res;
  52052. }
  52053. static int test_wolfSSL_DH_LoadDer(void)
  52054. {
  52055. int res = TEST_SKIPPED;
  52056. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  52057. defined(OPENSSL_EXTRA)
  52058. static const byte dh2048[] = {
  52059. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52060. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52061. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52062. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52063. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52064. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52065. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52066. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52067. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52068. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52069. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52070. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52071. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52072. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52073. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52074. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52075. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52076. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52077. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52078. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52079. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52080. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52081. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52082. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52083. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52084. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52085. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52086. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52087. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52088. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52089. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52090. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52091. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52092. 0x93, 0x02, 0x01, 0x02
  52093. };
  52094. WOLFSSL_DH* dh;
  52095. dh = wolfSSL_DH_new();
  52096. AssertNotNull(dh);
  52097. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  52098. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  52099. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  52100. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  52101. wolfSSL_DH_free(dh);
  52102. res = TEST_RES_CHECK(1);
  52103. #endif
  52104. return res;
  52105. }
  52106. static int test_wolfSSL_i2d_DHparams(void)
  52107. {
  52108. int res = TEST_SKIPPED;
  52109. #ifdef OPENSSL_ALL
  52110. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52111. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52112. FILE* f;
  52113. unsigned char buf[4096];
  52114. const unsigned char* pt;
  52115. unsigned char* pt2;
  52116. #ifdef HAVE_FFDHE_2048
  52117. const char* params1 = "./certs/dh2048.der";
  52118. #endif
  52119. #ifdef HAVE_FFDHE_3072
  52120. const char* params2 = "./certs/dh3072.der";
  52121. #endif
  52122. long len;
  52123. WOLFSSL_DH* dh;
  52124. /* Test 2048 bit parameters */
  52125. #ifdef HAVE_FFDHE_2048
  52126. pt = buf;
  52127. pt2 = buf;
  52128. f = XFOPEN(params1, "rb");
  52129. AssertTrue(f != XBADFILE);
  52130. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  52131. XFCLOSE(f);
  52132. /* Valid case */
  52133. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52134. AssertTrue(pt == buf);
  52135. AssertIntEQ(DH_generate_key(dh), 1);
  52136. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  52137. /* Invalid case */
  52138. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52139. /* Return length only */
  52140. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  52141. DH_free(dh);
  52142. *buf = 0;
  52143. #endif
  52144. /* Test 3072 bit parameters */
  52145. #ifdef HAVE_FFDHE_3072
  52146. pt = buf;
  52147. pt2 = buf;
  52148. f = XFOPEN(params2, "rb");
  52149. AssertTrue(f != XBADFILE);
  52150. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  52151. XFCLOSE(f);
  52152. /* Valid case */
  52153. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52154. AssertTrue(pt == buf);
  52155. AssertIntEQ(DH_generate_key(dh), 1);
  52156. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  52157. /* Invalid case */
  52158. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52159. /* Return length only */
  52160. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  52161. DH_free(dh);
  52162. #endif
  52163. dh = DH_new();
  52164. AssertNotNull(dh);
  52165. pt2 = buf;
  52166. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  52167. DH_free(dh);
  52168. res = TEST_RES_CHECK(1);
  52169. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52170. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  52171. #endif
  52172. return res;
  52173. }
  52174. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  52175. /*----------------------------------------------------------------------------*
  52176. | EC
  52177. *----------------------------------------------------------------------------*/
  52178. static int test_wolfSSL_EC_GROUP(void)
  52179. {
  52180. int res = TEST_SKIPPED;
  52181. #ifdef OPENSSL_EXTRA
  52182. EC_GROUP *group;
  52183. EC_GROUP *group2;
  52184. EC_GROUP *group3;
  52185. #ifndef HAVE_ECC_BRAINPOOL
  52186. EC_GROUP *group4;
  52187. #endif
  52188. WOLFSSL_BIGNUM* order;
  52189. int group_bits;
  52190. int i;
  52191. static const int knownEccNids[] = {
  52192. NID_X9_62_prime192v1,
  52193. NID_X9_62_prime192v2,
  52194. NID_X9_62_prime192v3,
  52195. NID_X9_62_prime239v1,
  52196. NID_X9_62_prime239v2,
  52197. NID_X9_62_prime239v3,
  52198. NID_X9_62_prime256v1,
  52199. NID_secp112r1,
  52200. NID_secp112r2,
  52201. NID_secp128r1,
  52202. NID_secp128r2,
  52203. NID_secp160r1,
  52204. NID_secp160r2,
  52205. NID_secp224r1,
  52206. NID_secp384r1,
  52207. NID_secp521r1,
  52208. NID_secp160k1,
  52209. NID_secp192k1,
  52210. NID_secp224k1,
  52211. NID_secp256k1,
  52212. NID_brainpoolP160r1,
  52213. NID_brainpoolP192r1,
  52214. NID_brainpoolP224r1,
  52215. NID_brainpoolP256r1,
  52216. NID_brainpoolP320r1,
  52217. NID_brainpoolP384r1,
  52218. NID_brainpoolP512r1,
  52219. };
  52220. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  52221. static const int knownEccEnums[] = {
  52222. ECC_SECP192R1,
  52223. ECC_PRIME192V2,
  52224. ECC_PRIME192V3,
  52225. ECC_PRIME239V1,
  52226. ECC_PRIME239V2,
  52227. ECC_PRIME239V3,
  52228. ECC_SECP256R1,
  52229. ECC_SECP112R1,
  52230. ECC_SECP112R2,
  52231. ECC_SECP128R1,
  52232. ECC_SECP128R2,
  52233. ECC_SECP160R1,
  52234. ECC_SECP160R2,
  52235. ECC_SECP224R1,
  52236. ECC_SECP384R1,
  52237. ECC_SECP521R1,
  52238. ECC_SECP160K1,
  52239. ECC_SECP192K1,
  52240. ECC_SECP224K1,
  52241. ECC_SECP256K1,
  52242. ECC_BRAINPOOLP160R1,
  52243. ECC_BRAINPOOLP192R1,
  52244. ECC_BRAINPOOLP224R1,
  52245. ECC_BRAINPOOLP256R1,
  52246. ECC_BRAINPOOLP320R1,
  52247. ECC_BRAINPOOLP384R1,
  52248. ECC_BRAINPOOLP512R1,
  52249. };
  52250. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  52251. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52252. AssertNotNull(group2 = EC_GROUP_dup(group));
  52253. AssertNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  52254. #ifndef HAVE_ECC_BRAINPOOL
  52255. AssertNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  52256. NID_brainpoolP256r1));
  52257. #endif
  52258. AssertNull(EC_GROUP_dup(NULL));
  52259. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  52260. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52261. AssertIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  52262. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  52263. #ifndef HAVE_ECC_BRAINPOOL
  52264. AssertIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  52265. #endif
  52266. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  52267. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  52268. AssertNotNull(order = BN_new());
  52269. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  52270. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  52271. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  52272. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  52273. wolfSSL_BN_free(order);
  52274. AssertNotNull(EC_GROUP_method_of(group));
  52275. AssertIntEQ(EC_METHOD_get_field_type(NULL), 0);
  52276. AssertIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  52277. NID_X9_62_prime_field);
  52278. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  52279. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  52280. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  52281. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  52282. #ifndef NO_WOLFSSL_STUB
  52283. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  52284. #endif
  52285. #ifndef HAVE_ECC_BRAINPOOL
  52286. EC_GROUP_free(group4);
  52287. #endif
  52288. EC_GROUP_free(group3);
  52289. EC_GROUP_free(group2);
  52290. EC_GROUP_free(group);
  52291. for (i = 0; i < knowEccNidsLen; i++) {
  52292. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  52293. AssertIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  52294. EC_GROUP_free(group);
  52295. }
  52296. for (i = 0; i < knowEccEnumsLen; i++) {
  52297. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  52298. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  52299. EC_GROUP_free(group);
  52300. }
  52301. res = TEST_RES_CHECK(1);
  52302. #endif
  52303. return res;
  52304. }
  52305. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  52306. {
  52307. int res = TEST_SKIPPED;
  52308. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52309. EC_GROUP *group;
  52310. BIO* bio;
  52311. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52312. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52313. EC_GROUP *ret;
  52314. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  52315. "BgUrgQQAIg==\n"
  52316. "-----END EC PARAMETERS-----";
  52317. #endif
  52318. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  52319. "MAA=\n"
  52320. "-----END EC PARAMETERS-----";
  52321. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  52322. "BgA=\n"
  52323. "-----END EC PARAMETERS-----";
  52324. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  52325. "BgE=\n"
  52326. "-----END EC PARAMETERS-----";
  52327. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  52328. "BgE*\n"
  52329. "-----END EC PARAMETERS-----";
  52330. /* Test that first parameter, bio, being NULL fails. */
  52331. AssertNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  52332. /* Test that reading named parameters works. */
  52333. AssertNotNull(bio = BIO_new(BIO_s_file()));
  52334. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  52335. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52336. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52337. BIO_free(bio);
  52338. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52339. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52340. /* Test that reusing group works. */
  52341. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52342. sizeof(ec_nc_p384)));
  52343. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52344. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52345. BIO_free(bio);
  52346. EC_GROUP_free(group);
  52347. group = NULL;
  52348. /* Test that returning through group works. */
  52349. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52350. sizeof(ec_nc_p384)));
  52351. AssertNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52352. AssertIntEQ(group == ret, 1);
  52353. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52354. BIO_free(bio);
  52355. #endif
  52356. EC_GROUP_free(group);
  52357. /* Test 0x30, 0x00 (not and object id) fails. */
  52358. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  52359. sizeof(ec_nc_bad_1)));
  52360. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52361. BIO_free(bio);
  52362. /* Test 0x06, 0x00 (empty object id) fails. */
  52363. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  52364. sizeof(ec_nc_bad_2)));
  52365. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52366. BIO_free(bio);
  52367. /* Test 0x06, 0x01 (badly formed object id) fails. */
  52368. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  52369. sizeof(ec_nc_bad_3)));
  52370. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52371. BIO_free(bio);
  52372. /* Test invalid PEM encoding - invalid character. */
  52373. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  52374. sizeof(ec_nc_bad_4)));
  52375. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52376. BIO_free(bio);
  52377. res = TEST_RES_CHECK(1);
  52378. #endif
  52379. return res;
  52380. }
  52381. static int test_wolfSSL_EC_POINT(void)
  52382. {
  52383. int res = TEST_SKIPPED;
  52384. #if !defined(WOLFSSL_SP_MATH) && \
  52385. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  52386. #ifdef OPENSSL_EXTRA
  52387. BN_CTX* ctx;
  52388. EC_GROUP* group;
  52389. #ifndef HAVE_ECC_BRAINPOOL
  52390. EC_GROUP* group2;
  52391. #endif
  52392. EC_POINT* Gxy;
  52393. EC_POINT* new_point;
  52394. EC_POINT* set_point;
  52395. EC_POINT* infinity;
  52396. BIGNUM* k = NULL;
  52397. BIGNUM* Gx = NULL;
  52398. BIGNUM* Gy = NULL;
  52399. BIGNUM* Gz = NULL;
  52400. BIGNUM* X;
  52401. BIGNUM* Y;
  52402. BIGNUM* set_point_bn;
  52403. char* hexStr;
  52404. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  52405. "17AF381913FF7A96314EA47055EA0FD0";
  52406. /* NISTP256R1 Gx/Gy */
  52407. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  52408. "77037D812DEB33A0F4A13945D898C296";
  52409. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52410. "2BCE33576B315ECECBB6406837BF51F5";
  52411. #ifndef HAVE_SELFTEST
  52412. EC_POINT *tmp;
  52413. size_t bin_len;
  52414. unsigned int blen;
  52415. unsigned char* buf = NULL;
  52416. unsigned char bufInf[1] = { 0x00 };
  52417. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  52418. "77037D812DEB33A0F4A13945D898C296"
  52419. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52420. "2BCE33576B315ECECBB6406837BF51F5";
  52421. const unsigned char binUncompG[] = {
  52422. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52423. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52424. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52425. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52426. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52427. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52428. };
  52429. const unsigned char binUncompGBad[] = {
  52430. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52431. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52432. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52433. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52434. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52435. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52436. };
  52437. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  52438. "77037D812DEB33A0F4A13945D898C296";
  52439. #ifdef HAVE_COMP_KEY
  52440. const unsigned char binCompG[] = {
  52441. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52442. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52443. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52444. };
  52445. #endif
  52446. #endif
  52447. AssertNotNull(ctx = BN_CTX_new());
  52448. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52449. #ifndef HAVE_ECC_BRAINPOOL
  52450. /* Used to make groups curve_idx == -1. */
  52451. AssertNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  52452. #endif
  52453. AssertNull(EC_POINT_new(NULL));
  52454. AssertNotNull(Gxy = EC_POINT_new(group));
  52455. AssertNotNull(new_point = EC_POINT_new(group));
  52456. AssertNotNull(set_point = EC_POINT_new(group));
  52457. AssertNotNull(X = BN_new());
  52458. AssertNotNull(Y = BN_new());
  52459. AssertNotNull(set_point_bn = BN_new());
  52460. AssertNotNull(infinity = EC_POINT_new(group));
  52461. /* load test values */
  52462. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  52463. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  52464. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  52465. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  52466. /* populate coordinates for input point */
  52467. Gxy->X = Gx;
  52468. Gxy->Y = Gy;
  52469. Gxy->Z = Gz;
  52470. /* Test handling of NULL point. */
  52471. EC_POINT_clear_free(NULL);
  52472. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52473. NULL, NULL, ctx), 0);
  52474. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52475. NULL, NULL, ctx), 0);
  52476. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52477. NULL, NULL, ctx), 0);
  52478. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52479. X, NULL, ctx), 0);
  52480. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52481. NULL, Y, ctx), 0);
  52482. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52483. X, Y, ctx), 0);
  52484. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52485. X, Y, ctx), 0);
  52486. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52487. NULL, Y, ctx), 0);
  52488. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52489. X, NULL, ctx), 0);
  52490. /* Getting point at infinity returns an error. */
  52491. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  52492. X, Y, ctx), 0);
  52493. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  52494. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  52495. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52496. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  52497. AssertIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  52498. AssertIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  52499. AssertIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  52500. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  52501. AssertIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  52502. AssertIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  52503. AssertIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  52504. AssertIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  52505. AssertIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  52506. AssertIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  52507. AssertIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  52508. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  52509. /* perform point multiplication */
  52510. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  52511. AssertIntEQ(BN_is_zero(new_point->X), 0);
  52512. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  52513. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  52514. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  52515. AssertIntEQ(BN_is_zero(new_point->X), 0);
  52516. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  52517. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  52518. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  52519. AssertIntEQ(BN_is_zero(new_point->X), 0);
  52520. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  52521. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  52522. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  52523. AssertIntEQ(BN_is_zero(new_point->X), 1);
  52524. AssertIntEQ(BN_is_zero(new_point->Y), 1);
  52525. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  52526. /* Set point to something. */
  52527. AssertIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  52528. #else
  52529. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  52530. ctx), 1);
  52531. AssertIntEQ(BN_is_zero(new_point->X), 0);
  52532. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  52533. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  52534. #endif
  52535. /* check if point X coordinate is zero */
  52536. AssertIntEQ(BN_is_zero(new_point->X), 0);
  52537. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  52538. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  52539. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  52540. #endif
  52541. /* extract the coordinates from point */
  52542. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52543. ctx), WOLFSSL_SUCCESS);
  52544. /* check if point X coordinate is zero */
  52545. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  52546. /* set the same X and Y points in another object */
  52547. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  52548. ctx), WOLFSSL_SUCCESS);
  52549. /* compare points as they should be the same */
  52550. AssertIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  52551. AssertIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  52552. AssertIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  52553. AssertIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  52554. AssertIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  52555. AssertIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  52556. AssertIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  52557. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  52558. /* Test copying */
  52559. AssertIntEQ(EC_POINT_copy(NULL, NULL), 0);
  52560. AssertIntEQ(EC_POINT_copy(NULL, set_point), 0);
  52561. AssertIntEQ(EC_POINT_copy(new_point, NULL), 0);
  52562. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  52563. /* Test inverting */
  52564. AssertIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  52565. AssertIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  52566. AssertIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  52567. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  52568. /* Test getting affine converts from projective. */
  52569. AssertIntEQ(EC_POINT_copy(set_point, new_point), 1);
  52570. /* Force non-affine coordinates */
  52571. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52572. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  52573. new_point->inSet = 0;
  52574. /* extract the coordinates from point */
  52575. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52576. ctx), WOLFSSL_SUCCESS);
  52577. /* check if point ordinates have changed. */
  52578. AssertIntNE(BN_cmp(X, set_point->X), 0);
  52579. AssertIntNE(BN_cmp(Y, set_point->Y), 0);
  52580. /* Test check for infinity */
  52581. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  52582. AssertIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  52583. AssertIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  52584. AssertIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  52585. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  52586. AssertIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  52587. #else
  52588. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  52589. #endif
  52590. AssertPtrEq(EC_POINT_point2bn(group, set_point,
  52591. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  52592. /* check bn2hex */
  52593. hexStr = BN_bn2hex(k);
  52594. AssertStrEQ(hexStr, kTest);
  52595. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52596. BN_print_fp(stderr, k);
  52597. fprintf(stderr, "\n");
  52598. #endif
  52599. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52600. hexStr = BN_bn2hex(Gx);
  52601. AssertStrEQ(hexStr, kGx);
  52602. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52603. BN_print_fp(stderr, Gx);
  52604. fprintf(stderr, "\n");
  52605. #endif
  52606. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52607. hexStr = BN_bn2hex(Gy);
  52608. AssertStrEQ(hexStr, kGy);
  52609. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52610. BN_print_fp(stderr, Gy);
  52611. fprintf(stderr, "\n");
  52612. #endif
  52613. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52614. #ifndef HAVE_SELFTEST
  52615. /* Test point to hex */
  52616. AssertNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52617. ctx));
  52618. AssertNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52619. ctx));
  52620. AssertNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52621. ctx));
  52622. #ifndef HAVE_ECC_BRAINPOOL
  52623. /* Group not supported in wolfCrypt. */
  52624. AssertNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52625. ctx));
  52626. #endif
  52627. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  52628. AssertStrEQ(hexStr, uncompG);
  52629. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52630. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  52631. AssertStrEQ(hexStr, compG);
  52632. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52633. /* Test point to oct */
  52634. AssertIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52635. NULL, 0, ctx), 0);
  52636. AssertIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52637. NULL, 0, ctx), 0);
  52638. AssertIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52639. NULL, 0, ctx), 0);
  52640. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52641. NULL, 0, ctx);
  52642. AssertIntEQ(bin_len, sizeof(binUncompG));
  52643. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52644. DYNAMIC_TYPE_ECC));
  52645. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52646. buf, bin_len, ctx), bin_len);
  52647. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52648. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52649. /* Infinity (x=0, y=0) encodes as '0x00'. */
  52650. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  52651. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  52652. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  52653. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  52654. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  52655. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  52656. AssertIntEQ(bufInf[0], 0);
  52657. wolfSSL_EC_POINT_dump(NULL, NULL);
  52658. /* Test point i2d */
  52659. AssertIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  52660. AssertIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  52661. AssertIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  52662. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  52663. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  52664. AssertIntEQ(blen, sizeof(binUncompG));
  52665. AssertNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  52666. blen -= 1;
  52667. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  52668. blen += 1;
  52669. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  52670. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52671. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52672. #ifdef HAVE_COMP_KEY
  52673. /* Test point to oct compressed */
  52674. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  52675. 0, ctx);
  52676. AssertIntEQ(bin_len, sizeof(binCompG));
  52677. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52678. DYNAMIC_TYPE_ECC));
  52679. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  52680. bin_len, ctx), bin_len);
  52681. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  52682. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52683. #endif
  52684. /* Test point BN */
  52685. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  52686. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52687. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  52688. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52689. AssertNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  52690. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52691. AssertNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  52692. /* Test oct to point */
  52693. AssertNotNull(tmp = EC_POINT_new(group));
  52694. AssertIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  52695. ctx), 0);
  52696. AssertIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  52697. ctx), 0);
  52698. AssertIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  52699. ctx), 0);
  52700. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  52701. sizeof(binUncompGBad), ctx), 0);
  52702. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  52703. ctx), 1);
  52704. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52705. EC_POINT_free(tmp);
  52706. /* Test setting BN ordinates. */
  52707. AssertNotNull(tmp = EC_POINT_new(group));
  52708. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52709. NULL, ctx), 0);
  52710. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  52711. NULL, ctx), 0);
  52712. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  52713. NULL, ctx), 0);
  52714. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  52715. NULL, ctx), 0);
  52716. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52717. Gy, ctx), 0);
  52718. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  52719. ctx), 0);
  52720. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  52721. ctx), 0);
  52722. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  52723. Gy, ctx), 0);
  52724. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  52725. NULL, ctx), 0);
  52726. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  52727. ctx), 1);
  52728. EC_POINT_free(tmp);
  52729. /* Test point d2i */
  52730. AssertNotNull(tmp = EC_POINT_new(group));
  52731. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  52732. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  52733. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  52734. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  52735. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  52736. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  52737. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  52738. AssertIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  52739. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  52740. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52741. EC_POINT_free(tmp);
  52742. #ifdef HAVE_COMP_KEY
  52743. /* Test oct compressed to point */
  52744. AssertNotNull(tmp = EC_POINT_new(group));
  52745. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  52746. 1);
  52747. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52748. EC_POINT_free(tmp);
  52749. /* Test point d2i - compressed */
  52750. AssertNotNull(tmp = EC_POINT_new(group));
  52751. AssertIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  52752. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52753. EC_POINT_free(tmp);
  52754. #endif
  52755. #endif
  52756. /* test BN_mod_add */
  52757. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52758. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  52759. 1);
  52760. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  52761. /* cleanup */
  52762. BN_free(X);
  52763. BN_free(Y);
  52764. BN_free(k);
  52765. BN_free(set_point_bn);
  52766. EC_POINT_free(infinity);
  52767. EC_POINT_free(new_point);
  52768. EC_POINT_free(set_point);
  52769. EC_POINT_clear_free(Gxy);
  52770. #ifndef HAVE_ECC_BRAINPOOL
  52771. EC_GROUP_free(group2);
  52772. #endif
  52773. EC_GROUP_free(group);
  52774. BN_CTX_free(ctx);
  52775. res = TEST_RES_CHECK(1);
  52776. #endif
  52777. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  52778. return res;
  52779. }
  52780. static int test_wolfSSL_EC_KEY_generate(void)
  52781. {
  52782. int res = TEST_SKIPPED;
  52783. #ifdef OPENSSL_EXTRA
  52784. WOLFSSL_EC_KEY* key;
  52785. #ifndef HAVE_ECC_BRAINPOOL
  52786. WOLFSSL_EC_GROUP* group;
  52787. #endif
  52788. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52789. AssertIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  52790. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52791. wolfSSL_EC_KEY_free(key);
  52792. #ifndef HAVE_ECC_BRAINPOOL
  52793. AssertNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  52794. NID_brainpoolP256r1));
  52795. AssertNotNull(key = wolfSSL_EC_KEY_new());
  52796. AssertIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  52797. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  52798. wolfSSL_EC_KEY_free(key);
  52799. wolfSSL_EC_GROUP_free(group);
  52800. #endif
  52801. res = TEST_RES_CHECK(1);
  52802. #endif
  52803. return res;
  52804. }
  52805. static int test_EC_i2d(void)
  52806. {
  52807. int res = TEST_SKIPPED;
  52808. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  52809. EC_KEY *key;
  52810. EC_KEY *copy = NULL;
  52811. int len;
  52812. unsigned char *buf = NULL;
  52813. unsigned char *p;
  52814. const unsigned char *tmp = NULL;
  52815. const unsigned char octBad[] = {
  52816. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52817. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52818. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52819. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52820. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52821. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52822. };
  52823. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52824. AssertIntEQ(EC_KEY_generate_key(key), 1);
  52825. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  52826. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52827. DYNAMIC_TYPE_TMP_BUFFER));
  52828. p = buf;
  52829. AssertIntEQ(i2d_EC_PUBKEY(key, &p), len);
  52830. AssertNull(o2i_ECPublicKey(NULL, NULL, -1));
  52831. AssertNull(o2i_ECPublicKey(&copy, NULL, -1));
  52832. AssertNull(o2i_ECPublicKey(&key, NULL, -1));
  52833. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52834. AssertNull(o2i_ECPublicKey(NULL, NULL, 0));
  52835. AssertNull(o2i_ECPublicKey(&key, NULL, 0));
  52836. AssertNull(o2i_ECPublicKey(&key, &tmp, 0));
  52837. tmp = buf;
  52838. AssertNull(o2i_ECPublicKey(NULL, &tmp, 0));
  52839. AssertNull(o2i_ECPublicKey(&copy, &tmp, 0));
  52840. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52841. AssertNull(o2i_ECPublicKey(&key, &tmp, -1));
  52842. AssertIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  52843. AssertIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  52844. tmp = buf;
  52845. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  52846. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  52847. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  52848. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  52849. AssertNull(d2i_ECPrivateKey(&key, &tmp, 0));
  52850. AssertIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  52851. AssertIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  52852. AssertIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  52853. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  52854. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  52855. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  52856. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  52857. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  52858. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52859. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  52860. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52861. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  52862. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52863. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  52864. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52865. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  52866. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  52867. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52868. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52869. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52870. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52871. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52872. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52873. buf = NULL;
  52874. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  52875. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52876. DYNAMIC_TYPE_TMP_BUFFER));
  52877. p = buf;
  52878. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  52879. p = NULL;
  52880. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  52881. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52882. /* Bad point is also an invalid private key. */
  52883. tmp = octBad;
  52884. AssertNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  52885. tmp = buf;
  52886. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  52887. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52888. buf = NULL;
  52889. AssertIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  52890. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52891. DYNAMIC_TYPE_TMP_BUFFER));
  52892. p = buf;
  52893. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52894. p = NULL;
  52895. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52896. tmp = buf;
  52897. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  52898. tmp = octBad;
  52899. AssertNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  52900. AssertIntEQ(EC_KEY_check_key(NULL), 0);
  52901. AssertIntEQ(EC_KEY_check_key(key), 1);
  52902. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  52903. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  52904. EC_KEY_free(key);
  52905. EC_KEY_free(copy);
  52906. res = TEST_RES_CHECK(1);
  52907. #endif
  52908. return res;
  52909. }
  52910. static int test_wolfSSL_EC_curve(void)
  52911. {
  52912. int res = TEST_SKIPPED;
  52913. #if defined(OPENSSL_EXTRA)
  52914. int nid = NID_secp160k1;
  52915. const char* nid_name;
  52916. AssertNull(EC_curve_nid2nist(NID_sha256));
  52917. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  52918. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  52919. AssertIntEQ(EC_curve_nist2nid("INVALID"), 0);
  52920. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  52921. res = TEST_RES_CHECK(1);
  52922. #endif
  52923. return res;
  52924. }
  52925. static int test_wolfSSL_EC_KEY_dup(void)
  52926. {
  52927. int res = TEST_SKIPPED;
  52928. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  52929. WOLFSSL_EC_KEY* ecKey;
  52930. WOLFSSL_EC_KEY* dupKey;
  52931. ecc_key* srcKey;
  52932. ecc_key* destKey;
  52933. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52934. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52935. /* Valid cases */
  52936. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52937. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  52938. /* Compare pubkey */
  52939. srcKey = (ecc_key*)ecKey->internal;
  52940. destKey = (ecc_key*)dupKey->internal;
  52941. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  52942. /* compare EC_GROUP */
  52943. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  52944. /* compare EC_POINT */
  52945. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  52946. dupKey->pub_key, NULL), MP_EQ);
  52947. /* compare BIGNUM */
  52948. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  52949. wolfSSL_EC_KEY_free(dupKey);
  52950. /* Invalid cases */
  52951. /* NULL key */
  52952. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52953. /* NULL ecc_key */
  52954. wc_ecc_free((ecc_key*)ecKey->internal);
  52955. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52956. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52957. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52958. wolfSSL_EC_KEY_free(ecKey);
  52959. wolfSSL_EC_KEY_free(dupKey);
  52960. /* NULL Group */
  52961. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52962. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52963. wolfSSL_EC_GROUP_free(ecKey->group);
  52964. ecKey->group = NULL; /* Set group to NULL */
  52965. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52966. wolfSSL_EC_KEY_free(ecKey);
  52967. wolfSSL_EC_KEY_free(dupKey);
  52968. /* NULL public key */
  52969. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52970. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52971. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52972. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52973. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52974. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52975. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52976. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52977. wolfSSL_EC_KEY_free(ecKey);
  52978. wolfSSL_EC_KEY_free(dupKey);
  52979. /* NULL private key */
  52980. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52981. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52982. wolfSSL_BN_free(ecKey->priv_key);
  52983. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52984. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52985. wolfSSL_EC_KEY_free(ecKey);
  52986. wolfSSL_EC_KEY_free(dupKey);
  52987. /* Test EC_KEY_up_ref */
  52988. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52989. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52990. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52991. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52992. /* reference count doesn't follow duplicate */
  52993. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52994. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52995. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52996. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52997. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52998. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52999. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  53000. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  53001. res = TEST_RES_CHECK(1);
  53002. #endif
  53003. return res;
  53004. }
  53005. static int test_wolfSSL_EC_KEY_set_group(void)
  53006. {
  53007. int res = TEST_SKIPPED;
  53008. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53009. defined(OPENSSL_EXTRA)
  53010. EC_KEY *key = NULL;
  53011. EC_GROUP *group = NULL;
  53012. const EC_GROUP *group2 = NULL;
  53013. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53014. AssertNotNull(key = EC_KEY_new());
  53015. AssertNull(EC_KEY_get0_group(NULL));
  53016. AssertIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  53017. AssertIntEQ(EC_KEY_set_group(key, NULL), 0);
  53018. AssertIntEQ(EC_KEY_set_group(NULL, group), 0);
  53019. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  53020. AssertNotNull(group2 = EC_KEY_get0_group(key));
  53021. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  53022. EC_GROUP_free(group);
  53023. EC_KEY_free(key);
  53024. res = TEST_RES_CHECK(1);
  53025. #endif
  53026. return res;
  53027. }
  53028. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  53029. {
  53030. int res = TEST_SKIPPED;
  53031. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53032. BIO* bio;
  53033. EC_KEY* key;
  53034. /* Error condition: NULL key. */
  53035. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  53036. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  53037. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  53038. /* Conversion form defaults to uncompressed. */
  53039. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53040. #ifdef HAVE_COMP_KEY
  53041. /* Explicitly set to compressed. */
  53042. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53043. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  53044. #else
  53045. /* Will still work just won't change anything. */
  53046. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53047. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53048. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  53049. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53050. #endif
  53051. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  53052. BIO_free(bio);
  53053. EC_KEY_free(key);
  53054. res = TEST_RES_CHECK(1);
  53055. #endif
  53056. return res;
  53057. }
  53058. static int test_wolfSSL_EC_KEY_private_key(void)
  53059. {
  53060. int res = TEST_SKIPPED;
  53061. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53062. WOLFSSL_EC_KEY* key;
  53063. WOLFSSL_BIGNUM* priv = NULL;
  53064. WOLFSSL_BIGNUM* priv2 = NULL;
  53065. WOLFSSL_BIGNUM* bn;
  53066. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53067. AssertNotNull(priv = wolfSSL_BN_new());
  53068. AssertNotNull(priv2 = wolfSSL_BN_new());
  53069. AssertIntNE(BN_set_word(priv, 2), 0);
  53070. AssertIntNE(BN_set_word(priv2, 2), 0);
  53071. AssertNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  53072. /* No private key set. */
  53073. AssertNull(wolfSSL_EC_KEY_get0_private_key(key));
  53074. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  53075. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  53076. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  53077. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  53078. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53079. AssertPtrNE(bn, priv);
  53080. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  53081. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53082. AssertPtrNE(bn, priv2);
  53083. wolfSSL_BN_free(priv2);
  53084. wolfSSL_BN_free(priv);
  53085. wolfSSL_EC_KEY_free(key);
  53086. res = TEST_RES_CHECK(1);
  53087. #endif
  53088. return res;
  53089. }
  53090. static int test_wolfSSL_EC_KEY_public_key(void)
  53091. {
  53092. int res = TEST_SKIPPED;
  53093. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53094. WOLFSSL_EC_KEY* key;
  53095. WOLFSSL_EC_POINT* pub;
  53096. WOLFSSL_EC_POINT* point;
  53097. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53098. AssertNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  53099. AssertNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  53100. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53101. AssertNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  53102. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  53103. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  53104. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  53105. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  53106. AssertNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  53107. AssertPtrEq(point, pub);
  53108. wolfSSL_EC_KEY_free(key);
  53109. res = TEST_RES_CHECK(1);
  53110. #endif
  53111. return res;
  53112. }
  53113. static int test_wolfSSL_EC_KEY_print_fp(void)
  53114. {
  53115. int res = TEST_SKIPPED;
  53116. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  53117. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  53118. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  53119. !defined(NO_STDIO_FILESYSTEM)
  53120. EC_KEY* key = NULL;
  53121. /* Bad file pointer. */
  53122. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  53123. /* NULL key. */
  53124. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  53125. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  53126. /* Negative indent. */
  53127. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  53128. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53129. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53130. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53131. wolfSSL_EC_KEY_free(key);
  53132. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  53133. NID_X9_62_prime256v1)));
  53134. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53135. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53136. wolfSSL_EC_KEY_free(key);
  53137. res = TEST_RES_CHECK(1);
  53138. #endif
  53139. return res;
  53140. }
  53141. static int test_wolfSSL_EC_get_builtin_curves(void)
  53142. {
  53143. int res = TEST_SKIPPED;
  53144. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  53145. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53146. EC_builtin_curve* curves = NULL;
  53147. size_t crv_len = 0;
  53148. size_t i = 0;
  53149. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  53150. AssertNotNull(curves = (EC_builtin_curve*)XMALLOC(
  53151. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  53152. AssertIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  53153. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  53154. for (i = 0; i < crv_len; i++) {
  53155. if (curves[i].comment != NULL) {
  53156. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  53157. }
  53158. }
  53159. if (crv_len > 1) {
  53160. AssertIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  53161. }
  53162. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53163. res = TEST_RES_CHECK(1);
  53164. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53165. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  53166. return res;
  53167. }
  53168. static int test_wolfSSL_ECDSA_SIG(void)
  53169. {
  53170. int res = TEST_SKIPPED;
  53171. #ifdef OPENSSL_EXTRA
  53172. WOLFSSL_ECDSA_SIG* sig = NULL;
  53173. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  53174. WOLFSSL_BIGNUM* r;
  53175. WOLFSSL_BIGNUM* s;
  53176. const WOLFSSL_BIGNUM* r2;
  53177. const WOLFSSL_BIGNUM* s2;
  53178. const unsigned char* cp;
  53179. unsigned char* p;
  53180. unsigned char outSig[8];
  53181. unsigned char sigData[8] =
  53182. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53183. unsigned char sigDataBad[8] =
  53184. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53185. wolfSSL_ECDSA_SIG_free(NULL);
  53186. AssertNotNull(sig = wolfSSL_ECDSA_SIG_new());
  53187. AssertNotNull(r = wolfSSL_BN_new());
  53188. AssertNotNull(s = wolfSSL_BN_new());
  53189. AssertIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  53190. AssertIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  53191. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  53192. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  53193. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  53194. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53195. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  53196. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  53197. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  53198. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  53199. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  53200. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  53201. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  53202. r2 = NULL;
  53203. s2 = NULL;
  53204. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53205. AssertNull(r2);
  53206. AssertNull(s2);
  53207. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  53208. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  53209. AssertPtrEq(r2, r);
  53210. AssertPtrEq(s2, s);
  53211. r2 = NULL;
  53212. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  53213. AssertPtrEq(r2, r);
  53214. s2 = NULL;
  53215. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  53216. AssertPtrEq(s2, s);
  53217. /* r and s are freed when sig is freed. */
  53218. wolfSSL_ECDSA_SIG_free(sig);
  53219. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  53220. cp = sigDataBad;
  53221. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  53222. cp = sigData;
  53223. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  53224. AssertIntEQ((cp == sigData + 8), 1);
  53225. cp = sigData;
  53226. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  53227. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  53228. AssertIntEQ((sig == sig2), 1);
  53229. cp = outSig;
  53230. p = outSig;
  53231. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  53232. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  53233. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  53234. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  53235. AssertIntEQ((p == outSig + 8), 1);
  53236. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  53237. wolfSSL_ECDSA_SIG_free(sig);
  53238. res = TEST_RES_CHECK(1);
  53239. #endif
  53240. return res;
  53241. }
  53242. static int test_ECDSA_size_sign(void)
  53243. {
  53244. int res = TEST_SKIPPED;
  53245. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  53246. EC_KEY* key;
  53247. ECDSA_SIG* ecdsaSig;
  53248. int id;
  53249. byte hash[WC_MAX_DIGEST_SIZE];
  53250. byte hash2[WC_MAX_DIGEST_SIZE];
  53251. byte sig[ECC_MAX_SIG_SIZE];
  53252. unsigned int sigSz = sizeof(sig);
  53253. XMEMSET(hash, 123, sizeof(hash));
  53254. XMEMSET(hash2, 234, sizeof(hash2));
  53255. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  53256. AssertIntEQ(id, ECC_SECP256R1);
  53257. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53258. AssertIntEQ(EC_KEY_generate_key(key), 1);
  53259. AssertIntGE(ECDSA_size(NULL), 0);
  53260. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  53261. AssertIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  53262. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  53263. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  53264. AssertIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  53265. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  53266. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  53267. AssertIntGE(ECDSA_size(key), sigSz);
  53268. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  53269. AssertIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  53270. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  53271. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  53272. AssertNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  53273. AssertNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  53274. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  53275. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  53276. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  53277. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  53278. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  53279. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  53280. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  53281. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  53282. AssertIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  53283. ECDSA_SIG_free(ecdsaSig);
  53284. EC_KEY_free(key);
  53285. res = TEST_RES_CHECK(1);
  53286. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  53287. return res;
  53288. }
  53289. static int test_ECDH_compute_key(void)
  53290. {
  53291. int res = TEST_SKIPPED;
  53292. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53293. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53294. EC_KEY* key1;
  53295. EC_KEY* key2;
  53296. EC_POINT* pub1;
  53297. EC_POINT* pub2;
  53298. byte secret1[32];
  53299. byte secret2[32];
  53300. int i;
  53301. AssertNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53302. AssertIntEQ(EC_KEY_generate_key(key1), 1);
  53303. AssertNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  53304. AssertNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53305. AssertIntEQ(EC_KEY_generate_key(key2), 1);
  53306. AssertNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  53307. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  53308. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  53309. 0);
  53310. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  53311. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  53312. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  53313. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  53314. 0);
  53315. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  53316. 0);
  53317. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  53318. NULL), 0);
  53319. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  53320. sizeof(secret1));
  53321. AssertIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  53322. sizeof(secret2));
  53323. for (i = 0; i < (int)sizeof(secret1); i++) {
  53324. AssertIntEQ(secret1[i], secret2[i]);
  53325. }
  53326. EC_KEY_free(key2);
  53327. EC_KEY_free(key1);
  53328. res = TEST_RES_CHECK(1);
  53329. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  53330. * !WOLF_CRYPTO_CB_ONLY_ECC */
  53331. return res;
  53332. }
  53333. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  53334. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  53335. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53336. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  53337. {
  53338. X509* x509 = NULL;
  53339. BIGNUM* serial_number = NULL;
  53340. X509_NAME* name = NULL;
  53341. time_t epoch_off = 0;
  53342. ASN1_INTEGER* asn1_serial_number;
  53343. long not_before, not_after;
  53344. AssertNotNull(x509 = X509_new());
  53345. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  53346. AssertNotNull(serial_number = BN_new());
  53347. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  53348. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  53349. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  53350. /* version 3 */
  53351. AssertIntNE(X509_set_version(x509, 2L), 0);
  53352. AssertNotNull(name = X509_NAME_new());
  53353. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  53354. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  53355. AssertIntNE(X509_set_subject_name(x509, name), 0);
  53356. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  53357. not_before = (long)wc_Time(NULL);
  53358. not_after = not_before + (365 * 24 * 60 * 60);
  53359. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  53360. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  53361. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  53362. BN_free(serial_number);
  53363. X509_NAME_free(name);
  53364. X509_free(x509);
  53365. return 0;
  53366. }
  53367. #endif
  53368. static int test_openssl_generate_key_and_cert(void)
  53369. {
  53370. int res = TEST_SKIPPED;
  53371. #if defined(OPENSSL_EXTRA)
  53372. #if !defined(NO_RSA)
  53373. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  53374. EVP_PKEY* pkey = EVP_PKEY_new();
  53375. int key_length = 2048;
  53376. BIGNUM* exponent = BN_new();
  53377. RSA* rsa = RSA_new();
  53378. AssertNotNull(pkey);
  53379. AssertNotNull(exponent);
  53380. AssertNotNull(rsa);
  53381. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  53382. #ifndef WOLFSSL_KEY_GEN
  53383. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53384. #if defined(USE_CERT_BUFFERS_1024)
  53385. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  53386. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53387. key_length = 1024;
  53388. #elif defined(USE_CERT_BUFFERS_2048)
  53389. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  53390. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53391. #else
  53392. RSA_free(rsa);
  53393. rsa = NULL;
  53394. #endif
  53395. #else
  53396. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  53397. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  53398. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  53399. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53400. #endif
  53401. if (rsa) {
  53402. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  53403. BN_free(exponent);
  53404. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53405. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53406. test_openssl_make_self_signed_certificate(pkey);
  53407. #endif
  53408. }
  53409. EVP_PKEY_free(pkey);
  53410. res = TEST_RES_CHECK(1);
  53411. }
  53412. #endif /* !NO_RSA */
  53413. #ifdef HAVE_ECC
  53414. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  53415. EVP_PKEY* pkey = EVP_PKEY_new();
  53416. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  53417. AssertNotNull(pkey);
  53418. AssertNotNull(ec_key);
  53419. #ifndef NO_WOLFSSL_STUB
  53420. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  53421. #endif
  53422. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  53423. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  53424. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53425. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53426. test_openssl_make_self_signed_certificate(pkey);
  53427. #endif
  53428. EVP_PKEY_free(pkey);
  53429. res = TEST_RES_CHECK(1);
  53430. }
  53431. #endif /* HAVE_ECC */
  53432. #endif /* OPENSSL_EXTRA */
  53433. return res;
  53434. }
  53435. static int test_stubs_are_stubs(void)
  53436. {
  53437. int res = TEST_SKIPPED;
  53438. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  53439. WOLFSSL_CTX* ctx = NULL;
  53440. WOLFSSL_CTX* ctxN = NULL;
  53441. #ifndef NO_WOLFSSL_CLIENT
  53442. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  53443. AssertNotNull(ctx);
  53444. #elif !defined(NO_WOLFSSL_SERVER)
  53445. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  53446. AssertNotNull(ctx);
  53447. #else
  53448. return res;
  53449. #endif
  53450. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  53451. AssertIntEQ((int) x(z), 0)
  53452. /* test logic, all stubs return same result regardless of ctx being NULL
  53453. * as there are no sanity checks, it's just a stub! If at some
  53454. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  53455. * if invalid inputs are supplied. Test calling both
  53456. * with and without valid inputs, if a stub functionality remains unchanged.
  53457. */
  53458. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  53459. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  53460. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  53461. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  53462. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  53463. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  53464. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  53465. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  53466. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  53467. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  53468. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  53469. wolfSSL_CTX_free(ctx);
  53470. ctx = NULL;
  53471. res = TEST_RES_CHECK(1);
  53472. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  53473. return res;
  53474. }
  53475. static int test_CONF_modules_xxx(void)
  53476. {
  53477. int res = TEST_SKIPPED;
  53478. #if defined(OPENSSL_EXTRA)
  53479. CONF_modules_free();
  53480. AssertTrue(1); /* to confirm previous call gives no harm */
  53481. CONF_modules_unload(0);
  53482. AssertTrue(1);
  53483. CONF_modules_unload(1);
  53484. AssertTrue(1);
  53485. CONF_modules_unload(-1);
  53486. AssertTrue(1);
  53487. res = TEST_RES_CHECK(1);
  53488. #endif /* OPENSSL_EXTRA */
  53489. return res;
  53490. }
  53491. static int test_CRYPTO_set_dynlock_xxx(void)
  53492. {
  53493. int res = TEST_SKIPPED;
  53494. #if defined(OPENSSL_EXTRA)
  53495. CRYPTO_set_dynlock_create_callback(
  53496. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  53497. CRYPTO_set_dynlock_create_callback(
  53498. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  53499. CRYPTO_set_dynlock_destroy_callback(
  53500. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  53501. CRYPTO_set_dynlock_destroy_callback(
  53502. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  53503. CRYPTO_set_dynlock_lock_callback(
  53504. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  53505. CRYPTO_set_dynlock_lock_callback(
  53506. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  53507. AssertTrue(1); /* to confirm previous call gives no harm */
  53508. res = TEST_RES_CHECK(1);
  53509. #endif /* OPENSSL_EXTRA */
  53510. return res;
  53511. }
  53512. static int test_CRYPTO_THREADID_xxx(void)
  53513. {
  53514. int res = TEST_SKIPPED;
  53515. #if defined(OPENSSL_EXTRA)
  53516. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  53517. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  53518. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  53519. res = TEST_RES_CHECK(1);
  53520. #endif /* OPENSSL_EXTRA */
  53521. return res;
  53522. }
  53523. static int test_ENGINE_cleanup(void)
  53524. {
  53525. int res = TEST_SKIPPED;
  53526. #if defined(OPENSSL_EXTRA)
  53527. ENGINE_cleanup();
  53528. AssertTrue(1); /* to confirm previous call gives no harm */
  53529. res = TEST_RES_CHECK(1);
  53530. #endif /* OPENSSL_EXTRA */
  53531. return res;
  53532. }
  53533. static int test_wolfSSL_CTX_LoadCRL(void)
  53534. {
  53535. int res = TEST_SKIPPED;
  53536. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  53537. WOLFSSL_CTX* ctx = NULL;
  53538. WOLFSSL* ssl = NULL;
  53539. const char* badPath = "dummypath";
  53540. const char* validPath = "./certs/crl";
  53541. const char* validFilePath = "./certs/crl/cliCrl.pem";
  53542. const char* issuerCert = "./certs/client-cert.pem";
  53543. int derType = WOLFSSL_FILETYPE_ASN1;
  53544. int pemType = WOLFSSL_FILETYPE_PEM;
  53545. int monitor = WOLFSSL_CRL_MONITOR;
  53546. WOLFSSL_CERT_MANAGER* cm = NULL;
  53547. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  53548. BAD_FUNC_ARG)
  53549. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  53550. WOLFSSL_SUCCESS)
  53551. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53552. #ifndef NO_WOLFSSL_CLIENT
  53553. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53554. #elif !defined(NO_WOLFSSL_SERVER)
  53555. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53556. #else
  53557. return;
  53558. #endif
  53559. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53560. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  53561. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  53562. wolfSSL_CTX_free(ctx);
  53563. #ifndef NO_WOLFSSL_CLIENT
  53564. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53565. #elif !defined(NO_WOLFSSL_SERVER)
  53566. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53567. #else
  53568. return;
  53569. #endif
  53570. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53571. WOLFSSL_SUCCESS);
  53572. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  53573. wolfSSL_CTX_free(ctx);
  53574. #ifndef NO_WOLFSSL_CLIENT
  53575. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53576. #elif !defined(NO_WOLFSSL_SERVER)
  53577. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53578. #else
  53579. return;
  53580. #endif
  53581. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53582. WOLFSSL_SUCCESS);
  53583. AssertNotNull(ssl = wolfSSL_new(ctx));
  53584. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  53585. wolfSSL_free(ssl);
  53586. wolfSSL_CTX_free(ctx);
  53587. AssertNotNull(cm = wolfSSL_CertManagerNew());
  53588. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  53589. WOLFSSL_SUCCESS);
  53590. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  53591. wolfSSL_CertManagerFree(cm);
  53592. res = TEST_RES_CHECK(1);
  53593. #endif
  53594. return res;
  53595. }
  53596. static int test_SetTmpEC_DHE_Sz(void)
  53597. {
  53598. int res = TEST_SKIPPED;
  53599. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  53600. EXPECT_DECLS;
  53601. WOLFSSL_CTX *ctx = NULL;
  53602. WOLFSSL *ssl = NULL;
  53603. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53604. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  53605. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53606. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  53607. wolfSSL_free(ssl);
  53608. wolfSSL_CTX_free(ctx);
  53609. res = EXPECT_RESULT();
  53610. #endif
  53611. return res;
  53612. }
  53613. static int test_wolfSSL_CTX_get0_privatekey(void)
  53614. {
  53615. int res = TEST_SKIPPED;
  53616. #ifdef OPENSSL_ALL
  53617. EXPECT_DECLS;
  53618. WOLFSSL_CTX* ctx = NULL;
  53619. (void)ctx;
  53620. #ifndef NO_RSA
  53621. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53622. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53623. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53624. WOLFSSL_FILETYPE_PEM));
  53625. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53626. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53627. WOLFSSL_FILETYPE_PEM));
  53628. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53629. wolfSSL_CTX_free(ctx);
  53630. ctx = NULL;
  53631. #endif
  53632. #ifdef HAVE_ECC
  53633. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53634. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53635. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  53636. WOLFSSL_FILETYPE_PEM));
  53637. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53638. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  53639. WOLFSSL_FILETYPE_PEM));
  53640. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53641. wolfSSL_CTX_free(ctx);
  53642. #endif
  53643. res = EXPECT_RESULT();
  53644. #endif
  53645. return res;
  53646. }
  53647. static int test_wolfSSL_dtls_set_mtu(void)
  53648. {
  53649. int res = TEST_SKIPPED;
  53650. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  53651. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  53652. EXPECT_DECLS;
  53653. WOLFSSL_CTX* ctx = NULL;
  53654. WOLFSSL* ssl = NULL;
  53655. const char* testCertFile;
  53656. const char* testKeyFile;
  53657. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  53658. #ifndef NO_RSA
  53659. testCertFile = svrCertFile;
  53660. testKeyFile = svrKeyFile;
  53661. #elif defined(HAVE_ECC)
  53662. testCertFile = eccCertFile;
  53663. testKeyFile = eccKeyFile;
  53664. #endif
  53665. if (testCertFile != NULL && testKeyFile != NULL) {
  53666. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  53667. WOLFSSL_FILETYPE_PEM));
  53668. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  53669. WOLFSSL_FILETYPE_PEM));
  53670. }
  53671. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53672. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53673. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53674. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  53675. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  53676. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  53677. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  53678. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  53679. wolfSSL_free(ssl);
  53680. wolfSSL_CTX_free(ctx);
  53681. res = EXPECT_RESULT();
  53682. #endif
  53683. return res;
  53684. }
  53685. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53686. defined(WOLFSSL_DTLS)
  53687. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  53688. enum HandShakeType hsType, word16 length)
  53689. {
  53690. size_t idx = 0;
  53691. byte* l;
  53692. /* record layer */
  53693. /* handshake type */
  53694. out[idx++] = handshake;
  53695. /* protocol version */
  53696. out[idx++] = 0xfe;
  53697. out[idx++] = 0xfd; /* DTLS 1.2 */
  53698. /* epoch 0 */
  53699. XMEMSET(out + idx, 0, 2);
  53700. idx += 2;
  53701. /* sequence number */
  53702. XMEMSET(out + idx, 0, 6);
  53703. c32toa(seq, out + idx + 2);
  53704. idx += 6;
  53705. /* length in BE */
  53706. if (length)
  53707. c16toa(length, out + idx);
  53708. else
  53709. c16toa(outSz - idx - 2, out + idx);
  53710. idx += 2;
  53711. /* handshake layer */
  53712. /* handshake type */
  53713. out[idx++] = (byte)hsType;
  53714. /* length */
  53715. l = out + idx;
  53716. idx += 3;
  53717. /* message seq */
  53718. c16toa(0, out + idx);
  53719. idx += 2;
  53720. /* frag offset */
  53721. c32to24(0, out + idx);
  53722. idx += 3;
  53723. /* frag length */
  53724. c32to24((word32)outSz - (word32)idx - 3, l);
  53725. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  53726. idx += 3;
  53727. XMEMSET(out + idx, 0, outSz - idx);
  53728. }
  53729. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  53730. {
  53731. byte msg[] = "This is a msg for the client";
  53732. byte reply[40];
  53733. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53734. reply[sizeof(reply) - 1] = '\0';
  53735. fprintf(stderr, "Client message: %s\n", reply);
  53736. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53737. }
  53738. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  53739. {
  53740. byte ch[50];
  53741. int fd = wolfSSL_get_fd(ssl);
  53742. byte msg[] = "This is a msg for the server";
  53743. byte reply[40];
  53744. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  53745. /* Server should ignore this datagram */
  53746. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53747. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  53748. /* Server should ignore this datagram */
  53749. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53750. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53751. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53752. reply[sizeof(reply) - 1] = '\0';
  53753. fprintf(stderr, "Server response: %s\n", reply);
  53754. }
  53755. static int test_wolfSSL_dtls_plaintext(void)
  53756. {
  53757. callback_functions func_cb_client;
  53758. callback_functions func_cb_server;
  53759. size_t i;
  53760. struct test_params {
  53761. method_provider client_meth;
  53762. method_provider server_meth;
  53763. ssl_callback on_result_server;
  53764. ssl_callback on_result_client;
  53765. } params[] = {
  53766. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53767. test_wolfSSL_dtls_plaintext_server,
  53768. test_wolfSSL_dtls_plaintext_client},
  53769. };
  53770. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53771. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53772. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53773. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53774. func_cb_server.method = params[i].server_meth;
  53775. func_cb_client.method = params[i].client_meth;
  53776. func_cb_client.on_result = params[i].on_result_client;
  53777. func_cb_server.on_result = params[i].on_result_server;
  53778. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53779. if (!func_cb_client.return_code)
  53780. return TEST_FAIL;
  53781. if (!func_cb_server.return_code)
  53782. return TEST_FAIL;
  53783. }
  53784. return TEST_RES_CHECK(1);
  53785. }
  53786. #else
  53787. static int test_wolfSSL_dtls_plaintext(void) {
  53788. return TEST_SKIPPED;
  53789. }
  53790. #endif
  53791. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53792. defined(WOLFSSL_DTLS)
  53793. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  53794. {
  53795. byte b[1100]; /* buffer for the messages to send */
  53796. size_t idx = 0;
  53797. size_t seq_offset = 0;
  53798. size_t msg_offset = 0;
  53799. int i;
  53800. int fd = wolfSSL_get_fd(ssl);
  53801. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53802. word32 seq_number = 100; /* start high so server definitely reads this */
  53803. word16 msg_number = 50; /* start high so server has to buffer this */
  53804. AssertIntEQ(ret, 1);
  53805. /* Now let's start spamming the peer with fragments it needs to store */
  53806. XMEMSET(b, -1, sizeof(b));
  53807. /* record layer */
  53808. /* handshake type */
  53809. b[idx++] = 22;
  53810. /* protocol version */
  53811. b[idx++] = 0xfe;
  53812. b[idx++] = 0xfd; /* DTLS 1.2 */
  53813. /* epoch 0 */
  53814. XMEMSET(b + idx, 0, 2);
  53815. idx += 2;
  53816. /* sequence number */
  53817. XMEMSET(b + idx, 0, 6);
  53818. seq_offset = idx + 2; /* increment only the low 32 bits */
  53819. idx += 6;
  53820. /* static length in BE */
  53821. c16toa(42, b + idx);
  53822. idx += 2;
  53823. /* handshake layer */
  53824. /* cert type */
  53825. b[idx++] = 11;
  53826. /* length */
  53827. c32to24(1000, b + idx);
  53828. idx += 3;
  53829. /* message seq */
  53830. c16toa(0, b + idx);
  53831. msg_offset = idx;
  53832. idx += 2;
  53833. /* frag offset */
  53834. c32to24(500, b + idx);
  53835. idx += 3;
  53836. /* frag length */
  53837. c32to24(30, b + idx);
  53838. idx += 3;
  53839. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  53840. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  53841. seq_number++, msg_number++, i++) {
  53842. struct timespec delay;
  53843. XMEMSET(&delay, 0, sizeof(delay));
  53844. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53845. c32toa(seq_number, b + seq_offset);
  53846. c16toa(msg_number, b + msg_offset);
  53847. ret = (int)send(fd, b, 55, 0);
  53848. nanosleep(&delay, NULL);
  53849. }
  53850. }
  53851. #ifdef WOLFSSL_DTLS13
  53852. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  53853. {
  53854. const word16 sendCountMax = 100;
  53855. byte b[150]; /* buffer for the messages to send */
  53856. size_t idx = 0;
  53857. size_t msg_offset = 0;
  53858. int fd = wolfSSL_get_fd(ssl);
  53859. word16 msg_number = 10; /* start high so server has to buffer this */
  53860. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53861. AssertIntEQ(ret, 1);
  53862. /* Now let's start spamming the peer with fragments it needs to store */
  53863. XMEMSET(b, -1, sizeof(b));
  53864. /* handshake type */
  53865. b[idx++] = 11;
  53866. /* length */
  53867. c32to24(10000, b + idx);
  53868. idx += 3;
  53869. /* message_seq */
  53870. msg_offset = idx;
  53871. idx += 2;
  53872. /* fragment_offset */
  53873. c32to24(5000, b + idx);
  53874. idx += 3;
  53875. /* fragment_length */
  53876. c32to24(100, b + idx);
  53877. idx += 3;
  53878. /* fragment contents */
  53879. idx += 100;
  53880. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  53881. byte sendBuf[150];
  53882. int sendSz = sizeof(sendBuf);
  53883. struct timespec delay;
  53884. XMEMSET(&delay, 0, sizeof(delay));
  53885. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53886. c16toa(msg_number, b + msg_offset);
  53887. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  53888. (int)idx, handshake, 0, 0, 0);
  53889. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  53890. nanosleep(&delay, NULL);
  53891. }
  53892. }
  53893. #endif
  53894. static int test_wolfSSL_dtls_fragments(void)
  53895. {
  53896. callback_functions func_cb_client;
  53897. callback_functions func_cb_server;
  53898. size_t i;
  53899. struct test_params {
  53900. method_provider client_meth;
  53901. method_provider server_meth;
  53902. ssl_callback spammer;
  53903. } params[] = {
  53904. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53905. test_wolfSSL_dtls12_fragments_spammer},
  53906. #ifdef WOLFSSL_DTLS13
  53907. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53908. test_wolfSSL_dtls13_fragments_spammer},
  53909. #endif
  53910. };
  53911. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53912. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53913. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53914. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53915. func_cb_server.method = params[i].server_meth;
  53916. func_cb_client.method = params[i].client_meth;
  53917. func_cb_client.ssl_ready = params[i].spammer;
  53918. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53919. AssertFalse(func_cb_client.return_code);
  53920. AssertFalse(func_cb_server.return_code);
  53921. /* The socket should be closed by the server resulting in a
  53922. * socket error, fatal error or reading a close notify alert */
  53923. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  53924. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  53925. func_cb_client.last_err != FATAL_ERROR) {
  53926. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  53927. }
  53928. /* Check the server returned an error indicating the msg buffer
  53929. * was full */
  53930. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  53931. }
  53932. return TEST_RES_CHECK(1);
  53933. }
  53934. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  53935. {
  53936. int fd, ret;
  53937. byte alert_msg[] = {
  53938. 0x15, /* alert type */
  53939. 0xfe, 0xfd, /* version */
  53940. 0x00, 0x00, /* epoch */
  53941. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  53942. 0x00, 0x02, /* length */
  53943. 0x02, /* level: fatal */
  53944. 0x46 /* protocol version */
  53945. };
  53946. fd = wolfSSL_get_fd(ssl);
  53947. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  53948. AssertIntGT(ret, 0);
  53949. }
  53950. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  53951. {
  53952. callback_functions client_cbs, server_cbs;
  53953. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53954. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53955. client_cbs.doUdp = server_cbs.doUdp = 1;
  53956. if (version12) {
  53957. client_cbs.method = wolfDTLSv1_2_client_method;
  53958. server_cbs.method = wolfDTLSv1_2_server_method;
  53959. }
  53960. else {
  53961. #ifdef WOLFSSL_DTLS13
  53962. client_cbs.method = wolfDTLSv1_3_client_method;
  53963. server_cbs.method = wolfDTLSv1_3_server_method;
  53964. #else
  53965. return TEST_SKIPPED;
  53966. #endif /* WOLFSSL_DTLS13 */
  53967. }
  53968. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53969. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53970. if (!client_cbs.return_code)
  53971. return TEST_FAIL;
  53972. if (!server_cbs.return_code)
  53973. return TEST_FAIL;
  53974. return TEST_SUCCESS;
  53975. }
  53976. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53977. {
  53978. int ret;
  53979. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53980. if (ret != 0)
  53981. return ret;
  53982. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53983. if (ret != 0)
  53984. return ret;
  53985. return 0;
  53986. }
  53987. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53988. {
  53989. int ret;
  53990. int fd;
  53991. byte bad_msg[] = {
  53992. 0x17, /* app data */
  53993. 0xaa, 0xfd, /* bad version */
  53994. 0x00, 0x01, /* epoch 1 */
  53995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53996. 0x00, 0x26, /* length: 38 bytes */
  53997. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53998. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53999. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  54000. 0x03, 0x18, 0x72
  54001. };
  54002. fd = wolfSSL_get_fd(ssl);
  54003. AssertIntGE(fd, 0);
  54004. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  54005. AssertIntEQ(ret, sizeof(bad_msg));
  54006. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  54007. AssertIntEQ(ret, sizeof("badrecordtest"));
  54008. }
  54009. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  54010. {
  54011. byte buf[100];
  54012. int ret;
  54013. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  54014. AssertIntGT(ret, 0);
  54015. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  54016. }
  54017. static int _test_wolfSSL_dtls_bad_record(
  54018. method_provider client_method, method_provider server_method)
  54019. {
  54020. callback_functions client_cbs, server_cbs;
  54021. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54022. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54023. client_cbs.doUdp = server_cbs.doUdp = 1;
  54024. client_cbs.method = client_method;
  54025. server_cbs.method = server_method;
  54026. client_cbs.on_result = test_wolfSSL_send_bad_record;
  54027. server_cbs.on_result = test_wolfSSL_read_string;
  54028. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54029. if (!client_cbs.return_code)
  54030. return TEST_FAIL;
  54031. if (!server_cbs.return_code)
  54032. return TEST_FAIL;
  54033. return TEST_SUCCESS;
  54034. }
  54035. static int test_wolfSSL_dtls_bad_record(void)
  54036. {
  54037. int ret;
  54038. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  54039. wolfDTLSv1_2_server_method);
  54040. #ifdef WOLFSSL_DTLS13
  54041. if (ret != TEST_SUCCESS)
  54042. return ret;
  54043. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  54044. wolfDTLSv1_3_server_method);
  54045. #else
  54046. return ret;
  54047. #endif /* WOLFSSL_DTLS13 */
  54048. }
  54049. #else
  54050. static int test_wolfSSL_dtls_fragments(void) {
  54051. return TEST_SKIPPED;
  54052. }
  54053. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  54054. return TEST_SKIPPED;
  54055. }
  54056. static int test_wolfSSL_dtls_bad_record(void) {
  54057. return TEST_SKIPPED;
  54058. }
  54059. #endif
  54060. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  54061. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54062. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54063. static byte test_AEAD_fail_decryption = 0;
  54064. static byte test_AEAD_seq_num = 0;
  54065. static byte test_AEAD_done = 0;
  54066. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  54067. {
  54068. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  54069. if (ret > 0) {
  54070. if (test_AEAD_fail_decryption) {
  54071. /* Modify the packet to trigger a decryption failure */
  54072. buf[ret/2] ^= 0xFF;
  54073. if (test_AEAD_fail_decryption == 1)
  54074. test_AEAD_fail_decryption = 0;
  54075. }
  54076. }
  54077. (void)ctx;
  54078. return ret;
  54079. }
  54080. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  54081. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  54082. {
  54083. if (sendLimit)
  54084. w64Zero(sendLimit);
  54085. switch (ssl->specs.bulk_cipher_algorithm) {
  54086. case wolfssl_aes_gcm:
  54087. if (sendLimit)
  54088. *sendLimit = AEAD_AES_LIMIT;
  54089. FALL_THROUGH;
  54090. case wolfssl_chacha:
  54091. if (hardLimit)
  54092. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  54093. if (keyUpdateLimit)
  54094. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  54095. break;
  54096. case wolfssl_aes_ccm:
  54097. if (sendLimit)
  54098. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  54099. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  54100. if (hardLimit)
  54101. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  54102. if (keyUpdateLimit)
  54103. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  54104. }
  54105. else {
  54106. if (hardLimit)
  54107. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  54108. if (keyUpdateLimit)
  54109. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  54110. }
  54111. break;
  54112. default:
  54113. fprintf(stderr, "Unrecognized bulk cipher");
  54114. AssertFalse(1);
  54115. break;
  54116. }
  54117. }
  54118. static void test_AEAD_limit_client(WOLFSSL* ssl)
  54119. {
  54120. int ret;
  54121. int i;
  54122. int didReKey = 0;
  54123. char msgBuf[20];
  54124. w64wrapper hardLimit;
  54125. w64wrapper keyUpdateLimit;
  54126. w64wrapper counter;
  54127. w64wrapper sendLimit;
  54128. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  54129. w64Zero(&counter);
  54130. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  54131. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  54132. for (i = 0; i < 10; i++) {
  54133. /* Test some failed decryptions */
  54134. test_AEAD_fail_decryption = 1;
  54135. w64Increment(&counter);
  54136. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54137. /* Should succeed since decryption failures are dropped */
  54138. AssertIntGT(ret, 0);
  54139. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  54140. }
  54141. test_AEAD_fail_decryption = 1;
  54142. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  54143. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54144. /* 100 read calls should be enough to complete the key update */
  54145. w64Zero(&counter);
  54146. for (i = 0; i < 100; i++) {
  54147. /* Key update should be sent and negotiated */
  54148. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54149. AssertIntGT(ret, 0);
  54150. /* Epoch after one key update is 4 */
  54151. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  54152. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  54153. didReKey = 1;
  54154. break;
  54155. }
  54156. }
  54157. AssertTrue(didReKey);
  54158. if (!w64IsZero(sendLimit)) {
  54159. /* Test the sending limit for AEAD ciphers */
  54160. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  54161. test_AEAD_seq_num = 1;
  54162. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54163. AssertIntGT(ret, 0);
  54164. didReKey = 0;
  54165. w64Zero(&counter);
  54166. /* 100 read calls should be enough to complete the key update */
  54167. for (i = 0; i < 100; i++) {
  54168. /* Key update should be sent and negotiated */
  54169. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54170. AssertIntGT(ret, 0);
  54171. /* Epoch after another key update is 5 */
  54172. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  54173. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  54174. didReKey = 1;
  54175. break;
  54176. }
  54177. }
  54178. AssertTrue(didReKey);
  54179. }
  54180. test_AEAD_fail_decryption = 2;
  54181. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  54182. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54183. /* Connection should fail with a DECRYPT_ERROR */
  54184. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54185. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  54186. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  54187. test_AEAD_done = 1;
  54188. }
  54189. int counter = 0;
  54190. static void test_AEAD_limit_server(WOLFSSL* ssl)
  54191. {
  54192. char msgBuf[] = "Sending data";
  54193. int ret = WOLFSSL_SUCCESS;
  54194. w64wrapper sendLimit;
  54195. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54196. struct timespec delay;
  54197. XMEMSET(&delay, 0, sizeof(delay));
  54198. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  54199. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  54200. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54201. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  54202. while (!test_AEAD_done && ret > 0) {
  54203. counter++;
  54204. if (test_AEAD_seq_num) {
  54205. /* We need to update the seq number so that we can understand the
  54206. * peer. Otherwise we will incorrectly interpret the seq number. */
  54207. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  54208. AssertNotNull(e);
  54209. e->nextPeerSeqNumber = sendLimit;
  54210. test_AEAD_seq_num = 0;
  54211. }
  54212. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54213. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54214. nanosleep(&delay, NULL);
  54215. }
  54216. }
  54217. static int test_wolfSSL_dtls_AEAD_limit(void)
  54218. {
  54219. callback_functions func_cb_client;
  54220. callback_functions func_cb_server;
  54221. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54222. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54223. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54224. func_cb_server.method = wolfDTLSv1_3_server_method;
  54225. func_cb_client.method = wolfDTLSv1_3_client_method;
  54226. func_cb_server.on_result = test_AEAD_limit_server;
  54227. func_cb_client.on_result = test_AEAD_limit_client;
  54228. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54229. if (!func_cb_client.return_code)
  54230. return TEST_FAIL;
  54231. if (!func_cb_server.return_code)
  54232. return TEST_FAIL;
  54233. return TEST_SUCCESS;
  54234. }
  54235. #else
  54236. static int test_wolfSSL_dtls_AEAD_limit(void)
  54237. {
  54238. return TEST_SKIPPED;
  54239. }
  54240. #endif
  54241. #if defined(WOLFSSL_DTLS) && \
  54242. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  54243. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  54244. {
  54245. int fd, ret;
  54246. byte ch_msg[] = {
  54247. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54248. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54249. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  54250. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  54251. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  54252. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54253. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54254. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54255. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54256. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54257. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54258. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54259. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54260. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  54261. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  54262. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  54263. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  54264. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  54265. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  54266. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  54267. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  54268. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  54269. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  54270. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  54271. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  54272. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  54273. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  54274. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  54275. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  54276. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  54277. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  54278. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  54279. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  54280. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  54281. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  54282. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  54283. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  54284. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  54285. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  54286. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  54287. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  54288. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  54289. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  54290. 0x31, 0x68, 0x4c
  54291. };
  54292. fd = wolfSSL_get_fd(ssl);
  54293. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  54294. AssertIntGT(ret, 0);
  54295. /* consume the HRR otherwise handshake will fail */
  54296. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  54297. AssertIntGT(ret, 0);
  54298. }
  54299. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54300. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  54301. {
  54302. int fd, ret;
  54303. byte ch_msh_invalid_cookie[] = {
  54304. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  54305. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  54306. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  54307. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  54308. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  54309. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54310. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54311. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54312. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54313. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54314. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54315. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54316. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54317. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  54318. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54319. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54320. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54321. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54322. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54323. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  54324. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  54325. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  54326. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  54327. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  54328. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  54329. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  54330. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  54331. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  54332. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  54333. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  54334. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  54335. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  54336. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  54337. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  54338. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  54339. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  54340. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  54341. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  54342. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  54343. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  54344. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  54345. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  54346. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  54347. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  54348. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  54349. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  54350. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  54351. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  54352. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  54353. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  54354. 0x23, 0x00, 0x00
  54355. };
  54356. byte alert_reply[50];
  54357. byte expected_alert_reply[] = {
  54358. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  54359. 0x02, 0x02, 0x2f
  54360. };
  54361. fd = wolfSSL_get_fd(ssl);
  54362. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  54363. AssertIntGT(ret, 0);
  54364. /* should reply with an illegal_parameter reply */
  54365. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  54366. AssertIntEQ(ret, sizeof(expected_alert_reply));
  54367. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  54368. }
  54369. #endif
  54370. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  54371. {
  54372. #ifndef NO_MD5
  54373. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  54374. #elif !defined(NO_SHA)
  54375. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  54376. #elif !defined(NO_SHA256)
  54377. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  54378. #else
  54379. #error "We need a digest to hash the WOLFSSL object"
  54380. #endif
  54381. byte hashBuf[WC_MAX_DIGEST_SIZE];
  54382. wc_HashAlg hash;
  54383. const TLSX* exts = ssl->extensions;
  54384. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  54385. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  54386. * InitHandshakeHashes */
  54387. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  54388. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  54389. /* Following fields are not important to compare */
  54390. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54391. sslCopy.buffers.inputBuffer.buffer = NULL;
  54392. sslCopy.buffers.inputBuffer.bufferSize = 0;
  54393. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  54394. sslCopy.buffers.inputBuffer.offset = 0;
  54395. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54396. sslCopy.buffers.outputBuffer.buffer = NULL;
  54397. sslCopy.buffers.outputBuffer.bufferSize = 0;
  54398. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  54399. sslCopy.buffers.outputBuffer.offset = 0;
  54400. sslCopy.error = 0;
  54401. sslCopy.curSize = 0;
  54402. sslCopy.keys.curSeq_lo = 0;
  54403. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  54404. #ifdef WOLFSSL_DTLS13
  54405. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  54406. sslCopy.dtls13FastTimeout = 0;
  54407. #endif
  54408. sslCopy.keys.dtls_peer_handshake_number = 0;
  54409. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  54410. sslCopy.hsHashes = NULL;
  54411. #ifdef WOLFSSL_ASYNC_IO
  54412. #ifdef WOLFSSL_ASYNC_CRYPT
  54413. sslCopy.asyncDev = NULL;
  54414. #endif
  54415. sslCopy.async = NULL;
  54416. #endif
  54417. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  54418. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  54419. /* hash extension list */
  54420. while (exts != NULL) {
  54421. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  54422. exts = exts->next;
  54423. }
  54424. /* Hash suites */
  54425. if (sslCopy.suites != NULL) {
  54426. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  54427. sizeof(struct Suites)), 0);
  54428. }
  54429. /* Hash hsHashes */
  54430. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  54431. sizeof(*hsHashes)), 0);
  54432. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  54433. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  54434. return MakeWordFromHash(hashBuf);
  54435. }
  54436. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  54437. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  54438. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  54439. char *buf, int sz, void *ctx)
  54440. {
  54441. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  54442. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  54443. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  54444. }
  54445. else {
  54446. return WOLFSSL_CBIO_ERR_WANT_READ;
  54447. }
  54448. }
  54449. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  54450. {
  54451. /* Compare the ssl object before and after one ClientHello msg */
  54452. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54453. int res;
  54454. int err;
  54455. word32 initHash;
  54456. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  54457. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  54458. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54459. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  54460. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  54461. (void)initHash;
  54462. res = tcp_select(fd, 5);
  54463. /* We are expecting a msg. A timeout indicates failure. */
  54464. AssertIntEQ(res, TEST_RECV_READY);
  54465. res = wolfSSL_accept(ssl);
  54466. err = wolfSSL_get_error(ssl, res);
  54467. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  54468. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  54469. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  54470. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  54471. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  54472. }
  54473. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54474. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  54475. {
  54476. int ret;
  54477. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  54478. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54479. test_wolfSSL_dtls_compare_stateless(ssl);
  54480. }
  54481. #endif
  54482. static int test_wolfSSL_dtls_stateless(void)
  54483. {
  54484. callback_functions client_cbs, server_cbs;
  54485. size_t i;
  54486. struct {
  54487. method_provider client_meth;
  54488. method_provider server_meth;
  54489. ssl_callback client_ssl_ready;
  54490. ssl_callback server_ssl_ready;
  54491. } test_params[] = {
  54492. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54493. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  54494. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54495. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54496. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  54497. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54498. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  54499. #endif
  54500. };
  54501. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  54502. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54503. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54504. client_cbs.doUdp = server_cbs.doUdp = 1;
  54505. client_cbs.method = test_params[i].client_meth;
  54506. server_cbs.method = test_params[i].server_meth;
  54507. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  54508. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  54509. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54510. if (!client_cbs.return_code)
  54511. return TEST_FAIL;
  54512. if (!server_cbs.return_code)
  54513. return TEST_FAIL;
  54514. }
  54515. return TEST_SUCCESS;
  54516. }
  54517. #else
  54518. static int test_wolfSSL_dtls_stateless(void)
  54519. {
  54520. return TEST_SKIPPED;
  54521. }
  54522. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  54523. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  54524. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54525. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54526. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54527. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54528. {
  54529. int ret;
  54530. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  54531. fprintf(stderr, "loading cert %s failed\n", certA);
  54532. fprintf(stderr, "Error: (%d): %s\n", ret,
  54533. wolfSSL_ERR_reason_error_string(ret));
  54534. return -1;
  54535. }
  54536. return 0;
  54537. }
  54538. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54539. {
  54540. int ret;
  54541. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  54542. != WOLFSSL_SUCCESS) {
  54543. fprintf(stderr, "could not verify the cert: %s\n", certA);
  54544. fprintf(stderr, "Error: (%d): %s\n", ret,
  54545. wolfSSL_ERR_reason_error_string(ret));
  54546. return -1;
  54547. }
  54548. else {
  54549. fprintf(stderr, "successfully verified: %s\n", certA);
  54550. }
  54551. return 0;
  54552. }
  54553. #define LOAD_ONE_CA(a, b, c, d) \
  54554. do { \
  54555. (a) = load_ca_into_cm(c, d); \
  54556. if ((a) != 0) \
  54557. return (b); \
  54558. else \
  54559. (b)--; \
  54560. } while(0)
  54561. #define VERIFY_ONE_CERT(a, b, c, d) \
  54562. do { \
  54563. (a) = verify_cert_with_cm(c, d); \
  54564. if ((a) != 0) \
  54565. return (b); \
  54566. else \
  54567. (b)--; \
  54568. } while(0)
  54569. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  54570. {
  54571. int ret;
  54572. int i = -1;
  54573. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  54574. char chainGArr[9][50] = {"certs/ca-cert.pem",
  54575. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  54576. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  54577. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  54578. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  54579. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  54580. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  54581. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  54582. "certs/test-pathlen/chainG-entity.pem"};
  54583. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  54584. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  54585. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  54586. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  54587. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  54588. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  54589. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  54590. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  54591. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  54592. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  54593. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  54594. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  54595. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  54596. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  54597. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  54598. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  54599. /* test validating the entity twice, should have no effect on pathLen since
  54600. * entity/leaf cert */
  54601. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  54602. return ret;
  54603. }
  54604. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  54605. {
  54606. int ret;
  54607. int i = -1;
  54608. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  54609. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  54610. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  54611. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  54612. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  54613. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54614. */
  54615. char chainHArr[6][50] = {"certs/ca-cert.pem",
  54616. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  54617. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  54618. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  54619. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  54620. "certs/test-pathlen/chainH-entity.pem"};
  54621. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  54622. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  54623. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  54624. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  54625. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  54626. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  54627. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  54628. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  54629. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  54630. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  54631. return ret;
  54632. }
  54633. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  54634. {
  54635. int ret;
  54636. int i = -1;
  54637. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  54638. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  54639. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  54640. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  54641. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54642. */
  54643. char chainIArr[5][50] = {"certs/ca-cert.pem",
  54644. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  54645. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  54646. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  54647. "certs/test-pathlen/chainI-entity.pem"};
  54648. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  54649. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  54650. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  54651. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  54652. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  54653. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  54654. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  54655. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  54656. return ret;
  54657. }
  54658. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  54659. {
  54660. int ret;
  54661. int i = -1;
  54662. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  54663. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  54664. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  54665. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  54666. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  54667. */
  54668. char chainJArr[6][50] = {"certs/ca-cert.pem",
  54669. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  54670. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  54671. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  54672. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  54673. "certs/test-pathlen/chainJ-entity.pem"};
  54674. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  54675. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  54676. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  54677. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  54678. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  54679. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  54680. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  54681. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  54682. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  54683. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  54684. return ret;
  54685. }
  54686. static int test_various_pathlen_chains(void)
  54687. {
  54688. int ret;
  54689. WOLFSSL_CERT_MANAGER* cm;
  54690. /* Test chain G (large chain with varying pathLens) */
  54691. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54692. fprintf(stderr, "cert manager new failed\n");
  54693. return -1;
  54694. }
  54695. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54696. AssertIntEQ(test_chainG(cm), -1);
  54697. #else
  54698. AssertIntEQ(test_chainG(cm), 0);
  54699. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54700. ret = wolfSSL_CertManagerUnloadCAs(cm);
  54701. if (ret != WOLFSSL_SUCCESS)
  54702. return -1;
  54703. wolfSSL_CertManagerFree(cm);
  54704. /* end test chain G */
  54705. /* Test chain H (5 chain with same pathLens) */
  54706. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54707. fprintf(stderr, "cert manager new failed\n");
  54708. return -1;
  54709. }
  54710. AssertIntLT(test_chainH(cm), 0);
  54711. wolfSSL_CertManagerUnloadCAs(cm);
  54712. wolfSSL_CertManagerFree(cm);
  54713. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54714. fprintf(stderr, "cert manager new failed\n");
  54715. return -1;
  54716. }
  54717. ret = wolfSSL_CertManagerUnloadCAs(cm);
  54718. if (ret != WOLFSSL_SUCCESS)
  54719. return -1;
  54720. wolfSSL_CertManagerFree(cm);
  54721. /* end test chain H */
  54722. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  54723. * followed by 2 ICA's, should pass) */
  54724. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54725. fprintf(stderr, "cert manager new failed\n");
  54726. return -1;
  54727. }
  54728. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54729. AssertIntEQ(test_chainI(cm), -1);
  54730. #else
  54731. AssertIntEQ(test_chainI(cm), 0);
  54732. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54733. wolfSSL_CertManagerUnloadCAs(cm);
  54734. wolfSSL_CertManagerFree(cm);
  54735. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54736. fprintf(stderr, "cert manager new failed\n");
  54737. return -1;
  54738. }
  54739. ret = wolfSSL_CertManagerUnloadCAs(cm);
  54740. if (ret != WOLFSSL_SUCCESS)
  54741. return -1;
  54742. wolfSSL_CertManagerFree(cm);
  54743. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  54744. * this time followed by 3 ICA's, should fail */
  54745. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54746. fprintf(stderr, "cert manager new failed\n");
  54747. return -1;
  54748. }
  54749. AssertIntLT(test_chainJ(cm), 0);
  54750. wolfSSL_CertManagerUnloadCAs(cm);
  54751. wolfSSL_CertManagerFree(cm);
  54752. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  54753. fprintf(stderr, "cert manager new failed\n");
  54754. return -1;
  54755. }
  54756. ret = wolfSSL_CertManagerUnloadCAs(cm);
  54757. wolfSSL_CertManagerFree(cm);
  54758. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  54759. }
  54760. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  54761. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54762. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  54763. {
  54764. EXPECT_DECLS;
  54765. byte ekm[100] = {0};
  54766. (void)ctx;
  54767. /* Succes Cases */
  54768. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54769. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  54770. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54771. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  54772. /* Use some random context */
  54773. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54774. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  54775. /* Failure cases */
  54776. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54777. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  54778. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54779. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  54780. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54781. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  54782. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54783. "extended master secret", XSTR_SIZEOF("extended master secret"),
  54784. NULL, 0, 0), 0);
  54785. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54786. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  54787. return EXPECT_RESULT();
  54788. }
  54789. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  54790. {
  54791. wolfSSL_KeepArrays(ssl);
  54792. return TEST_SUCCESS;
  54793. }
  54794. static int test_export_keying_material(void)
  54795. {
  54796. EXPECT_DECLS;
  54797. test_ssl_cbf serverCb;
  54798. test_ssl_cbf clientCb;
  54799. XMEMSET(&serverCb, 0, sizeof(serverCb));
  54800. XMEMSET(&clientCb, 0, sizeof(clientCb));
  54801. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  54802. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  54803. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  54804. return EXPECT_RESULT();
  54805. }
  54806. #endif /* HAVE_KEYING_MATERIAL */
  54807. static int test_wolfSSL_THREADID_hash(void)
  54808. {
  54809. int res = TEST_SKIPPED;
  54810. #if defined(OPENSSL_EXTRA)
  54811. EXPECT_DECLS;
  54812. CRYPTO_THREADID id;
  54813. CRYPTO_THREADID_current(NULL);
  54814. /* Hash result is unsigned long. */
  54815. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  54816. XMEMSET(&id, 0, sizeof(id));
  54817. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  54818. res = EXPECT_RESULT();
  54819. #endif /* OPENSSL_EXTRA */
  54820. return res;
  54821. }
  54822. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  54823. {
  54824. int res = TEST_SKIPPED;
  54825. #if defined(OPENSSL_EXTRA)
  54826. EXPECT_DECLS;
  54827. WOLFSSL_CTX* ctx = NULL;
  54828. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  54829. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  54830. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  54831. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  54832. res = EXPECT_RESULT();
  54833. #endif /* OPENSSL_EXTRA */
  54834. return res;
  54835. }
  54836. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54837. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54838. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  54839. {
  54840. callback_functions* callbacks = NULL;
  54841. WOLFSSL_CTX* ctx = NULL;
  54842. WOLFSSL* ssl = NULL;
  54843. SOCKET_T sfd = 0;
  54844. SOCKET_T cfd = 0;
  54845. word16 port;
  54846. char msg[] = "I hear you fa shizzle!";
  54847. int len = (int) XSTRLEN(msg);
  54848. char input[1024];
  54849. int ret, err;
  54850. if (!args)
  54851. return 0;
  54852. ((func_args*)args)->return_code = TEST_FAIL;
  54853. callbacks = ((func_args*)args)->callbacks;
  54854. ctx = wolfSSL_CTX_new(callbacks->method());
  54855. #if defined(USE_WINDOWS_API)
  54856. port = ((func_args*)args)->signal->port;
  54857. #else
  54858. /* Let tcp_listen assign port */
  54859. port = 0;
  54860. #endif
  54861. #ifdef WOLFSSL_TIRTOS
  54862. fdOpenSession(Task_self());
  54863. #endif
  54864. AssertIntEQ(WOLFSSL_SUCCESS,
  54865. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  54866. AssertIntEQ(WOLFSSL_SUCCESS,
  54867. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  54868. WOLFSSL_FILETYPE_PEM));
  54869. AssertIntEQ(WOLFSSL_SUCCESS,
  54870. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  54871. WOLFSSL_FILETYPE_PEM));
  54872. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  54873. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  54874. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54875. #elif !defined(NO_DH)
  54876. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  54877. #endif
  54878. if (callbacks->ctx_ready)
  54879. callbacks->ctx_ready(ctx);
  54880. ssl = wolfSSL_new(ctx);
  54881. AssertNotNull(ssl);
  54882. /* listen and accept */
  54883. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  54884. CloseSocket(sfd);
  54885. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  54886. if (callbacks->ssl_ready)
  54887. callbacks->ssl_ready(ssl);
  54888. do {
  54889. err = 0; /* Reset error */
  54890. ret = wolfSSL_accept(ssl);
  54891. if (ret != WOLFSSL_SUCCESS) {
  54892. err = wolfSSL_get_error(ssl, 0);
  54893. }
  54894. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54895. if (ret != WOLFSSL_SUCCESS) {
  54896. wolfSSL_free(ssl);
  54897. wolfSSL_CTX_free(ctx);
  54898. CloseSocket(cfd);
  54899. ((func_args*)args)->return_code = TEST_FAIL;
  54900. return 0;
  54901. }
  54902. /* read and write data */
  54903. XMEMSET( input, 0, sizeof(input));
  54904. while (1) {
  54905. ret = wolfSSL_read(ssl, input, sizeof(input));
  54906. if (ret > 0) {
  54907. break;
  54908. }
  54909. else {
  54910. err = wolfSSL_get_error(ssl,ret);
  54911. if (err == WOLFSSL_ERROR_WANT_READ) {
  54912. continue;
  54913. }
  54914. break;
  54915. }
  54916. }
  54917. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  54918. do {
  54919. ret = wolfSSL_write(ssl, msg, len);
  54920. if (ret > 0) {
  54921. break;
  54922. }
  54923. } while (ret < 0);
  54924. }
  54925. /* bidirectional shutdown */
  54926. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  54927. continue;
  54928. }
  54929. /* wait for the peer to disconnect the tcp connection */
  54930. do {
  54931. ret = wolfSSL_read(ssl, input, sizeof(input));
  54932. err = wolfSSL_get_error(ssl, ret);
  54933. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54934. /* detect TCP disconnect */
  54935. AssertIntLE(ret,WOLFSSL_FAILURE);
  54936. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54937. ((func_args*)args)->return_code = TEST_SUCCESS;
  54938. wolfSSL_free(ssl);
  54939. wolfSSL_CTX_free(ctx);
  54940. CloseSocket(cfd);
  54941. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54942. wc_ecc_fp_free(); /* free per thread cache */
  54943. #endif
  54944. return 0;
  54945. }
  54946. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54947. {
  54948. callback_functions* callbacks = NULL;
  54949. WOLFSSL_CTX* ctx = NULL;
  54950. WOLFSSL* ssl = NULL;
  54951. SOCKET_T sfd = 0;
  54952. char msg[] = "hello wolfssl server!";
  54953. int len = (int) XSTRLEN(msg);
  54954. char input[1024];
  54955. int idx;
  54956. int ret, err;
  54957. if (!args)
  54958. return 0;
  54959. ((func_args*)args)->return_code = TEST_FAIL;
  54960. callbacks = ((func_args*)args)->callbacks;
  54961. ctx = wolfSSL_CTX_new(callbacks->method());
  54962. #ifdef WOLFSSL_TIRTOS
  54963. fdOpenSession(Task_self());
  54964. #endif
  54965. AssertIntEQ(WOLFSSL_SUCCESS,
  54966. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  54967. AssertIntEQ(WOLFSSL_SUCCESS,
  54968. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  54969. WOLFSSL_FILETYPE_PEM));
  54970. AssertIntEQ(WOLFSSL_SUCCESS,
  54971. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  54972. WOLFSSL_FILETYPE_PEM));
  54973. AssertNotNull((ssl = wolfSSL_new(ctx)));
  54974. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54975. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54976. do {
  54977. err = 0; /* Reset error */
  54978. ret = wolfSSL_connect(ssl);
  54979. if (ret != WOLFSSL_SUCCESS) {
  54980. err = wolfSSL_get_error(ssl, 0);
  54981. }
  54982. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54983. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  54984. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54985. input[idx] = 0;
  54986. }
  54987. ret = wolfSSL_shutdown(ssl);
  54988. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54989. ret = wolfSSL_shutdown(ssl);
  54990. }
  54991. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54992. ((func_args*)args)->return_code = TEST_SUCCESS;
  54993. wolfSSL_free(ssl);
  54994. wolfSSL_CTX_free(ctx);
  54995. CloseSocket(sfd);
  54996. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54997. wc_ecc_fp_free(); /* free per thread cache */
  54998. #endif
  54999. return 0;
  55000. }
  55001. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  55002. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  55003. /* This test is to check wolfSSL_read behaves as same as
  55004. * openSSL when it is called after SSL_shutdown completes.
  55005. */
  55006. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  55007. {
  55008. int res = TEST_SKIPPED;
  55009. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55010. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55011. tcp_ready ready;
  55012. func_args client_args;
  55013. func_args server_args;
  55014. THREAD_TYPE serverThread;
  55015. THREAD_TYPE clientThread;
  55016. callback_functions server_cbf;
  55017. callback_functions client_cbf;
  55018. #ifdef WOLFSSL_TIRTOS
  55019. fdOpenSession(Task_self());
  55020. #endif
  55021. StartTCP();
  55022. InitTcpReady(&ready);
  55023. #if defined(USE_WINDOWS_API)
  55024. /* use RNG to get random port if using windows */
  55025. ready.port = GetRandomPort();
  55026. #endif
  55027. XMEMSET(&client_args, 0, sizeof(func_args));
  55028. XMEMSET(&server_args, 0, sizeof(func_args));
  55029. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  55030. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  55031. server_cbf.method = wolfTLSv1_2_server_method;
  55032. client_cbf.method = wolfTLSv1_2_client_method;
  55033. server_args.callbacks = &server_cbf;
  55034. client_args.callbacks = &client_cbf;
  55035. server_args.signal = &ready;
  55036. client_args.signal = &ready;
  55037. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  55038. wait_tcp_ready(&server_args);
  55039. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  55040. join_thread(clientThread);
  55041. join_thread(serverThread);
  55042. AssertTrue(client_args.return_code);
  55043. AssertTrue(server_args.return_code);
  55044. FreeTcpReady(&ready);
  55045. res = TEST_RES_CHECK(1);
  55046. #endif
  55047. return res;
  55048. }
  55049. static int test_wolfSSL_CTX_get_min_proto_version(void)
  55050. {
  55051. int res = TEST_SKIPPED;
  55052. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55053. WOLFSSL_CTX *ctx;
  55054. (void)ctx;
  55055. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55056. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  55057. #ifdef WOLFSSL_ALLOW_SSLV3
  55058. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55059. #else
  55060. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55061. #endif
  55062. wolfSSL_CTX_free(ctx);
  55063. #ifdef WOLFSSL_ALLOW_TLSV10
  55064. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  55065. #else
  55066. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55067. #endif
  55068. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  55069. #ifdef WOLFSSL_ALLOW_TLSV10
  55070. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55071. #else
  55072. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55073. #endif
  55074. wolfSSL_CTX_free(ctx);
  55075. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55076. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  55077. #ifndef NO_OLD_TLS
  55078. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55079. #else
  55080. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55081. #endif
  55082. wolfSSL_CTX_free(ctx);
  55083. #ifndef WOLFSSL_NO_TLS12
  55084. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  55085. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  55086. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  55087. wolfSSL_CTX_free(ctx);
  55088. #endif
  55089. #ifdef WOLFSSL_TLS13
  55090. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  55091. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  55092. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  55093. wolfSSL_CTX_free(ctx);
  55094. #endif
  55095. res = TEST_RES_CHECK(1);
  55096. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55097. return res;
  55098. }
  55099. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55100. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55101. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55102. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  55103. static int test_wolfSSL_set_SSL_CTX(void)
  55104. {
  55105. int res = TEST_SKIPPED;
  55106. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  55107. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  55108. !defined(NO_RSA)
  55109. WOLFSSL_CTX *ctx1, *ctx2;
  55110. WOLFSSL *ssl;
  55111. const byte *session_id1 = (const byte *)"CTX1";
  55112. const byte *session_id2 = (const byte *)"CTX2";
  55113. AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55114. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  55115. WOLFSSL_FILETYPE_PEM));
  55116. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  55117. WOLFSSL_FILETYPE_PEM));
  55118. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  55119. WOLFSSL_SUCCESS);
  55120. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  55121. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  55122. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  55123. WOLFSSL_SUCCESS);
  55124. AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55125. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  55126. WOLFSSL_FILETYPE_PEM));
  55127. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  55128. WOLFSSL_FILETYPE_PEM));
  55129. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  55130. WOLFSSL_SUCCESS);
  55131. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  55132. WOLFSSL_SUCCESS);
  55133. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  55134. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  55135. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  55136. WOLFSSL_SUCCESS);
  55137. #ifdef HAVE_SESSION_TICKET
  55138. AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  55139. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  55140. AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  55141. #endif
  55142. AssertNotNull(ssl = wolfSSL_new(ctx2));
  55143. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55144. #ifdef WOLFSSL_INT_H
  55145. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  55146. AssertTrue(ssl->buffers.certificate == ctx2->certificate);
  55147. AssertTrue(ssl->buffers.certChain == ctx2->certChain);
  55148. #endif
  55149. #ifdef HAVE_SESSION_TICKET
  55150. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55151. #endif
  55152. /* Set the ctx1 that has TLSv1.3 as max proto version */
  55153. AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  55154. /* MUST not change proto versions of ssl */
  55155. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55156. #ifdef HAVE_SESSION_TICKET
  55157. /* MUST not change */
  55158. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55159. #endif
  55160. /* MUST change */
  55161. #ifdef WOLFSSL_INT_H
  55162. AssertTrue(ssl->buffers.certificate == ctx1->certificate);
  55163. AssertTrue(ssl->buffers.certChain == ctx1->certChain);
  55164. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  55165. #endif
  55166. wolfSSL_free(ssl);
  55167. wolfSSL_CTX_free(ctx1);
  55168. wolfSSL_CTX_free(ctx2);
  55169. res = TEST_RES_CHECK(1);
  55170. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55171. return res;
  55172. }
  55173. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55174. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55175. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55176. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  55177. static int test_wolfSSL_security_level(void)
  55178. {
  55179. int res = TEST_SKIPPED;
  55180. #if defined(OPENSSL_EXTRA)
  55181. SSL_CTX *ctx;
  55182. #ifdef WOLFSSL_TLS13
  55183. #ifdef NO_WOLFSSL_SERVER
  55184. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55185. #else
  55186. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55187. #endif
  55188. SSL_CTX_set_security_level(ctx, 1);
  55189. AssertTrue(1);
  55190. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  55191. SSL_CTX_free(ctx);
  55192. #else
  55193. (void)ctx;
  55194. #endif
  55195. res = TEST_RES_CHECK(1);
  55196. #endif
  55197. return res;
  55198. }
  55199. static int test_wolfSSL_SSL_in_init(void)
  55200. {
  55201. int res = TEST_SKIPPED;
  55202. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  55203. SSL_CTX* ctx;
  55204. SSL* ssl;
  55205. const char* testCertFile;
  55206. const char* testKeyFile;
  55207. #ifdef WOLFSSL_TLS13
  55208. #ifdef NO_WOLFSSL_SERVER
  55209. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55210. #else
  55211. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55212. #endif
  55213. #ifndef NO_RSA
  55214. testCertFile = svrCertFile;
  55215. testKeyFile = svrKeyFile;
  55216. #elif defined(HAVE_ECC)
  55217. testCertFile = eccCertFile;
  55218. testKeyFile = eccKeyFile;
  55219. #else
  55220. testCertFile = NULL;
  55221. testKeyFile = NULL;
  55222. #endif
  55223. if (testCertFile != NULL && testKeyFile != NULL) {
  55224. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55225. SSL_FILETYPE_PEM));
  55226. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55227. SSL_FILETYPE_PEM));
  55228. }
  55229. ssl = SSL_new(ctx);
  55230. AssertNotNull(ssl);
  55231. AssertIntEQ(SSL_in_init(ssl), 1);
  55232. SSL_CTX_free(ctx);
  55233. SSL_free(ssl);
  55234. #else
  55235. (void)ctx;
  55236. (void)ssl;
  55237. (void)testCertFile;
  55238. (void)testKeyFile;
  55239. #endif
  55240. res = TEST_RES_CHECK(1);
  55241. #endif
  55242. return res;
  55243. }
  55244. static int test_wolfSSL_CTX_set_timeout(void)
  55245. {
  55246. int res = TEST_SKIPPED;
  55247. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  55248. int timeout;
  55249. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  55250. (void)timeout;
  55251. AssertNotNull(ctx);
  55252. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  55253. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  55254. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  55255. */
  55256. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55257. /* giving 0 as timeout value sets default timeout */
  55258. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  55259. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  55260. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  55261. #else
  55262. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55263. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  55264. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  55265. #endif
  55266. wolfSSL_CTX_free(ctx);
  55267. res = TEST_RES_CHECK(1);
  55268. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  55269. return res;
  55270. }
  55271. static int test_wolfSSL_OpenSSL_version(void)
  55272. {
  55273. int res = TEST_SKIPPED;
  55274. #if defined(OPENSSL_EXTRA)
  55275. const char* ver;
  55276. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55277. AssertNotNull(ver = OpenSSL_version(0));
  55278. #else
  55279. AssertNotNull(ver = OpenSSL_version());
  55280. #endif
  55281. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  55282. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  55283. res = TEST_RES_CHECK(1);
  55284. #endif
  55285. return res;
  55286. }
  55287. static int test_CONF_CTX_CMDLINE(void)
  55288. {
  55289. int res = TEST_SKIPPED;
  55290. #if defined(OPENSSL_ALL)
  55291. SSL_CTX* ctx = NULL;
  55292. SSL_CONF_CTX* cctx = NULL;
  55293. AssertNotNull(cctx = SSL_CONF_CTX_new());
  55294. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55295. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55296. AssertTrue(1);
  55297. /* set flags */
  55298. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  55299. WOLFSSL_CONF_FLAG_CMDLINE);
  55300. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55301. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55302. /* cmd invalid command */
  55303. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55304. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55305. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55306. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55307. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55308. /* cmd Certificate and Private Key*/
  55309. {
  55310. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55311. const char* ourCert = svrCertFile;
  55312. const char* ourKey = svrKeyFile;
  55313. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  55314. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  55315. WOLFSSL_SUCCESS);
  55316. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  55317. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  55318. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55319. #endif
  55320. }
  55321. /* cmd curves */
  55322. {
  55323. #if defined(HAVE_ECC)
  55324. const char* curve = "secp256r1";
  55325. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  55326. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  55327. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55328. #endif
  55329. }
  55330. /* cmd CipherString */
  55331. {
  55332. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55333. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  55334. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  55335. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55336. }
  55337. /* cmd DH parameter */
  55338. {
  55339. #if !defined(NO_DH) && !defined(NO_BIO)
  55340. const char* ourdhcert = "./certs/dh2048.pem";
  55341. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  55342. -3);
  55343. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  55344. WOLFSSL_SUCCESS);
  55345. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55346. #endif
  55347. }
  55348. SSL_CTX_free(ctx);
  55349. SSL_CONF_CTX_free(cctx);
  55350. res = TEST_RES_CHECK(1);
  55351. #endif /* OPENSSL_EXTRA */
  55352. return res;
  55353. }
  55354. static int test_CONF_CTX_FILE(void)
  55355. {
  55356. int res = TEST_SKIPPED;
  55357. #if defined(OPENSSL_ALL)
  55358. SSL_CTX* ctx = NULL;
  55359. SSL_CONF_CTX* cctx = NULL;
  55360. AssertNotNull(cctx = SSL_CONF_CTX_new());
  55361. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55362. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55363. AssertTrue(1);
  55364. /* set flags */
  55365. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  55366. WOLFSSL_CONF_FLAG_FILE);
  55367. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55368. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55369. /* sanity check */
  55370. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55371. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55372. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55373. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55374. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55375. /* cmd Certificate and Private Key*/
  55376. {
  55377. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55378. const char* ourCert = svrCertFile;
  55379. const char* ourKey = svrKeyFile;
  55380. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  55381. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  55382. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  55383. WOLFSSL_SUCCESS);
  55384. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  55385. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55386. #endif
  55387. }
  55388. /* cmd curves */
  55389. {
  55390. #if defined(HAVE_ECC)
  55391. const char* curve = "secp256r1";
  55392. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  55393. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  55394. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55395. #endif
  55396. }
  55397. /* cmd CipherString */
  55398. {
  55399. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55400. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  55401. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  55402. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55403. }
  55404. /* cmd DH parameter */
  55405. {
  55406. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  55407. const char* ourdhcert = "./certs/dh3072.pem";
  55408. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  55409. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  55410. WOLFSSL_SUCCESS);
  55411. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55412. #endif
  55413. }
  55414. SSL_CTX_free(ctx);
  55415. SSL_CONF_CTX_free(cctx);
  55416. res = TEST_RES_CHECK(1);
  55417. #endif /* OPENSSL_EXTRA */
  55418. return res;
  55419. }
  55420. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  55421. {
  55422. int res = TEST_SKIPPED;
  55423. #ifdef HAVE_EX_DATA
  55424. int idx1, idx2;
  55425. /* test for unsupported class index */
  55426. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  55427. 0,NULL, NULL, NULL, NULL ), -1);
  55428. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  55429. 0,NULL, NULL, NULL, NULL ), -1);
  55430. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  55431. 0,NULL, NULL, NULL, NULL ), -1);
  55432. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  55433. 0,NULL, NULL, NULL, NULL ), -1);
  55434. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  55435. 0,NULL, NULL, NULL, NULL ), -1);
  55436. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  55437. 0,NULL, NULL, NULL, NULL ), -1);
  55438. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  55439. 0,NULL, NULL, NULL, NULL ), -1);
  55440. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  55441. 0,NULL, NULL, NULL, NULL ), -1);
  55442. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  55443. 0,NULL, NULL, NULL, NULL ), -1);
  55444. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  55445. 0,NULL, NULL, NULL, NULL ), -1);
  55446. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  55447. 0,NULL, NULL, NULL, NULL ), -1);
  55448. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  55449. 0,NULL, NULL, NULL, NULL ), -1);
  55450. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  55451. /* test for supported class index */
  55452. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55453. 0,NULL, NULL, NULL, NULL );
  55454. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55455. 0,NULL, NULL, NULL, NULL );
  55456. AssertIntNE(idx1, -1);
  55457. AssertIntNE(idx2, -1);
  55458. AssertIntNE(idx1, idx2);
  55459. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55460. 0,NULL, NULL, NULL, NULL );
  55461. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55462. 0,NULL, NULL, NULL, NULL );
  55463. AssertIntNE(idx1, -1);
  55464. AssertIntNE(idx2, -1);
  55465. AssertIntNE(idx1, idx2);
  55466. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55467. 0,NULL, NULL, NULL, NULL );
  55468. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55469. 0,NULL, NULL, NULL, NULL );
  55470. AssertIntNE(idx1, -1);
  55471. AssertIntNE(idx2, -1);
  55472. AssertIntNE(idx1, idx2);
  55473. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55474. 0,NULL, NULL, NULL, NULL );
  55475. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55476. 0,NULL, NULL, NULL, NULL );
  55477. AssertIntNE(idx1, -1);
  55478. AssertIntNE(idx2, -1);
  55479. AssertIntNE(idx1, idx2);
  55480. res = TEST_RES_CHECK(1);
  55481. #endif /* HAVE_EX_DATA */
  55482. return res;
  55483. }
  55484. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  55485. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55486. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55487. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55488. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  55489. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  55490. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  55491. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  55492. #define SESSION_NEW_IDX_PTR "Testing"
  55493. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  55494. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55495. {
  55496. AssertNotNull(p);
  55497. AssertNull(ptr);
  55498. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  55499. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55500. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55501. }
  55502. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  55503. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  55504. void* arg)
  55505. {
  55506. AssertNotNull(out);
  55507. AssertNotNull(in);
  55508. AssertPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  55509. AssertPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  55510. AssertPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  55511. AssertIntEQ(argV, SESSION_NEW_IDX_LONG);
  55512. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55513. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  55514. return SSL_SUCCESS;
  55515. }
  55516. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  55517. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  55518. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55519. {
  55520. AssertNotNull(p);
  55521. AssertNull(ptr);
  55522. AssertPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  55523. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55524. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55525. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  55526. }
  55527. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55528. {
  55529. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  55530. (void*)SESSION_NEW_IDX_PTR,
  55531. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  55532. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  55533. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  55534. SSL_SESSION* s = SSL_SESSION_new();
  55535. SSL_SESSION* d = NULL;
  55536. AssertNotNull(s);
  55537. AssertPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  55538. AssertNotNull(d = SSL_SESSION_dup(s));
  55539. AssertPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  55540. SSL_SESSION_free(s);
  55541. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  55542. SSL_SESSION_free(d);
  55543. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  55544. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  55545. crypto_ex_cb_ctx_session = NULL;
  55546. return TEST_RES_CHECK(1);
  55547. }
  55548. #else
  55549. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55550. {
  55551. return TEST_SKIPPED;
  55552. }
  55553. #endif
  55554. static int test_wolfSSL_set_psk_use_session_callback(void)
  55555. {
  55556. int res = TEST_SKIPPED;
  55557. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  55558. SSL_CTX* ctx;
  55559. SSL* ssl;
  55560. const char* testCertFile;
  55561. const char* testKeyFile;
  55562. #ifdef WOLFSSL_TLS13
  55563. #ifdef NO_WOLFSSL_SERVER
  55564. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55565. #else
  55566. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55567. #endif
  55568. #ifndef NO_RSA
  55569. testCertFile = svrCertFile;
  55570. testKeyFile = svrKeyFile;
  55571. #elif defined(HAVE_ECC)
  55572. testCertFile = eccCertFile;
  55573. testKeyFile = eccKeyFile;
  55574. #else
  55575. testCertFile = NULL;
  55576. testKeyFile = NULL;
  55577. #endif
  55578. if (testCertFile != NULL && testKeyFile != NULL) {
  55579. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55580. SSL_FILETYPE_PEM));
  55581. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55582. SSL_FILETYPE_PEM));
  55583. }
  55584. ssl = SSL_new(ctx);
  55585. AssertNotNull(ssl);
  55586. SSL_set_psk_use_session_callback(ssl,
  55587. my_psk_use_session_cb);
  55588. AssertTrue(1);
  55589. SSL_CTX_free(ctx);
  55590. SSL_free(ssl);
  55591. #else
  55592. (void)ctx;
  55593. (void)ssl;
  55594. (void)testCertFile;
  55595. (void)testKeyFile;
  55596. #endif
  55597. res = TEST_RES_CHECK(1);
  55598. #endif
  55599. return res;
  55600. }
  55601. static int test_wolfSSL_ERR_strings(void)
  55602. {
  55603. int res = TEST_SKIPPED;
  55604. #if !defined(NO_ERROR_STRINGS)
  55605. EXPECT_DECLS;
  55606. const char* err1 = "unsupported cipher suite";
  55607. const char* err2 = "wolfSSL PEM routines";
  55608. const char* err = NULL;
  55609. (void)err;
  55610. (void)err1;
  55611. (void)err2;
  55612. #if defined(OPENSSL_EXTRA)
  55613. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  55614. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55615. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  55616. ExpectIntEQ((*err == '\0'), 1);
  55617. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  55618. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  55619. #else
  55620. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  55621. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55622. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  55623. ExpectIntEQ((*err == '\0'), 1);
  55624. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  55625. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  55626. ExpectIntEQ((*err == '\0'), 1);
  55627. #endif
  55628. res = EXPECT_RESULT();
  55629. #endif
  55630. return res;
  55631. }
  55632. static int test_wolfSSL_EVP_shake128(void)
  55633. {
  55634. int res = TEST_SKIPPED;
  55635. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55636. defined(WOLFSSL_SHAKE128)
  55637. EXPECT_DECLS;
  55638. const EVP_MD* md = NULL;
  55639. ExpectNotNull(md = EVP_shake128());
  55640. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  55641. res = EXPECT_RESULT();
  55642. #endif
  55643. return res;
  55644. }
  55645. static int test_wolfSSL_EVP_shake256(void)
  55646. {
  55647. int res = TEST_SKIPPED;
  55648. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55649. defined(WOLFSSL_SHAKE256)
  55650. EXPECT_DECLS;
  55651. const EVP_MD* md = NULL;
  55652. ExpectNotNull(md = EVP_shake256());
  55653. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  55654. res = EXPECT_RESULT();
  55655. #endif
  55656. return res;
  55657. }
  55658. static int test_EVP_blake2(void)
  55659. {
  55660. int res = TEST_SKIPPED;
  55661. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  55662. EXPECT_DECLS;
  55663. const EVP_MD* md = NULL;
  55664. (void)md;
  55665. #if defined(HAVE_BLAKE2)
  55666. ExpectNotNull(md = EVP_blake2b512());
  55667. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  55668. #endif
  55669. #if defined(HAVE_BLAKE2S)
  55670. ExpectNotNull(md = EVP_blake2s256());
  55671. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  55672. #endif
  55673. res = EXPECT_RESULT();
  55674. #endif
  55675. return res;
  55676. }
  55677. #if defined(OPENSSL_EXTRA)
  55678. static void list_md_fn(const EVP_MD* m, const char* from,
  55679. const char* to, void* arg)
  55680. {
  55681. const char* mn;
  55682. BIO *bio;
  55683. (void) from;
  55684. (void) to;
  55685. (void) arg;
  55686. (void) mn;
  55687. (void) bio;
  55688. if (!m) {
  55689. /* alias */
  55690. AssertNull(m);
  55691. AssertNotNull(to);
  55692. }
  55693. else {
  55694. AssertNotNull(m);
  55695. AssertNull(to);
  55696. }
  55697. AssertNotNull(from);
  55698. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55699. mn = EVP_get_digestbyname(from);
  55700. /* print to stderr */
  55701. AssertNotNull(arg);
  55702. bio = BIO_new(BIO_s_file());
  55703. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55704. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  55705. BIO_free(bio);
  55706. #endif
  55707. }
  55708. #endif
  55709. static int test_EVP_MD_do_all(void)
  55710. {
  55711. int res = TEST_SKIPPED;
  55712. #if defined(OPENSSL_EXTRA)
  55713. EVP_MD_do_all(NULL, stderr);
  55714. EVP_MD_do_all(list_md_fn, stderr);
  55715. res = TEST_SUCCESS;
  55716. #endif
  55717. return res;
  55718. }
  55719. #if defined(OPENSSL_EXTRA)
  55720. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  55721. {
  55722. (void)arg;
  55723. (void)nm;
  55724. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  55725. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55726. /* print to stderr */
  55727. AssertNotNull(arg);
  55728. BIO *bio = BIO_new(BIO_s_file());
  55729. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55730. BIO_printf(bio, "%s\n", nm);
  55731. BIO_free(bio);
  55732. #endif
  55733. }
  55734. #endif
  55735. static int test_OBJ_NAME_do_all(void)
  55736. {
  55737. int res = TEST_SKIPPED;
  55738. #if defined(OPENSSL_EXTRA)
  55739. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  55740. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  55741. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  55742. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  55743. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  55744. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  55745. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  55746. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  55747. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  55748. res = TEST_SUCCESS;
  55749. #endif
  55750. return res;
  55751. }
  55752. static int test_SSL_CIPHER_get_xxx(void)
  55753. {
  55754. int res = TEST_SKIPPED;
  55755. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  55756. !defined(NO_FILESYSTEM)
  55757. EXPECT_DECLS;
  55758. const SSL_CIPHER* cipher = NULL;
  55759. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  55760. int i, numCiphers = 0;
  55761. SSL_CTX* ctx = NULL;
  55762. SSL* ssl = NULL;
  55763. const char* testCertFile;
  55764. const char* testKeyFile;
  55765. char buf[256] = {0};
  55766. const char* cipher_id = NULL;
  55767. int expect_nid1 = NID_undef;
  55768. int expect_nid2 = NID_undef;
  55769. int expect_nid3 = NID_undef;
  55770. int expect_nid4 = NID_undef;
  55771. int expect_nid5 = 0;
  55772. const char* cipher_id2 = NULL;
  55773. int expect_nid21 = NID_undef;
  55774. int expect_nid22 = NID_undef;
  55775. int expect_nid23 = NID_undef;
  55776. int expect_nid24 = NID_undef;
  55777. int expect_nid25 = 0;
  55778. (void)cipher;
  55779. (void)supportedCiphers;
  55780. (void)i;
  55781. (void)numCiphers;
  55782. (void)ctx;
  55783. (void)ssl;
  55784. (void)testCertFile;
  55785. (void)testKeyFile;
  55786. #if defined(WOLFSSL_TLS13)
  55787. cipher_id = "TLS13-AES128-GCM-SHA256";
  55788. expect_nid1 = NID_auth_rsa;
  55789. expect_nid2 = NID_aes_128_gcm;
  55790. expect_nid3 = NID_sha256;
  55791. expect_nid4 = NID_kx_any;
  55792. expect_nid5 = 1;
  55793. #if !defined(WOLFSSL_NO_TLS12)
  55794. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  55795. expect_nid21 = NID_auth_rsa;
  55796. expect_nid22 = NID_aes_256_gcm;
  55797. expect_nid23 = NID_sha384;
  55798. expect_nid24 = NID_kx_ecdhe;
  55799. expect_nid25 = 1;
  55800. #endif
  55801. #endif
  55802. #ifdef NO_WOLFSSL_SERVER
  55803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55804. #else
  55805. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55806. #endif
  55807. if (cipher_id) {
  55808. #ifndef NO_RSA
  55809. testCertFile = svrCertFile;
  55810. testKeyFile = svrKeyFile;
  55811. #elif defined(HAVE_ECC)
  55812. testCertFile = eccCertFile;
  55813. testKeyFile = eccKeyFile;
  55814. #else
  55815. testCertFile = NULL;
  55816. testKeyFile = NULL;
  55817. #endif
  55818. if (testCertFile != NULL && testKeyFile != NULL) {
  55819. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55820. SSL_FILETYPE_PEM));
  55821. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55822. SSL_FILETYPE_PEM));
  55823. }
  55824. ExpectNotNull(ssl = SSL_new(ctx));
  55825. ExpectIntEQ(SSL_in_init(ssl), 1);
  55826. supportedCiphers = SSL_get_ciphers(ssl);
  55827. numCiphers = sk_num(supportedCiphers);
  55828. for (i = 0; i < numCiphers; ++i) {
  55829. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55830. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55831. }
  55832. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  55833. break;
  55834. }
  55835. }
  55836. /* test case for */
  55837. if (i != numCiphers) {
  55838. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  55839. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  55840. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  55841. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  55842. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  55843. }
  55844. if (cipher_id2) {
  55845. for (i = 0; i < numCiphers; ++i) {
  55846. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55847. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55848. }
  55849. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  55850. break;
  55851. }
  55852. }
  55853. /* test case for */
  55854. if (i != numCiphers) {
  55855. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  55856. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  55857. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  55858. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  55859. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  55860. }
  55861. }
  55862. }
  55863. SSL_CTX_free(ctx);
  55864. SSL_free(ssl);
  55865. res = EXPECT_RESULT();
  55866. #endif
  55867. return res;
  55868. }
  55869. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55870. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  55871. int* keyFormat)
  55872. {
  55873. int ret;
  55874. byte* key_buf = NULL;
  55875. size_t key_sz = 0;
  55876. EncryptedInfo encInfo;
  55877. XMEMSET(&encInfo, 0, sizeof(encInfo));
  55878. ret = load_file(privKeyFile, &key_buf, &key_sz);
  55879. if (ret == 0) {
  55880. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  55881. NULL, &encInfo, keyFormat);
  55882. }
  55883. if (key_buf != NULL) {
  55884. free(key_buf); key_buf = NULL;
  55885. }
  55886. (void)encInfo; /* not used in this test */
  55887. #ifdef DEBUG_WOLFSSL
  55888. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  55889. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  55890. (*pDer)->length);
  55891. #endif
  55892. return ret;
  55893. }
  55894. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  55895. {
  55896. int ret = CRYPTOCB_UNAVAILABLE;
  55897. const char* privKeyFile = (const char*)ctx;
  55898. DerBuffer* pDer = NULL;
  55899. int keyFormat = 0;
  55900. if (info->algo_type == WC_ALGO_TYPE_PK) {
  55901. #ifdef DEBUG_WOLFSSL
  55902. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  55903. #endif
  55904. #ifndef NO_RSA
  55905. if (info->pk.type == WC_PK_TYPE_RSA) {
  55906. switch (info->pk.rsa.type) {
  55907. case RSA_PUBLIC_ENCRYPT:
  55908. case RSA_PUBLIC_DECRYPT:
  55909. /* perform software based RSA public op */
  55910. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55911. break;
  55912. case RSA_PRIVATE_ENCRYPT:
  55913. case RSA_PRIVATE_DECRYPT:
  55914. {
  55915. RsaKey key;
  55916. /* perform software based RSA private op */
  55917. #ifdef DEBUG_WOLFSSL
  55918. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55919. #endif
  55920. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55921. &keyFormat);
  55922. if (ret != 0) {
  55923. return ret;
  55924. }
  55925. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55926. if (ret == 0) {
  55927. word32 keyIdx = 0;
  55928. /* load RSA private key and perform private transform */
  55929. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55930. &key, pDer->length);
  55931. if (ret == 0) {
  55932. ret = wc_RsaFunction(
  55933. info->pk.rsa.in, info->pk.rsa.inLen,
  55934. info->pk.rsa.out, info->pk.rsa.outLen,
  55935. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55936. }
  55937. else {
  55938. /* if decode fails, then fall-back to software based crypto */
  55939. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55940. "key decode failed %d, falling back to "
  55941. "software\n", ret);
  55942. ret = CRYPTOCB_UNAVAILABLE;
  55943. }
  55944. wc_FreeRsaKey(&key);
  55945. }
  55946. wc_FreeDer(&pDer); pDer = NULL;
  55947. break;
  55948. }
  55949. }
  55950. #ifdef DEBUG_WOLFSSL
  55951. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55952. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55953. #endif
  55954. }
  55955. #endif /* !NO_RSA */
  55956. #ifdef HAVE_ECC
  55957. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55958. /* mark this key as ephemeral */
  55959. if (info->pk.eckg.key != NULL) {
  55960. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  55961. sizeof(info->pk.eckg.key->label));
  55962. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  55963. }
  55964. }
  55965. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  55966. ecc_key key;
  55967. /* perform software based ECC sign */
  55968. #ifdef DEBUG_WOLFSSL
  55969. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  55970. #endif
  55971. if (info->pk.eccsign.key != NULL &&
  55972. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  55973. /* this is an empheral key */
  55974. #ifdef DEBUG_WOLFSSL
  55975. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  55976. "ephemeral key\n");
  55977. #endif
  55978. return CRYPTOCB_UNAVAILABLE;
  55979. }
  55980. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55981. if (ret != 0) {
  55982. return ret;
  55983. }
  55984. ret = wc_ecc_init(&key);
  55985. if (ret == 0) {
  55986. word32 keyIdx = 0;
  55987. /* load ECC private key and perform private transform */
  55988. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  55989. &key, pDer->length);
  55990. if (ret == 0) {
  55991. ret = wc_ecc_sign_hash(
  55992. info->pk.eccsign.in, info->pk.eccsign.inlen,
  55993. info->pk.eccsign.out, info->pk.eccsign.outlen,
  55994. info->pk.eccsign.rng, &key);
  55995. }
  55996. else {
  55997. /* if decode fails, then fall-back to software based crypto */
  55998. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  55999. "decode failed %d, falling back to software\n", ret);
  56000. ret = CRYPTOCB_UNAVAILABLE;
  56001. }
  56002. wc_ecc_free(&key);
  56003. }
  56004. wc_FreeDer(&pDer); pDer = NULL;
  56005. #ifdef DEBUG_WOLFSSL
  56006. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  56007. ret, *info->pk.eccsign.outlen);
  56008. #endif
  56009. }
  56010. #endif /* HAVE_ECC */
  56011. #ifdef HAVE_ED25519
  56012. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  56013. ed25519_key key;
  56014. /* perform software based ED25519 sign */
  56015. #ifdef DEBUG_WOLFSSL
  56016. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  56017. #endif
  56018. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56019. if (ret != 0) {
  56020. return ret;
  56021. }
  56022. ret = wc_ed25519_init(&key);
  56023. if (ret == 0) {
  56024. word32 keyIdx = 0;
  56025. /* load ED25519 private key and perform private transform */
  56026. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  56027. &key, pDer->length);
  56028. if (ret == 0) {
  56029. /* calculate public key */
  56030. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  56031. if (ret == 0) {
  56032. key.pubKeySet = 1;
  56033. ret = wc_ed25519_sign_msg_ex(
  56034. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  56035. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  56036. &key, info->pk.ed25519sign.type,
  56037. info->pk.ed25519sign.context,
  56038. info->pk.ed25519sign.contextLen);
  56039. }
  56040. }
  56041. else {
  56042. /* if decode fails, then fall-back to software based crypto */
  56043. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  56044. "decode failed %d, falling back to software\n", ret);
  56045. ret = CRYPTOCB_UNAVAILABLE;
  56046. }
  56047. wc_ed25519_free(&key);
  56048. }
  56049. wc_FreeDer(&pDer); pDer = NULL;
  56050. #ifdef DEBUG_WOLFSSL
  56051. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  56052. ret, *info->pk.ed25519sign.outLen);
  56053. #endif
  56054. }
  56055. #endif /* HAVE_ED25519 */
  56056. }
  56057. (void)thisDevId;
  56058. (void)keyFormat;
  56059. return ret;
  56060. }
  56061. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56062. static void test_wc_CryptoCb_TLS(int tlsVer,
  56063. const char* cliCaPemFile, const char* cliCertPemFile,
  56064. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  56065. const char* svrCaPemFile, const char* svrCertPemFile,
  56066. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  56067. {
  56068. callback_functions client_cbf;
  56069. callback_functions server_cbf;
  56070. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56071. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56072. if (tlsVer == WOLFSSL_TLSV1_3) {
  56073. #ifdef WOLFSSL_TLS13
  56074. server_cbf.method = wolfTLSv1_3_server_method;
  56075. client_cbf.method = wolfTLSv1_3_client_method;
  56076. #endif
  56077. }
  56078. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56079. #ifndef WOLFSSL_NO_TLS12
  56080. server_cbf.method = wolfTLSv1_2_server_method;
  56081. client_cbf.method = wolfTLSv1_2_client_method;
  56082. #endif
  56083. }
  56084. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56085. #ifndef NO_OLD_TLS
  56086. server_cbf.method = wolfTLSv1_1_server_method;
  56087. client_cbf.method = wolfTLSv1_1_client_method;
  56088. #endif
  56089. }
  56090. else if (tlsVer == WOLFSSL_TLSV1) {
  56091. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56092. server_cbf.method = wolfTLSv1_server_method;
  56093. client_cbf.method = wolfTLSv1_client_method;
  56094. #endif
  56095. }
  56096. else if (tlsVer == WOLFSSL_SSLV3) {
  56097. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56098. defined(WOLFSSL_STATIC_RSA)
  56099. server_cbf.method = wolfSSLv3_server_method;
  56100. client_cbf.method = wolfSSLv3_client_method;
  56101. #endif
  56102. }
  56103. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56104. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56105. server_cbf.method = wolfDTLSv1_2_server_method;
  56106. client_cbf.method = wolfDTLSv1_2_client_method;
  56107. #endif
  56108. }
  56109. else if (tlsVer == WOLFSSL_DTLSV1) {
  56110. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56111. server_cbf.method = wolfDTLSv1_server_method;
  56112. client_cbf.method = wolfDTLSv1_client_method;
  56113. #endif
  56114. }
  56115. if (server_cbf.method == NULL) {
  56116. /* not enabled */
  56117. return;
  56118. }
  56119. /* Setup the keys for the TLS test */
  56120. client_cbf.certPemFile = cliCertPemFile;
  56121. client_cbf.keyPemFile = cliPubKeyPemFile;
  56122. client_cbf.caPemFile = cliCaPemFile;
  56123. server_cbf.certPemFile = svrCertPemFile;
  56124. server_cbf.keyPemFile = svrPubKeyPemFile;
  56125. server_cbf.caPemFile = svrCaPemFile;
  56126. /* Setup a crypto callback with pointer to private key file for testing */
  56127. client_cbf.devId = 1;
  56128. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  56129. (void*)cliPrivKeyPemFile);
  56130. server_cbf.devId = 2;
  56131. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  56132. (void*)svrPrivKeyPemFile);
  56133. /* Perform TLS server and client test */
  56134. /* First test is at WOLFSSL_CTX level */
  56135. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56136. /* Check for success */
  56137. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56138. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56139. /* Second test is a WOLFSSL object level */
  56140. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56141. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56142. /* Check for success */
  56143. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56144. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56145. /* Un register the devId's */
  56146. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  56147. client_cbf.devId = INVALID_DEVID;
  56148. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  56149. server_cbf.devId = INVALID_DEVID;
  56150. }
  56151. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  56152. static int test_wc_CryptoCb(void)
  56153. {
  56154. int res = TEST_SKIPPED;
  56155. #ifdef WOLF_CRYPTO_CB
  56156. /* TODO: Add crypto callback API tests */
  56157. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56158. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56159. int tlsVer;
  56160. #endif
  56161. #ifndef NO_RSA
  56162. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56163. test_wc_CryptoCb_TLS(tlsVer,
  56164. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  56165. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  56166. }
  56167. #endif
  56168. #ifdef HAVE_ECC
  56169. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56170. test_wc_CryptoCb_TLS(tlsVer,
  56171. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  56172. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  56173. }
  56174. #endif
  56175. #ifdef HAVE_ED25519
  56176. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56177. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56178. test_wc_CryptoCb_TLS(tlsVer,
  56179. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  56180. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  56181. }
  56182. #endif
  56183. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56184. res = TEST_RES_CHECK(1);
  56185. #endif /* WOLF_CRYPTO_CB */
  56186. return res;
  56187. }
  56188. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56189. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56190. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  56191. const char* cliCaPemFile, const char* cliCertPemFile,
  56192. const char* cliPrivKeyPemFile,
  56193. const char* svrCaPemFile, const char* svrCertPemFile,
  56194. const char* svrPrivKeyPemFile,
  56195. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  56196. {
  56197. callback_functions client_cbf;
  56198. callback_functions server_cbf;
  56199. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56200. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56201. if (tlsVer == WOLFSSL_TLSV1_3) {
  56202. #ifdef WOLFSSL_TLS13
  56203. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  56204. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  56205. #endif
  56206. }
  56207. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56208. #ifndef WOLFSSL_NO_TLS12
  56209. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  56210. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  56211. #endif
  56212. }
  56213. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56214. #ifndef NO_OLD_TLS
  56215. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  56216. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  56217. #endif
  56218. }
  56219. else if (tlsVer == WOLFSSL_TLSV1) {
  56220. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56221. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  56222. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  56223. #endif
  56224. }
  56225. else if (tlsVer == WOLFSSL_SSLV3) {
  56226. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56227. defined(WOLFSSL_STATIC_RSA)
  56228. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  56229. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  56230. #endif
  56231. }
  56232. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56233. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56234. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  56235. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  56236. #endif
  56237. }
  56238. else if (tlsVer == WOLFSSL_DTLSV1) {
  56239. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56240. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  56241. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  56242. #endif
  56243. }
  56244. if (server_cbf.method_ex == NULL) {
  56245. /* not enabled */
  56246. return;
  56247. }
  56248. /* Setup the keys for the TLS test */
  56249. client_cbf.certPemFile = cliCertPemFile;
  56250. client_cbf.keyPemFile = cliPrivKeyPemFile;
  56251. client_cbf.caPemFile = cliCaPemFile;
  56252. server_cbf.certPemFile = svrCertPemFile;
  56253. server_cbf.keyPemFile = svrPrivKeyPemFile;
  56254. server_cbf.caPemFile = svrCaPemFile;
  56255. client_cbf.mem = cliMem;
  56256. client_cbf.memSz = cliMemSz;
  56257. server_cbf.mem = svrMem;
  56258. server_cbf.memSz = svrMemSz;
  56259. client_cbf.devId = INVALID_DEVID;
  56260. server_cbf.devId = INVALID_DEVID;
  56261. /* Perform TLS server and client test */
  56262. /* First test is at WOLFSSL_CTX level */
  56263. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56264. /* Check for success */
  56265. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56266. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56267. /* Second test is a WOLFSSL object level */
  56268. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56269. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56270. /* Check for success */
  56271. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56272. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56273. }
  56274. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  56275. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56276. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  56277. defined(SESSION_CERTS)
  56278. #ifdef OPENSSL_EXTRA
  56279. #define TEST_TLS_STATIC_MEMSZ (400000)
  56280. #else
  56281. #define TEST_TLS_STATIC_MEMSZ (320000)
  56282. #endif
  56283. #else
  56284. #define TEST_TLS_STATIC_MEMSZ (80000)
  56285. #endif
  56286. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  56287. {
  56288. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  56289. WOLFSSL_MEM_STATS mem_stats;
  56290. WOLFSSL_MEM_CONN_STATS ssl_stats;
  56291. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  56292. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  56293. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56294. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  56295. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56296. #endif
  56297. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  56298. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  56299. /* this should fail because kMaxCtxClients == 2 */
  56300. AssertNull((ssl3 = wolfSSL_new(ctx)));
  56301. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  56302. #ifdef DEBUG_WOLFSSL
  56303. wolfSSL_PrintStatsConn(&ssl_stats);
  56304. #endif
  56305. (void)ssl_stats;
  56306. }
  56307. /* display collected statistics */
  56308. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  56309. #ifdef DEBUG_WOLFSSL
  56310. wolfSSL_PrintStats(&mem_stats);
  56311. #endif
  56312. (void)mem_stats;
  56313. }
  56314. wolfSSL_free(ssl1);
  56315. wolfSSL_free(ssl2);
  56316. return TEST_RES_CHECK(1);
  56317. }
  56318. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56319. static int test_wolfSSL_CTX_StaticMemory(void)
  56320. {
  56321. int res = TEST_SKIPPED;
  56322. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56323. wolfSSL_method_func method_func;
  56324. WOLFSSL_CTX* ctx;
  56325. const int kMaxCtxClients = 2;
  56326. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56327. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56328. int tlsVer;
  56329. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  56330. #endif
  56331. #endif
  56332. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  56333. #ifndef NO_WOLFSSL_SERVER
  56334. #ifndef WOLFSSL_NO_TLS12
  56335. method_func = wolfTLSv1_2_server_method_ex;
  56336. #else
  56337. method_func = wolfTLSv1_3_server_method_ex;
  56338. #endif
  56339. #else
  56340. #ifndef WOLFSSL_NO_TLS12
  56341. method_func = wolfTLSv1_2_client_method_ex;
  56342. #else
  56343. method_func = wolfTLSv1_3_client_method_ex;
  56344. #endif
  56345. #endif
  56346. /* Test creating CTX directly from static memory pool */
  56347. ctx = NULL;
  56348. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  56349. &ctx, method_func, svrMem, sizeof(svrMem),
  56350. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56351. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  56352. wolfSSL_CTX_free(ctx);
  56353. ctx = NULL;
  56354. /* Test for heap allocated CTX, then assigning static pool to it */
  56355. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  56356. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  56357. NULL, svrMem, sizeof(svrMem),
  56358. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56359. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  56360. wolfSSL_CTX_free(ctx);
  56361. /* TLS Level Tests using static memory */
  56362. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56363. #ifndef NO_RSA
  56364. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56365. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56366. svrCertFile, cliCertFile, cliKeyFile,
  56367. cliCertFile, svrCertFile, svrKeyFile,
  56368. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  56369. }
  56370. #endif
  56371. #ifdef HAVE_ECC
  56372. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56373. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56374. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  56375. cliEccCertFile, eccCertFile, eccKeyFile,
  56376. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  56377. }
  56378. #endif
  56379. #ifdef HAVE_ED25519
  56380. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56381. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56382. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56383. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  56384. cliEdCertFile, edCertFile, edKeyFile,
  56385. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  56386. }
  56387. #endif
  56388. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56389. res = TEST_RES_CHECK(1);
  56390. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56391. return res;
  56392. }
  56393. static int test_openssl_FIPS_drbg(void)
  56394. {
  56395. int res = TEST_SKIPPED;
  56396. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  56397. DRBG_CTX* dctx;
  56398. byte data1[32], data2[32], zeroData[32];
  56399. byte testSeed[16];
  56400. size_t dlen = sizeof(data1);
  56401. int i;
  56402. XMEMSET(data1, 0, dlen);
  56403. XMEMSET(data2, 0, dlen);
  56404. XMEMSET(zeroData, 0, sizeof(zeroData));
  56405. for (i=0; i<(int)sizeof(testSeed); i++) {
  56406. testSeed[i] = (byte)i;
  56407. }
  56408. AssertNotNull(dctx = FIPS_get_default_drbg());
  56409. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  56410. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  56411. WOLFSSL_SUCCESS);
  56412. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  56413. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  56414. WOLFSSL_SUCCESS);
  56415. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56416. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  56417. WOLFSSL_SUCCESS);
  56418. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  56419. WOLFSSL_SUCCESS);
  56420. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56421. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  56422. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  56423. #ifndef HAVE_GLOBAL_RNG
  56424. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  56425. wolfSSL_FIPS_drbg_free(dctx);
  56426. #endif
  56427. res = TEST_RES_CHECK(1);
  56428. #endif
  56429. return res;
  56430. }
  56431. static int test_wolfSSL_FIPS_mode(void)
  56432. {
  56433. int res = TEST_SKIPPED;
  56434. #if defined(OPENSSL_ALL)
  56435. #ifdef HAVE_FIPS
  56436. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  56437. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  56438. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  56439. #else
  56440. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  56441. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  56442. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  56443. #endif
  56444. res = TEST_RES_CHECK(1);
  56445. #endif
  56446. return res;
  56447. }
  56448. #ifdef WOLFSSL_DTLS
  56449. /* Prints out the current window */
  56450. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  56451. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56452. int i;
  56453. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  56454. word32 b = w[i];
  56455. int j;
  56456. /* Prints out a 32 bit binary number in big endian order */
  56457. for (j = 0; j < 32; j++, b <<= 1) {
  56458. if (b & (((word32)1) << 31))
  56459. fprintf(stderr, "1");
  56460. else
  56461. fprintf(stderr, "0");
  56462. }
  56463. fprintf(stderr, " ");
  56464. }
  56465. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  56466. #else
  56467. (void)h;
  56468. (void)l;
  56469. (void)w;
  56470. #endif
  56471. }
  56472. /* a - cur_hi
  56473. * b - cur_lo
  56474. * c - next_hi
  56475. * d - next_lo
  56476. * e - window
  56477. * f - expected next_hi
  56478. * g - expected next_lo
  56479. * h - expected window[1]
  56480. * i - expected window[0]
  56481. */
  56482. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  56483. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  56484. DUW_TEST_print_window_binary((a), (b), (e)); \
  56485. AssertIntEQ((c), (f)); \
  56486. AssertIntEQ((d), (g)); \
  56487. AssertIntEQ((e)[1], (h)); \
  56488. AssertIntEQ((e)[0], (i)); \
  56489. } while (0)
  56490. static int test_wolfSSL_DtlsUpdateWindow(void)
  56491. {
  56492. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  56493. word32 next_lo = 0;
  56494. word16 next_hi = 0;
  56495. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56496. fprintf(stderr, "\n");
  56497. #endif
  56498. XMEMSET(window, 0, sizeof window);
  56499. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  56500. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  56501. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  56502. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  56503. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  56504. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  56505. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  56506. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  56507. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  56508. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  56509. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  56510. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  56511. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  56512. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  56513. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  56514. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  56515. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  56516. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  56517. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  56518. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  56519. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  56520. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56521. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56522. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  56523. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  56524. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56525. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56526. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56527. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56528. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56529. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  56530. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  56531. return TEST_RES_CHECK(1);
  56532. }
  56533. #endif /* WOLFSSL_DTLS */
  56534. #ifdef WOLFSSL_DTLS
  56535. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  56536. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  56537. {
  56538. DtlsMsg* cur;
  56539. static byte msg[100];
  56540. static byte msgInit = 0;
  56541. if (!msgInit) {
  56542. int i;
  56543. for (i = 0; i < 100; i++)
  56544. msg[i] = i + 1;
  56545. msgInit = 1;
  56546. }
  56547. /* Sanitize test parameters */
  56548. if (len > sizeof(msg))
  56549. return -1;
  56550. if (f_offset + f_len > sizeof(msg))
  56551. return -1;
  56552. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  56553. if (ssl->dtls_rx_msg_list == NULL)
  56554. return -100;
  56555. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  56556. return -200;
  56557. if (cur->fragBucketListCount != f_count)
  56558. return -300;
  56559. if (cur->ready != ready)
  56560. return -400;
  56561. if (cur->bytesReceived != bytesReceived)
  56562. return -500;
  56563. if (ready) {
  56564. if (cur->fragBucketList != NULL)
  56565. return -600;
  56566. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  56567. return -700;
  56568. }
  56569. else {
  56570. DtlsFragBucket* fb;
  56571. if (cur->fragBucketList == NULL)
  56572. return -800;
  56573. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  56574. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  56575. return -900;
  56576. }
  56577. }
  56578. return 0;
  56579. }
  56580. static void DFB_TEST_RESET(WOLFSSL* ssl)
  56581. {
  56582. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  56583. ssl->dtls_rx_msg_list = NULL;
  56584. ssl->dtls_rx_msg_list_sz = 0;
  56585. }
  56586. static int test_wolfSSL_DTLS_fragment_buckets(void)
  56587. {
  56588. WOLFSSL ssl[1];
  56589. XMEMSET(ssl, 0, sizeof(*ssl));
  56590. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  56591. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56592. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  56593. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  56594. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  56595. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  56596. /* Test all permutations of 3 regions */
  56597. /* 1 2 3 */
  56598. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56599. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  56600. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56601. /* 1 3 2 */
  56602. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56603. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  56604. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56605. /* 2 1 3 */
  56606. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56607. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  56608. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56609. /* 2 3 1 */
  56610. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56611. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  56612. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56613. /* 3 1 2 */
  56614. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56615. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  56616. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56617. /* 3 2 1 */
  56618. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56619. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  56620. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56621. /* Test overlapping regions */
  56622. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56623. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  56624. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  56625. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  56626. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  56627. /* Test overlapping multiple regions */
  56628. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56629. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  56630. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  56631. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  56632. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  56633. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  56634. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  56635. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  56636. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  56637. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  56638. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56639. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  56640. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  56641. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  56642. DFB_TEST_RESET(ssl);
  56643. return TEST_RES_CHECK(1);
  56644. }
  56645. #endif
  56646. #if !defined(NO_FILESYSTEM) && \
  56647. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56648. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56649. !defined(NO_RSA)
  56650. static int test_wolfSSL_dtls_stateless2(void)
  56651. {
  56652. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  56653. struct test_memio_ctx test_ctx;
  56654. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56655. int ret;
  56656. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56657. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56658. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  56659. if (ret != 0)
  56660. return -1;
  56661. ssl_c2 = wolfSSL_new(ctx_c);
  56662. if (ssl_c2 == NULL)
  56663. return -2;
  56664. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56665. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56666. /* send CH */
  56667. ret = wolfSSL_connect(ssl_c2);
  56668. if (ret == 0 || ssl_c2->error != WANT_READ)
  56669. return -3;
  56670. ret = wolfSSL_accept(ssl_s);
  56671. if (ret == 0 || ssl_s->error != WANT_READ)
  56672. return -4;
  56673. if (test_ctx.c_len == 0)
  56674. return -5;
  56675. /* consume HRR */
  56676. test_ctx.c_len = 0;
  56677. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56678. if (ret != 0)
  56679. return -6;
  56680. wolfSSL_free(ssl_c2);
  56681. wolfSSL_free(ssl_c);
  56682. wolfSSL_free(ssl_s);
  56683. wolfSSL_CTX_free(ctx_c);
  56684. wolfSSL_CTX_free(ctx_s);
  56685. return TEST_SUCCESS;
  56686. }
  56687. #ifdef HAVE_MAX_FRAGMENT
  56688. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  56689. {
  56690. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  56691. struct test_memio_ctx test_ctx;
  56692. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56693. word16 max_fragment;
  56694. int ret;
  56695. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56696. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56697. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  56698. if (ret != 0)
  56699. return -1;
  56700. ssl_c2 = wolfSSL_new(ctx_c);
  56701. if (ssl_c2 == NULL)
  56702. return -2;
  56703. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  56704. if (ret != WOLFSSL_SUCCESS)
  56705. return -3;
  56706. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56707. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56708. max_fragment = ssl_s->max_fragment;
  56709. /* send CH */
  56710. ret = wolfSSL_connect(ssl_c2);
  56711. if (ret == 0 || ssl_c2->error != WANT_READ)
  56712. return -4;
  56713. ret = wolfSSL_accept(ssl_s);
  56714. if (ret == 0 || ssl_s->error != WANT_READ)
  56715. return -5;
  56716. /* CH without cookie shouldn't change state */
  56717. if (ssl_s->max_fragment != max_fragment)
  56718. return -6;
  56719. if (test_ctx.c_len == 0)
  56720. return -7;
  56721. /* consume HRR from buffer */
  56722. test_ctx.c_len = 0;
  56723. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56724. if (ret != 0)
  56725. return -8;
  56726. wolfSSL_free(ssl_c2);
  56727. wolfSSL_free(ssl_c);
  56728. wolfSSL_free(ssl_s);
  56729. wolfSSL_CTX_free(ctx_c);
  56730. wolfSSL_CTX_free(ctx_s);
  56731. return TEST_SUCCESS;
  56732. }
  56733. #endif /* HAVE_MAX_FRAGMENT */
  56734. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  56735. #define ROUNDS_WITH_HVR 4
  56736. #define ROUNDS_WITHOUT_HVR 2
  56737. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  56738. static int buf_is_hvr(const byte *data, int len)
  56739. {
  56740. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  56741. return 0;
  56742. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  56743. }
  56744. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  56745. {
  56746. struct test_memio_ctx test_ctx;
  56747. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56748. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56749. WOLFSSL_SESSION *sess;
  56750. int ret, round_trips;
  56751. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56752. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56753. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  56754. if (ret != 0)
  56755. return -1;
  56756. #ifdef HAVE_SESSION_TICKET
  56757. if (useticket) {
  56758. ret = wolfSSL_UseSessionTicket(ssl_c);
  56759. if (ret != WOLFSSL_SUCCESS)
  56760. return -2;
  56761. }
  56762. #endif
  56763. round_trips = ROUNDS_WITH_HVR;
  56764. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  56765. if (ret != 0)
  56766. return -3;
  56767. if (round_trips != ROUNDS_WITH_HVR)
  56768. return -4;
  56769. sess = wolfSSL_get1_session(ssl_c);
  56770. if (sess == NULL)
  56771. return -5;
  56772. wolfSSL_shutdown(ssl_c);
  56773. wolfSSL_shutdown(ssl_s);
  56774. wolfSSL_free(ssl_c);
  56775. wolfSSL_free(ssl_s);
  56776. test_ctx.c_len = test_ctx.s_len = 0;
  56777. /* make resumption invalid */
  56778. if (bad) {
  56779. if (useticket) {
  56780. #ifdef HAVE_SESSION_TICKET
  56781. sess->ticket[0] = !sess->ticket[0];
  56782. #endif /* HAVE_SESSION_TICKET */
  56783. }
  56784. else {
  56785. sess->sessionID[0] = !sess->sessionID[0];
  56786. }
  56787. }
  56788. ssl_c = wolfSSL_new(ctx_c);
  56789. ssl_s = wolfSSL_new(ctx_s);
  56790. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56791. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56792. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56793. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56794. ret = wolfSSL_set_session(ssl_c, sess);
  56795. if (ret != WOLFSSL_SUCCESS)
  56796. return -6;
  56797. ret = wolfSSL_connect(ssl_c);
  56798. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  56799. return -7;
  56800. ret = wolfSSL_accept(ssl_s);
  56801. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  56802. return -8;
  56803. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  56804. return -9;
  56805. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  56806. return -10;
  56807. if (!useticket) {
  56808. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  56809. if (ret != 0)
  56810. return -11;
  56811. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  56812. return -12;
  56813. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  56814. return -13;
  56815. }
  56816. wolfSSL_SESSION_free(sess);
  56817. wolfSSL_free(ssl_c);
  56818. wolfSSL_free(ssl_s);
  56819. wolfSSL_CTX_free(ctx_c);
  56820. wolfSSL_CTX_free(ctx_s);
  56821. return 0;
  56822. }
  56823. static int test_wolfSSL_dtls_stateless_resume(void)
  56824. {
  56825. int ret;
  56826. #ifdef HAVE_SESSION_TICKET
  56827. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  56828. if (ret != 0)
  56829. return TEST_RES_CHECK(ret);
  56830. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  56831. if (ret != 0)
  56832. return TEST_RES_CHECK(ret - 100);
  56833. #endif /* HAVE_SESION_TICKET */
  56834. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  56835. if (ret != 0)
  56836. return TEST_RES_CHECK(ret - 200);
  56837. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  56838. if (ret != 0)
  56839. return TEST_RES_CHECK(ret - 300);
  56840. return TEST_RES_CHECK(TEST_SUCCESS);
  56841. }
  56842. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  56843. #if !defined(NO_OLD_TLS)
  56844. static int test_wolfSSL_dtls_stateless_downgrade(void)
  56845. {
  56846. WOLFSSL_CTX *ctx_c = NULL, *ctx_c2 = NULL, *ctx_s = NULL;
  56847. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  56848. struct test_memio_ctx test_ctx;
  56849. int ret;
  56850. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56851. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56852. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  56853. if (ret != 0)
  56854. return -1;
  56855. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  56856. if (ret != WOLFSSL_SUCCESS)
  56857. return -2;
  56858. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  56859. if (ctx_c2 == NULL)
  56860. return -3;
  56861. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  56862. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  56863. ssl_c2 = wolfSSL_new(ctx_c2);
  56864. if (ssl_c2 == NULL)
  56865. return -4;
  56866. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56867. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56868. /* send CH */
  56869. ret = wolfSSL_connect(ssl_c2);
  56870. if (ret == 0 || ssl_c2->error != WANT_READ)
  56871. return -5;
  56872. ret = wolfSSL_accept(ssl_s);
  56873. if (ret == 0 || ssl_s->error != WANT_READ)
  56874. return -6;
  56875. if (test_ctx.c_len == 0)
  56876. return -7;
  56877. /* consume HRR */
  56878. test_ctx.c_len = 0;
  56879. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56880. if (ret != 0)
  56881. return -8;
  56882. wolfSSL_free(ssl_c2);
  56883. wolfSSL_free(ssl_c);
  56884. wolfSSL_free(ssl_s);
  56885. wolfSSL_CTX_free(ctx_c);
  56886. wolfSSL_CTX_free(ctx_c2);
  56887. wolfSSL_CTX_free(ctx_s);
  56888. return TEST_SUCCESS;
  56889. }
  56890. #endif /* !defined(NO_OLD_TLS) */
  56891. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56892. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  56893. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56894. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56895. !defined(NO_OLD_TLS) && !defined(NO_RSA)
  56896. static int test_WOLFSSL_dtls_version_alert(void)
  56897. {
  56898. struct test_memio_ctx test_ctx;
  56899. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56900. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56901. int ret;
  56902. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56903. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56904. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  56905. if (ret != 0)
  56906. return -1;
  56907. /* client hello */
  56908. ret = wolfSSL_connect(ssl_c);
  56909. if (ret == 0 || ssl_c->error != WANT_READ )
  56910. return -2;
  56911. /* hrr */
  56912. ret = wolfSSL_accept(ssl_s);
  56913. if (ret == 0 || ssl_s->error != WANT_READ )
  56914. return -3;
  56915. /* client hello 1 */
  56916. ret = wolfSSL_connect(ssl_c);
  56917. if (ret == 0 || ssl_c->error != WANT_READ )
  56918. return -4;
  56919. /* server hello */
  56920. ret = wolfSSL_accept(ssl_s);
  56921. if (ret == 0 || ssl_s->error != WANT_READ )
  56922. return -5;
  56923. /* should fail */
  56924. ret = wolfSSL_connect(ssl_c);
  56925. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  56926. return -6;
  56927. /* shuould fail */
  56928. ret = wolfSSL_accept(ssl_s);
  56929. if (ret == 0 ||
  56930. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  56931. return -7;
  56932. wolfSSL_free(ssl_c);
  56933. wolfSSL_free(ssl_s);
  56934. wolfSSL_CTX_free(ctx_c);
  56935. wolfSSL_CTX_free(ctx_s);
  56936. return TEST_RES_CHECK(1);
  56937. }
  56938. #else
  56939. static int test_WOLFSSL_dtls_version_alert(void)
  56940. {
  56941. return TEST_SKIPPED;
  56942. }
  56943. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56944. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56945. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  56946. */
  56947. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56948. && defined(WOLFSSL_TLS13) && \
  56949. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  56950. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56951. {
  56952. struct test_memio_ctx *test_ctx;
  56953. byte buf[2048];
  56954. int idx, sz;
  56955. word32 tmp;
  56956. int ret;
  56957. idx = 5; /* space for record header */
  56958. buf[idx] = session_ticket; /* type */
  56959. idx++;
  56960. tmp = OPAQUE32_LEN +
  56961. OPAQUE32_LEN +
  56962. OPAQUE8_LEN + nonceLength +
  56963. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56964. c32to24(tmp, buf + idx);
  56965. idx += OPAQUE24_LEN;
  56966. c32toa((word32)12345, buf+idx); /* lifetime */
  56967. idx += OPAQUE32_LEN;
  56968. c32toa((word32)12345, buf+idx); /* add */
  56969. idx += OPAQUE32_LEN;
  56970. buf[idx] = nonceLength; /* nonce length */
  56971. idx++;
  56972. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56973. idx += nonceLength;
  56974. tmp = 1; /* ticket len */
  56975. c16toa((word16)tmp, buf+idx);
  56976. idx += 2;
  56977. buf[idx] = 0xFF; /* ticket */
  56978. idx++;
  56979. tmp = 0; /* ext len */
  56980. c16toa((word16)tmp, buf+idx);
  56981. idx += 2;
  56982. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56983. handshake, 0, 0, 0);
  56984. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56985. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56986. return !(ret == sz);
  56987. }
  56988. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56989. {
  56990. int i;
  56991. if (sess == NULL)
  56992. return -1;
  56993. if (sess->ticketNonce.len != len)
  56994. return -1;
  56995. for (i = 0; i < len; i++)
  56996. if (sess->ticketNonce.data[i] != len)
  56997. return -1;
  56998. return 0;
  56999. }
  57000. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57001. {
  57002. char *buf[1024];
  57003. int ret;
  57004. ret = send_new_session_ticket(ssl_s, len, len);
  57005. if (ret != 0)
  57006. return -1;
  57007. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  57008. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  57009. return -1;
  57010. return test_ticket_nonce_check(ssl_c->session, len);
  57011. }
  57012. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57013. {
  57014. WOLFSSL_SESSION *sess, *cached;
  57015. WOLFSSL_CTX *ctx;
  57016. int ret;
  57017. ctx = ssl_c->ctx;
  57018. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  57019. if (ret != 0)
  57020. return -1;
  57021. sess = wolfSSL_get1_session(ssl_c);
  57022. if (sess == NULL)
  57023. return -1;
  57024. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  57025. NULL, ssl_c->options.side, 1,NULL);
  57026. if (ret != 0)
  57027. return -1;
  57028. cached = wolfSSL_SESSION_new();
  57029. if (cached == NULL)
  57030. return -1;
  57031. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  57032. if (ret != WOLFSSL_SUCCESS)
  57033. return -1;
  57034. ret = test_ticket_nonce_check(cached, len);
  57035. if (ret != 0)
  57036. return -1;
  57037. wolfSSL_SESSION_free(cached);
  57038. wolfSSL_SESSION_free(sess);
  57039. return 0;
  57040. }
  57041. static int test_ticket_nonce_malloc(void)
  57042. {
  57043. struct test_memio_ctx test_ctx;
  57044. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57045. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57046. byte small, medium, big;
  57047. int ret;
  57048. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57049. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57050. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  57051. if (ret != 0)
  57052. return -1;
  57053. /* will send ticket manually */
  57054. wolfSSL_no_ticket_TLSv13(ssl_s);
  57055. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57056. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57057. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  57058. ret = wolfSSL_connect(ssl_c);
  57059. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  57060. return -2;
  57061. ret = wolfSSL_accept(ssl_s);
  57062. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  57063. return -3;
  57064. }
  57065. small = TLS13_TICKET_NONCE_STATIC_SZ;
  57066. medium = small + 20 <= 255 ? small + 20 : 255;
  57067. big = medium + 20 <= 255 ? small + 20 : 255;
  57068. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  57069. return -1;
  57070. if (ssl_c->session->ticketNonce.data !=
  57071. ssl_c->session->ticketNonce.dataStatic)
  57072. return -1;
  57073. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  57074. return -1;
  57075. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  57076. return -1;
  57077. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  57078. return -5;
  57079. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  57080. return -6;
  57081. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  57082. return -1;
  57083. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  57084. return -1;
  57085. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  57086. return -1;
  57087. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  57088. return -1;
  57089. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  57090. return -1;
  57091. wolfSSL_free(ssl_c);
  57092. wolfSSL_free(ssl_s);
  57093. wolfSSL_CTX_free(ctx_c);
  57094. wolfSSL_CTX_free(ctx_s);
  57095. return 0;
  57096. }
  57097. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  57098. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  57099. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  57100. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57101. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  57102. defined(HAVE_ECC)
  57103. static int test_ticket_ret_create(void)
  57104. {
  57105. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57106. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57107. byte ticket[SESSION_TICKET_LEN];
  57108. struct test_memio_ctx test_ctx;
  57109. WOLFSSL_SESSION *sess = NULL;
  57110. word16 ticketLen;
  57111. int ret;
  57112. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57113. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57114. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  57115. if (ret != 0)
  57116. return TEST_FAIL;
  57117. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57118. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57119. wolfSSL_CTX_UseSessionTicket(ctx_c);
  57120. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57121. if (ret != 0)
  57122. return TEST_FAIL;
  57123. sess = wolfSSL_get1_session(ssl_c);
  57124. if (sess->ticketLen > SESSION_TICKET_LEN)
  57125. return TEST_FAIL;
  57126. ticketLen = sess->ticketLen;
  57127. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  57128. wolfSSL_free(ssl_c);
  57129. wolfSSL_free(ssl_s);
  57130. ssl_s = wolfSSL_new(ctx_s);
  57131. if (ssl_s == NULL)
  57132. return TEST_FAIL;
  57133. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57134. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57135. ssl_c = wolfSSL_new(ctx_c);
  57136. if (ssl_c == NULL)
  57137. return TEST_FAIL;
  57138. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57139. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57140. wolfSSL_set_session(ssl_c, sess);
  57141. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57142. if (ret != 0)
  57143. return TEST_FAIL;
  57144. if (ssl_c->session->ticketLen > SESSION_TICKET_LEN)
  57145. return TEST_FAIL;
  57146. if (ssl_c->session->ticketLen != ticketLen)
  57147. return TEST_FAIL;
  57148. if (XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) == 0)
  57149. return TEST_FAIL;
  57150. wolfSSL_SESSION_free(sess);
  57151. wolfSSL_free(ssl_c);
  57152. wolfSSL_free(ssl_s);
  57153. wolfSSL_CTX_free(ctx_c);
  57154. wolfSSL_CTX_free(ctx_s);
  57155. return TEST_SUCCESS;
  57156. }
  57157. #else
  57158. static int test_ticket_ret_create(void)
  57159. {
  57160. return TEST_SKIPPED;
  57161. }
  57162. #endif
  57163. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  57164. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57165. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57166. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57167. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  57168. {
  57169. int ret;
  57170. WOLFSSL_SESSION* session = NULL;
  57171. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57172. if (!wolfSSL_is_server(ssl)) {
  57173. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57174. AssertNotNull(session);
  57175. }
  57176. do {
  57177. ret = wolfSSL_shutdown(ssl);
  57178. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57179. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57180. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57181. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57182. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57183. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57184. #endif
  57185. if (!wolfSSL_is_server(ssl)) {
  57186. /* client */
  57187. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57188. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57189. wolfSSL_set_session(ssl, session);
  57190. wolfSSL_SESSION_free(session);
  57191. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57192. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57193. }
  57194. else {
  57195. /* server */
  57196. /* Different ciphersuite so that the ticket will be invalidated based on
  57197. * the ciphersuite */
  57198. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  57199. WOLFSSL_SUCCESS);
  57200. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  57201. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57202. }
  57203. }
  57204. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  57205. {
  57206. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  57207. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57208. WOLFSSL_SUCCESS);
  57209. }
  57210. static int test_ticket_and_psk_mixing(void)
  57211. {
  57212. /* Test mixing tickets and regular PSK */
  57213. callback_functions client_cbs, server_cbs;
  57214. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57215. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57216. client_cbs.method = wolfTLSv1_3_client_method;
  57217. server_cbs.method = wolfTLSv1_3_server_method;
  57218. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  57219. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57220. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57221. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57222. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57223. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57224. return TEST_RES_CHECK(1);
  57225. }
  57226. #else
  57227. static int test_ticket_and_psk_mixing(void)
  57228. {
  57229. return TEST_SKIPPED;
  57230. }
  57231. #endif
  57232. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  57233. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57234. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57235. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57236. static int test_prioritize_psk_cb_called = FALSE;
  57237. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  57238. const char* identity, unsigned char* key, unsigned int key_max_len,
  57239. const char** ciphersuite)
  57240. {
  57241. test_prioritize_psk_cb_called = TRUE;
  57242. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  57243. }
  57244. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  57245. {
  57246. int ret;
  57247. WOLFSSL_SESSION* session = NULL;
  57248. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57249. if (!wolfSSL_is_server(ssl)) {
  57250. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57251. AssertNotNull(session);
  57252. }
  57253. do {
  57254. ret = wolfSSL_shutdown(ssl);
  57255. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57256. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57257. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57258. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  57259. * important. */
  57260. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57261. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57262. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57263. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57264. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57265. #endif
  57266. if (!wolfSSL_is_server(ssl)) {
  57267. /* client */
  57268. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57269. wolfSSL_set_session(ssl, session);
  57270. wolfSSL_SESSION_free(session);
  57271. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57272. }
  57273. else {
  57274. /* server */
  57275. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  57276. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57277. #ifdef WOLFSSL_PRIORITIZE_PSK
  57278. /* The ticket should be first tried with all ciphersuites and chosen */
  57279. AssertFalse(test_prioritize_psk_cb_called);
  57280. #else
  57281. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  57282. * callback that sets this var. */
  57283. AssertTrue(test_prioritize_psk_cb_called);
  57284. #endif
  57285. }
  57286. }
  57287. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  57288. {
  57289. if (!wolfSSL_is_server(ssl))
  57290. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57291. WOLFSSL_SUCCESS);
  57292. else
  57293. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57294. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57295. }
  57296. static int test_prioritize_psk(void)
  57297. {
  57298. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  57299. * of the PSK's will be followed instead of the ciphersuite. */
  57300. callback_functions client_cbs, server_cbs;
  57301. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57302. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57303. client_cbs.method = wolfTLSv1_3_client_method;
  57304. server_cbs.method = wolfTLSv1_3_server_method;
  57305. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57306. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57307. client_cbs.on_result = test_prioritize_psk_on_result;
  57308. server_cbs.on_result = test_prioritize_psk_on_result;
  57309. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57310. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57311. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57312. return TEST_RES_CHECK(1);
  57313. }
  57314. #else
  57315. static int test_prioritize_psk(void)
  57316. {
  57317. return TEST_SKIPPED;
  57318. }
  57319. #endif
  57320. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  57321. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57322. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57323. !defined(WOLFSSL_NO_TLS12)
  57324. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  57325. {
  57326. EXPECT_DECLS;
  57327. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  57328. /* Set TLS 1.3 specific suite */
  57329. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  57330. return EXPECT_RESULT();
  57331. }
  57332. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57333. {
  57334. EXPECT_DECLS;
  57335. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  57336. * do a 1.2 connection. */
  57337. test_ssl_cbf client_cbs, server_cbs;
  57338. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57339. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57340. client_cbs.method = wolfTLSv1_2_client_method;
  57341. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  57342. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  57343. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57344. &server_cbs, NULL), TEST_SUCCESS);
  57345. return EXPECT_RESULT();
  57346. }
  57347. #else
  57348. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57349. {
  57350. return TEST_SKIPPED;
  57351. }
  57352. #endif
  57353. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  57354. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57355. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  57356. {
  57357. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57358. return TEST_SUCCESS;
  57359. }
  57360. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  57361. {
  57362. EXPECT_DECLS;
  57363. WOLFSSL_ALERT_HISTORY h;
  57364. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57365. ExpectIntEQ(h.last_rx.level, alert_fatal);
  57366. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  57367. return EXPECT_RESULT();
  57368. }
  57369. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57370. {
  57371. EXPECT_DECLS;
  57372. test_ssl_cbf client_cbs, server_cbs;
  57373. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57374. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57375. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  57376. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  57377. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  57378. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  57379. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  57380. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57381. &server_cbs, NULL), TEST_FAIL);
  57382. return EXPECT_RESULT();
  57383. }
  57384. #else
  57385. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57386. {
  57387. return TEST_SKIPPED;
  57388. }
  57389. #endif
  57390. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  57391. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57392. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57393. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  57394. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  57395. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57396. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  57397. static int test_TLS_13_ticket_different_ciphers_run = 0;
  57398. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  57399. {
  57400. EXPECT_DECLS;
  57401. switch (test_TLS_13_ticket_different_ciphers_run) {
  57402. case 0:
  57403. /* First run */
  57404. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57405. WOLFSSL_SUCCESS);
  57406. if (wolfSSL_is_server(ssl)) {
  57407. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  57408. wolfSSL_get_SSL_CTX(ssl));
  57409. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  57410. test_TLS_13_ticket_different_ciphers_ctx));
  57411. }
  57412. break;
  57413. case 1:
  57414. /* Second run */
  57415. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57416. "TLS13-AES128-GCM-SHA256"),
  57417. WOLFSSL_SUCCESS);
  57418. if (!wolfSSL_is_server(ssl)) {
  57419. ExpectIntEQ(wolfSSL_set_session(ssl,
  57420. test_TLS_13_ticket_different_ciphers_session),
  57421. WOLFSSL_SUCCESS);
  57422. }
  57423. break;
  57424. default:
  57425. /* Bad state? */
  57426. Fail(("Should not enter here"), ("Should not enter here"));
  57427. }
  57428. return EXPECT_RESULT();
  57429. }
  57430. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  57431. {
  57432. EXPECT_DECLS;
  57433. switch (test_TLS_13_ticket_different_ciphers_run) {
  57434. case 0:
  57435. /* First run */
  57436. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  57437. wolfSSL_get1_session(ssl));
  57438. break;
  57439. case 1:
  57440. /* Second run */
  57441. ExpectTrue(wolfSSL_session_reused(ssl));
  57442. break;
  57443. default:
  57444. /* Bad state? */
  57445. Fail(("Should not enter here"), ("Should not enter here"));
  57446. }
  57447. return EXPECT_RESULT();
  57448. }
  57449. static int test_TLS_13_ticket_different_ciphers(void)
  57450. {
  57451. EXPECT_DECLS;
  57452. /* Check that we handle the connection when the ticket doesn't match
  57453. * the first ciphersuite. */
  57454. test_ssl_cbf client_cbs, server_cbs;
  57455. struct test_params {
  57456. method_provider client_meth;
  57457. method_provider server_meth;
  57458. int doUdp;
  57459. } params[] = {
  57460. #ifdef WOLFSSL_DTLS13
  57461. /* Test that the stateless code handles sessions correctly */
  57462. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  57463. #endif
  57464. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  57465. };
  57466. size_t i;
  57467. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57468. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57469. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57470. test_TLS_13_ticket_different_ciphers_run = 0;
  57471. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  57472. client_cbs.method = params[i].client_meth;
  57473. server_cbs.method = params[i].server_meth;
  57474. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57475. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57476. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  57477. server_cbs.ticNoInit = 1;
  57478. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57479. &server_cbs, NULL), TEST_SUCCESS);
  57480. test_TLS_13_ticket_different_ciphers_run++;
  57481. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  57482. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57483. &server_cbs, NULL), TEST_SUCCESS);
  57484. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  57485. test_TLS_13_ticket_different_ciphers_session = NULL;
  57486. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  57487. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57488. }
  57489. return EXPECT_RESULT();
  57490. }
  57491. #else
  57492. static int test_TLS_13_ticket_different_ciphers(void)
  57493. {
  57494. return TEST_SKIPPED;
  57495. }
  57496. #endif
  57497. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  57498. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57499. #define TEST_WRONG_CS_CLIENT "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"
  57500. byte test_extra_alerts_wrong_cs_sh[] = {
  57501. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  57502. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  57503. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  57504. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  57505. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  57506. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  57507. 0xbb, 0x07, 0x54, 0x1c,
  57508. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  57509. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  57510. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  57511. };
  57512. static int test_extra_alerts_wrong_cs(void)
  57513. {
  57514. struct test_memio_ctx test_ctx;
  57515. WOLFSSL_CTX *ctx_c = NULL;
  57516. WOLFSSL_ALERT_HISTORY h;
  57517. WOLFSSL *ssl_c = NULL;
  57518. int ret, err;
  57519. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57520. ret = test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57521. wolfTLSv1_2_client_method, NULL);
  57522. if (ret != 0)
  57523. return TEST_FAIL;
  57524. ret = wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT);
  57525. if (ret != WOLFSSL_SUCCESS) {
  57526. wolfSSL_free(ssl_c);
  57527. wolfSSL_CTX_free(ctx_c);
  57528. return TEST_SKIPPED;
  57529. }
  57530. /* CH */
  57531. ret = wolfSSL_connect(ssl_c);
  57532. err = wolfSSL_get_error(ssl_c, ret);
  57533. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  57534. return TEST_FAIL;
  57535. /* consume CH */
  57536. test_ctx.s_len = 0;
  57537. /* inject SH */
  57538. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  57539. sizeof(test_extra_alerts_wrong_cs_sh));
  57540. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  57541. ret = wolfSSL_connect(ssl_c);
  57542. err = wolfSSL_get_error(ssl_c, ret);
  57543. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  57544. return TEST_FAIL;
  57545. ret = wolfSSL_get_alert_history(ssl_c, &h);
  57546. if (ret != WOLFSSL_SUCCESS)
  57547. return TEST_FAIL;
  57548. if (h.last_tx.code != illegal_parameter)
  57549. return TEST_FAIL;
  57550. if (h.last_tx.level != alert_fatal)
  57551. return TEST_FAIL;
  57552. wolfSSL_free(ssl_c);
  57553. wolfSSL_CTX_free(ctx_c);
  57554. return TEST_SUCCESS;
  57555. }
  57556. #else
  57557. static int test_extra_alerts_wrong_cs(void)
  57558. {
  57559. return TEST_SKIPPED;
  57560. }
  57561. #endif
  57562. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  57563. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  57564. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  57565. {
  57566. tail_len -= msg_length;
  57567. XMEMMOVE(msg, msg + msg_length, tail_len);
  57568. *len = *len - msg_length;
  57569. }
  57570. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  57571. byte *found)
  57572. {
  57573. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  57574. const unsigned int _RECORD_HEADER_SZ = 5;
  57575. const int _change_cipher_hs = 55;
  57576. const int _change_cipher = 20;
  57577. const int _handshake = 22;
  57578. unsigned int tail_len;
  57579. byte *idx, *curr;
  57580. word8 currType;
  57581. word16 rLength;
  57582. word32 hLength;
  57583. idx = buf;
  57584. tail_len = *len;
  57585. *found = 0;
  57586. while (tail_len > _RECORD_HEADER_SZ) {
  57587. curr = idx;
  57588. currType = *idx;
  57589. ato16(idx + 3, &rLength);
  57590. idx += _RECORD_HEADER_SZ;
  57591. tail_len -= _RECORD_HEADER_SZ;
  57592. if (tail_len < rLength)
  57593. return -1;
  57594. if (type == _change_cipher_hs && currType == _change_cipher) {
  57595. if (rLength != 1)
  57596. return -1;
  57597. /* match */
  57598. test_remove_msg(curr, *len - (int)(curr - buf),
  57599. len, _RECORD_HEADER_SZ + 1);
  57600. *found = 1;
  57601. return 0;
  57602. }
  57603. if (currType != _handshake) {
  57604. idx += rLength;
  57605. tail_len -= rLength;
  57606. continue;
  57607. }
  57608. if (rLength < _HANDSHAKE_HEADER_SZ)
  57609. return -1;
  57610. currType = *idx;
  57611. ato24(idx+1, &hLength);
  57612. hLength += _HANDSHAKE_HEADER_SZ;
  57613. if (tail_len < hLength)
  57614. return -1;
  57615. if (currType != type) {
  57616. idx += hLength;
  57617. tail_len -= hLength;
  57618. continue;
  57619. }
  57620. /* match */
  57621. test_remove_msg(curr, *len - (int)(curr - buf), len,
  57622. hLength + _RECORD_HEADER_SZ);
  57623. *found = 1;
  57624. return 0;
  57625. }
  57626. /* not found */
  57627. return 0;
  57628. }
  57629. static int test_remove_hs_message(byte hs_message_type,
  57630. int extra_round, byte alert_type)
  57631. {
  57632. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57633. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57634. struct test_memio_ctx test_ctx;
  57635. WOLFSSL_ALERT_HISTORY h;
  57636. int ret, err;
  57637. byte found;
  57638. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57639. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57640. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  57641. AssertIntEQ(ret, 0);
  57642. ret = wolfSSL_connect(ssl_c);
  57643. err = wolfSSL_get_error(ssl_c, ret);
  57644. AssertIntNE(ret, WOLFSSL_SUCCESS);
  57645. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  57646. ret = wolfSSL_accept(ssl_s);
  57647. err = wolfSSL_get_error(ssl_s, ret);
  57648. AssertIntNE(ret, WOLFSSL_SUCCESS);
  57649. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  57650. if (extra_round) {
  57651. ret = wolfSSL_connect(ssl_c);
  57652. err = wolfSSL_get_error(ssl_c, ret);
  57653. AssertIntNE(ret, WOLFSSL_SUCCESS);
  57654. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  57655. /* this will complete handshake from server side */
  57656. ret = wolfSSL_accept(ssl_s);
  57657. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  57658. }
  57659. ret = test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  57660. &test_ctx.c_len, hs_message_type, &found);
  57661. AssertIntEQ(ret, 0);
  57662. if (!found) {
  57663. wolfSSL_free(ssl_c);
  57664. wolfSSL_CTX_free(ctx_c);
  57665. wolfSSL_free(ssl_s);
  57666. wolfSSL_CTX_free(ctx_s);
  57667. return TEST_SKIPPED;
  57668. }
  57669. ret = wolfSSL_connect(ssl_c);
  57670. err = wolfSSL_get_error(ssl_c, ret);
  57671. AssertIntNE(ret, WOLFSSL_SUCCESS);
  57672. AssertIntNE(err, WOLFSSL_ERROR_WANT_READ);
  57673. ret = wolfSSL_get_alert_history(ssl_c, &h);
  57674. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  57675. if (alert_type != 0xff && h.last_tx.code != alert_type)
  57676. return TEST_FAIL;
  57677. if (h.last_tx.level != alert_fatal)
  57678. return TEST_FAIL;
  57679. wolfSSL_free(ssl_c);
  57680. wolfSSL_CTX_free(ctx_c);
  57681. wolfSSL_free(ssl_s);
  57682. wolfSSL_CTX_free(ctx_s);
  57683. return TEST_SUCCESS;
  57684. }
  57685. static int test_extra_alerts_skip_hs(void)
  57686. {
  57687. const byte _server_key_exchange = 12;
  57688. const byte _server_hello = 2;
  57689. const byte _certificate = 11;
  57690. int ret;
  57691. /* server_hello */
  57692. ret = test_remove_hs_message(_server_hello, 0,
  57693. unexpected_message);
  57694. AssertIntNE(ret, TEST_FAIL);
  57695. ret = test_remove_hs_message(_certificate, 0,
  57696. 0xff);
  57697. AssertIntNE(ret, TEST_FAIL);
  57698. ret = test_remove_hs_message(_server_key_exchange, 0,
  57699. unexpected_message);
  57700. AssertIntNE(ret, TEST_FAIL);
  57701. return TEST_SUCCESS;
  57702. }
  57703. #else
  57704. static int test_extra_alerts_skip_hs(void)
  57705. {
  57706. return TEST_SKIPPED;
  57707. }
  57708. #endif
  57709. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57710. defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  57711. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  57712. unsigned char* key, unsigned int key_max_len)
  57713. {
  57714. (void)ssl;
  57715. (void)id;
  57716. (void)key_max_len;
  57717. /* zero means error */
  57718. key[0] = 0x10;
  57719. return 1;
  57720. }
  57721. static int test_extra_alerts_bad_psk(void)
  57722. {
  57723. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57724. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57725. struct test_memio_ctx test_ctx;
  57726. WOLFSSL_ALERT_HISTORY h;
  57727. int ret, err;
  57728. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57729. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57730. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  57731. if (ret != 0)
  57732. return TEST_FAIL;
  57733. ret = wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256");
  57734. if (ret != WOLFSSL_SUCCESS)
  57735. return TEST_FAIL;
  57736. ret = wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256");
  57737. if (ret != WOLFSSL_SUCCESS)
  57738. return TEST_FAIL;
  57739. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  57740. ret = wolfSSL_connect(ssl_c);
  57741. err = wolfSSL_get_error(ssl_c, ret);
  57742. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  57743. return TEST_FAIL;
  57744. ret = wolfSSL_accept(ssl_s);
  57745. err = wolfSSL_get_error(ssl_s, ret);
  57746. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  57747. return TEST_FAIL;
  57748. ret = wolfSSL_connect(ssl_c);
  57749. err = wolfSSL_get_error(ssl_c, ret);
  57750. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  57751. return TEST_FAIL;
  57752. ret = wolfSSL_get_alert_history(ssl_c, &h);
  57753. if (ret != WOLFSSL_SUCCESS)
  57754. return TEST_FAIL;
  57755. if (h.last_tx.code != handshake_failure)
  57756. return TEST_FAIL;
  57757. if (h.last_tx.level != alert_fatal)
  57758. return TEST_FAIL;
  57759. wolfSSL_free(ssl_c);
  57760. wolfSSL_CTX_free(ctx_c);
  57761. wolfSSL_free(ssl_s);
  57762. wolfSSL_CTX_free(ctx_s);
  57763. return TEST_SUCCESS;
  57764. }
  57765. #else
  57766. static int test_extra_alerts_bad_psk(void)
  57767. {
  57768. return TEST_SKIPPED;
  57769. }
  57770. #endif
  57771. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  57772. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57773. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  57774. int sz, void *ctx)
  57775. {
  57776. static int sentServerHello = FALSE;
  57777. if (!sentServerHello) {
  57778. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  57779. size_t i;
  57780. if (sz < (int)sizeof(renegExt))
  57781. return WOLFSSL_CBIO_ERR_GENERAL;
  57782. /* Remove SCR from ServerHello */
  57783. for (i = 0; i < sz - sizeof(renegExt); i++) {
  57784. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  57785. /* Found the extension. Change it to something unrecognized. */
  57786. buf[i+1] = 0x11;
  57787. break;
  57788. }
  57789. }
  57790. sentServerHello = TRUE;
  57791. }
  57792. return EmbedSend(ssl, buf, sz, ctx);
  57793. }
  57794. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  57795. {
  57796. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  57797. }
  57798. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  57799. {
  57800. WOLFSSL_ALERT_HISTORY h;
  57801. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57802. AssertIntEQ(h.last_rx.code, handshake_failure);
  57803. AssertIntEQ(h.last_rx.level, alert_fatal);
  57804. }
  57805. static int test_harden_no_secure_renegotiation(void)
  57806. {
  57807. callback_functions client_cbs, server_cbs;
  57808. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57809. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57810. client_cbs.method = wolfTLSv1_2_client_method;
  57811. server_cbs.method = wolfTLSv1_2_server_method;
  57812. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  57813. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  57814. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57815. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  57816. AssertIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  57817. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  57818. AssertIntEQ(server_cbs.last_err, SOCKET_ERROR_E);
  57819. return TEST_RES_CHECK(1);
  57820. }
  57821. #else
  57822. static int test_harden_no_secure_renegotiation(void)
  57823. {
  57824. return TEST_SKIPPED;
  57825. }
  57826. #endif
  57827. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57828. static int test_override_alt_cert_chain_cert_cb(int preverify,
  57829. WOLFSSL_X509_STORE_CTX* store)
  57830. {
  57831. printf("preverify: %d\n", preverify);
  57832. printf("store->error: %d\n", store->error);
  57833. printf("error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  57834. if (store->error == OCSP_INVALID_STATUS) {
  57835. printf("Overriding OCSP error\n");
  57836. return 1;
  57837. }
  57838. #ifndef WOLFSSL_ALT_CERT_CHAINS
  57839. else if ((store->error == ASN_NO_SIGNER_E ||
  57840. store->error == ASN_SELF_SIGNED_E
  57841. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  57842. defined(HAVE_WEBSERVER)
  57843. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  57844. #endif
  57845. ) && store->error_depth == store->totalCerts - 1) {
  57846. printf("Overriding no signer error only for root cert\n");
  57847. return 1;
  57848. }
  57849. #endif
  57850. else
  57851. return preverify;
  57852. }
  57853. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  57854. int urlSz, unsigned char* request, int requestSz,
  57855. unsigned char** response)
  57856. {
  57857. (void)ioCtx;
  57858. (void)url;
  57859. (void)urlSz;
  57860. (void)request;
  57861. (void)requestSz;
  57862. (void)response;
  57863. return -1;
  57864. }
  57865. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  57866. {
  57867. EXPECT_DECLS;
  57868. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  57869. test_override_alt_cert_chain_cert_cb);
  57870. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57871. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57872. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57873. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57874. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57875. WOLFSSL_SUCCESS);
  57876. return EXPECT_RESULT();
  57877. }
  57878. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  57879. {
  57880. EXPECT_DECLS;
  57881. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57882. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57883. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57884. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57885. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57886. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57887. WOLFSSL_SUCCESS);
  57888. return EXPECT_RESULT();
  57889. }
  57890. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  57891. {
  57892. EXPECT_DECLS;
  57893. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  57894. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  57895. return EXPECT_RESULT();
  57896. }
  57897. static int test_override_alt_cert_chain(void)
  57898. {
  57899. EXPECT_DECLS;
  57900. size_t i;
  57901. struct test_params {
  57902. ctx_cb client_ctx_cb;
  57903. ctx_cb server_ctx_cb;
  57904. int result;
  57905. } params[] = {
  57906. {test_override_alt_cert_chain_client_ctx_ready,
  57907. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  57908. {test_override_alt_cert_chain_client_ctx_ready2,
  57909. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  57910. };
  57911. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57912. test_ssl_cbf client_cbs, server_cbs;
  57913. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57914. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57915. printf("test config: %d\n", (int)i);
  57916. client_cbs.ctx_ready = params[i].client_ctx_cb;
  57917. server_cbs.ctx_ready = params[i].server_ctx_cb;
  57918. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57919. &server_cbs, NULL), params[i].result);
  57920. ExpectIntEQ(client_cbs.return_code, params[i].result);
  57921. ExpectIntEQ(server_cbs.return_code, params[i].result);
  57922. }
  57923. return EXPECT_RESULT();
  57924. }
  57925. #else
  57926. static int test_override_alt_cert_chain(void)
  57927. {
  57928. return TEST_SKIPPED;
  57929. }
  57930. #endif
  57931. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  57932. static int test_dtls13_bad_epoch_ch(void)
  57933. {
  57934. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  57935. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  57936. struct test_memio_ctx test_ctx;
  57937. const int EPOCH_OFF = 3;
  57938. int ret, err;
  57939. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57940. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57941. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method);
  57942. if (ret != 0)
  57943. return TEST_FAIL;
  57944. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  57945. * with just one message */
  57946. ret = wolfSSL_disable_hrr_cookie(ssl_s);
  57947. if (ret != WOLFSSL_SUCCESS)
  57948. return TEST_FAIL;
  57949. ret = wolfSSL_connect(ssl_c);
  57950. err = wolfSSL_get_error(ssl_c, ret);
  57951. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  57952. return TEST_FAIL;
  57953. if (test_ctx.s_len < EPOCH_OFF + 2)
  57954. return TEST_FAIL;
  57955. /* first CH should use epoch 0x0 */
  57956. if (test_ctx.s_buff[EPOCH_OFF] != 0x0 ||
  57957. test_ctx.s_buff[EPOCH_OFF + 1] != 0x0)
  57958. return TEST_FAIL;
  57959. /* change epoch to 2 */
  57960. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  57961. ret = wolfSSL_accept(ssl_s);
  57962. err = wolfSSL_get_error(ssl_s, ret);
  57963. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  57964. return TEST_FAIL;
  57965. if (ssl_s->msgsReceived.got_client_hello == 1)
  57966. return TEST_FAIL;
  57967. /* resend the CH */
  57968. ret = wolfSSL_dtls_got_timeout(ssl_c);
  57969. if (ret != WOLFSSL_SUCCESS)
  57970. return TEST_FAIL;
  57971. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57972. if (ret != 0)
  57973. return TEST_FAIL;
  57974. wolfSSL_free(ssl_c);
  57975. wolfSSL_CTX_free(ctx_c);
  57976. wolfSSL_free(ssl_s);
  57977. wolfSSL_CTX_free(ctx_s);
  57978. return TEST_SUCCESS;
  57979. }
  57980. #else
  57981. static int test_dtls13_bad_epoch_ch(void)
  57982. {
  57983. return TEST_SKIPPED;
  57984. }
  57985. #endif
  57986. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57987. defined(WOLFSSL_DTLS13)
  57988. static byte* test_find_string(const char *string,
  57989. byte *buf, int buf_size)
  57990. {
  57991. int string_size, i;
  57992. string_size = (int)XSTRLEN(string);
  57993. for (i = 0; i < buf_size - string_size - 1; i++) {
  57994. if (XSTRCMP((char*)&buf[i], string) == 0)
  57995. return &buf[i];
  57996. }
  57997. return NULL;
  57998. }
  57999. static int test_wolfSSL_dtls13_null_cipher(void)
  58000. {
  58001. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  58002. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  58003. struct test_memio_ctx test_ctx;
  58004. const char *test_str = "test";
  58005. int ret, test_str_size;
  58006. byte buf[255], *ptr;
  58007. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58008. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  58009. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58010. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method);
  58011. if (ret != 0)
  58012. return TEST_FAIL;
  58013. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58014. if (ret != 0)
  58015. return TEST_FAIL;
  58016. test_str_size = XSTRLEN("test") + 1;
  58017. ret = wolfSSL_write(ssl_c, test_str, test_str_size);
  58018. if (ret != test_str_size)
  58019. return TEST_FAIL;
  58020. ret = wolfSSL_read(ssl_s, buf, sizeof(buf));
  58021. if (ret != test_str_size || XSTRCMP((char*)buf, test_str) != 0)
  58022. return TEST_FAIL;
  58023. ret = wolfSSL_write(ssl_c, test_str, test_str_size);
  58024. if (ret != test_str_size)
  58025. return TEST_FAIL;
  58026. /* check that the packet was sent cleartext */
  58027. ptr = test_find_string(test_str, test_ctx.s_buff, test_ctx.s_len);
  58028. if (ptr == NULL)
  58029. return TEST_FAIL;
  58030. /* modify the message */
  58031. *ptr = 'H';
  58032. /* bad messages should be ignored in DTLS */
  58033. ret = wolfSSL_read(ssl_s, buf, sizeof(buf));
  58034. if (ret != -1 || ssl_s->error != WANT_READ)
  58035. return TEST_FAIL;
  58036. wolfSSL_free(ssl_c);
  58037. wolfSSL_free(ssl_s);
  58038. wolfSSL_CTX_free(ctx_c);
  58039. wolfSSL_CTX_free(ctx_s);
  58040. return TEST_SUCCESS;
  58041. }
  58042. #else
  58043. static int test_wolfSSL_dtls13_null_cipher(void)
  58044. {
  58045. return TEST_SKIPPED;
  58046. }
  58047. #endif
  58048. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58049. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58050. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  58051. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  58052. {
  58053. SOCKADDR_S peer;
  58054. XSOCKLENT len;
  58055. int ret;
  58056. XMEMSET((byte*)&peer, 0, sizeof(peer));
  58057. len = sizeof(peer);
  58058. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  58059. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  58060. return -1;
  58061. switch (peer.ss_family) {
  58062. #ifdef WOLFSSL_IPV6
  58063. case WOLFSSL_IP6: {
  58064. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  58065. break;
  58066. }
  58067. #endif /* WOLFSSL_IPV6 */
  58068. case WOLFSSL_IP4:
  58069. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  58070. break;
  58071. default:
  58072. return -1;
  58073. }
  58074. return 0;
  58075. }
  58076. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58077. {
  58078. char buf[1] = {'t'};
  58079. SOCKADDR_IN_T addr;
  58080. int sock_fd;
  58081. word16 port;
  58082. int err;
  58083. (void)ssl;
  58084. (void)ctx;
  58085. if (ssl == NULL)
  58086. return -1;
  58087. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  58088. if (err != 0)
  58089. return -1;
  58090. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  58091. if (sock_fd == -1)
  58092. return -1;
  58093. build_addr(&addr, wolfSSLIP, port, 1, 0);
  58094. /* send a packet to the server. Being another socket, the kernel will ensure
  58095. * the source port will be different. */
  58096. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  58097. sizeof(addr));
  58098. close(sock_fd);
  58099. if (err == -1)
  58100. return -1;
  58101. return 0;
  58102. }
  58103. /* setup a SSL session but just after the handshake send a packet to the server
  58104. * with a source address different than the one of the connected client. The I/O
  58105. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  58106. * is done in test_dtls_msg_from_other_peer_cb */
  58107. static int test_dtls_msg_from_other_peer(void)
  58108. {
  58109. callback_functions client_cbs;
  58110. callback_functions server_cbs;
  58111. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  58112. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  58113. client_cbs.method = wolfDTLSv1_2_client_method;
  58114. server_cbs.method = wolfDTLSv1_2_server_method;
  58115. client_cbs.doUdp = 1;
  58116. server_cbs.doUdp = 1;
  58117. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  58118. test_dtls_msg_from_other_peer_cb);
  58119. if (client_cbs.return_code != WOLFSSL_SUCCESS ||
  58120. server_cbs.return_code != WOLFSSL_SUCCESS)
  58121. return TEST_FAIL;
  58122. return TEST_SUCCESS;
  58123. }
  58124. #else
  58125. static int test_dtls_msg_from_other_peer(void)
  58126. {
  58127. return TEST_SKIPPED;
  58128. }
  58129. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58130. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58131. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  58132. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  58133. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58134. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58135. static int test_dtls_ipv6_check(void)
  58136. {
  58137. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  58138. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  58139. SOCKADDR_IN fake_addr6;
  58140. int sockfd;
  58141. int ret;
  58142. ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  58143. if (ctx_c == NULL)
  58144. return TEST_FAIL;
  58145. ssl_c = wolfSSL_new(ctx_c);
  58146. if (ssl_c == NULL)
  58147. return TEST_FAIL;
  58148. ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  58149. if (ctx_s == NULL)
  58150. return TEST_FAIL;
  58151. ret = wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  58152. WOLFSSL_FILETYPE_PEM);
  58153. if (ret != WOLFSSL_SUCCESS)
  58154. return- -1;
  58155. ret = wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  58156. WOLFSSL_FILETYPE_PEM);
  58157. if (ret != WOLFSSL_SUCCESS)
  58158. return -1;
  58159. ssl_s = wolfSSL_new(ctx_s);
  58160. if (ssl_s == NULL)
  58161. return TEST_FAIL;
  58162. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  58163. /* mimic a sockaddr_in6 struct, this way we can't test without
  58164. * WOLFSSL_IPV6 */
  58165. fake_addr6.sin_family = WOLFSSL_IP6;
  58166. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  58167. if (sockfd == -1)
  58168. return TEST_FAIL;
  58169. ret = wolfSSL_set_fd(ssl_c, sockfd);
  58170. if (ret != WOLFSSL_SUCCESS)
  58171. return TEST_FAIL;
  58172. /* can't return error here, as the peer is opaque for wolfssl library at
  58173. * this point */
  58174. ret = wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6));
  58175. if (ret != WOLFSSL_SUCCESS)
  58176. return TEST_FAIL;
  58177. ret = fcntl(sockfd, F_SETFL, O_NONBLOCK);
  58178. if (ret == -1)
  58179. return TEST_FAIL;
  58180. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  58181. ret = wolfSSL_connect(ssl_c);
  58182. if (ret != WOLFSSL_FAILURE && ssl_c->error != SOCKET_ERROR_E)
  58183. return TEST_FAIL;
  58184. ret = wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6));
  58185. if (ret != WOLFSSL_SUCCESS)
  58186. return TEST_FAIL;
  58187. /* re-use the socket */
  58188. ret = wolfSSL_set_fd(ssl_c, sockfd);
  58189. if (ret != WOLFSSL_SUCCESS)
  58190. return TEST_FAIL;
  58191. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  58192. ret = wolfSSL_accept(ssl_s);
  58193. if (ret != WOLFSSL_FAILURE && ssl_s->error != SOCKET_ERROR_E)
  58194. return TEST_FAIL;
  58195. close(sockfd);
  58196. wolfSSL_free(ssl_c);
  58197. wolfSSL_CTX_free(ctx_c);
  58198. wolfSSL_free(ssl_s);
  58199. wolfSSL_CTX_free(ctx_s);
  58200. return TEST_SUCCESS;
  58201. }
  58202. #else
  58203. static int test_dtls_ipv6_check(void)
  58204. {
  58205. return TEST_SKIPPED;
  58206. }
  58207. #endif
  58208. static int test_wolfSSL_configure_args(void)
  58209. {
  58210. int res = TEST_SKIPPED;
  58211. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  58212. EXPECT_DECLS;
  58213. ExpectNotNull(wolfSSL_configure_args());
  58214. res = EXPECT_RESULT();
  58215. #endif
  58216. return res;
  58217. }
  58218. /*----------------------------------------------------------------------------*
  58219. | Main
  58220. *----------------------------------------------------------------------------*/
  58221. typedef int (*TEST_FUNC)(void);
  58222. typedef struct {
  58223. const char *name;
  58224. TEST_FUNC func;
  58225. byte run:1;
  58226. byte fail:1;
  58227. } TEST_CASE;
  58228. #define TEST_DECL(func) { #func, func, 0, 0 }
  58229. int testAll = 1;
  58230. TEST_CASE testCases[] = {
  58231. TEST_DECL(test_fileAccess),
  58232. TEST_DECL(test_wolfSSL_Init),
  58233. TEST_DECL(test_wolfSSL_Method_Allocators),
  58234. #ifndef NO_WOLFSSL_SERVER
  58235. TEST_DECL(test_wolfSSL_CTX_new),
  58236. #endif
  58237. TEST_DECL(test_server_wolfSSL_new),
  58238. TEST_DECL(test_client_wolfSSL_new),
  58239. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  58240. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  58241. TEST_DECL(test_for_double_Free),
  58242. #endif
  58243. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58244. TEST_DECL(test_wolfSSL_get_finished),
  58245. /* Uses Assert in handshake callback. */
  58246. TEST_DECL(test_wolfSSL_CTX_add_session),
  58247. /* Uses Assert in handshake callback. */
  58248. TEST_DECL(test_wolfSSL_CTX_add_session_ext),
  58249. #endif
  58250. TEST_DECL(test_SSL_CIPHER_get_xxx),
  58251. TEST_DECL(test_wolfSSL_ERR_strings),
  58252. TEST_DECL(test_wolfSSL_EVP_shake128),
  58253. TEST_DECL(test_wolfSSL_EVP_shake256),
  58254. TEST_DECL(test_EVP_blake2),
  58255. TEST_DECL(test_EVP_MD_do_all),
  58256. TEST_DECL(test_OBJ_NAME_do_all),
  58257. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  58258. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  58259. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  58260. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  58261. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  58262. /* Large number of memory allocations. */
  58263. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  58264. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  58265. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  58266. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  58267. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  58268. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  58269. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  58270. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  58271. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  58272. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  58273. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  58274. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  58275. TEST_DECL(test_wolfSSL_FPKI),
  58276. TEST_DECL(test_wolfSSL_OtherName),
  58277. TEST_DECL(test_wolfSSL_CertRsaPss),
  58278. TEST_DECL(test_wolfSSL_CertManagerCRL),
  58279. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  58280. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  58281. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  58282. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  58283. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  58284. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  58285. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  58286. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  58287. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  58288. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  58289. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  58290. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  58291. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  58292. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  58293. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  58294. TEST_DECL(test_SetTmpEC_DHE_Sz),
  58295. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  58296. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  58297. /* Uses Assert in handshake callback. */
  58298. TEST_DECL(test_wolfSSL_dtls_plaintext),
  58299. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58300. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58301. TEST_DECL(test_wolfSSL_read_write),
  58302. /* Can't memory test as server hangs if client fails before second connect.
  58303. */
  58304. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  58305. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  58306. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  58307. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  58308. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  58309. /* Can't memory test as server hangs. */
  58310. TEST_DECL(test_wolfSSL_dtls_export),
  58311. /* Uses Assert in handshake callback. */
  58312. TEST_DECL(test_wolfSSL_tls_export),
  58313. #endif
  58314. TEST_DECL(test_wolfSSL_SetMinVersion),
  58315. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  58316. /* TLS extensions tests */
  58317. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58318. #ifdef HAVE_SNI
  58319. TEST_DECL(test_wolfSSL_UseSNI_params),
  58320. /* Uses Assert in handshake callback. */
  58321. TEST_DECL(test_wolfSSL_UseSNI_connection),
  58322. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  58323. #endif /* HAVE_SNI */
  58324. #endif
  58325. TEST_DECL(test_wolfSSL_UseTrustedCA),
  58326. TEST_DECL(test_wolfSSL_UseMaxFragment),
  58327. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  58328. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  58329. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58330. /* Uses Assert in handshake callback. */
  58331. TEST_DECL(test_wolfSSL_UseALPN_connection),
  58332. TEST_DECL(test_wolfSSL_UseALPN_params),
  58333. #endif
  58334. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  58335. /* Uses Assert in handshake callback. */
  58336. TEST_DECL(test_wolfSSL_set_alpn_protos),
  58337. #endif
  58338. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  58339. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  58340. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  58341. TEST_DECL(test_tls_ext_duplicate),
  58342. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  58343. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  58344. /* Uses Assert in handshake callback. */
  58345. TEST_DECL(test_wolfSSL_Tls13_ECH),
  58346. #endif
  58347. /* X509 tests */
  58348. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  58349. TEST_DECL(test_wolfSSL_PKCS12),
  58350. TEST_DECL(test_wolfSSL_no_password_cb),
  58351. TEST_DECL(test_wolfSSL_PKCS8),
  58352. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  58353. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  58354. TEST_DECL(test_wolfSSL_PKCS5),
  58355. TEST_DECL(test_wolfSSL_URI),
  58356. TEST_DECL(test_wolfSSL_TBS),
  58357. TEST_DECL(test_wolfSSL_X509_verify),
  58358. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  58359. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  58360. TEST_DECL(test_wc_PemToDer),
  58361. TEST_DECL(test_wc_AllocDer),
  58362. TEST_DECL(test_wc_CertPemToDer),
  58363. TEST_DECL(test_wc_KeyPemToDer),
  58364. TEST_DECL(test_wc_PubKeyPemToDer),
  58365. TEST_DECL(test_wc_PemPubKeyToDer),
  58366. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  58367. TEST_DECL(test_wc_CheckCertSigPubKey),
  58368. /* OCSP Stapling */
  58369. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  58370. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  58371. /* Multicast */
  58372. TEST_DECL(test_wolfSSL_mcast),
  58373. /* ASN.1 compatibility API tests */
  58374. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  58375. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  58376. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  58377. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  58378. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  58379. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  58380. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  58381. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  58382. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  58383. TEST_DECL(test_wolfSSL_ASN1_get_object),
  58384. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  58385. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  58386. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  58387. TEST_DECL(test_wolfSSL_ASN1_STRING),
  58388. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  58389. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  58390. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  58391. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  58392. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  58393. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  58394. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  58395. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  58396. TEST_DECL(test_wolfSSL_ASN1_TIME),
  58397. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  58398. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  58399. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  58400. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  58401. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  58402. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  58403. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  58404. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  58405. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  58406. /* compatibility tests */
  58407. TEST_DECL(test_wolfSSL_lhash),
  58408. TEST_DECL(test_wolfSSL_X509_NAME),
  58409. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  58410. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  58411. #ifndef NO_BIO
  58412. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  58413. TEST_DECL(test_wolfSSL_X509_INFO),
  58414. #endif
  58415. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  58416. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  58417. TEST_DECL(test_wolfSSL_X509_check_host),
  58418. TEST_DECL(test_wolfSSL_X509_check_email),
  58419. TEST_DECL(test_wolfSSL_DES),
  58420. TEST_DECL(test_wolfSSL_certs),
  58421. TEST_DECL(test_wolfSSL_X509_check_private_key),
  58422. TEST_DECL(test_wolfSSL_private_keys),
  58423. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  58424. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  58425. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  58426. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  58427. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  58428. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  58429. #ifndef NO_BIO
  58430. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  58431. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  58432. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  58433. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  58434. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  58435. #endif
  58436. TEST_DECL(test_DSA_do_sign_verify),
  58437. TEST_DECL(test_wolfSSL_tmp_dh),
  58438. TEST_DECL(test_wolfSSL_ctrl),
  58439. TEST_DECL(test_wolfSSL_EVP_MD_size),
  58440. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  58441. TEST_DECL(test_wolfSSL_EVP_Digest),
  58442. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  58443. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  58444. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  58445. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  58446. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  58447. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  58448. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  58449. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  58450. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  58451. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  58452. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  58453. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  58454. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  58455. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  58456. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  58457. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  58458. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  58459. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  58460. #endif
  58461. #ifndef NO_BIO
  58462. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  58463. TEST_DECL(test_wolfSSL_GetLoggingCb),
  58464. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  58465. TEST_DECL(test_wc_ERR_remove_state),
  58466. TEST_DECL(test_wc_ERR_print_errors_fp),
  58467. #endif
  58468. TEST_DECL(test_wolfSSL_configure_args),
  58469. TEST_DECL(test_wolfSSL_set_options),
  58470. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  58471. TEST_DECL(test_wolfSSL_set1_curves_list),
  58472. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  58473. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  58474. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  58475. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  58476. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  58477. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  58478. TEST_DECL(test_wolfSSL_X509_Name_canon),
  58479. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  58480. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  58481. TEST_DECL(test_wolfSSL_X509_NID),
  58482. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  58483. TEST_DECL(test_wolfSSL_get0_param),
  58484. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  58485. TEST_DECL(test_wolfSSL_set1_host),
  58486. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  58487. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  58488. TEST_DECL(test_wolfSSL_X509_STORE),
  58489. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  58490. TEST_DECL(test_X509_STORE_get0_objects),
  58491. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  58492. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  58493. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  58494. TEST_DECL(test_wolfSSL_X509_cmp_time),
  58495. TEST_DECL(test_wolfSSL_X509_time_adj),
  58496. TEST_DECL(test_wolfSSL_X509),
  58497. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  58498. TEST_DECL(test_wolfSSL_X509_sign),
  58499. TEST_DECL(test_wolfSSL_X509_sign2),
  58500. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  58501. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  58502. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  58503. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  58504. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  58505. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  58506. TEST_DECL(test_wolfSSL_msgCb),
  58507. TEST_DECL(test_wolfSSL_either_side),
  58508. TEST_DECL(test_wolfSSL_DTLS_either_side),
  58509. /* Uses Assert in handshake callback. */
  58510. TEST_DECL(test_wolfSSL_dtls_fragments),
  58511. /* Uses Assert in handshake callback. */
  58512. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  58513. /* Uses Assert in handshake callback. */
  58514. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  58515. /* Uses Assert in handshake callback. */
  58516. TEST_DECL(test_wolfSSL_dtls_bad_record),
  58517. /* Uses Assert in handshake callback. */
  58518. TEST_DECL(test_wolfSSL_dtls_stateless),
  58519. TEST_DECL(test_generate_cookie),
  58520. /* RAND compatability API */
  58521. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  58522. TEST_DECL(test_wolfSSL_RAND_bytes),
  58523. TEST_DECL(test_wolfSSL_RAND),
  58524. /* BN compatability API */
  58525. TEST_DECL(test_wolfSSL_BN_CTX),
  58526. TEST_DECL(test_wolfSSL_BN),
  58527. TEST_DECL(test_wolfSSL_BN_init),
  58528. TEST_DECL(test_wolfSSL_BN_enc_dec),
  58529. TEST_DECL(test_wolfSSL_BN_word),
  58530. TEST_DECL(test_wolfSSL_BN_bits),
  58531. TEST_DECL(test_wolfSSL_BN_shift),
  58532. TEST_DECL(test_wolfSSL_BN_math),
  58533. TEST_DECL(test_wolfSSL_BN_math_mod),
  58534. TEST_DECL(test_wolfSSL_BN_math_other),
  58535. TEST_DECL(test_wolfSSL_BN_rand),
  58536. TEST_DECL(test_wolfSSL_BN_prime),
  58537. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  58538. #ifndef NO_BIO
  58539. TEST_DECL(test_wolfSSL_PEM_read_bio),
  58540. TEST_DECL(test_wolfSSL_BIO),
  58541. #endif
  58542. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  58543. TEST_DECL(test_wolfSSL_BUF),
  58544. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  58545. /* Can't memory test as server hangs. */
  58546. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  58547. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  58548. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  58549. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  58550. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  58551. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  58552. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  58553. /* Can't memory test as server hangs. */
  58554. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  58555. TEST_DECL(test_wolfSSL_Tls13_postauth),
  58556. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  58557. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  58558. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  58559. TEST_DECL(test_wolfSSL_THREADID_hash),
  58560. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  58561. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  58562. /* Can't memory test as callbacks use Assert. */
  58563. TEST_DECL(test_error_queue_per_thread),
  58564. TEST_DECL(test_wolfSSL_ERR_put_error),
  58565. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  58566. #ifndef NO_BIO
  58567. TEST_DECL(test_wolfSSL_ERR_print_errors),
  58568. #endif
  58569. TEST_DECL(test_wolfSSL_HMAC),
  58570. TEST_DECL(test_wolfSSL_CMAC),
  58571. TEST_DECL(test_wolfSSL_OBJ),
  58572. TEST_DECL(test_wolfSSL_OBJ_cmp),
  58573. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  58574. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  58575. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  58576. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  58577. TEST_DECL(test_GENERAL_NAME_set0_othername),
  58578. TEST_DECL(test_othername_and_SID_ext),
  58579. TEST_DECL(test_wolfSSL_X509_set_name),
  58580. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  58581. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  58582. TEST_DECL(test_wolfSSL_X509_set_version),
  58583. #ifndef NO_BIO
  58584. TEST_DECL(test_wolfSSL_BIO_gets),
  58585. TEST_DECL(test_wolfSSL_BIO_puts),
  58586. TEST_DECL(test_wolfSSL_BIO_dump),
  58587. /* Can't memory test as server hangs. */
  58588. TEST_DECL(test_wolfSSL_BIO_should_retry),
  58589. TEST_DECL(test_wolfSSL_BIO_write),
  58590. /* Can't memory test as server hangs. */
  58591. TEST_DECL(test_wolfSSL_BIO_connect),
  58592. /* Can't memory test as server Asserts in thread. */
  58593. TEST_DECL(test_wolfSSL_BIO_accept),
  58594. TEST_DECL(test_wolfSSL_BIO_printf),
  58595. TEST_DECL(test_wolfSSL_BIO_f_md),
  58596. TEST_DECL(test_wolfSSL_BIO_up_ref),
  58597. TEST_DECL(test_wolfSSL_BIO_reset),
  58598. TEST_DECL(test_wolfSSL_BIO_tls),
  58599. #endif
  58600. /* Converted above to use Expect unless where stated. */
  58601. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58602. /* Converted to use Expect. */
  58603. TEST_DECL(test_wolfSSL_check_domain),
  58604. #endif
  58605. /* Converted to use Expect. */
  58606. TEST_DECL(test_wolfSSL_cert_cb),
  58607. TEST_DECL(test_wolfSSL_SESSION),
  58608. /* Converted to use Expect. */
  58609. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  58610. TEST_DECL(test_wolfSSL_ticket_keys),
  58611. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  58612. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  58613. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  58614. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  58615. TEST_DECL(test_wolfSSL_MD4),
  58616. TEST_DECL(test_wolfSSL_verify_mode),
  58617. TEST_DECL(test_wolfSSL_verify_depth),
  58618. TEST_DECL(test_wolfSSL_HMAC_CTX),
  58619. TEST_DECL(test_wolfSSL_msg_callback),
  58620. TEST_DECL(test_wolfSSL_SHA),
  58621. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  58622. TEST_DECL(test_wolfSSL_MD5),
  58623. TEST_DECL(test_wolfSSL_MD5_Transform),
  58624. TEST_DECL(test_wolfSSL_SHA_Transform),
  58625. TEST_DECL(test_wolfSSL_SHA256),
  58626. TEST_DECL(test_wolfSSL_SHA256_Transform),
  58627. TEST_DECL(test_wolfSSL_SHA224),
  58628. TEST_DECL(test_wolfSSL_SHA512_Transform),
  58629. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  58630. TEST_DECL(test_wolfSSL_X509_CRL),
  58631. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  58632. TEST_DECL(test_wolfSSL_PEM_read_X509),
  58633. TEST_DECL(test_wolfSSL_PEM_read),
  58634. #ifndef NO_BIO
  58635. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  58636. #endif
  58637. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  58638. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  58639. TEST_DECL(test_wolfSSL_X509_check_ca),
  58640. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  58641. TEST_DECL(test_wolfSSL_make_cert),
  58642. TEST_DECL(test_wolfSSL_DES_ncbc),
  58643. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  58644. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  58645. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  58646. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  58647. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  58648. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  58649. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  58650. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  58651. TEST_DECL(test_wolfssl_EVP_chacha20),
  58652. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  58653. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  58654. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  58655. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  58656. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  58657. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  58658. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  58659. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  58660. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  58661. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  58662. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  58663. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  58664. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  58665. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  58666. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  58667. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  58668. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  58669. TEST_DECL(test_CONF_modules_xxx),
  58670. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  58671. TEST_DECL(test_CRYPTO_THREADID_xxx),
  58672. TEST_DECL(test_ENGINE_cleanup),
  58673. #ifdef OPENSSL_ALL
  58674. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  58675. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  58676. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  58677. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  58678. TEST_DECL(test_wolfSSL_DSA_SIG),
  58679. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  58680. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  58681. TEST_DECL(test_wolfSSL_CTX_ctrl),
  58682. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  58683. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  58684. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  58685. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  58686. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  58687. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  58688. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  58689. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  58690. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  58691. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  58692. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  58693. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  58694. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  58695. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  58696. TEST_DECL(test_wolfSSL_EVP_rc4),
  58697. TEST_DECL(test_wolfSSL_EVP_enc_null),
  58698. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  58699. TEST_DECL(test_wolfSSL_EVP_mdc2),
  58700. TEST_DECL(test_wolfSSL_EVP_md4),
  58701. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  58702. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  58703. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  58704. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  58705. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  58706. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  58707. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  58708. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  58709. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  58710. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  58711. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  58712. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  58713. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  58714. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  58715. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  58716. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  58717. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  58718. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  58719. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  58720. TEST_DECL(test_evp_cipher_aes_gcm),
  58721. TEST_DECL(test_wolfSSL_OBJ_ln),
  58722. TEST_DECL(test_wolfSSL_OBJ_sn),
  58723. TEST_DECL(test_wolfSSL_TXT_DB),
  58724. TEST_DECL(test_wolfSSL_NCONF),
  58725. #endif /* OPENSSL_ALL */
  58726. #ifndef NO_BIO
  58727. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  58728. #endif
  58729. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  58730. #ifndef NO_BIO
  58731. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  58732. #endif /* !NO_BIO */
  58733. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  58734. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  58735. TEST_DECL(test_wolfSSL_X509_CA_num),
  58736. TEST_DECL(test_wolfSSL_X509_get_version),
  58737. #ifndef NO_BIO
  58738. TEST_DECL(test_wolfSSL_X509_print),
  58739. TEST_DECL(test_wolfSSL_X509_CRL_print),
  58740. TEST_DECL(test_wolfSSL_BIO_get_len),
  58741. #endif
  58742. TEST_DECL(test_wolfSSL_RSA),
  58743. TEST_DECL(test_wolfSSL_RSA_DER),
  58744. TEST_DECL(test_wolfSSL_RSA_print),
  58745. #ifndef NO_RSA
  58746. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  58747. #endif
  58748. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  58749. TEST_DECL(test_wolfSSL_RSA_get0_key),
  58750. TEST_DECL(test_wolfSSL_RSA_meth),
  58751. TEST_DECL(test_wolfSSL_RSA_verify),
  58752. TEST_DECL(test_wolfSSL_RSA_sign),
  58753. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  58754. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  58755. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  58756. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  58757. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  58758. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  58759. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  58760. TEST_DECL(test_wolfSSL_RSA_ex_data),
  58761. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  58762. TEST_DECL(test_wolfSSL_RSA_To_Der),
  58763. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  58764. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  58765. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  58766. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  58767. TEST_DECL(test_wolfSSL_DH),
  58768. TEST_DECL(test_wolfSSL_DH_dup),
  58769. TEST_DECL(test_wolfSSL_DH_check),
  58770. TEST_DECL(test_wolfSSL_DH_prime),
  58771. TEST_DECL(test_wolfSSL_DH_1536_prime),
  58772. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  58773. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  58774. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  58775. TEST_DECL(test_wolfSSL_d2i_DHparams),
  58776. TEST_DECL(test_wolfSSL_DH_LoadDer),
  58777. TEST_DECL(test_wolfSSL_i2d_DHparams),
  58778. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  58779. TEST_DECL(test_wolfSSL_EC_GROUP),
  58780. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  58781. TEST_DECL(test_wolfSSL_EC_POINT),
  58782. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  58783. TEST_DECL(test_EC_i2d),
  58784. TEST_DECL(test_wolfSSL_EC_curve),
  58785. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  58786. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  58787. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  58788. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  58789. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  58790. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  58791. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  58792. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  58793. TEST_DECL(test_ECDSA_size_sign),
  58794. TEST_DECL(test_ECDH_compute_key),
  58795. #endif
  58796. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  58797. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  58798. TEST_DECL(test_wolfSSL_X509V3_EXT),
  58799. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  58800. TEST_DECL(test_wolfSSL_X509_get_ext),
  58801. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  58802. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  58803. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  58804. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  58805. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  58806. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  58807. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  58808. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  58809. TEST_DECL(test_wolfSSL_X509_cmp),
  58810. TEST_DECL(test_openssl_generate_key_and_cert),
  58811. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  58812. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  58813. /* test the no op functions for compatibility */
  58814. TEST_DECL(test_no_op_functions),
  58815. /* OpenSSL EVP_PKEY API tests */
  58816. TEST_DECL(test_EVP_PKEY_rsa),
  58817. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  58818. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  58819. TEST_DECL(test_EVP_PKEY_ec),
  58820. TEST_DECL(test_EVP_PKEY_cmp),
  58821. /* OpenSSL error API tests */
  58822. TEST_DECL(test_ERR_load_crypto_strings),
  58823. /* OpenSSL sk_X509 API test */
  58824. TEST_DECL(test_sk_X509),
  58825. /* OpenSSL sk_X509_CRL API test */
  58826. TEST_DECL(test_sk_X509_CRL),
  58827. /* OpenSSL X509 API test */
  58828. TEST_DECL(test_X509_get_signature_nid),
  58829. /* OpenSSL X509 REQ API test */
  58830. TEST_DECL(test_X509_REQ),
  58831. /* OpenSSL PKCS7 API test */
  58832. TEST_DECL(test_wolfssl_PKCS7),
  58833. /* Converted to use Expect. */
  58834. TEST_DECL(test_wolfSSL_PKCS7_certs),
  58835. TEST_DECL(test_wolfSSL_PKCS7_sign),
  58836. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  58837. #ifndef NO_BIO
  58838. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  58839. #ifdef HAVE_SMIME
  58840. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  58841. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  58842. #endif /* HAVE_SMIME */
  58843. #endif /* !NO_BIO */
  58844. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  58845. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  58846. TEST_DECL(test_X509_LOOKUP_add_dir),
  58847. /* wolfCrypt ASN tests */
  58848. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  58849. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  58850. TEST_DECL(test_wc_SetSubjectRaw),
  58851. TEST_DECL(test_wc_GetSubjectRaw),
  58852. TEST_DECL(test_wc_SetIssuerRaw),
  58853. TEST_DECL(test_wc_SetIssueBuffer),
  58854. TEST_DECL(test_wc_SetSubjectKeyId),
  58855. TEST_DECL(test_wc_SetSubject),
  58856. TEST_DECL(test_CheckCertSignature),
  58857. TEST_DECL(test_wc_ParseCert),
  58858. TEST_DECL(test_wc_ParseCert_Error),
  58859. TEST_DECL(test_MakeCertWithPathLen),
  58860. /* wolfCrypt ECC tests */
  58861. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  58862. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  58863. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  58864. #ifdef WOLFSSL_TLS13
  58865. /* TLS v1.3 API tests */
  58866. TEST_DECL(test_tls13_apis),
  58867. TEST_DECL(test_tls13_cipher_suites),
  58868. #endif
  58869. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  58870. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  58871. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  58872. /* Bad certificate signature tests */
  58873. TEST_DECL(test_EccSigFailure_cm),
  58874. TEST_DECL(test_RsaSigFailure_cm),
  58875. #endif /* NO_CERTS */
  58876. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  58877. /* Converted to use Expect. */
  58878. TEST_DECL(test_DhCallbacks),
  58879. #endif
  58880. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58881. /* Converted to use Expect. */
  58882. TEST_DECL(test_export_keying_material),
  58883. #endif
  58884. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  58885. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  58886. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  58887. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  58888. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  58889. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  58890. #endif
  58891. TEST_DECL(test_wolfSSL_security_level),
  58892. TEST_DECL(test_wolfSSL_SSL_in_init),
  58893. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  58894. TEST_DECL(test_wolfSSL_OpenSSL_version),
  58895. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  58896. TEST_DECL(test_ticket_and_psk_mixing),
  58897. TEST_DECL(test_prioritize_psk),
  58898. TEST_DECL(test_CONF_CTX_FILE),
  58899. TEST_DECL(test_CONF_CTX_CMDLINE),
  58900. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  58901. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  58902. /* wolfcrypt */
  58903. TEST_DECL(test_wolfCrypt_Init),
  58904. TEST_DECL(test_wc_InitMd5),
  58905. TEST_DECL(test_wc_Md5Update),
  58906. TEST_DECL(test_wc_Md5Final),
  58907. TEST_DECL(test_wc_InitSha),
  58908. TEST_DECL(test_wc_ShaUpdate),
  58909. TEST_DECL(test_wc_ShaFinal),
  58910. TEST_DECL(test_wc_InitSha256),
  58911. TEST_DECL(test_wc_Sha256Update),
  58912. TEST_DECL(test_wc_Sha256Final),
  58913. TEST_DECL(test_wc_Sha256FinalRaw),
  58914. TEST_DECL(test_wc_Sha256GetFlags),
  58915. TEST_DECL(test_wc_Sha256Free),
  58916. TEST_DECL(test_wc_Sha256GetHash),
  58917. TEST_DECL(test_wc_Sha256Copy),
  58918. TEST_DECL(test_wc_InitSha512),
  58919. TEST_DECL(test_wc_Sha512Update),
  58920. TEST_DECL(test_wc_Sha512Final),
  58921. TEST_DECL(test_wc_Sha512GetFlags),
  58922. TEST_DECL(test_wc_Sha512FinalRaw),
  58923. TEST_DECL(test_wc_Sha512Free),
  58924. TEST_DECL(test_wc_Sha512GetHash),
  58925. TEST_DECL(test_wc_Sha512Copy),
  58926. TEST_DECL(test_wc_InitSha512_224),
  58927. TEST_DECL(test_wc_Sha512_224Update),
  58928. TEST_DECL(test_wc_Sha512_224Final),
  58929. TEST_DECL(test_wc_Sha512_224GetFlags),
  58930. TEST_DECL(test_wc_Sha512_224FinalRaw),
  58931. TEST_DECL(test_wc_Sha512_224Free),
  58932. TEST_DECL(test_wc_Sha512_224GetHash),
  58933. TEST_DECL(test_wc_Sha512_224Copy),
  58934. TEST_DECL(test_wc_InitSha512_256),
  58935. TEST_DECL(test_wc_Sha512_256Update),
  58936. TEST_DECL(test_wc_Sha512_256Final),
  58937. TEST_DECL(test_wc_Sha512_256GetFlags),
  58938. TEST_DECL(test_wc_Sha512_256FinalRaw),
  58939. TEST_DECL(test_wc_Sha512_256Free),
  58940. TEST_DECL(test_wc_Sha512_256GetHash),
  58941. TEST_DECL(test_wc_Sha512_256Copy),
  58942. TEST_DECL(test_wc_InitSha384),
  58943. TEST_DECL(test_wc_Sha384Update),
  58944. TEST_DECL(test_wc_Sha384Final),
  58945. TEST_DECL(test_wc_Sha384GetFlags),
  58946. TEST_DECL(test_wc_Sha384FinalRaw),
  58947. TEST_DECL(test_wc_Sha384Free),
  58948. TEST_DECL(test_wc_Sha384GetHash),
  58949. TEST_DECL(test_wc_Sha384Copy),
  58950. TEST_DECL(test_wc_InitSha224),
  58951. TEST_DECL(test_wc_Sha224Update),
  58952. TEST_DECL(test_wc_Sha224Final),
  58953. TEST_DECL(test_wc_Sha224SetFlags),
  58954. TEST_DECL(test_wc_Sha224GetFlags),
  58955. TEST_DECL(test_wc_Sha224Free),
  58956. TEST_DECL(test_wc_Sha224GetHash),
  58957. TEST_DECL(test_wc_Sha224Copy),
  58958. TEST_DECL(test_wc_InitBlake2b),
  58959. TEST_DECL(test_wc_InitBlake2b_WithKey),
  58960. TEST_DECL(test_wc_InitBlake2s_WithKey),
  58961. TEST_DECL(test_wc_InitRipeMd),
  58962. TEST_DECL(test_wc_RipeMdUpdate),
  58963. TEST_DECL(test_wc_RipeMdFinal),
  58964. TEST_DECL(test_wc_InitSha3),
  58965. TEST_DECL(testing_wc_Sha3_Update),
  58966. TEST_DECL(test_wc_Sha3_224_Final),
  58967. TEST_DECL(test_wc_Sha3_256_Final),
  58968. TEST_DECL(test_wc_Sha3_384_Final),
  58969. TEST_DECL(test_wc_Sha3_512_Final),
  58970. TEST_DECL(test_wc_Sha3_224_Copy),
  58971. TEST_DECL(test_wc_Sha3_256_Copy),
  58972. TEST_DECL(test_wc_Sha3_384_Copy),
  58973. TEST_DECL(test_wc_Sha3_512_Copy),
  58974. TEST_DECL(test_wc_Sha3_GetFlags),
  58975. TEST_DECL(test_wc_InitShake256),
  58976. TEST_DECL(testing_wc_Shake256_Update),
  58977. TEST_DECL(test_wc_Shake256_Final),
  58978. TEST_DECL(test_wc_Shake256_Copy),
  58979. TEST_DECL(test_wc_Shake256Hash),
  58980. TEST_DECL(test_wc_Md5HmacSetKey),
  58981. TEST_DECL(test_wc_Md5HmacUpdate),
  58982. TEST_DECL(test_wc_Md5HmacFinal),
  58983. TEST_DECL(test_wc_ShaHmacSetKey),
  58984. TEST_DECL(test_wc_ShaHmacUpdate),
  58985. TEST_DECL(test_wc_ShaHmacFinal),
  58986. TEST_DECL(test_wc_Sha224HmacSetKey),
  58987. TEST_DECL(test_wc_Sha224HmacUpdate),
  58988. TEST_DECL(test_wc_Sha224HmacFinal),
  58989. TEST_DECL(test_wc_Sha256HmacSetKey),
  58990. TEST_DECL(test_wc_Sha256HmacUpdate),
  58991. TEST_DECL(test_wc_Sha256HmacFinal),
  58992. TEST_DECL(test_wc_Sha384HmacSetKey),
  58993. TEST_DECL(test_wc_Sha384HmacUpdate),
  58994. TEST_DECL(test_wc_Sha384HmacFinal),
  58995. TEST_DECL(test_wc_HashInit),
  58996. TEST_DECL(test_wc_HashSetFlags),
  58997. TEST_DECL(test_wc_HashGetFlags),
  58998. TEST_DECL(test_wc_InitCmac),
  58999. TEST_DECL(test_wc_CmacUpdate),
  59000. TEST_DECL(test_wc_CmacFinal),
  59001. TEST_DECL(test_wc_AesCmacGenerate),
  59002. TEST_DECL(test_wc_AesGcmStream),
  59003. TEST_DECL(test_wc_Des3_SetIV),
  59004. TEST_DECL(test_wc_Des3_SetKey),
  59005. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  59006. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  59007. TEST_DECL(test_wc_Des3_EcbEncrypt),
  59008. TEST_DECL(test_wc_Chacha_SetKey),
  59009. TEST_DECL(test_wc_Chacha_Process),
  59010. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  59011. TEST_DECL(test_wc_Poly1305SetKey),
  59012. TEST_DECL(test_wc_CamelliaSetKey),
  59013. TEST_DECL(test_wc_CamelliaSetIV),
  59014. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  59015. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  59016. TEST_DECL(test_wc_Arc4SetKey),
  59017. TEST_DECL(test_wc_Arc4Process),
  59018. TEST_DECL(test_wc_Rc2SetKey),
  59019. TEST_DECL(test_wc_Rc2SetIV),
  59020. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  59021. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  59022. TEST_DECL(test_wc_AesSetKey),
  59023. TEST_DECL(test_wc_AesSetIV),
  59024. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  59025. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  59026. TEST_DECL(test_wc_AesGcmSetKey),
  59027. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  59028. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  59029. TEST_DECL(test_wc_GmacSetKey),
  59030. TEST_DECL(test_wc_GmacUpdate),
  59031. TEST_DECL(test_wc_InitRsaKey),
  59032. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  59033. TEST_DECL(test_wc_RsaPublicKeyDecode),
  59034. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  59035. TEST_DECL(test_wc_MakeRsaKey),
  59036. TEST_DECL(test_wc_SetKeyUsage),
  59037. TEST_DECL(test_wc_CheckProbablePrime),
  59038. TEST_DECL(test_wc_RsaPSS_Verify),
  59039. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  59040. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  59041. TEST_DECL(test_wc_SetMutexCb),
  59042. TEST_DECL(test_wc_LockMutex_ex),
  59043. TEST_DECL(test_wc_RsaKeyToDer),
  59044. TEST_DECL(test_wc_RsaKeyToPublicDer),
  59045. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  59046. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  59047. TEST_DECL(test_wc_RsaEncryptSize),
  59048. TEST_DECL(test_wc_RsaSSL_SignVerify),
  59049. TEST_DECL(test_wc_RsaFlattenPublicKey),
  59050. TEST_DECL(test_RsaDecryptBoundsCheck),
  59051. TEST_DECL(test_wc_AesCcmSetKey),
  59052. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  59053. TEST_DECL(test_wc_InitDsaKey),
  59054. TEST_DECL(test_wc_DsaSignVerify),
  59055. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  59056. TEST_DECL(test_wc_MakeDsaKey),
  59057. TEST_DECL(test_wc_DsaKeyToDer),
  59058. TEST_DECL(test_wc_DsaKeyToPublicDer),
  59059. TEST_DECL(test_wc_DsaImportParamsRaw),
  59060. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  59061. TEST_DECL(test_wc_DsaExportParamsRaw),
  59062. TEST_DECL(test_wc_DsaExportKeyRaw),
  59063. TEST_DECL(test_wc_SignatureGetSize_ecc),
  59064. TEST_DECL(test_wc_SignatureGetSize_rsa),
  59065. /*
  59066. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  59067. * avoid memory leaks.
  59068. */
  59069. TEST_DECL(test_wolfCrypt_Cleanup),
  59070. #ifdef OPENSSL_EXTRA
  59071. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  59072. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  59073. TEST_DECL(test_ED25519),
  59074. TEST_DECL(test_ED448),
  59075. #endif
  59076. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  59077. !defined(HAVE_SELFTEST) && \
  59078. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  59079. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  59080. #endif
  59081. #ifdef HAVE_HASHDRBG
  59082. #ifdef TEST_RESEED_INTERVAL
  59083. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  59084. #endif
  59085. TEST_DECL(test_wc_RNG_GenerateBlock),
  59086. #endif
  59087. TEST_DECL(test_get_rand_digit),
  59088. TEST_DECL(test_get_digit_count),
  59089. TEST_DECL(test_mp_cond_copy),
  59090. TEST_DECL(test_mp_rand),
  59091. TEST_DECL(test_get_digit),
  59092. TEST_DECL(test_wc_export_int),
  59093. TEST_DECL(test_wc_InitRngNonce),
  59094. TEST_DECL(test_wc_InitRngNonce_ex),
  59095. TEST_DECL(test_wc_ed25519_make_key),
  59096. TEST_DECL(test_wc_ed25519_init),
  59097. TEST_DECL(test_wc_ed25519_sign_msg),
  59098. TEST_DECL(test_wc_ed25519_import_public),
  59099. TEST_DECL(test_wc_ed25519_import_private_key),
  59100. TEST_DECL(test_wc_ed25519_export),
  59101. TEST_DECL(test_wc_ed25519_size),
  59102. TEST_DECL(test_wc_ed25519_exportKey),
  59103. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  59104. TEST_DECL(test_wc_curve25519_init),
  59105. TEST_DECL(test_wc_curve25519_size),
  59106. TEST_DECL(test_wc_curve25519_export_key_raw),
  59107. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  59108. TEST_DECL(test_wc_curve25519_make_key),
  59109. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  59110. TEST_DECL(test_wc_curve25519_make_pub),
  59111. TEST_DECL(test_wc_curve25519_export_public_ex),
  59112. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  59113. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  59114. TEST_DECL(test_wc_curve25519_import_private),
  59115. TEST_DECL(test_wc_ed448_make_key),
  59116. TEST_DECL(test_wc_ed448_init),
  59117. TEST_DECL(test_wc_ed448_sign_msg),
  59118. TEST_DECL(test_wc_ed448_import_public),
  59119. TEST_DECL(test_wc_ed448_import_private_key),
  59120. TEST_DECL(test_wc_ed448_export),
  59121. TEST_DECL(test_wc_ed448_size),
  59122. TEST_DECL(test_wc_ed448_exportKey),
  59123. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  59124. TEST_DECL(test_wc_curve448_make_key),
  59125. TEST_DECL(test_wc_curve448_shared_secret_ex),
  59126. TEST_DECL(test_wc_curve448_export_public_ex),
  59127. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  59128. TEST_DECL(test_wc_curve448_export_key_raw),
  59129. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  59130. TEST_DECL(test_wc_curve448_import_private),
  59131. TEST_DECL(test_wc_curve448_init),
  59132. TEST_DECL(test_wc_curve448_size),
  59133. TEST_DECL(test_wc_ecc_make_key),
  59134. TEST_DECL(test_wc_ecc_init),
  59135. TEST_DECL(test_wc_ecc_check_key),
  59136. TEST_DECL(test_wc_ecc_get_generator),
  59137. TEST_DECL(test_wc_ecc_size),
  59138. TEST_DECL(test_wc_ecc_params),
  59139. TEST_DECL(test_wc_ecc_signVerify_hash),
  59140. TEST_DECL(test_wc_ecc_shared_secret),
  59141. TEST_DECL(test_wc_ecc_export_x963),
  59142. TEST_DECL(test_wc_ecc_export_x963_ex),
  59143. TEST_DECL(test_wc_ecc_import_x963),
  59144. TEST_DECL(ecc_import_private_key),
  59145. TEST_DECL(test_wc_ecc_export_private_only),
  59146. TEST_DECL(test_wc_ecc_rs_to_sig),
  59147. TEST_DECL(test_wc_ecc_import_raw),
  59148. TEST_DECL(test_wc_ecc_import_unsigned),
  59149. TEST_DECL(test_wc_ecc_sig_size),
  59150. TEST_DECL(test_wc_ecc_ctx_new),
  59151. TEST_DECL(test_wc_ecc_ctx_reset),
  59152. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  59153. TEST_DECL(test_wc_ecc_ctx_set_info),
  59154. TEST_DECL(test_wc_ecc_encryptDecrypt),
  59155. TEST_DECL(test_wc_ecc_del_point),
  59156. TEST_DECL(test_wc_ecc_pointFns),
  59157. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  59158. TEST_DECL(test_wc_ecc_verify_hash_ex),
  59159. TEST_DECL(test_wc_ecc_mulmod),
  59160. TEST_DECL(test_wc_ecc_is_valid_idx),
  59161. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  59162. TEST_DECL(test_wc_ecc_sig_size_calc),
  59163. TEST_DECL(test_ToTraditional),
  59164. TEST_DECL(test_wc_EccPrivateKeyToDer),
  59165. TEST_DECL(test_wc_DhPublicKeyDecode),
  59166. TEST_DECL(test_wc_Ed25519KeyToDer),
  59167. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  59168. TEST_DECL(test_wc_Ed448KeyToDer),
  59169. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  59170. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  59171. TEST_DECL(test_wc_SetSubjectBuffer),
  59172. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  59173. TEST_DECL(test_wc_PKCS7_New),
  59174. TEST_DECL(test_wc_PKCS7_Init),
  59175. TEST_DECL(test_wc_PKCS7_InitWithCert),
  59176. TEST_DECL(test_wc_PKCS7_EncodeData),
  59177. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  59178. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  59179. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  59180. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  59181. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  59182. TEST_DECL(test_wc_PKCS7_Degenerate),
  59183. TEST_DECL(test_wc_PKCS7_BER),
  59184. TEST_DECL(test_PKCS7_signed_enveloped),
  59185. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  59186. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  59187. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  59188. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  59189. TEST_DECL(test_wc_i2d_PKCS12),
  59190. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  59191. TEST_DECL(test_openssl_FIPS_drbg),
  59192. TEST_DECL(test_wc_CryptoCb),
  59193. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  59194. TEST_DECL(test_wolfSSL_FIPS_mode),
  59195. #ifdef WOLFSSL_DTLS
  59196. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  59197. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  59198. #endif
  59199. #if !defined(NO_FILESYSTEM) && \
  59200. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59201. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  59202. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  59203. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  59204. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  59205. #ifdef HAVE_MAX_FRAGMENT
  59206. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  59207. #endif /* HAVE_MAX_FRAGMENT */
  59208. #ifndef NO_RSA
  59209. TEST_DECL(test_wolfSSL_dtls_stateless2),
  59210. #if !defined(NO_OLD_TLS)
  59211. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  59212. #endif /* !defined(NO_OLD_TLS) */
  59213. #endif /* ! NO_RSA */
  59214. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59215. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  59216. /* Converted to use Expect. */
  59217. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  59218. /* Converted to use Expect. */
  59219. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  59220. /* Converted to use Expect. */
  59221. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  59222. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  59223. TEST_DECL(test_ForceZero),
  59224. TEST_DECL(test_wolfSSL_Cleanup),
  59225. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  59226. && defined(WOLFSSL_TLS13) && \
  59227. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  59228. TEST_DECL(test_ticket_nonce_malloc),
  59229. #endif
  59230. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  59231. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  59232. !defined(WOLFSSL_NO_CLIENT_AUTH))
  59233. TEST_DECL(test_various_pathlen_chains),
  59234. #endif
  59235. TEST_DECL(test_ticket_ret_create),
  59236. TEST_DECL(test_extra_alerts_wrong_cs),
  59237. TEST_DECL(test_extra_alerts_skip_hs),
  59238. TEST_DECL(test_extra_alerts_bad_psk),
  59239. TEST_DECL(test_harden_no_secure_renegotiation),
  59240. /* Converted to use Expect. */
  59241. TEST_DECL(test_override_alt_cert_chain),
  59242. TEST_DECL(test_dtls13_bad_epoch_ch),
  59243. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  59244. TEST_DECL(test_dtls_msg_from_other_peer),
  59245. TEST_DECL(test_dtls_ipv6_check),
  59246. /* If at some point a stub get implemented this test should fail indicating
  59247. * a need to implement a new test case
  59248. */
  59249. TEST_DECL(test_stubs_are_stubs)
  59250. };
  59251. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  59252. static void TestSetup(void)
  59253. {
  59254. /* Stub, for now. Add common test setup code here. */
  59255. }
  59256. static void TestCleanup(void)
  59257. {
  59258. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  59259. /* Clear any errors added to the error queue during the test run. */
  59260. wolfSSL_ERR_clear_error();
  59261. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  59262. }
  59263. /* Print out all API test cases with numeric identifier.
  59264. */
  59265. void ApiTest_PrintTestCases(void)
  59266. {
  59267. int i;
  59268. printf("All Test Cases:\n");
  59269. for (i = 0; i < TEST_CASE_CNT; i++) {
  59270. printf("%3d: %s\n", i + 1, testCases[i].name);
  59271. }
  59272. }
  59273. /* Add test case with index to the list to run.
  59274. *
  59275. * @param [in] idx Index of test case to run starting at 1.
  59276. * @return 0 on success.
  59277. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  59278. */
  59279. int ApiTest_RunIdx(int idx)
  59280. {
  59281. if (idx < 1 || idx > TEST_CASE_CNT) {
  59282. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  59283. return BAD_FUNC_ARG;
  59284. }
  59285. testAll = 0;
  59286. testCases[idx-1].run = 1;
  59287. return 0;
  59288. }
  59289. /* Add test case with name to the list to run.
  59290. *
  59291. * @param [in] name Name of test case to run.
  59292. * @return 0 on success.
  59293. * @return BAD_FUNC_ARG when name is not a known test case name.
  59294. */
  59295. int ApiTest_RunName(char* name)
  59296. {
  59297. int i;
  59298. for (i = 0; i < TEST_CASE_CNT; i++) {
  59299. if (XSTRCMP(testCases[i].name, name) == 0) {
  59300. testAll = 0;
  59301. testCases[i].run = 1;
  59302. return 0;
  59303. }
  59304. }
  59305. printf("Test case name not found: %s\n", name);
  59306. printf("Use --list to see all test case names.\n");
  59307. return BAD_FUNC_ARG;
  59308. }
  59309. /* Converts the result code to a string.
  59310. *
  59311. * @param [in] res Test result code.
  59312. * @return String describing test result.
  59313. */
  59314. static const char* apitest_res_string(int res)
  59315. {
  59316. const char* str = "invalid result";
  59317. switch (res) {
  59318. case TEST_SUCCESS:
  59319. str = "passed";
  59320. break;
  59321. case TEST_FAIL:
  59322. str = "failed";
  59323. break;
  59324. case TEST_SKIPPED:
  59325. str = "skipped";
  59326. break;
  59327. }
  59328. return str;
  59329. }
  59330. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  59331. static double gettime_secs(void)
  59332. {
  59333. struct timeval tv;
  59334. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  59335. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  59336. }
  59337. #endif
  59338. int ApiTest(void)
  59339. {
  59340. int i;
  59341. int ret;
  59342. int res = 0;
  59343. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  59344. double timeDiff;
  59345. #endif
  59346. EXPECT_DECLS;
  59347. printf(" Begin API Tests\n");
  59348. fflush(stdout);
  59349. /* we must perform init and cleanup if not all tests are running */
  59350. if (!testAll) {
  59351. #ifdef WOLFCRYPT_ONLY
  59352. if (wolfCrypt_Init() != 0) {
  59353. printf("wolfCrypt Initialization failed\n");
  59354. res = 1;
  59355. }
  59356. #else
  59357. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  59358. printf("wolfSSL Initialization failed\n");
  59359. res = 1;
  59360. }
  59361. #endif
  59362. }
  59363. if (res == 0) {
  59364. for (i = 0; i < TEST_CASE_CNT; ++i) {
  59365. /* When not testing all cases then skip if not marked for running.
  59366. */
  59367. if (!testAll && !testCases[i].run) {
  59368. continue;
  59369. }
  59370. TestSetup();
  59371. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  59372. fflush(stdout);
  59373. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  59374. timeDiff = gettime_secs();
  59375. #endif
  59376. ret = testCases[i].func();
  59377. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  59378. timeDiff = gettime_secs() - timeDiff;
  59379. #endif
  59380. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  59381. if (ret != TEST_SKIPPED) {
  59382. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  59383. }
  59384. else
  59385. #endif
  59386. {
  59387. printf(" %s\n", apitest_res_string(ret));
  59388. }
  59389. fflush(stdout);
  59390. /* if return code is < 0 and not skipped then assert error */
  59391. Expect((ret > 0 || ret == TEST_SKIPPED),
  59392. ("Test failed\n"),
  59393. ("ret %d", ret));
  59394. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  59395. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  59396. TestCleanup();
  59397. }
  59398. }
  59399. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  59400. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  59401. wc_ecc_fp_free(); /* free per thread cache */
  59402. #endif
  59403. if (!testAll) {
  59404. #ifdef WOLFCRYPT_ONLY
  59405. wolfCrypt_Cleanup();
  59406. #else
  59407. wolfSSL_Cleanup();
  59408. #endif
  59409. }
  59410. (void)testDevId;
  59411. if (res != 0) {
  59412. printf("\nFAILURES:\n");
  59413. for (i = 0; i < TEST_CASE_CNT; ++i) {
  59414. if (testCases[i].fail) {
  59415. printf(" %3d: %s\n", i + 1, testCases[i].name);
  59416. }
  59417. }
  59418. printf("\n");
  59419. fflush(stdout);
  59420. }
  59421. printf(" End API Tests\n");
  59422. fflush(stdout);
  59423. return res;
  59424. }